Skip to content

Commit

Permalink
Try #71:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Oct 15, 2020
2 parents 1f138d1 + 3715a7d commit e294c39
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 15 deletions.
37 changes: 33 additions & 4 deletions Sources/MeiliSearch/Model/Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public struct Update: Codable, Equatable {
// MARK: Properties

///Returns if the update has been sucessful or not.
public let status: String
public let status: Status

///Unique ID for the current `Update`.
public let updateId: Int

///Type of update.
public let type: Type
public let type: UpdateType

///Duration of the update process.
public let duration: TimeInterval?
Expand All @@ -34,8 +34,8 @@ public struct Update: Codable, Equatable {
///Date when the update has been processed.
public let processedAt: Date?

///Typr of `Update`.
public struct `Type`: Codable, Equatable {
///Type of `Update`
public struct UpdateType: Codable, Equatable {

// MARK: Properties

Expand All @@ -49,4 +49,33 @@ public struct Update: Codable, Equatable {

}

public enum Status: Codable, Equatable {

case enqueued
case processed
case failed

public enum StatusError: Error {
case unknown
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawStatus = try container.decode(String.self)
switch rawStatus {
case "enqueued":
self = .enqueued
case "processed":
self = .processed
case "failed":
self = .failed
default:
throw StatusError.unknown
}
}

public func encode(to encoder: Encoder) throws { }

}

}
3 changes: 1 addition & 2 deletions Tests/MeiliSearchIntegrationTests/DocumentsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class DocumentsTests: XCTestCase {
}
self.wait(for: [getExpectation], timeout: 3.0)
}

func testAddAndGetOneDocumentWithIntIdentifierAndSucceed() {

let movie: Movie = Movie(id: 10, title: "test", comment: "test movie")
Expand Down Expand Up @@ -240,7 +240,6 @@ class DocumentsTests: XCTestCase {

case .success(let update):


XCTAssertEqual(Update(updateId: 0), update)

Thread.sleep(forTimeInterval: 1.0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/MeiliSearchIntegrationTests/SettingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SettingsTests: XCTestCase {
private let defaultAttributesForFaceting: [String] = []
private let defaultStopWords: [String] = []
private let defaultSynonyms: [String: [String]] = [:]
private var defaultGlobalSettings: Setting? = nil
private var defaultGlobalSettings: Setting?

// MARK: Setup

Expand Down
11 changes: 6 additions & 5 deletions Tests/MeiliSearchIntegrationTests/UpdatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class UpdatesTests: XCTestCase {
switch result {
case .success(let update):

self.client.getUpdate(UID: self.uid, update) { result in
self.client.getUpdate(UID: self.uid, update) { (result: Result<Update.Result, Swift.Error>) in

switch result {
case .success(let update):
XCTAssertTrue(["enqueued", "processed", "fail"].contains(update.status))
XCTAssertEqual("DocumentsAddition", update.type.name)
XCTAssertTrue(update.type.number >= 0)
case .failure(let error):
print(error)
XCTFail()
Expand Down Expand Up @@ -102,13 +103,13 @@ class UpdatesTests: XCTestCase {
self.client.addDocuments(UID: self.uid, documents: documents, primaryKey: nil) { _ in }
}

self.client.getAllUpdates(UID: self.uid) { result in
self.client.getAllUpdates(UID: self.uid) { (result: Result<[Update.Result], Swift.Error>) in

switch result {
case .success(let updates):
let statuses: [String] = ["enqueued", "processed", "fail"]
updates.forEach { (update: Update.Result) in
XCTAssertTrue(statuses.contains(update.status))
XCTAssertEqual("DocumentsAddition", update.type.name)
XCTAssertTrue(update.type.number >= 0)
}

case .failure(let error):
Expand Down
48 changes: 45 additions & 3 deletions Tests/MeiliSearchUnitTests/UpdatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class UpdatesTests: XCTestCase {

let data = json.data(using: .utf8)!

let stubResult: Update.Result = try! Constants.customJSONDecoder.decode(Update.Result.self, from: data)
let stubResult: Update.Result = try! Constants.customJSONDecoder.decode(
Update.Result.self, from: data)

session.pushData(json)

Expand All @@ -46,10 +47,51 @@ class UpdatesTests: XCTestCase {
switch result {
case .success(let result):
XCTAssertEqual(stubResult, result)
expectation.fulfill()
case .failure:
XCTFail("Failed to get settings")
}
expectation.fulfill()
}

self.wait(for: [expectation], timeout: 1.0)

}

func testGetUpdateInvalidStatus() {

//Prepare the mock server

let badStatusUpdateJson = """
{
"status": "something",
"updateId": 1,
"type": {
"name": "DocumentsAddition",
"number": 4
},
"duration": 0.076980613,
"enqueuedAt": "2019-12-07T21:16:09.623944Z",
"processedAt": "2019-12-07T21:16:09.703509Z"
}
"""

session.pushData(badStatusUpdateJson)

// Start the test with the mocked server

let UID: String = "movies"
let update = Update(updateId: 1)

let expectation = XCTestExpectation(description: "Get settings")

self.client.getUpdate(UID: UID, update) { result in
switch result {
case .success:
XCTFail("The server send a invalid status and it should not succeed")
case .failure(let error):
XCTAssertTrue(error is Update.Status.StatusError)
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: 1.0)
Expand Down Expand Up @@ -92,10 +134,10 @@ class UpdatesTests: XCTestCase {
switch result {
case .success(let results):
XCTAssertEqual(stubResults, results)
expectation.fulfill()
case .failure:
XCTFail("Failed to get settings")
}
expectation.fulfill()
}

self.wait(for: [expectation], timeout: 1.0)
Expand Down

0 comments on commit e294c39

Please sign in to comment.