Skip to content

Commit

Permalink
chore(test): add more integration test cases for API lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Feb 15, 2023
1 parent 040647f commit a0d2288
Show file tree
Hide file tree
Showing 5 changed files with 665 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,132 @@ extension GraphQLLazyLoadCompositePKTests {
}
assertList(queriedChild, state: .isLoaded(count: 1))
}

/*
- Given: Api category setup with CompositePKModels
- When:
- Subscribe onCreate events of ChildSansBelongsTo
- Create new CompositePKParent instance with API
- Create new ChildSansBelongsTo instance with API
- Then:
- the newly created instance is successfully created through API. onCreate event is received.
*/
func testSubscribeChildSansBelongsToOnCreate() async throws {
await setup(withModels: CompositePKModels())
let connected = asyncExpectation(description: "Subscription connected")
let onCreate = asyncExpectation(description: "onCreate received")

let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
let child = initChildSansBelongsTo(with: parent)
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onCreate))
Task {
do {
for try await subscriptionEvent in subscription {
switch subscriptionEvent {
case .connection(.connected):
await connected.fulfill()
case let .data(.success(newModel)):
if newModel.identifier == child.identifier {
await onCreate.fulfill()
}
case let .data(.failure(error)):
XCTFail("Failed to create ChildSansBelongsTo, error: \(error.errorDescription)")
default: ()
}
}
}
}

await waitForExpectations([connected], timeout: 10)
try await mutate(.create(parent))
try await mutate(.create(child))
await waitForExpectations([onCreate], timeout: 10)
subscription.cancel()
}

/*
- Given: Api category setup with CompositePKModels
- When:
- Subscribe onCreate events of ChildSansBelongsTo
- Create new CompositePKParent instance with API
- Create new ChildSansBelongsTo instance with API
- Update newly created ChildSansBelongsTo instance with API
- Then:
- the newly created instance is successfully updated through API. onUpdate event is received.
*/
func testSubscribeChildSansBelongsToOnUpdate() async throws {
await setup(withModels: CompositePKModels())
let connected = asyncExpectation(description: "Subscription connected")
let onUpdate = asyncExpectation(description: "onUpdate received")
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
let child = initChildSansBelongsTo(with: parent)
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onUpdate))
Task {
do {
for try await subscriptionEvent in subscription {
switch subscriptionEvent {
case .connection(.connected):
await connected.fulfill()
case let .data(.success(newModel)):
if newModel.identifier == child.identifier {
await onUpdate.fulfill()
}
case let .data(.failure(error)):
XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)")
default: ()
}
}
}
}

await waitForExpectations([connected], timeout: 10)
try await mutate(.create(parent))
try await mutate(.create(child))
try await mutate(.update(child))
await waitForExpectations([onUpdate], timeout: 10)
subscription.cancel()
}

/*
- Given: Api category setup with CompositePKModels
- When:
- Subscribe onCreate events of ChildSansBelongsTo
- Create new CompositePKParent instance with API
- Create new ChildSansBelongsTo instance with API
- Delete newly created ChildSansBelongsTo with API
- Then:
- the newly created instance is successfully deleted through API. onDelete event is received.
*/
func testSubscribeChildSansBelongsToOnDelete() async throws {
await setup(withModels: CompositePKModels())
let connected = asyncExpectation(description: "Subscription connected")
let onDelete = asyncExpectation(description: "onUpdate received")
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
let child = initChildSansBelongsTo(with: parent)
let subscription = Amplify.API.subscribe(request: .subscription(of: ChildSansBelongsTo.self, type: .onDelete))
Task {
do {
for try await subscriptionEvent in subscription {
switch subscriptionEvent {
case .connection(.connected):
await connected.fulfill()
case let .data(.success(newModel)):
if newModel.identifier == child.identifier {
await onDelete.fulfill()
}
case let .data(.failure(error)):
XCTFail("Failed to update ChildSansBelongsTo, error: \(error.errorDescription)")
default: ()
}
}
}
}

await waitForExpectations([connected], timeout: 10)
try await mutate(.create(parent))
try await mutate(.create(child))
try await mutate(.delete(child))
await waitForExpectations([onDelete], timeout: 10)
subscription.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,131 @@ extension GraphQLLazyLoadCompositePKTests {
}
assertList(queriedChild, state: .isLoaded(count: 1))
}

/*
- Given: Api category setup with CompositePKModels
- When:
- Subscribe onCreate events of CompositePKChild
- Create new CompositePKChild instance with API
- Then:
- the newly created instance is successfully created through API. onCreate event is received.
*/
func testSubscribeCompositePKChildOnCreate() async throws {
await setup(withModels: CompositePKModels())
let connected = asyncExpectation(description: "Subscription connected")
let onCreate = asyncExpectation(description: "onCreate received")
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onCreate))
Task {
do {
for try await subscriptionEvent in subscription {
switch subscriptionEvent {
case .connection(.connected):
await connected.fulfill()
case let .data(.success(newModel)):
if newModel.identifier == child.identifier {
await onCreate.fulfill()
}
case let .data(.failure(error)):
XCTFail("Failed to create CompositePKChild, error: \(error.errorDescription)")
default: ()
}
}
}
}

await waitForExpectations([connected], timeout: 10)
try await mutate(.create(child))
await waitForExpectations([onCreate], timeout: 10)
subscription.cancel()
}

/*
- Given: Api category setup with CompositePKModels
- When:
- Subscribe onCreate events of CompositePKChild
- Create new CompositePKChild instance with API
- Create new CompositePKParent instance with API
- Update newly created CompositePKParent instance with API
- Then:
- the newly created instance is successfully updated through API. onUpdate event is received.
*/
func testSubscribeCompositePKChildOnUpdate() async throws {
await setup(withModels: CompositePKModels())
let connected = asyncExpectation(description: "Subscription connected")
let onUpdate = asyncExpectation(description: "onUpdate received")
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
let parent = CompositePKParent(customId: UUID().uuidString, content: UUID().uuidString)
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onUpdate))
Task {
do {
for try await subscriptionEvent in subscription {
switch subscriptionEvent {
case .connection(.connected):
await connected.fulfill()
case let .data(.success(newModel)):
let associatedParent = try await newModel.parent
if newModel.identifier == child.identifier,
associatedParent?.identifier == parent.identifier
{
await onUpdate.fulfill()
}
case let .data(.failure(error)):
XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)")
default: ()
}
}
}
}

await waitForExpectations([connected], timeout: 10)
try await mutate(.create(child))
try await mutate(.create(parent))

var updatingChild = child
updatingChild.setParent(parent)
try await mutate(.update(updatingChild))
await waitForExpectations([onUpdate], timeout: 10)
subscription.cancel()
}

/*
- Given: Api category setup with CompositePKModels
- When:
- Subscribe onCreate events of CompositePKChild
- Create new CompositePKChild instance with API
- Delete newly created CompositePKChild with API
- Then:
- the newly created instance is successfully deleted through API. onDelete event is received.
*/
func testSubscribeCompositePKChildOnDelete() async throws {
await setup(withModels: CompositePKModels())
let connected = asyncExpectation(description: "Subscription connected")
let onDelete = asyncExpectation(description: "onUpdate received")
let child = CompositePKChild(childId: UUID().uuidString, content: UUID().uuidString)
let subscription = Amplify.API.subscribe(request: .subscription(of: CompositePKChild.self, type: .onDelete))
Task {
do {
for try await subscriptionEvent in subscription {
switch subscriptionEvent {
case .connection(.connected):
await connected.fulfill()
case let .data(.success(newModel)):
if newModel.identifier == child.identifier {
await onDelete.fulfill()
}
case let .data(.failure(error)):
XCTFail("Failed to update CompositePKParent, error: \(error.errorDescription)")
default: ()
}
}
}
}

await waitForExpectations([connected], timeout: 10)
try await mutate(.create(child))
try await mutate(.delete(child))
await waitForExpectations([onDelete], timeout: 10)
subscription.cancel()
}
}
Loading

0 comments on commit a0d2288

Please sign in to comment.