Skip to content

Commit 763f143

Browse files
thisisabhashdiegocstnlawmicha
committed
test(Datastore): Integration test in Datastore for custom primary keys (#1256)
* Integration test for datastore create,query, delete and query * Add datastore update mutation before delete Co-authored-by: Diego Costantino <[email protected]> Co-authored-by: Michael <[email protected]>
1 parent 182c6a6 commit 763f143

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
10+
import AmplifyPlugins
11+
import AWSPluginsCore
12+
13+
@testable import Amplify
14+
@testable import AmplifyTestCommon
15+
@testable import AWSDataStoreCategoryPlugin
16+
17+
// swiftlint:disable cyclomatic_complexity
18+
class DataStoreCustomPrimaryKeyTests: SyncEngineIntegrationTestBase {
19+
20+
/// - Given: API has been setup with CustomerOrder model registered
21+
/// - When: A new customer order is
22+
/// - saved, updated and queried - should return the saved model
23+
/// - deleted
24+
/// - queried
25+
/// - Then: The model should be deleted finally and the sync events should be received in order
26+
func testDeleteModelWithCustomPrimaryKey() throws {
27+
28+
try startAmplifyAndWaitForSync()
29+
let customerOrder = CustomerOrder(orderId: UUID().uuidString, email: "[email protected]")
30+
31+
let createReceived = expectation(description: "Create notification received")
32+
let deleteReceived = expectation(description: "Delete notification received")
33+
let updateReceived = expectation(description: "Update notification received")
34+
35+
var updatedCustomerOrder = customerOrder
36+
updatedCustomerOrder.email = "[email protected]"
37+
38+
let hubListener = Amplify.Hub.listen(
39+
to: .dataStore,
40+
eventName: HubPayload.EventName.DataStore.syncReceived) { payload in
41+
guard let mutationEvent = payload.data as? MutationEvent
42+
else {
43+
XCTFail("Can't cast payload as mutation event")
44+
return
45+
}
46+
47+
guard let order = try? mutationEvent.decodeModel() as? CustomerOrder,
48+
order.id == customerOrder.id else {
49+
return
50+
}
51+
52+
if mutationEvent.mutationType == GraphQLMutationType.create.rawValue {
53+
XCTAssertEqual(order.email, customerOrder.email)
54+
XCTAssertEqual(order.orderId, customerOrder.orderId)
55+
XCTAssertEqual(mutationEvent.version, 1)
56+
createReceived.fulfill()
57+
return
58+
}
59+
60+
if mutationEvent.mutationType == GraphQLMutationType.update.rawValue {
61+
XCTAssertEqual(order.email, updatedCustomerOrder.email)
62+
XCTAssertEqual(order.orderId, updatedCustomerOrder.orderId)
63+
XCTAssertEqual(mutationEvent.version, 2)
64+
updateReceived.fulfill()
65+
return
66+
}
67+
68+
if mutationEvent.mutationType == GraphQLMutationType.delete.rawValue {
69+
XCTAssertEqual(order.email, updatedCustomerOrder.email)
70+
XCTAssertEqual(order.orderId, updatedCustomerOrder.orderId)
71+
XCTAssertEqual(mutationEvent.version, 3)
72+
deleteReceived.fulfill()
73+
return
74+
}
75+
}
76+
77+
guard try HubListenerTestUtilities.waitForListener(with: hubListener, timeout: 5.0) else {
78+
XCTFail("Listener not registered for hub")
79+
return
80+
}
81+
82+
// create customer order
83+
Amplify.DataStore.save(customerOrder) { _ in }
84+
wait(for: [createReceived], timeout: networkTimeout)
85+
86+
// update customer order
87+
Amplify.DataStore.save(updatedCustomerOrder) { _ in }
88+
wait(for: [updateReceived], timeout: networkTimeout)
89+
90+
// query the updated order
91+
let queryBeforeDeleteExpectation = expectation(description: "Queried model should be same as created one")
92+
Amplify.DataStore.query(CustomerOrder.self, byId: updatedCustomerOrder.id) { result in
93+
switch result {
94+
case .success(let value):
95+
guard let order = value else {
96+
XCTFail("Queried model is nil")
97+
return
98+
}
99+
XCTAssertEqual(order.id, updatedCustomerOrder.id)
100+
XCTAssertEqual(order.orderId, updatedCustomerOrder.orderId)
101+
XCTAssertEqual(order.email, updatedCustomerOrder.email)
102+
queryBeforeDeleteExpectation.fulfill()
103+
case .failure(let error):
104+
print("Error : \(error)")
105+
}
106+
}
107+
wait(for: [queryBeforeDeleteExpectation], timeout: networkTimeout)
108+
109+
// delete the customer order
110+
Amplify.DataStore.delete(CustomerOrder.self, withId: updatedCustomerOrder.id) { _ in }
111+
wait(for: [deleteReceived], timeout: networkTimeout)
112+
113+
// query the customer order after deletion
114+
let queryAfterDeleteExpectation = expectation(description: "Deleted model not found upon querying")
115+
Amplify.DataStore.query(CustomerOrder.self, byId: updatedCustomerOrder.id) { result in
116+
switch result {
117+
case .success(let value):
118+
XCTAssertNil(value)
119+
queryAfterDeleteExpectation.fulfill()
120+
case .failure(let error):
121+
print("Error : \(error)")
122+
}
123+
}
124+
wait(for: [queryAfterDeleteExpectation], timeout: networkTimeout)
125+
}
126+
127+
}

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ type Nested {
228228
valueTwo: String
229229
}
230230
231+
type CustomerOrder @model
232+
@key(fields: ["orderId","id"]) {
233+
id: ID!
234+
orderId: String!
235+
email: String!
236+
}
231237
```
232238
3. `amplify push`
233239

AmplifyPlugins/DataStore/AWSDataStoreCategoryPluginIntegrationTests/TestSupport/TestModelRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct TestModelRegistration: AmplifyModelRegistration {
3232
registry.register(modelType: ListStringContainer.self)
3333
registry.register(modelType: EnumTestModel.self)
3434
registry.register(modelType: NestedTypeTestModel.self)
35+
registry.register(modelType: CustomerOrder.self)
3536
}
3637

3738
let version: String = "1"

AmplifyPlugins/DataStore/DataStoreCategoryPlugin.xcodeproj/project.pbxproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@
106106
6BE9D6F325A665EA00AB5C9A /* StorageEngineTestsBase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BE9D6F225A665EA00AB5C9A /* StorageEngineTestsBase.swift */; };
107107
6BE9D73E25A6800100AB5C9A /* StorageEngineTestsManyToMany.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BE9D73D25A6800100AB5C9A /* StorageEngineTestsManyToMany.swift */; };
108108
7678B31025FAD93200B4917F /* InitialSyncOperationSyncExpressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7678B30F25FAD93200B4917F /* InitialSyncOperationSyncExpressionTests.swift */; };
109+
79AB68CC1F8B4029BBE74F65 /* awsconfiguration.json in Resources */ = {isa = PBXBuildFile; fileRef = 8F08E127481F4B9089D0D466 /* awsconfiguration.json */; };
109110
8141657E756CC7B2EE1CE851 /* Pods_HostApp_AWSDataStoreCategoryPluginAuthIntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA320D973669D3843FDF755E /* Pods_HostApp_AWSDataStoreCategoryPluginAuthIntegrationTests.framework */; };
111+
97406B382666DC0200C41E19 /* DataStoreCustomPrimaryKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97406B372666DC0200C41E19 /* DataStoreCustomPrimaryKeyTests.swift */; };
110112
A209418EED69D7B1BB8A55C2 /* Pods_AWSDataStoreCategoryPlugin_AWSDataStoreCategoryPluginTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C63AE18F2569D004E94BD550 /* Pods_AWSDataStoreCategoryPlugin_AWSDataStoreCategoryPluginTests.framework */; };
111113
A569484A6FBAE7EC7ADF3FD4 /* Pods_AWSDataStoreCategoryPlugin.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A1D332BE6CF885805360B3D /* Pods_AWSDataStoreCategoryPlugin.framework */; };
112114
B10BF4A52A1C9840C208C8A8 /* Pods_HostApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 19AFF6D00CF3AF927BA90382 /* Pods_HostApp.framework */; };
@@ -255,6 +257,7 @@
255257
/* Begin PBXFileReference section */
256258
08C5AE6BB158C1525C9AD57D /* Pods-HostApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HostApp.debug.xcconfig"; path = "Target Support Files/Pods-HostApp/Pods-HostApp.debug.xcconfig"; sourceTree = "<group>"; };
257259
187B250EA1E6EB23F8B7F5A3 /* Pods-AWSDataStoreCategoryPlugin-AWSDataStoreCategoryPluginTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AWSDataStoreCategoryPlugin-AWSDataStoreCategoryPluginTests.release.xcconfig"; path = "Target Support Files/Pods-AWSDataStoreCategoryPlugin-AWSDataStoreCategoryPluginTests/Pods-AWSDataStoreCategoryPlugin-AWSDataStoreCategoryPluginTests.release.xcconfig"; sourceTree = "<group>"; };
260+
19219AD5736741EFA6A91086 /* schema.graphql */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 4; includeInIndex = 0; lastKnownFileType = text; name = schema.graphql; path = amplify/backend/api/apiName/schema.graphql; sourceTree = "<group>"; };
258261
19AFF6D00CF3AF927BA90382 /* Pods_HostApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_HostApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
259262
1AF725CF8D5E6F10D86CB75C /* Pods-HostApp-AWSDataStoreCategoryPluginAuthIntegrationTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HostApp-AWSDataStoreCategoryPluginAuthIntegrationTests.debug.xcconfig"; path = "Target Support Files/Pods-HostApp-AWSDataStoreCategoryPluginAuthIntegrationTests/Pods-HostApp-AWSDataStoreCategoryPluginAuthIntegrationTests.debug.xcconfig"; sourceTree = "<group>"; };
260263
2102DD46260D87BB00B80FE2 /* ReconcileAndLocalSaveQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReconcileAndLocalSaveQueue.swift; sourceTree = "<group>"; };
@@ -364,7 +367,10 @@
364367
6BE9D6F025A6643100AB5C9A /* StorageEngineTestsHasOne.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageEngineTestsHasOne.swift; sourceTree = "<group>"; };
365368
6BE9D6F225A665EA00AB5C9A /* StorageEngineTestsBase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageEngineTestsBase.swift; sourceTree = "<group>"; };
366369
6BE9D73D25A6800100AB5C9A /* StorageEngineTestsManyToMany.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageEngineTestsManyToMany.swift; sourceTree = "<group>"; };
370+
743EFF58866C48BDA24992CC /* amplifytools.xcconfig */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 4; includeInIndex = 0; lastKnownFileType = text.xcconfig; path = amplifytools.xcconfig; sourceTree = "<group>"; };
367371
7678B30F25FAD93200B4917F /* InitialSyncOperationSyncExpressionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InitialSyncOperationSyncExpressionTests.swift; sourceTree = "<group>"; };
372+
8F08E127481F4B9089D0D466 /* awsconfiguration.json */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = unknown; path = awsconfiguration.json; sourceTree = "<group>"; };
373+
97406B372666DC0200C41E19 /* DataStoreCustomPrimaryKeyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreCustomPrimaryKeyTests.swift; sourceTree = "<group>"; };
368374
9D42A96449A4B73735566C07 /* Pods-AWSDataStoreCategoryPlugin.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AWSDataStoreCategoryPlugin.debug.xcconfig"; path = "Target Support Files/Pods-AWSDataStoreCategoryPlugin/Pods-AWSDataStoreCategoryPlugin.debug.xcconfig"; sourceTree = "<group>"; };
369375
9F987EC33AAD7C0904A598CA /* Pods_HostApp_AWSDataStoreCategoryPluginIntegrationTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_HostApp_AWSDataStoreCategoryPluginIntegrationTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
370376
B40EF02724BF68C900F2264C /* ConfigurationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurationTests.swift; sourceTree = "<group>"; };
@@ -541,6 +547,7 @@
541547
2149E591238867E100873955 = {
542548
isa = PBXGroup;
543549
children = (
550+
57E5A630880B405BACB64C79 /* AmplifyConfig */,
544551
2149E5A62388684F00873955 /* AWSDataStoreCategoryPlugin */,
545552
2149E5E42388692E00873955 /* AWSDataStoreCategoryPluginTests */,
546553
2149E61E23886CEE00873955 /* AWSDataStoreCategoryPluginIntegrationTests */,
@@ -697,6 +704,7 @@
697704
FA6B0EA9249445D50062AA59 /* AWSDataStorePluginConfigurationTests.swift */,
698705
217D61552578AEB0009F0639 /* Connection */,
699706
D8C5BA58249815A6007C3A68 /* DataStoreConfigurationTests.swift */,
707+
97406B372666DC0200C41E19 /* DataStoreCustomPrimaryKeyTests.swift */,
700708
FAB571412395A3E80006A5F8 /* DataStoreEndToEndTests.swift */,
701709
D8E9992325013C2F0006170A /* DataStoreHubEventsTests.swift */,
702710
D888E80924A65B3800F4CE3E /* DataStoreLocalStoreTests.swift */,
@@ -740,6 +748,17 @@
740748
path = Core;
741749
sourceTree = "<group>";
742750
};
751+
57E5A630880B405BACB64C79 /* AmplifyConfig */ = {
752+
isa = PBXGroup;
753+
children = (
754+
743EFF58866C48BDA24992CC /* amplifytools.xcconfig */,
755+
8F08E127481F4B9089D0D466 /* awsconfiguration.json */,
756+
19219AD5736741EFA6A91086 /* schema.graphql */,
757+
);
758+
name = AmplifyConfig;
759+
path = .;
760+
sourceTree = "<group>";
761+
};
743762
6B7743E5259071AB001469F5 /* Storage */ = {
744763
isa = PBXGroup;
745764
children = (
@@ -1226,6 +1245,7 @@
12261245
buildActionMask = 2147483647;
12271246
files = (
12281247
21233DF02475AF6A00039337 /* README.md in Resources */,
1248+
79AB68CC1F8B4029BBE74F65 /* awsconfiguration.json in Resources */,
12291249
);
12301250
runOnlyForDeploymentPostprocessing = 0;
12311251
};
@@ -1803,6 +1823,7 @@
18031823
217D622225798ED3009F0639 /* DataStoreConnectionScenario1Tests.swift in Sources */,
18041824
217D61752578AF85009F0639 /* DataStoreConnectionScenario2Tests.swift in Sources */,
18051825
D888E80C24A65DC200F4CE3E /* LocalStoreIntegrationTestBase.swift in Sources */,
1826+
97406B382666DC0200C41E19 /* DataStoreCustomPrimaryKeyTests.swift in Sources */,
18061827
FA3841EB23889D6C0070AD5B /* SubscriptionEndToEndTests.swift in Sources */,
18071828
217D622B2579E15F009F0639 /* DataStoreConnectionScenario5Tests.swift in Sources */,
18081829
21FDBBDB2587DB7A0086FCDC /* DataStoreConnectionScenario6Tests.swift in Sources */,

0 commit comments

Comments
 (0)