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 12, 2020
2 parents 1f138d1 + 3d3ad75 commit fb06fbe
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 14 deletions.
47 changes: 43 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,43 @@ public struct Update: Codable, Equatable {

}

public enum Status: Codable, Equatable {

case enqueued
case processed
case failed

public enum CodingError: Error {
case unknownStatus
}

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 CodingError.unknownStatus
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .enqueued:
try container.encode("enqueued")
case .processed:
try container.encode("processed")
case .failed:
try container.encode("failed")
}
}

}

}
11 changes: 4 additions & 7 deletions Tests/MeiliSearchIntegrationTests/UpdatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class UpdatesTests: XCTestCase {
self.client.getUpdate(UID: self.uid, update) { result in

switch result {
case .success(let update):
XCTAssertTrue(["enqueued", "processed", "fail"].contains(update.status))
case .success:
break
case .failure(let error):
print(error)
XCTFail()
Expand Down Expand Up @@ -105,11 +105,8 @@ class UpdatesTests: XCTestCase {
self.client.getAllUpdates(UID: self.uid) { result in

switch result {
case .success(let updates):
let statuses: [String] = ["enqueued", "processed", "fail"]
updates.forEach { (update: Update.Result) in
XCTAssertTrue(statuses.contains(update.status))
}
case .success:
break

case .failure(let error):
print(error)
Expand Down
71 changes: 68 additions & 3 deletions Tests/MeiliSearchUnitTests/UpdatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ class UpdatesTests: XCTestCase {
client = try! MeiliSearch(Config(hostURL: nil, session: session))
}

func testEncodeStatusResult() {

let updateResult = Update.Result(
status: Update.Status.enqueued,
updateId: 456,
type: Update.Result.UpdateType(name: "DocumentsAddition", number: 123),
duration: TimeInterval.zero,
enqueuedAt: Date(timeIntervalSince1970: 0),
processedAt: nil)

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
encoder.dateEncodingStrategy = .formatted(Formatter.iso8601)

let jsonData: Data = try! encoder.encode(updateResult)

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

XCTAssertEqual(updateResult, decoded)
}

func testGetUpdate() {

//Prepare the mock server
Expand All @@ -31,7 +54,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 +70,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 json = """
{
"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(json)

// 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.CodingError)
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: 1.0)
Expand Down Expand Up @@ -92,10 +157,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 fb06fbe

Please sign in to comment.