Skip to content

Commit

Permalink
chore: refactor test to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed May 13, 2024
1 parent 2071894 commit dbe487e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 80 deletions.
26 changes: 16 additions & 10 deletions Tests/InfluxDBSwiftApisTests/InfluxDB2APITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ final class InfluxDB2APITests: XCTestCase {
}
}

override func tearDownWithError() throws {
if let client = client {
client.close()
}
}

func testCreateInstance() {
let api = InfluxDB2API(client: client!)
XCTAssertNotNil(api)
Expand Down Expand Up @@ -79,13 +85,13 @@ class APIXCTestCase: XCTestCase {
XCTFail("Request is not defined!")
}

let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

if let request = request {
request(nil, nil, checkResponse(check: checker, expectation: expectation))
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}

func checkPost<BodyType: Codable, ResponseType: Codable>(_ request: ((BodyType,
Expand All @@ -100,13 +106,13 @@ class APIXCTestCase: XCTestCase {
return
}

let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

if let request = request {
request(body, nil, checkResponse(check: checker, expectation: expectation))
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}

func checkPost<BodyType: Codable, ResponseType: Codable>(_ request: ((BodyType,
Expand All @@ -122,13 +128,13 @@ class APIXCTestCase: XCTestCase {
return
}

let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

if let request = request {
request(body, nil, nil, checkResponse(check: checker, expectation: expectation))
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}

private func checkResponse<ResponseType: Codable>(check: @escaping (ResponseType) -> Void,
Expand All @@ -153,7 +159,7 @@ class APIXCTestCase: XCTestCase {
guard Self.orgID.isEmpty else {
return
}
let expectation = self.expectation(description: "Cannot find my-org")
let expectation = XCTestExpectation(description: "Cannot find my-org")
api.organizationsAPI.getOrgs(limit: 100) { organizations, error in
if let error = error {
XCTFail("\(error)")
Expand All @@ -165,14 +171,14 @@ class APIXCTestCase: XCTestCase {
expectation.fulfill()
}
}
waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}

private func findMyBucket() {
guard Self.bucketID.isEmpty else {
return
}
let expectation = self.expectation(description: "Cannot find my-bucket")
let expectation = XCTestExpectation(description: "Cannot find my-bucket")
api.bucketsAPI.getBuckets(limit: 100) { response, error in
if let error = error {
XCTFail("\(error)")
Expand All @@ -184,7 +190,7 @@ class APIXCTestCase: XCTestCase {
expectation.fulfill()
}
}
waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/InfluxDBSwiftApisTests/PingAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import XCTest

final class PingAPITests: APIXCTestCase {
func testPing() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

api.pingAPI.getPing { headers, error -> Void in
if let error = error {
Expand All @@ -23,7 +23,7 @@ final class PingAPITests: APIXCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}

#if swift(>=5.5)
Expand Down
4 changes: 2 additions & 2 deletions Tests/InfluxDBSwiftApisTests/SecretsAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SecretsAPITests: APIXCTestCase {
func testCreateSecret() {
let request = [generateName("secret"): generateName("secret")]

let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

self.api.secretsAPI.patchOrgsIDSecrets(orgID: Self.orgID, requestBody: request) { _, error in
if let error = error {
Expand All @@ -35,6 +35,6 @@ class SecretsAPITests: APIXCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}
}
24 changes: 12 additions & 12 deletions Tests/InfluxDBSwiftTests/DeleteAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class DeleteAPITests: XCTestCase {
}

func testBucketOrgParameters() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 2

MockURLProtocol.handler = { request, _ in
Expand All @@ -53,11 +53,11 @@ final class DeleteAPITests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

func testBucketIDOrgIDParameters() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 2

MockURLProtocol.handler = { request, _ in
Expand All @@ -80,11 +80,11 @@ final class DeleteAPITests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

func testWithoutBucketOrgParameters() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 2

MockURLProtocol.handler = { request, _ in
Expand All @@ -106,11 +106,11 @@ final class DeleteAPITests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

func testPredicateRequestSerialization() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 2

MockURLProtocol.handler = { _, bodyData in
Expand Down Expand Up @@ -140,11 +140,11 @@ final class DeleteAPITests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

func testErrorResponse() {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 2

MockURLProtocol.handler = { request, _ in
Expand All @@ -165,12 +165,12 @@ final class DeleteAPITests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

#if swift(>=5.5)
func testDeleteAsync() async throws {
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 1

MockURLProtocol.handler = { _, _ in
Expand All @@ -187,7 +187,7 @@ final class DeleteAPITests: XCTestCase {

try await client.deleteAPI.delete(predicate: predicate)

await waitForExpectations(timeout: 1, handler: nil)
await wait(for: [expectation], timeout: 1)
}
#endif
}
Expand Down
14 changes: 8 additions & 6 deletions Tests/InfluxDBSwiftTests/InfluxDBClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class InfluxDBClientTests: XCTestCase {
options: InfluxDBClient.InfluxDBOptions(bucket: "my-bucket", org: "my-org"),
protocolClasses: [MockURLProtocol.self])

let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.expectedFulfillmentCount = 3

MockURLProtocol.handler = { request, _ in
Expand All @@ -126,12 +126,13 @@ final class InfluxDBClientTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

func testDisableRedirect() {
let expectation = self.expectation(description: "Redirect response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Redirect response from API doesn't arrive")
expectation.expectedFulfillmentCount = 2
expectation.assertForOverFulfill = false

class DisableRedirect: NSObject, URLSessionTaskDelegate {
let expectation: XCTestExpectation
Expand Down Expand Up @@ -174,12 +175,13 @@ final class InfluxDBClientTests: XCTestCase {
client.queryAPI.query(query: "from ...") { _, _ in
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 1)
}

func testHTTPLogging() {
TestLogHandler.content = ""
let expectation = self.expectation(description: "Success response from API doesn't arrive")
let expectation = XCTestExpectation(description: "Success response from API doesn't arrive")
expectation.assertForOverFulfill = false
LoggingSystem.bootstrap(TestLogHandler.init)

client = InfluxDBClient(url: Self.dbURL(), token: "my-token", debugging: true)
Expand All @@ -199,7 +201,7 @@ final class InfluxDBClientTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 1, handler: nil)
wait(for: [expectation], timeout: 5)

XCTAssertTrue(TestLogHandler.content.contains("Authorization: ***"), TestLogHandler.content)
}
Expand Down
24 changes: 12 additions & 12 deletions Tests/InfluxDBSwiftTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class IntegrationTests: XCTestCase {
}

func testQueryWriteIntegration() {
var expectation = self.expectation(description: "Success response from API doesn't arrive")
var expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

let measurement = "h2o_\(Date().timeIntervalSince1970)"

Expand All @@ -52,8 +52,8 @@ final class IntegrationTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
expectation = self.expectation(description: "Success response from API doesn't arrive")
wait(for: [expectation], timeout: 5)
expectation = XCTestExpectation(description: "Success response from API doesn't arrive")

let query = """
from(bucket: "my-bucket")
Expand Down Expand Up @@ -84,11 +84,11 @@ final class IntegrationTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}

func testDelete() {
var expectation = self.expectation(description: "Success response from Write API doesn't arrive")
var expectation = XCTestExpectation(description: "Success response from Write API doesn't arrive")

let measurement = "h2o_\(Date().timeIntervalSince1970)"

Expand Down Expand Up @@ -117,8 +117,8 @@ final class IntegrationTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
expectation = self.expectation(description: "Success response from Query API doesn't arrive")
wait(for: [expectation], timeout: 5)
expectation = XCTestExpectation(description: "Success response from Query API doesn't arrive")

let query = """
from(bucket: "my-bucket")
Expand Down Expand Up @@ -146,9 +146,9 @@ final class IntegrationTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)

expectation = self.expectation(description: "Success response from Delete API doesn't arrive")
expectation = XCTestExpectation(description: "Success response from Delete API doesn't arrive")

let predicate = DeletePredicateRequest(
start: Date(2019, 10, 5),
Expand All @@ -162,9 +162,9 @@ final class IntegrationTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)

expectation = self.expectation(description: "Success response from Query API doesn't arrive")
expectation = XCTestExpectation(description: "Success response from Query API doesn't arrive")

client.queryAPI.query(query: query) { response, error in
if let error = error {
Expand All @@ -185,6 +185,6 @@ final class IntegrationTests: XCTestCase {
expectation.fulfill()
}

waitForExpectations(timeout: 5, handler: nil)
wait(for: [expectation], timeout: 5)
}
}
Loading

0 comments on commit dbe487e

Please sign in to comment.