|
| 1 | +import XCTest |
| 2 | +@testable import Typesense |
| 3 | + |
| 4 | +final class OperationTests: XCTestCase { |
| 5 | + func testGetHealth() async { |
| 6 | + let newConfig = Configuration(nodes: [Node(host: "localhost", port: "8108", nodeProtocol: "http")], apiKey: "xyz", logger: Logger(debugMode: true)) |
| 7 | + let myClient = Client(config: newConfig) |
| 8 | + |
| 9 | + do { |
| 10 | + let (data, _) = try await myClient.operations().getHealth() |
| 11 | + XCTAssertNotNil(data) |
| 12 | + guard let validData = data else { |
| 13 | + throw DataError.dataNotFound |
| 14 | + } |
| 15 | + print(validData) |
| 16 | + XCTAssertEqual(validData.ok, true) |
| 17 | + } catch HTTPError.serverError(let code, let desc) { |
| 18 | + print(desc) |
| 19 | + print("The response status code is \(code)") |
| 20 | + XCTAssertTrue(false) |
| 21 | + } catch (let error) { |
| 22 | + print(error.localizedDescription) |
| 23 | + XCTAssertTrue(false) //To prevent this, check availability of Typesense Server and retry |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + func testGetStats() async { |
| 28 | + let newConfig = Configuration(nodes: [Node(host: "localhost", port: "8108", nodeProtocol: "http")], apiKey: "xyz", logger: Logger(debugMode: true)) |
| 29 | + let myClient = Client(config: newConfig) |
| 30 | + |
| 31 | + do { |
| 32 | + let (data, _) = try await myClient.operations().getStats() |
| 33 | + XCTAssertNotNil(data) |
| 34 | + guard let validData = data else { |
| 35 | + throw DataError.dataNotFound |
| 36 | + } |
| 37 | + print(String(data: validData, encoding: .utf8)!) |
| 38 | + } catch HTTPError.serverError(let code, let desc) { |
| 39 | + print(desc) |
| 40 | + print("The response status code is \(code)") |
| 41 | + XCTAssertTrue(false) |
| 42 | + } catch (let error) { |
| 43 | + print(error.localizedDescription) |
| 44 | + XCTAssertTrue(false) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func testGetMetrics() async { |
| 49 | + let newConfig = Configuration(nodes: [Node(host: "localhost", port: "8108", nodeProtocol: "http")], apiKey: "xyz", logger: Logger(debugMode: true)) |
| 50 | + let myClient = Client(config: newConfig) |
| 51 | + |
| 52 | + do { |
| 53 | + let (data, _) = try await myClient.operations().getMetrics() |
| 54 | + XCTAssertNotNil(data) |
| 55 | + guard let validData = data else { |
| 56 | + throw DataError.dataNotFound |
| 57 | + } |
| 58 | + print(String(data: validData, encoding: .utf8)!) |
| 59 | + } catch HTTPError.serverError(let code, let desc) { |
| 60 | + print(desc) |
| 61 | + print("The response status code is \(code)") |
| 62 | + XCTAssertTrue(false) |
| 63 | + } catch (let error) { |
| 64 | + print(error.localizedDescription) |
| 65 | + XCTAssertTrue(false) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + func testVote() async { |
| 70 | + let newConfig = Configuration(nodes: [Node(host: "localhost", port: "8108", nodeProtocol: "http")], apiKey: "xyz", logger: Logger(debugMode: true)) |
| 71 | + let myClient = Client(config: newConfig) |
| 72 | + |
| 73 | + do { |
| 74 | + let (data, _) = try await myClient.operations().vote() |
| 75 | + XCTAssertNotNil(data) |
| 76 | + guard let validData = data else { |
| 77 | + throw DataError.dataNotFound |
| 78 | + } |
| 79 | + print(validData) |
| 80 | + XCTAssertNotNil(validData.success) |
| 81 | + } catch HTTPError.serverError(let code, let desc) { |
| 82 | + print(desc) |
| 83 | + print("The response status code is \(code)") |
| 84 | + XCTAssertTrue(false) |
| 85 | + } catch (let error) { |
| 86 | + print(error.localizedDescription) |
| 87 | + XCTAssertTrue(false) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + func testSnapshot() async { |
| 92 | + let newConfig = Configuration(nodes: [Node(host: "localhost", port: "8108", nodeProtocol: "http")], apiKey: "xyz", logger: Logger(debugMode: true)) |
| 93 | + let myClient = Client(config: newConfig) |
| 94 | + |
| 95 | + do { |
| 96 | + let (data, _) = try await myClient.operations().snapshot(path: "/tmp/typesense-data-snapshot") |
| 97 | + XCTAssertNotNil(data) |
| 98 | + guard let validData = data else { |
| 99 | + throw DataError.dataNotFound |
| 100 | + } |
| 101 | + print(validData) |
| 102 | + XCTAssertNotNil(validData.success) |
| 103 | + } catch HTTPError.serverError(let code, let desc) { |
| 104 | + print(desc) |
| 105 | + print("The response status code is \(code)") |
| 106 | + XCTAssertTrue(false) |
| 107 | + } catch (let error) { |
| 108 | + print(error.localizedDescription) |
| 109 | + XCTAssertTrue(false) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + func testSlowRequestLog() async { |
| 114 | + let newConfig = Configuration(nodes: [Node(host: "localhost", port: "8108", nodeProtocol: "http")], apiKey: "xyz", logger: Logger(debugMode: true)) |
| 115 | + let myClient = Client(config: newConfig) |
| 116 | + |
| 117 | + do { |
| 118 | + let (data, _) = try await myClient.operations().toggleSlowRequestLog(seconds: 2) |
| 119 | + XCTAssertNotNil(data) |
| 120 | + guard let validData = data else { |
| 121 | + throw DataError.dataNotFound |
| 122 | + } |
| 123 | + print(validData) |
| 124 | + XCTAssertNotNil(validData.success) |
| 125 | + } catch HTTPError.serverError(let code, let desc) { |
| 126 | + print(desc) |
| 127 | + print("The response status code is \(code)") |
| 128 | + XCTAssertTrue(false) |
| 129 | + } catch (let error) { |
| 130 | + print(error.localizedDescription) |
| 131 | + XCTAssertTrue(false) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments