Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 committed Jan 12, 2023
1 parent d411546 commit 948335f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Types/ParseHealth+combine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public extension ParseHealth {
switch result {
case .success(let status):
subject.send(status)
if status == .ok {
if status == .ok || status == .error {
subject.send(completion: .finished)
}
case .failure(let error):
Expand Down
62 changes: 38 additions & 24 deletions Tests/ParseSwiftTests/APICommandMultipleAttemptsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import FoundationNetworking
import XCTest
@testable import ParseSwift

// swiftlint:disable function_body_length

// swiftlint:disable:next type_body_length
class APICommandMultipleAttemptsTests: XCTestCase {
struct Level: ParseObject {
Expand Down Expand Up @@ -72,7 +74,7 @@ class APICommandMultipleAttemptsTests: XCTestCase {
XCTAssertLessThan(date.timeIntervalSinceNow - computedDate, 1)
}

func testErrorHTTP400JSON() async throws {
func testErrorHTTP400JSON() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand All @@ -95,7 +97,7 @@ class APICommandMultipleAttemptsTests: XCTestCase {
}
}

let expectation1 = XCTestExpectation(description: "Wait")
let expectation1 = XCTestExpectation(description: "Wait 1")

API.NonParseBodyCommand<NoBody, NoBody>(method: .GET,
path: .login,
Expand All @@ -114,8 +116,10 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
Expand Down Expand Up @@ -151,7 +155,7 @@ class APICommandMultipleAttemptsTests: XCTestCase {
wait(for: [expectation1], timeout: 20.0)
}

func testErrorHTTP429JSONInterval() async throws {
func testErrorHTTP429JSONInterval() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -196,17 +200,18 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
}
wait(for: [expectation1], timeout: 20.0)
}

// swiftlint:disable:next function_body_length
func testErrorHTTP429JSONDate() async throws {
func testErrorHTTP429JSONDate() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -259,16 +264,18 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
}
wait(for: [expectation1], timeout: 20.0)
}

func testErrorHTTP429JSONNoHeader() async throws {
func testErrorHTTP429JSONNoHeader() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -310,16 +317,18 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
}
wait(for: [expectation1], timeout: 20.0)
}

func testErrorHTTP503JSONInterval() async throws {
func testErrorHTTP503JSONInterval() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -364,17 +373,18 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
}
wait(for: [expectation1], timeout: 20.0)
}

// swiftlint:disable:next function_body_length
func testErrorHTTP503JSONDate() async throws {
func testErrorHTTP503JSONDate() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -427,16 +437,18 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
}
wait(for: [expectation1], timeout: 20.0)
}

func testErrorHTTP503JSONNoHeader() async throws {
func testErrorHTTP503JSONNoHeader() throws {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
let errorValue = "yarr"
Expand Down Expand Up @@ -478,8 +490,10 @@ class APICommandMultipleAttemptsTests: XCTestCase {
Task {
await currentAttempts.incrementAttempts()
let current = await currentAttempts.attempts
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
DispatchQueue.main.async {
if current >= Parse.configuration.maxConnectionAttempts {
expectation1.fulfill()
}
}
}
}
Expand Down
43 changes: 39 additions & 4 deletions Tests/ParseSwiftTests/ParseHealthCombineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class ParseHealthCombineTests: XCTestCase {

func testCheckOk() {
var subscriptions = Set<AnyCancellable>()
let expectation1 = XCTestExpectation(description: "Save")
let expectation1 = XCTestExpectation(description: "Received Value")
let expectation2 = XCTestExpectation(description: "Received Complete")

let healthOfServer = ParseHealth.Status.ok
let serverResponse = HealthResponse(status: healthOfServer)
Expand All @@ -59,19 +60,53 @@ class ParseHealthCombineTests: XCTestCase {
if case let .failure(error) = result {
XCTFail(error.localizedDescription)
}
expectation2.fulfill()
}, receiveValue: { health in
XCTAssertEqual(health, healthOfServer)
expectation1.fulfill()
})
.store(in: &subscriptions)

wait(for: [expectation1, expectation2], timeout: 20.0)
}

func testCheckError() {
var subscriptions = Set<AnyCancellable>()
let expectation1 = XCTestExpectation(description: "Received Value")
let expectation2 = XCTestExpectation(description: "Received Complete")

let healthOfServer = ParseHealth.Status.error
let serverResponse = HealthResponse(status: healthOfServer)
let encoded: Data!
do {
encoded = try ParseCoding.jsonEncoder().encode(serverResponse)
} catch {
XCTFail("Should encode/decode. Error \(error)")
return
}

MockURLProtocol.mockRequests { _ in
return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
}

ParseHealth.checkPublisher()
.sink(receiveCompletion: { result in
if case let .failure(error) = result {
XCTFail(error.localizedDescription)
}
expectation2.fulfill()
}, receiveValue: { health in
XCTAssertEqual(health, healthOfServer)
expectation1.fulfill()
})
.store(in: &subscriptions)

wait(for: [expectation1], timeout: 20.0)
wait(for: [expectation1, expectation2], timeout: 20.0)
}

func testCheckInitialized() {
var subscriptions = Set<AnyCancellable>()
let expectation1 = XCTestExpectation(description: "Save")
let expectation1 = XCTestExpectation(description: "Received Value")

let healthOfServer = ParseHealth.Status.initialized
let serverResponse = HealthResponse(status: healthOfServer)
Expand Down Expand Up @@ -105,7 +140,7 @@ class ParseHealthCombineTests: XCTestCase {

func testCheckStarting() {
var subscriptions = Set<AnyCancellable>()
let expectation1 = XCTestExpectation(description: "Save")
let expectation1 = XCTestExpectation(description: "Received Value")

let healthOfServer = ParseHealth.Status.starting
let serverResponse = HealthResponse(status: healthOfServer)
Expand Down
4 changes: 3 additions & 1 deletion Tests/ParseSwiftTests/ParseLiveQueryAsyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ParseLiveQueryAsyncTests: XCTestCase {
clientKey: "clientKey",
primaryKey: "primaryKey",
serverURL: url,
testing: true)
liveQueryMaxConnectionAttempts: 1,
testing: true,
testLiveQueryDontCloseSocket: true)
ParseLiveQuery.defaultClient = try ParseLiveQuery(isDefault: true)
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/ParseSwiftTests/ParseLiveQueryCombineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class ParseLiveQueryCombineTests: XCTestCase {
clientKey: "clientKey",
primaryKey: "primaryKey",
serverURL: url,
testing: true)
liveQueryMaxConnectionAttempts: 1,
testing: true,
testLiveQueryDontCloseSocket: true)
ParseLiveQuery.defaultClient = try ParseLiveQuery(isDefault: true)
}

Expand Down

0 comments on commit 948335f

Please sign in to comment.