|
| 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 | +@testable import Amplify |
| 10 | +@testable import AmplifyTestCommon |
| 11 | +@testable import AWSPluginsCore |
| 12 | + |
| 13 | +// swiftlint:disable type_name |
| 14 | +class GraphQLRequestSyncCustomPrimaryKeyWithMultipleFieldsTests: XCTestCase { |
| 15 | + |
| 16 | + override func setUp() { |
| 17 | + ModelRegistry.register(modelType: CustomerWithMultipleFieldsinPK.self) |
| 18 | + } |
| 19 | + |
| 20 | + override func tearDown() { |
| 21 | + ModelRegistry.reset() |
| 22 | + } |
| 23 | + |
| 24 | + func testDeleteMutationGraphQLRequestWithDateInPK() throws { |
| 25 | + let customer = CustomerWithMultipleFieldsinPK(dob: Temporal.DateTime.now(), |
| 26 | + date: Temporal.Date.now(), |
| 27 | + time: Temporal.Time.now(), |
| 28 | + phoneNumber: 1_234_567, |
| 29 | + priority: Priority.high, |
| 30 | + height: 6.1, |
| 31 | + firstName: "John", |
| 32 | + lastName: "Doe") |
| 33 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: customer.modelName, |
| 34 | + operationType: .mutation) |
| 35 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete)) |
| 36 | + documentBuilder.add(decorator: ModelIdDecorator(model: customer)) |
| 37 | + documentBuilder.add(decorator: ConflictResolutionDecorator(version: 1, lastSync: nil)) |
| 38 | + let document = documentBuilder.build() |
| 39 | + let documentStringValue = """ |
| 40 | + mutation DeleteCustomerWithMultipleFieldsinPK($input: DeleteCustomerWithMultipleFieldsinPKInput!) { |
| 41 | + deleteCustomerWithMultipleFieldsinPK(input: $input) { |
| 42 | + id |
| 43 | + createdAt |
| 44 | + date |
| 45 | + dob |
| 46 | + firstName |
| 47 | + height |
| 48 | + lastName |
| 49 | + phoneNumber |
| 50 | + priority |
| 51 | + time |
| 52 | + updatedAt |
| 53 | + __typename |
| 54 | + _version |
| 55 | + _deleted |
| 56 | + _lastChangedAt |
| 57 | + } |
| 58 | + } |
| 59 | + """ |
| 60 | + XCTAssertEqual(document.stringValue, documentStringValue) |
| 61 | + |
| 62 | + guard let expectedInput = document.variables?["input"] as? [String: Any] else { |
| 63 | + XCTFail("The document variables property doesn't contain a valid input") |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + let request = GraphQLRequest<MutationSyncResult>.deleteMutation(of: customer, |
| 68 | + modelSchema: customer.schema, |
| 69 | + version: 1) |
| 70 | + |
| 71 | + XCTAssertEqual(request.document, document.stringValue) |
| 72 | + XCTAssert(request.responseType == MutationSyncResult.self) |
| 73 | + |
| 74 | + guard let variables = request.variables else { |
| 75 | + XCTFail("The request doesn't contain variables") |
| 76 | + return |
| 77 | + } |
| 78 | + guard let input = variables["input"] as? [String: Any] else { |
| 79 | + XCTFail("The document variables property doesn't contain a valid input") |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + XCTAssertEqual(input["id"] as? String, customer.id) |
| 84 | + XCTAssertEqual(input["dob"] as? String, customer.dob.iso8601String) |
| 85 | + XCTAssertEqual(input["date"] as? String, customer.date.iso8601String) |
| 86 | + XCTAssertEqual(input["time"] as? String, customer.time.iso8601String) |
| 87 | + XCTAssertEqual(input["phoneNumber"] as? String, String(describing: customer.phoneNumber)) |
| 88 | + XCTAssertEqual(input["priority"] as? String, customer.priority.rawValue) |
| 89 | + XCTAssertEqual(input["height"] as? String, String(describing: customer.height)) |
| 90 | + XCTAssertEqual(input["_version"] as? Int, 1) |
| 91 | + |
| 92 | + XCTAssertEqual(input["id"] as? String, expectedInput["id"] as? String) |
| 93 | + XCTAssertEqual(input["dob"] as? String, expectedInput["dob"] as? String) |
| 94 | + XCTAssertEqual(input["date"] as? String, expectedInput["date"] as? String) |
| 95 | + XCTAssertEqual(input["time"] as? String, expectedInput["time"] as? String) |
| 96 | + XCTAssertEqual(input["phoneNumber"] as? String, expectedInput["phoneNumber"] as? String) |
| 97 | + XCTAssertEqual(input["priority"] as? String, expectedInput["priority"] as? String) |
| 98 | + XCTAssertEqual(input["height"] as? String, expectedInput["height"] as? String) |
| 99 | + XCTAssertEqual(input["_version"] as? Int, expectedInput["_version"] as? Int) |
| 100 | + } |
| 101 | + |
| 102 | + func testOnCreateSubscriptionGraphQLRequestWithDateInPK() throws { |
| 103 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: CustomerWithMultipleFieldsinPK.modelName, |
| 104 | + operationType: .subscription) |
| 105 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .onCreate)) |
| 106 | + documentBuilder.add(decorator: ConflictResolutionDecorator()) |
| 107 | + let document = documentBuilder.build() |
| 108 | + let documentStringValue = """ |
| 109 | + subscription OnCreateCustomerWithMultipleFieldsinPK { |
| 110 | + onCreateCustomerWithMultipleFieldsinPK { |
| 111 | + id |
| 112 | + createdAt |
| 113 | + date |
| 114 | + dob |
| 115 | + firstName |
| 116 | + height |
| 117 | + lastName |
| 118 | + phoneNumber |
| 119 | + priority |
| 120 | + time |
| 121 | + updatedAt |
| 122 | + __typename |
| 123 | + _version |
| 124 | + _deleted |
| 125 | + _lastChangedAt |
| 126 | + } |
| 127 | + } |
| 128 | + """ |
| 129 | + XCTAssertEqual(document.stringValue, documentStringValue) |
| 130 | + |
| 131 | + let request = GraphQLRequest<MutationSyncResult>.subscription(to: CustomerWithMultipleFieldsinPK.self, |
| 132 | + subscriptionType: .onCreate) |
| 133 | + XCTAssertEqual(document.stringValue, request.document) |
| 134 | + XCTAssertEqual(documentStringValue, request.document) |
| 135 | + } |
| 136 | + |
| 137 | + func testSyncQueryGraphQLRequestWithDateInPK() throws { |
| 138 | + let nextToken = "nextToken" |
| 139 | + let limit = 100 |
| 140 | + let lastSync = 123 |
| 141 | + var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: CustomerWithMultipleFieldsinPK.modelName, |
| 142 | + operationType: .query) |
| 143 | + documentBuilder.add(decorator: DirectiveNameDecorator(type: .sync)) |
| 144 | + documentBuilder.add(decorator: PaginationDecorator(limit: limit, nextToken: nextToken)) |
| 145 | + documentBuilder.add(decorator: ConflictResolutionDecorator(lastSync: lastSync)) |
| 146 | + let document = documentBuilder.build() |
| 147 | + let documentStringValue = """ |
| 148 | + query SyncCustomerWithMultipleFieldsinPKs($lastSync: AWSTimestamp, $limit: Int, $nextToken: String) { |
| 149 | + syncCustomerWithMultipleFieldsinPKs(lastSync: $lastSync, limit: $limit, nextToken: $nextToken) { |
| 150 | + items { |
| 151 | + id |
| 152 | + createdAt |
| 153 | + date |
| 154 | + dob |
| 155 | + firstName |
| 156 | + height |
| 157 | + lastName |
| 158 | + phoneNumber |
| 159 | + priority |
| 160 | + time |
| 161 | + updatedAt |
| 162 | + __typename |
| 163 | + _version |
| 164 | + _deleted |
| 165 | + _lastChangedAt |
| 166 | + } |
| 167 | + nextToken |
| 168 | + startedAt |
| 169 | + } |
| 170 | + } |
| 171 | + """ |
| 172 | + XCTAssertEqual(document.stringValue, documentStringValue) |
| 173 | + |
| 174 | + let request = GraphQLRequest<SyncQueryResult>.syncQuery(modelSchema: CustomerWithMultipleFieldsinPK.schema, |
| 175 | + limit: limit, |
| 176 | + nextToken: nextToken, |
| 177 | + lastSync: lastSync) |
| 178 | + |
| 179 | + XCTAssertEqual(document.stringValue, request.document) |
| 180 | + XCTAssertEqual(documentStringValue, request.document) |
| 181 | + XCTAssert(request.responseType == SyncQueryResult.self) |
| 182 | + guard let variables = request.variables else { |
| 183 | + XCTFail("The request doesn't contain variables") |
| 184 | + return |
| 185 | + } |
| 186 | + XCTAssertNotNil(variables["limit"]) |
| 187 | + XCTAssertEqual(variables["limit"] as? Int, limit) |
| 188 | + XCTAssertNotNil(variables["nextToken"]) |
| 189 | + XCTAssertEqual(variables["nextToken"] as? String, nextToken) |
| 190 | + XCTAssertNotNil(variables["lastSync"]) |
| 191 | + XCTAssertEqual(variables["lastSync"] as? Int, lastSync) |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments