Skip to content

Commit d2454d8

Browse files
authored
test(data): Gen2 data customize data model doc example testing (#3699)
1 parent 8534d75 commit d2454d8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3051
-6
lines changed

AmplifyPlugins/API/Tests/APIHostApp/APIHostApp.xcodeproj/project.pbxproj

Lines changed: 274 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Foundation
9+
import XCTest
10+
@testable import Amplify
11+
12+
final class GraphQLLocationPostUser1Tests: AWSAPIPluginGen2GraphQLBaseTest {
13+
14+
// Code Snippet for
15+
// https://docs.amplify.aws/swift/build-a-backend/data/data-modeling/add-fields/#specify-an-enum-field-type
16+
func testCodeSnippet() async throws {
17+
await setup(withModels: PostUserLocation1Models())
18+
19+
let post = Post(
20+
location: .init(
21+
lat: 48.837006,
22+
long: 8.28245))
23+
let createdPost = try await Amplify.API.mutate(request: .create(post)).get()
24+
print("\(createdPost)")
25+
26+
XCTAssertEqual(createdPost.location?.lat, 48.837006)
27+
XCTAssertEqual(createdPost.location?.long, 8.28245)
28+
}
29+
}
30+
31+
extension GraphQLLocationPostUser1Tests: DefaultLogger { }
32+
33+
extension GraphQLLocationPostUser1Tests {
34+
typealias Post = Post1
35+
typealias User = User1
36+
typealias Location = Location1
37+
38+
struct PostUserLocation1Models: AmplifyModelRegistration {
39+
public let version: String = "version"
40+
func registerModels(registry: ModelRegistry.Type) {
41+
ModelRegistry.register(modelType: Post1.self)
42+
ModelRegistry.register(modelType: User1.self)
43+
}
44+
}
45+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
extension Location1 {
6+
// MARK: - CodingKeys
7+
public enum CodingKeys: String, ModelKey {
8+
case lat
9+
case long
10+
}
11+
12+
public static let keys = CodingKeys.self
13+
// MARK: - ModelSchema
14+
15+
public static let schema = defineSchema { model in
16+
let location1 = Location1.keys
17+
18+
model.listPluralName = "Location1s"
19+
model.syncPluralName = "Location1s"
20+
21+
model.fields(
22+
.field(location1.lat, is: .optional, ofType: .double),
23+
.field(location1.long, is: .optional, ofType: .double)
24+
)
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
public struct Location1: Embeddable {
6+
var lat: Double?
7+
var long: Double?
8+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
extension Post1 {
6+
// MARK: - CodingKeys
7+
public enum CodingKeys: String, ModelKey {
8+
case id
9+
case location
10+
case content
11+
case createdAt
12+
case updatedAt
13+
}
14+
15+
public static let keys = CodingKeys.self
16+
// MARK: - ModelSchema
17+
18+
public static let schema = defineSchema { model in
19+
let post1 = Post1.keys
20+
21+
model.authRules = [
22+
rule(allow: .public, provider: .apiKey, operations: [.create, .update, .delete, .read])
23+
]
24+
25+
model.listPluralName = "Post1s"
26+
model.syncPluralName = "Post1s"
27+
28+
model.attributes(
29+
.primaryKey(fields: [post1.id])
30+
)
31+
32+
model.fields(
33+
.field(post1.id, is: .required, ofType: .string),
34+
.field(post1.location, is: .optional, ofType: .embedded(type: Location1.self)),
35+
.field(post1.content, is: .optional, ofType: .string),
36+
.field(post1.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
37+
.field(post1.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
38+
)
39+
}
40+
public class Path: ModelPath<Post1> { }
41+
42+
public static var rootPath: PropertyContainerPath? { Path() }
43+
}
44+
45+
extension Post1: ModelIdentifiable {
46+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
47+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
48+
}
49+
extension ModelPath where ModelType == Post1 {
50+
public var id: FieldPath<String> {
51+
string("id")
52+
}
53+
public var content: FieldPath<String> {
54+
string("content")
55+
}
56+
public var createdAt: FieldPath<Temporal.DateTime> {
57+
datetime("createdAt")
58+
}
59+
public var updatedAt: FieldPath<Temporal.DateTime> {
60+
datetime("updatedAt")
61+
}
62+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
public struct Post1: Model {
6+
public let id: String
7+
public var location: Location1?
8+
public var content: String?
9+
public var createdAt: Temporal.DateTime?
10+
public var updatedAt: Temporal.DateTime?
11+
12+
public init(id: String = UUID().uuidString,
13+
location: Location1? = nil,
14+
content: String? = nil) {
15+
self.init(id: id,
16+
location: location,
17+
content: content,
18+
createdAt: nil,
19+
updatedAt: nil)
20+
}
21+
internal init(id: String = UUID().uuidString,
22+
location: Location1? = nil,
23+
content: String? = nil,
24+
createdAt: Temporal.DateTime? = nil,
25+
updatedAt: Temporal.DateTime? = nil) {
26+
self.id = id
27+
self.location = location
28+
self.content = content
29+
self.createdAt = createdAt
30+
self.updatedAt = updatedAt
31+
}
32+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
extension User1 {
6+
// MARK: - CodingKeys
7+
public enum CodingKeys: String, ModelKey {
8+
case id
9+
case lastKnownLocation
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 user1 = User1.keys
19+
20+
model.authRules = [
21+
rule(allow: .public, provider: .apiKey, operations: [.create, .update, .delete, .read])
22+
]
23+
24+
model.listPluralName = "User1s"
25+
model.syncPluralName = "User1s"
26+
27+
model.attributes(
28+
.primaryKey(fields: [user1.id])
29+
)
30+
31+
model.fields(
32+
.field(user1.id, is: .required, ofType: .string),
33+
.field(user1.lastKnownLocation, is: .optional, ofType: .embedded(type: Location1.self)),
34+
.field(user1.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
35+
.field(user1.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
36+
)
37+
}
38+
public class Path: ModelPath<User1> { }
39+
40+
public static var rootPath: PropertyContainerPath? { Path() }
41+
}
42+
43+
extension User1: ModelIdentifiable {
44+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
45+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
46+
}
47+
extension ModelPath where ModelType == User1 {
48+
public var id: FieldPath<String> {
49+
string("id")
50+
}
51+
public var createdAt: FieldPath<Temporal.DateTime> {
52+
datetime("createdAt")
53+
}
54+
public var updatedAt: FieldPath<Temporal.DateTime> {
55+
datetime("updatedAt")
56+
}
57+
}
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 User1: Model {
6+
public let id: String
7+
public var lastKnownLocation: Location1?
8+
public var createdAt: Temporal.DateTime?
9+
public var updatedAt: Temporal.DateTime?
10+
11+
public init(id: String = UUID().uuidString,
12+
lastKnownLocation: Location1? = nil) {
13+
self.init(id: id,
14+
lastKnownLocation: lastKnownLocation,
15+
createdAt: nil,
16+
updatedAt: nil)
17+
}
18+
internal init(id: String = UUID().uuidString,
19+
lastKnownLocation: Location1? = nil,
20+
createdAt: Temporal.DateTime? = nil,
21+
updatedAt: Temporal.DateTime? = nil) {
22+
self.id = id
23+
self.lastKnownLocation = lastKnownLocation
24+
self.createdAt = createdAt
25+
self.updatedAt = updatedAt
26+
}
27+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
extension Customer10 {
6+
// MARK: - CodingKeys
7+
public enum CodingKeys: String, ModelKey {
8+
case id
9+
case name
10+
case phoneNumber
11+
case accountRepresentativeId
12+
case createdAt
13+
case updatedAt
14+
}
15+
16+
public static let keys = CodingKeys.self
17+
// MARK: - ModelSchema
18+
19+
public static let schema = defineSchema { model in
20+
let customer10 = Customer10.keys
21+
22+
model.authRules = [
23+
rule(allow: .public, provider: .apiKey, operations: [.create, .update, .delete, .read])
24+
]
25+
26+
model.listPluralName = "Customer10s"
27+
model.syncPluralName = "Customer10s"
28+
29+
model.attributes(
30+
.index(fields: ["accountRepresentativeId"], name: "customer10sByAccountRepresentativeId"),
31+
.primaryKey(fields: [customer10.id])
32+
)
33+
34+
model.fields(
35+
.field(customer10.id, is: .required, ofType: .string),
36+
.field(customer10.name, is: .optional, ofType: .string),
37+
.field(customer10.phoneNumber, is: .optional, ofType: .string),
38+
.field(customer10.accountRepresentativeId, is: .required, ofType: .string),
39+
.field(customer10.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
40+
.field(customer10.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
41+
)
42+
}
43+
public class Path: ModelPath<Customer10> { }
44+
45+
public static var rootPath: PropertyContainerPath? { Path() }
46+
}
47+
48+
extension Customer10: ModelIdentifiable {
49+
public typealias IdentifierFormat = ModelIdentifierFormat.Default
50+
public typealias IdentifierProtocol = DefaultModelIdentifier<Self>
51+
}
52+
extension ModelPath where ModelType == Customer10 {
53+
public var id: FieldPath<String> {
54+
string("id")
55+
}
56+
public var name: FieldPath<String> {
57+
string("name")
58+
}
59+
public var phoneNumber: FieldPath<String> {
60+
string("phoneNumber")
61+
}
62+
public var accountRepresentativeId: FieldPath<String> {
63+
string("accountRepresentativeId")
64+
}
65+
public var createdAt: FieldPath<Temporal.DateTime> {
66+
datetime("createdAt")
67+
}
68+
public var updatedAt: FieldPath<Temporal.DateTime> {
69+
datetime("updatedAt")
70+
}
71+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// swiftlint:disable all
2+
import Amplify
3+
import Foundation
4+
5+
public struct Customer10: Model {
6+
public let id: String
7+
public var name: String?
8+
public var phoneNumber: String?
9+
public var accountRepresentativeId: String
10+
public var createdAt: Temporal.DateTime?
11+
public var updatedAt: Temporal.DateTime?
12+
13+
public init(id: String = UUID().uuidString,
14+
name: String? = nil,
15+
phoneNumber: String? = nil,
16+
accountRepresentativeId: String) {
17+
self.init(id: id,
18+
name: name,
19+
phoneNumber: phoneNumber,
20+
accountRepresentativeId: accountRepresentativeId,
21+
createdAt: nil,
22+
updatedAt: nil)
23+
}
24+
internal init(id: String = UUID().uuidString,
25+
name: String? = nil,
26+
phoneNumber: String? = nil,
27+
accountRepresentativeId: String,
28+
createdAt: Temporal.DateTime? = nil,
29+
updatedAt: Temporal.DateTime? = nil) {
30+
self.id = id
31+
self.name = name
32+
self.phoneNumber = phoneNumber
33+
self.accountRepresentativeId = accountRepresentativeId
34+
self.createdAt = createdAt
35+
self.updatedAt = updatedAt
36+
}
37+
}

0 commit comments

Comments
 (0)