Skip to content

test(swift): add unit test cases for extension functions #4894

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
Show file tree
Hide file tree
Changes from 2 commits
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0


import XCTest
@testable import Amplify
@testable import amplify_datastore

class GraphQLRequestExtensionTests: XCTestCase {
func testToNativeGraphQLRequest_withCorrectData_convertToNativeGraphQLRequestSuccessfully() {

let graphQLRequest = GraphQLRequest(
apiName: UUID().uuidString,
document: UUID().uuidString,
variables: [UUID().uuidString: UUID().uuidString],
responseType: MutationEvent.self
)

let nativeGrqphQLRquest = graphQLRequest.toNativeGraphQLRequest()
XCTAssertEqual(graphQLRequest.apiName, nativeGrqphQLRquest.apiName)
XCTAssertEqual(graphQLRequest.document, nativeGrqphQLRquest.document)
XCTAssertEqual(String(describing: graphQLRequest.responseType), nativeGrqphQLRquest.responseType)
let graphQLVariablesJson = """
{"\(graphQLRequest.variables!.keys.first!)":"\(graphQLRequest.variables!.values.first!)"}
"""
XCTAssertEqual(graphQLVariablesJson, nativeGrqphQLRquest.variablesJson)
}

func testToNativeGraphQLRequest_withNilVariables_convertToNativeGraphQLRequestWithEmptyVariablesJsonObject() {
let graphQLRequest = GraphQLRequest(
apiName: UUID().uuidString,
document: UUID().uuidString,
variables: nil,
responseType: MutationEvent.self
)

let nativeGrqphQLRquest = graphQLRequest.toNativeGraphQLRequest()
XCTAssertEqual(graphQLRequest.apiName, nativeGrqphQLRquest.apiName)
XCTAssertEqual(graphQLRequest.document, nativeGrqphQLRquest.document)
XCTAssertEqual(String(describing: graphQLRequest.responseType), nativeGrqphQLRquest.responseType)
let graphQLVariablesJson = "{}"
XCTAssertEqual(graphQLVariablesJson, nativeGrqphQLRquest.variablesJson)
}

func testToNativeGraphQLRequest_withEmptyVariables_convertToNativeGraphQLRequestWithEmptyVariablesJsonObject() {
let graphQLRequest = GraphQLRequest(
apiName: UUID().uuidString,
document: UUID().uuidString,
variables: [:],
responseType: MutationEvent.self
)

let nativeGrqphQLRquest = graphQLRequest.toNativeGraphQLRequest()
XCTAssertEqual(graphQLRequest.apiName, nativeGrqphQLRquest.apiName)
XCTAssertEqual(graphQLRequest.document, nativeGrqphQLRquest.document)
XCTAssertEqual(String(describing: graphQLRequest.responseType), nativeGrqphQLRquest.responseType)
let graphQLVariablesJson = "{}"
XCTAssertEqual(graphQLVariablesJson, nativeGrqphQLRquest.variablesJson)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0


import XCTest
import Combine
@testable import amplify_datastore

class PublisherExtensionTests: XCTestCase {
func testToAmplifyAsyncThrowingSequence_createAsyncSequenceCorrect() async throws {
let subject = PassthroughSubject<Int, TestError>()
let (sequence, cancellable) = subject.eraseToAnyPublisher().toAmplifyAsyncThrowingSequence()

let expectation1 = expectation(description: "element 1 received")
let expectation2 = expectation(description: "element 2 received")
let expectation3 = expectation(description: "element 3 received")
Task {
for try await element in sequence {
switch element {
case 1: expectation1.fulfill()
case 2: expectation2.fulfill()
case 3: expectation3.fulfill()
default: break
}
}
}

Task {
subject.send(1)
subject.send(2)
subject.send(3)
subject.send(completion: .finished)
}

await fulfillment(of: [expectation1, expectation2, expectation3], timeout: 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are failing in CI with the message

❌  /Users/runner/work/amplify-flutter/amplify-flutter/packages/amplify_datastore/example/ios/unit_tests/PublisherExtensionTests.swift:39:15: cannot find 'fulfillment' in scope

Copy link
Author

@5d 5d May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing @Jordan-Nelson.

The API fulfillment mentioned in the error log was introduced in Xcode 14.3.

As discussed with @Equartey, to utilize the datastore v2, flutter will need to update the CI GitHub runner to macOS-14 or macOS-14-arm64, as the minimum supported version for datastore v2 has been updated to Swift 5.9.

}

func testToAmplifyAsyncThrowingSequence_withThrowingError_createAsyncSequenceCorrect() async throws {
let subject = PassthroughSubject<Int, TestError>()
let (sequence, cancellable) = subject.eraseToAnyPublisher().toAmplifyAsyncThrowingSequence()

let expectation1 = expectation(description: "element 1 received")
let expectation2 = expectation(description: "element 2 received")
let expectation3 = expectation(description: "element 3 received")
expectation3.isInverted = true
Task {
do {
for try await element in sequence {
switch element {
case 1: expectation1.fulfill()
case 2: expectation2.fulfill()
case 3: expectation3.fulfill()
default: break
}
}
} catch {
XCTAssertTrue(error is TestError)
}
}

subject.send(1)
subject.send(2)
subject.send(completion: .failure(.error))
await fulfillment(of: [expectation1, expectation2, expectation3], timeout: 1)
}

func testToAmplifyAsyncThrowingSequence_withCancelling_createAsyncSequenceCorrect() async throws {
let subject = PassthroughSubject<Int, TestError>()
let (sequence, cancellable) = subject.eraseToAnyPublisher().toAmplifyAsyncThrowingSequence()

let expectation1 = expectation(description: "element 1 received")
let expectation2 = expectation(description: "element 2 received")
let expectation3 = expectation(description: "element 3 received")
expectation3.isInverted = true
let expectation4 = expectation(description: "element 4 received")
expectation4.isInverted = true
Task {
for try await element in sequence {
switch element {
case 1: expectation1.fulfill()
case 2: expectation2.fulfill()
case 3: expectation3.fulfill()
case 4: expectation4.fulfill()
default: break
}
}
}

subject.send(1)
subject.send(2)
cancellable.cancel()
subject.send(3)
subject.send(4)
await fulfillment(of: [
expectation1,
expectation2,
expectation3,
expectation4
], timeout: 1)
}
}

fileprivate enum TestError: Error {
case error
}
Loading