Skip to content

Commit 048e003

Browse files
author
Di Wu
authored
test(datastore): add integ test cases for other aws scalar types as CPK sort key (#3022)
* fix(datastore): add integ test cases for other aws scalar types as CPK sort key * add schema defination at the top of test class
1 parent adb166c commit 048e003

23 files changed

+1036
-0
lines changed

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginCPKTests/PrimaryKey/11/AWSDataStoreIntSortKeyTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
/** Model Schema
9+
type Post11 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: Int!
12+
}
13+
*/
814

915
import Foundation
1016
import Combine

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginCPKTests/PrimaryKey/12/AWSDataStoreFloatSortKeyTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
/** Model Schema
9+
type Post12 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: Float!
12+
}
13+
*/
814

915
import Foundation
1016
import Combine

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginCPKTests/PrimaryKey/13/AWSDataStoreDateTimeSortKeyTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
/** Model Schema
9+
type Post13 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: AWSDateTime!
12+
}
13+
*/
814

915
import Foundation
1016
import Combine

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginCPKTests/PrimaryKey/14/AWSDataStoreDateSortKeyTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
/** Model Schema
9+
type Post14 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: AWSDate!
12+
}
13+
*/
814

915
import Foundation
1016
import Combine

AmplifyPlugins/DataStore/Tests/DataStoreHostApp/AWSDataStorePluginCPKTests/PrimaryKey/15/AWSDataStoreTimeSortKeyTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8+
/** Model Schema
9+
type Post15 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: AWSTime!
12+
}
13+
*/
814

915
import Foundation
1016
import Combine
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
/** Model Schema
9+
type Post16 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: AWSURL!
12+
}
13+
*/
14+
15+
16+
import Foundation
17+
import Combine
18+
import XCTest
19+
@testable import Amplify
20+
21+
fileprivate struct TestModels: AmplifyModelRegistration {
22+
func registerModels(registry: ModelRegistry.Type) {
23+
ModelRegistry.register(modelType: Post16.self)
24+
}
25+
26+
public let version: String = "test"
27+
}
28+
29+
30+
class AWSDataStoreAWSURLSortKeyTest: AWSDataStoreSortKeyBaseTest {
31+
func testCreateModel_withSortKeyInAWSURLType_success() async throws {
32+
try await setUp(models: TestModels())
33+
try await waitDataStoreReady()
34+
var requests: Set<AnyCancellable> = []
35+
36+
let url = URL(string: "https://google.com")
37+
let post = Post16(postId: UUID().uuidString, sk: url!.absoluteString)
38+
let postCreated = expectation(description: "Post is created")
39+
postCreated.assertForOverFulfill = false
40+
Amplify.Hub.publisher(for: .dataStore)
41+
.filter { $0.eventName == HubPayload.EventName.DataStore.syncReceived }
42+
.compactMap { $0.data as? MutationEvent }
43+
.filter { $0.modelId == post.identifier }
44+
.sink { _ in
45+
postCreated.fulfill()
46+
}.store(in: &requests)
47+
48+
try await Amplify.DataStore.save(post)
49+
await fulfillment(of: [postCreated], timeout: 5)
50+
}
51+
52+
func testQueryCreatedModel_withSortKeyInAWSURLType_success() async throws {
53+
try await setUp(models: TestModels())
54+
try await waitDataStoreReady()
55+
var requests: Set<AnyCancellable> = []
56+
57+
let url = URL(string: "https://google.com")
58+
let post = Post16(postId: UUID().uuidString, sk: url!.absoluteString)
59+
let postCreated = expectation(description: "Post is created")
60+
postCreated.assertForOverFulfill = false
61+
Amplify.Hub.publisher(for: .dataStore)
62+
.filter { $0.eventName == HubPayload.EventName.DataStore.syncReceived }
63+
.compactMap { $0.data as? MutationEvent }
64+
.filter { $0.modelId == post.identifier }
65+
.sink { _ in
66+
postCreated.fulfill()
67+
}.store(in: &requests)
68+
69+
try await Amplify.DataStore.save(post)
70+
await fulfillment(of: [postCreated], timeout: 5)
71+
72+
let queryResult = try await Amplify.API.query(
73+
request: .get(
74+
Post16.self,
75+
byIdentifier: .identifier(postId: post.postId, sk: post.sk)
76+
)
77+
)
78+
79+
switch queryResult {
80+
case .success(let queriedPost):
81+
XCTAssertEqual(post.identifier, queriedPost!.identifier)
82+
case .failure(let error):
83+
XCTFail("Failed to query comment \(error)")
84+
}
85+
}
86+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
extension Post16 {
6+
// MARK: - CodingKeys
7+
public enum CodingKeys: String, ModelKey {
8+
case postId
9+
case sk
10+
case createdAt
11+
case updatedAt
12+
}
13+
14+
public static let keys = CodingKeys.self
15+
// MARK: - ModelSchema
16+
17+
public static let schema = defineSchema { model in
18+
let post16 = Post16.keys
19+
20+
model.pluralName = "Post16s"
21+
22+
model.attributes(
23+
.index(fields: ["postId", "sk"], name: nil),
24+
.primaryKey(fields: [post16.postId, post16.sk])
25+
)
26+
27+
model.fields(
28+
.field(post16.postId, is: .required, ofType: .string),
29+
.field(post16.sk, is: .required, ofType: .string),
30+
.field(post16.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
31+
.field(post16.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
32+
)
33+
}
34+
}
35+
36+
extension Post16: ModelIdentifiable {
37+
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
38+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
39+
}
40+
41+
extension Post16.IdentifierProtocol {
42+
public static func identifier(postId: String,
43+
sk: String) -> Self {
44+
.make(fields:[(name: "postId", value: postId), (name: "sk", value: sk)])
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
public struct Post16: Model {
6+
public let postId: String
7+
public let sk: String
8+
public var createdAt: Temporal.DateTime?
9+
public var updatedAt: Temporal.DateTime?
10+
11+
public init(postId: String,
12+
sk: String) {
13+
self.init(postId: postId,
14+
sk: sk,
15+
createdAt: nil,
16+
updatedAt: nil)
17+
}
18+
internal init(postId: String,
19+
sk: String,
20+
createdAt: Temporal.DateTime? = nil,
21+
updatedAt: Temporal.DateTime? = nil) {
22+
self.postId = postId
23+
self.sk = sk
24+
self.createdAt = createdAt
25+
self.updatedAt = updatedAt
26+
}
27+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
/** Model Schema
9+
type Post17 @model {
10+
postId: ID! @primaryKey(sortKeyFields: ["sk"])
11+
sk: AWSEmail!
12+
}
13+
*/
14+
15+
16+
import Foundation
17+
import Combine
18+
import XCTest
19+
@testable import Amplify
20+
21+
fileprivate struct TestModels: AmplifyModelRegistration {
22+
func registerModels(registry: ModelRegistry.Type) {
23+
ModelRegistry.register(modelType: Post17.self)
24+
}
25+
26+
public let version: String = "test"
27+
}
28+
29+
30+
class AWSDataStoreAWSEmailSortKeyTest: AWSDataStoreSortKeyBaseTest {
31+
func testCreateModel_withSortKeyInAWSEmailType_success() async throws {
32+
try await setUp(models: TestModels())
33+
try await waitDataStoreReady()
34+
var requests: Set<AnyCancellable> = []
35+
36+
let post = Post17(postId: UUID().uuidString, sk: "[email protected]")
37+
let postCreated = expectation(description: "Post is created")
38+
postCreated.assertForOverFulfill = false
39+
Amplify.Hub.publisher(for: .dataStore)
40+
.filter { $0.eventName == HubPayload.EventName.DataStore.syncReceived }
41+
.compactMap { $0.data as? MutationEvent }
42+
.filter { $0.modelId == post.identifier }
43+
.sink { _ in
44+
postCreated.fulfill()
45+
}.store(in: &requests)
46+
47+
try await Amplify.DataStore.save(post)
48+
await fulfillment(of: [postCreated], timeout: 5)
49+
}
50+
51+
func testQueryCreatedModel_withSortKeyInAWSEmailType_success() async throws {
52+
try await setUp(models: TestModels())
53+
try await waitDataStoreReady()
54+
var requests: Set<AnyCancellable> = []
55+
56+
let post = Post17(postId: UUID().uuidString, sk: "[email protected]")
57+
let postCreated = expectation(description: "Post is created")
58+
postCreated.assertForOverFulfill = false
59+
Amplify.Hub.publisher(for: .dataStore)
60+
.filter { $0.eventName == HubPayload.EventName.DataStore.syncReceived }
61+
.compactMap { $0.data as? MutationEvent }
62+
.filter { $0.modelId == post.identifier }
63+
.sink { _ in
64+
postCreated.fulfill()
65+
}.store(in: &requests)
66+
67+
try await Amplify.DataStore.save(post)
68+
await fulfillment(of: [postCreated], timeout: 5)
69+
70+
let queryResult = try await Amplify.API.query(
71+
request: .get(
72+
Post17.self,
73+
byIdentifier: .identifier(postId: post.postId, sk: post.sk)
74+
)
75+
)
76+
77+
switch queryResult {
78+
case .success(let queriedPost):
79+
XCTAssertEqual(post.identifier, queriedPost!.identifier)
80+
case .failure(let error):
81+
XCTFail("Failed to query comment \(error)")
82+
}
83+
}
84+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
extension Post17 {
6+
// MARK: - CodingKeys
7+
public enum CodingKeys: String, ModelKey {
8+
case postId
9+
case sk
10+
case createdAt
11+
case updatedAt
12+
}
13+
14+
public static let keys = CodingKeys.self
15+
// MARK: - ModelSchema
16+
17+
public static let schema = defineSchema { model in
18+
let post17 = Post17.keys
19+
20+
model.pluralName = "Post17s"
21+
22+
model.attributes(
23+
.index(fields: ["postId", "sk"], name: nil),
24+
.primaryKey(fields: [post17.postId, post17.sk])
25+
)
26+
27+
model.fields(
28+
.field(post17.postId, is: .required, ofType: .string),
29+
.field(post17.sk, is: .required, ofType: .string),
30+
.field(post17.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
31+
.field(post17.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
32+
)
33+
}
34+
}
35+
36+
extension Post17: ModelIdentifiable {
37+
public typealias IdentifierFormat = ModelIdentifierFormat.Custom
38+
public typealias IdentifierProtocol = ModelIdentifier<Self, ModelIdentifierFormat.Custom>
39+
}
40+
41+
extension Post17.IdentifierProtocol {
42+
public static func identifier(postId: String,
43+
sk: String) -> Self {
44+
.make(fields:[(name: "postId", value: postId), (name: "sk", value: sk)])
45+
}
46+
}

0 commit comments

Comments
 (0)