Skip to content

Commit 524c5df

Browse files
committed
Updated Tests to Use Async
1 parent b16aeda commit 524c5df

File tree

4 files changed

+42
-107
lines changed

4 files changed

+42
-107
lines changed

Tests/MeiliSearchUnitTests/SearchTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class SearchTests: XCTestCase {
8080

8181
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
8282
}
83-
83+
8484
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
8585
func testSearchForBotmanMovieAsync() async throws {
8686
let jsonString = """
@@ -108,15 +108,15 @@ class SearchTests: XCTestCase {
108108
"query": "botman"
109109
}
110110
"""
111-
111+
112112
// Prepare the mock server
113113
let data = jsonString.data(using: .utf8)!
114114
let stubSearchResult: Searchable<Movie> = try! Constants.customJSONDecoder.decode(Searchable<Movie>.self, from: data)
115115
session.pushData(jsonString)
116-
116+
117117
// Start the test with the mocked server
118118
let searchParameters = SearchParameters.query("botman")
119-
119+
120120
let searchResult: Searchable<Movie> = try await self.index.search(searchParameters)
121121
XCTAssertEqual(stubSearchResult, searchResult)
122122
}
Lines changed: 28 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
@testable import MeiliSearch
22
import XCTest
33

4-
// swiftlint:disable force_unwrapping
5-
64
class SystemTests: XCTestCase {
75
private var client: MeiliSearch!
86

@@ -13,118 +11,64 @@ class SystemTests: XCTestCase {
1311
client = try MeiliSearch(host: "http://localhost:7700", apiKey: "masterKey", session: session)
1412
}
1513

16-
func testHealthStatusAvailable() throws {
14+
func testHealthStatusAvailable() async throws {
1715
// Prepare the mock server
18-
1916
let jsonString = """
20-
{
21-
"status": "available"
22-
}
23-
"""
24-
25-
let jsonData = jsonString.data(using: .utf8)!
26-
27-
let expectedHealthBody: Health = try Constants.customJSONDecoder.decode(Health.self, from: jsonData)
17+
{
18+
"status": "available"
19+
}
20+
"""
2821

22+
let expectedHealthBody: Health = try decodeJSON(from: jsonString)
2923
session.pushData(jsonString, code: 200)
3024

3125
// Start the test with the mocked server
32-
33-
let expectation = XCTestExpectation(description: "Check body of health server on health method")
34-
35-
self.client.health { result in
36-
switch result {
37-
case .success(let body):
38-
XCTAssertEqual(expectedHealthBody, body)
39-
expectation.fulfill()
40-
case .failure:
41-
XCTFail("Failed on available status check on health method")
42-
}
43-
}
44-
45-
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
26+
let body = try await self.client.health()
27+
XCTAssertEqual(expectedHealthBody, body)
4628
}
4729

48-
func testIsHealthyTrue() {
30+
func testIsHealthyTrue() async throws {
4931
// Prepare the mock server
50-
5132
let jsonString = """
52-
{
53-
"status": "available"
54-
}
55-
"""
33+
{
34+
"status": "available"
35+
}
36+
"""
5637

5738
session.pushData(jsonString, code: 200)
5839

5940
// Start the test with the mocked server
6041

6142
let expectation = XCTestExpectation(description: "Check if is healthy is true")
6243

63-
self.client.isHealthy { result in
64-
if result == true {
65-
XCTAssertEqual(result, true)
66-
expectation.fulfill()
67-
} else {
68-
XCTFail("Failed on isHealthy should be true")
69-
}
70-
}
71-
72-
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
44+
let result = await self.client.isHealthy()
45+
XCTAssertTrue(result)
7346
}
7447

75-
func testIsHealthyFalse() {
48+
func testIsHealthyFalse() async throws {
7649
// Prepare the mock server
77-
7850
session.pushData("", code: 400)
7951

8052
// Start the test with the mocked server
81-
82-
let expectation = XCTestExpectation(description: "Check if is healthy is false")
83-
84-
self.client.isHealthy { result in
85-
if result == false {
86-
XCTAssertEqual(result, false)
87-
expectation.fulfill()
88-
} else {
89-
XCTFail("Failed on isHealthy should be false")
90-
}
91-
}
92-
93-
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
53+
let result = await self.client.isHealthy()
54+
XCTAssertFalse(result)
9455
}
9556

96-
func testVersion() throws {
57+
func testVersion() async throws {
9758
// Prepare the mock server
98-
9959
let jsonString = """
100-
{
101-
"commitSha": "b46889b5f0f2f8b91438a08a358ba8f05fc09fc1",
102-
"commitDate": "2019-11-15T09:51:54.278247+00:00",
103-
"pkgVersion": "0.1.1"
104-
}
105-
"""
106-
107-
let jsonData = jsonString.data(using: .utf8)!
108-
109-
let stubVersion: Version = try Constants.customJSONDecoder.decode(Version.self, from: jsonData)
60+
{
61+
"commitSha": "b46889b5f0f2f8b91438a08a358ba8f05fc09fc1",
62+
"commitDate": "2019-11-15T09:51:54.278247+00:00",
63+
"pkgVersion": "0.1.1"
64+
}
65+
"""
11066

67+
let stubVersion: Version = try decodeJSON(from: jsonString)
11168
session.pushData(jsonString)
11269

11370
// Start the test with the mocked server
114-
115-
let expectation = XCTestExpectation(description: "Load server version")
116-
117-
self.client.version { result in
118-
switch result {
119-
case .success(let version):
120-
XCTAssertEqual(stubVersion, version)
121-
expectation.fulfill()
122-
case .failure:
123-
XCTFail("Failed to load server version")
124-
}
125-
}
126-
127-
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
71+
let version = try await client.version()
72+
XCTAssertEqual(stubVersion, version)
12873
}
12974
}
130-
// swiftlint:enable force_unwrapping

Tests/MeiliSearchUnitTests/TasksTests.swift

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TasksTests: XCTestCase {
1313
index = client.index(self.uid)
1414
}
1515

16-
func testGetTasksWithParametersFromClient() {
16+
func testGetTasksWithParametersFromClient() async throws {
1717
let jsonString = """
1818
{
1919
"results": [],
@@ -28,24 +28,10 @@ class TasksTests: XCTestCase {
2828
session.pushData(jsonString)
2929

3030
// Start the test with the mocked server
31-
let expectation = XCTestExpectation(description: "Get keys with parameters")
31+
let params = TasksQuery(limit: 20, from: 5, next: 98, types: [.indexCreation])
32+
_ = try await self.client.getTasks(params: params)
3233

33-
self.client.getTasks(params: TasksQuery(limit: 20, from: 5, next: 98, types: [.indexCreation])) { result in
34-
switch result {
35-
case .success:
36-
let requestQuery = self.session.nextDataTask.request?.url?.query
37-
38-
XCTAssertEqual(requestQuery, "from=5&limit=20&next=98&types=indexCreation")
39-
40-
expectation.fulfill()
41-
case .failure(let error):
42-
dump(error)
43-
XCTFail("Failed to get all Indexes")
44-
expectation.fulfill()
45-
}
46-
}
47-
48-
self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
34+
let requestQuery = self.session.nextDataTask.request?.url?.query
35+
XCTAssertEqual(requestQuery, "from=5&limit=20&next=98&types=indexCreation")
4936
}
5037
}
51-
// swiftlint:enable force_unwrapping

Tests/MeiliSearchUnitTests/Utils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ import XCTest
66
@testable import MeiliSearch
77

88
public let TESTS_TIME_OUT = 10.0
9+
10+
func decodeJSON<T: Decodable>(from string: String) throws -> T {
11+
let data = Data(string.utf8)
12+
return try Constants.customJSONDecoder.decode(T.self, from: data)
13+
}

0 commit comments

Comments
 (0)