-
Notifications
You must be signed in to change notification settings - Fork 210
/
Copy pathGraphQLDeleteMutationTests.swift
176 lines (168 loc) · 6.31 KB
/
GraphQLDeleteMutationTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import XCTest
@testable import Amplify
@testable import AmplifyTestCommon
@testable import AWSPluginsCore
class GraphQLDeleteMutationTests: XCTestCase {
override func setUp() {
ModelRegistry.register(modelType: Comment.self)
ModelRegistry.register(modelType: Post.self)
ModelRegistry.register(modelType: Record.self)
ModelRegistry.register(modelType: RecordCover.self)
}
override func tearDown() {
ModelRegistry.reset()
}
/// - Given: a `Model` instance
/// - When:
/// - the model is of type `Post`
/// - the model has no required associations
/// - the mutation is of type `.delete`
/// - Then:
/// - check if the generated GraphQL document is a valid mutation:
/// - it is named `deletePost`
/// - it contains an `input` of type `ID!`
/// - it has a list of fields with no nested models
func testDeleteGraphQLMutationFromSimpleModel() {
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelSchema: Post.schema, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))
let document = documentBuilder.build()
let expectedQueryDocument = """
mutation DeletePost($input: DeletePostInput!) {
deletePost(input: $input) {
id
content
createdAt
draft
rating
status
title
updatedAt
__typename
}
}
"""
XCTAssertEqual(document.name, "deletePost")
XCTAssertEqual(document.stringValue, expectedQueryDocument)
XCTAssertEqual(document.name, "deletePost")
guard let variables = document.variables else {
XCTFail("The document doesn't contain variables")
return
}
XCTAssert(variables["input"] != nil)
guard let input = variables["input"] as? [String: String] else {
XCTFail("Could not get object at `input`")
return
}
XCTAssertEqual(input["id"], post.id)
}
/// - Given: a `Model` instance
/// - When:
/// - the model is of type `Post`
/// - the model has no required associations
/// - the mutation is of type `.delete`
/// - Then:
/// - check if the generated GraphQL document is a valid mutation:
/// - it is named `deletePost`
/// - it contains an `input` of type `ID!`
/// - it has a list of fields with no nested models
func testDeleteGraphQLMutationFromSimpleModelWithVersion() {
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelSchema: Post.schema, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))
documentBuilder.add(decorator: ConflictResolutionDecorator(version: 5))
let document = documentBuilder.build()
let expectedQueryDocument = """
mutation DeletePost($input: DeletePostInput!) {
deletePost(input: $input) {
id
content
createdAt
draft
rating
status
title
updatedAt
__typename
_version
_deleted
_lastChangedAt
}
}
"""
XCTAssertEqual(document.name, "deletePost")
XCTAssertEqual(document.stringValue, expectedQueryDocument)
XCTAssertEqual(document.name, "deletePost")
guard let variables = document.variables else {
XCTFail("The document doesn't contain variables")
return
}
XCTAssert(variables["input"] != nil)
guard let input = variables["input"] as? [String: Any] else {
XCTFail("Could not get object at `input`")
return
}
XCTAssert(input["id"] as? String == post.id)
XCTAssert(input["_version"] as? Int == 5)
}
func testDeleteGraphQLMutationModelWithReadOnlyFields() {
let recordCover = RecordCover(artist: "artist")
let record = Record(name: "name", description: "description", cover: recordCover)
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelSchema: Record.schema,
operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelDecorator(model: record))
documentBuilder.add(decorator: ConflictResolutionDecorator())
let document = documentBuilder.build()
let expectedQueryDocument = """
mutation DeleteRecord($input: DeleteRecordInput!) {
deleteRecord(input: $input) {
id
coverId
createdAt
description
name
updatedAt
cover {
id
artist
createdAt
updatedAt
__typename
_version
_deleted
_lastChangedAt
}
__typename
_version
_deleted
_lastChangedAt
}
}
"""
XCTAssertEqual(document.name, "deleteRecord")
XCTAssertEqual(document.stringValue, expectedQueryDocument)
guard let variables = document.variables else {
XCTFail("The document doesn't contain variables")
return
}
guard let input = variables["input"] as? GraphQLInput else {
XCTFail("Variables should contain a valid input")
return
}
XCTAssertEqual(input["id"] as? String, record.id)
XCTAssertEqual(input["name"] as? String, record.name)
XCTAssertEqual(input["description"] as? String, record.description)
XCTAssertNil(input["createdAt"] as? Temporal.DateTime)
XCTAssertNil(input["updatedAt"] as? Temporal.DateTime)
XCTAssertNil(input["cover"] as? RecordCover)
}
}