Skip to content

Generate Async Overloads for All Public Functions #427

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

Merged
merged 6 commits into from
Nov 2, 2023
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
8 changes: 4 additions & 4 deletions Tests/MeiliSearchUnitTests/SearchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SearchTests: XCTestCase {

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

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
func testSearchForBotmanMovieAsync() async throws {
let jsonString = """
Expand Down Expand Up @@ -108,15 +108,15 @@ class SearchTests: XCTestCase {
"query": "botman"
}
"""

// Prepare the mock server
let data = jsonString.data(using: .utf8)!
let stubSearchResult: Searchable<Movie> = try! Constants.customJSONDecoder.decode(Searchable<Movie>.self, from: data)
session.pushData(jsonString)

// Start the test with the mocked server
let searchParameters = SearchParameters.query("botman")

let searchResult: Searchable<Movie> = try await self.index.search(searchParameters)
XCTAssertEqual(stubSearchResult, searchResult)
}
Expand Down
115 changes: 28 additions & 87 deletions Tests/MeiliSearchUnitTests/SystemTests.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@testable import MeiliSearch
import XCTest

// swiftlint:disable force_unwrapping

class SystemTests: XCTestCase {
private var client: MeiliSearch!

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

func testHealthStatusAvailable() throws {
func testHealthStatusAvailable() async throws {
// Prepare the mock server

let jsonString = """
{
"status": "available"
}
"""

let jsonData = jsonString.data(using: .utf8)!

let expectedHealthBody: Health = try Constants.customJSONDecoder.decode(Health.self, from: jsonData)
{
"status": "available"
}
"""

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

// Start the test with the mocked server

let expectation = XCTestExpectation(description: "Check body of health server on health method")

self.client.health { result in
switch result {
case .success(let body):
XCTAssertEqual(expectedHealthBody, body)
expectation.fulfill()
case .failure:
XCTFail("Failed on available status check on health method")
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
let body = try await self.client.health()
XCTAssertEqual(expectedHealthBody, body)
}

func testIsHealthyTrue() {
func testIsHealthyTrue() async throws {
// Prepare the mock server

let jsonString = """
{
"status": "available"
}
"""
{
"status": "available"
}
"""

session.pushData(jsonString, code: 200)

// Start the test with the mocked server

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

self.client.isHealthy { result in
if result == true {
XCTAssertEqual(result, true)
expectation.fulfill()
} else {
XCTFail("Failed on isHealthy should be true")
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
let result = await self.client.isHealthy()
XCTAssertTrue(result)
}

func testIsHealthyFalse() {
func testIsHealthyFalse() async throws {
// Prepare the mock server

session.pushData("", code: 400)

// Start the test with the mocked server

let expectation = XCTestExpectation(description: "Check if is healthy is false")

self.client.isHealthy { result in
if result == false {
XCTAssertEqual(result, false)
expectation.fulfill()
} else {
XCTFail("Failed on isHealthy should be false")
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
let result = await self.client.isHealthy()
XCTAssertFalse(result)
}

func testVersion() throws {
func testVersion() async throws {
// Prepare the mock server

let jsonString = """
{
"commitSha": "b46889b5f0f2f8b91438a08a358ba8f05fc09fc1",
"commitDate": "2019-11-15T09:51:54.278247+00:00",
"pkgVersion": "0.1.1"
}
"""

let jsonData = jsonString.data(using: .utf8)!

let stubVersion: Version = try Constants.customJSONDecoder.decode(Version.self, from: jsonData)
{
"commitSha": "b46889b5f0f2f8b91438a08a358ba8f05fc09fc1",
"commitDate": "2019-11-15T09:51:54.278247+00:00",
"pkgVersion": "0.1.1"
}
"""

let stubVersion: Version = try decodeJSON(from: jsonString)
session.pushData(jsonString)

// Start the test with the mocked server

let expectation = XCTestExpectation(description: "Load server version")

self.client.version { result in
switch result {
case .success(let version):
XCTAssertEqual(stubVersion, version)
expectation.fulfill()
case .failure:
XCTFail("Failed to load server version")
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
let version = try await client.version()
XCTAssertEqual(stubVersion, version)
}
}
// swiftlint:enable force_unwrapping
24 changes: 5 additions & 19 deletions Tests/MeiliSearchUnitTests/TasksTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TasksTests: XCTestCase {
index = client.index(self.uid)
}

func testGetTasksWithParametersFromClient() {
func testGetTasksWithParametersFromClient() async throws {
let jsonString = """
{
"results": [],
Expand All @@ -28,24 +28,10 @@ class TasksTests: XCTestCase {
session.pushData(jsonString)

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

self.client.getTasks(params: TasksQuery(limit: 20, from: 5, next: 98, types: [.indexCreation])) { result in
switch result {
case .success:
let requestQuery = self.session.nextDataTask.request?.url?.query

XCTAssertEqual(requestQuery, "from=5&limit=20&next=98&types=indexCreation")

expectation.fulfill()
case .failure(let error):
dump(error)
XCTFail("Failed to get all Indexes")
expectation.fulfill()
}
}

self.wait(for: [expectation], timeout: TESTS_TIME_OUT)
let requestQuery = self.session.nextDataTask.request?.url?.query
XCTAssertEqual(requestQuery, "from=5&limit=20&next=98&types=indexCreation")
}
}
// swiftlint:enable force_unwrapping
5 changes: 5 additions & 0 deletions Tests/MeiliSearchUnitTests/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ import XCTest
@testable import MeiliSearch

public let TESTS_TIME_OUT = 10.0

func decodeJSON<T: Decodable>(from string: String) throws -> T {
let data = Data(string.utf8)
return try Constants.customJSONDecoder.decode(T.self, from: data)
}