Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the update status as enum and rename Type into UpdateType in Update model #71

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
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")
}
}
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

}

}
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
ppamorim marked this conversation as resolved.
Show resolved Hide resolved
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
ppamorim marked this conversation as resolved.
Show resolved Hide resolved

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 = """
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved
{
"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)
bidoubiwa marked this conversation as resolved.
Show resolved Hide resolved

// 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