-
Notifications
You must be signed in to change notification settings - Fork 259
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
Equartey
merged 3 commits into
feat/datastore-spm-migration
from
5d/datastore-spm-swift-helper-functions-unit-test
May 29, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
packages/amplify_datastore/example/ios/Runner.xcodeproj/project.pbxproj
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
packages/amplify_datastore/example/ios/unit_tests/GraphQLRequestExtensionTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import XCTest | ||
@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) | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
packages/amplify_datastore/example/ios/unit_tests/PublisherExtensionTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
ormacOS-14-arm64
, as the minimum supported version for datastore v2 has been updated to Swift 5.9.