@@ -14,92 +14,21 @@ import 'package:aws_common/testing.dart';
14
14
import 'package:collection/collection.dart' ;
15
15
import 'package:test/test.dart' ;
16
16
17
+ import 'mocks.dart' ;
17
18
import 'test_data/fake_amplify_configuration.dart' ;
18
19
import 'test_models/ModelProvider.dart' ;
19
20
import 'util.dart' ;
20
21
21
22
final _deepEquals = const DeepCollectionEquality ().equals;
22
23
23
- // Success Mocks
24
- const _expectedQuerySuccessResponseBody = {
25
- 'data' : {
26
- 'listBlogs' : {
27
- 'items' : [
28
- {
29
- 'id' : 'TEST_ID' ,
30
- 'name' : 'Test App Blog' ,
31
- 'createdAt' : '2022-06-28T17:36:52.460Z' ,
32
- }
33
- ],
34
- },
35
- },
36
- };
37
-
38
- final _modelQueryId = uuid ();
39
- final _expectedModelQueryResult = {
40
- 'data' : {
41
- 'getBlog' : {
42
- 'createdAt' : '2021-07-21T22:23:33.707Z' ,
43
- 'id' : _modelQueryId,
44
- 'name' : 'Test App Blog' ,
45
- },
46
- },
47
- };
48
- const _expectedMutateSuccessResponseBody = {
49
- 'data' : {
50
- 'createBlog' : {
51
- 'id' : 'TEST_ID' ,
52
- 'name' : 'Test App Blog' ,
53
- 'createdAt' : '2022-07-06T18:42:26.126Z' ,
54
- },
55
- },
56
- };
57
-
58
- // Error Mocks
59
- const _errorMessage = 'Unable to parse GraphQL query.' ;
60
- const _errorLocations = [
61
- {'line' : 2 , 'column' : 3 },
62
- {'line' : 4 , 'column' : 5 },
63
- ];
64
- const _errorPath = ['a' , 1 , 'b' ];
65
- const _errorExtensions = {
66
- 'a' : 'blah' ,
67
- 'b' : {'c' : 'd' },
68
- };
69
- const _errorType = 'DynamoDB:ConditionalCheckFailedException' ;
70
- const _errorInfo = {'a' : 'b' };
71
- const _expectedErrorResponseBody = {
72
- 'data' : null ,
73
- 'errors' : [
74
- {
75
- 'message' : _errorMessage,
76
- 'locations' : _errorLocations,
77
- 'path' : _errorPath,
78
- 'extensions' : _errorExtensions,
79
- 'errorType' : _errorType,
80
- 'errorInfo' : _errorInfo,
81
- },
82
- ],
83
- };
84
-
85
- const _authErrorMessage = 'Not authorized' ;
86
- const _expectedAuthErrorResponseBody = {
87
- 'data' : null ,
88
- 'errors' : [
89
- {
90
- 'message' : _authErrorMessage,
91
- },
92
- ],
93
- };
94
-
95
24
final mockHttpClient = MockAWSHttpClient ((request, _) async {
96
25
if (request.headers[xApiKey] != 'abc123' &&
97
26
request.headers[AWSHeaders .authorization] == testFunctionToken) {
98
27
// Not authorized w API key but has lambda auth token, mock that auth mode
99
28
// does not work for this query.
100
29
return AWSHttpResponse (
101
30
statusCode: 401 ,
102
- body: utf8.encode (json.encode (_expectedAuthErrorResponseBody )),
31
+ body: utf8.encode (json.encode (expectedAuthErrorResponseBody )),
103
32
);
104
33
}
105
34
if (request.headers[xApiKey] != 'abc123' ) {
@@ -110,25 +39,32 @@ final mockHttpClient = MockAWSHttpClient((request, _) async {
110
39
if (body.contains ('getBlog' )) {
111
40
return AWSHttpResponse (
112
41
statusCode: 200 ,
113
- body: utf8.encode (json.encode (_expectedModelQueryResult )),
42
+ body: utf8.encode (json.encode (expectedModelQueryResult )),
114
43
);
115
44
}
116
45
if (body.contains ('TestMutate' )) {
117
46
return AWSHttpResponse (
118
47
statusCode: 400 ,
119
- body: utf8.encode (json.encode (_expectedMutateSuccessResponseBody )),
48
+ body: utf8.encode (json.encode (expectedMutateSuccessResponseBody )),
120
49
);
121
50
}
122
51
if (body.contains ('TestError' )) {
123
52
return AWSHttpResponse (
124
53
statusCode: 400 ,
125
- body: utf8.encode (json.encode (_expectedErrorResponseBody)),
54
+ body: utf8.encode (json.encode (expectedErrorResponseBody)),
55
+ );
56
+ }
57
+ if (body.contains ('createModelWithCustomType' )) {
58
+ return AWSHttpResponse (
59
+ statusCode: 200 ,
60
+ body: utf8
61
+ .encode (json.encode (expectedModelWithCustomTypeSuccessResponseBody)),
126
62
);
127
63
}
128
64
129
65
return AWSHttpResponse (
130
66
statusCode: 400 ,
131
- body: utf8.encode ((json.encode (_expectedQuerySuccessResponseBody ))),
67
+ body: utf8.encode ((json.encode (expectedQuerySuccessResponseBody ))),
132
68
);
133
69
});
134
70
@@ -195,7 +131,7 @@ void main() {
195
131
final operation = Amplify .API .query (request: req);
196
132
final res = await operation.response;
197
133
198
- final expected = json.encode (_expectedQuerySuccessResponseBody ['data' ]);
134
+ final expected = json.encode (expectedQuerySuccessResponseBody ['data' ]);
199
135
200
136
expect (res.data, equals (expected));
201
137
expect (res.errors, isEmpty);
@@ -219,7 +155,7 @@ void main() {
219
155
final operation = Amplify .API .query (request: req);
220
156
final res = await operation.response;
221
157
222
- final expected = json.encode (_expectedQuerySuccessResponseBody ['data' ]);
158
+ final expected = json.encode (expectedQuerySuccessResponseBody ['data' ]);
223
159
224
160
expect (res.data, equals (expected));
225
161
expect (res.errors, isEmpty);
@@ -242,7 +178,23 @@ void main() {
242
178
final operation = Amplify .API .mutate (request: req);
243
179
final res = await operation.response;
244
180
245
- final expected = json.encode (_expectedMutateSuccessResponseBody['data' ]);
181
+ final expected = json.encode (expectedMutateSuccessResponseBody['data' ]);
182
+
183
+ expect (res.data, equals (expected));
184
+ expect (res.errors, isEmpty);
185
+ });
186
+
187
+ test ('Mutate returns proper response.data for custom types' , () async {
188
+ final req = GraphQLRequest <String >(
189
+ document: modelWithCustomTypeDocument,
190
+ variables: modelWithCustomTypeVariables,
191
+ );
192
+
193
+ final operation = Amplify .API .mutate (request: req);
194
+ final res = await operation.response;
195
+
196
+ final expected =
197
+ json.encode (expectedModelWithCustomTypeSuccessResponseBody['data' ]);
246
198
247
199
expect (res.data, equals (expected));
248
200
expect (res.errors, isEmpty);
@@ -255,23 +207,57 @@ void main() {
255
207
const expectedDoc =
256
208
'query getBlog(\$ id: ID!) { getBlog(id: \$ id) { $blogSelectionSet } }' ;
257
209
const decodePath = 'getBlog' ;
258
- final blog = Blog (id: _modelQueryId , name: 'Lorem ipsum $_modelQueryId ' );
210
+ final blog = Blog (id: modelQueryId , name: 'Lorem ipsum $modelQueryId ' );
259
211
final req = ModelQueries .get <Blog >(Blog .classType, blog.modelIdentifier);
260
212
261
213
final operation = Amplify .API .query (request: req);
262
214
final res = await operation.response;
263
215
264
216
// request asserts
265
217
expect (req.document, expectedDoc);
266
- expect (_deepEquals (req.variables, {'id' : _modelQueryId }), isTrue);
218
+ expect (_deepEquals (req.variables, {'id' : modelQueryId }), isTrue);
267
219
expect (req.modelType, Blog .classType);
268
220
expect (req.decodePath, decodePath);
269
221
// response asserts
270
222
expect (res.data, isA <Blog >());
271
- expect (res.data? .id, _modelQueryId);
223
+ expect (res.data? .id, modelQueryId);
224
+ expect (res.errors, isEmpty);
225
+ });
226
+
227
+ test (
228
+ 'Mutation.create returns proper response.data for Models with custom types' ,
229
+ () async {
230
+ const expectedDoc = modelWithCustomTypeDocument;
231
+ const decodePath = 'createModelWithCustomType' ;
232
+ final req = ModelMutations .create <ModelWithCustomType >(
233
+ modelWithCustomType,
234
+ );
235
+
236
+ final operation = Amplify .API .query (request: req);
237
+ final res = await operation.response;
238
+
239
+ // request asserts
240
+ expect (req.document, expectedDoc);
241
+ expect (_deepEquals (req.variables, modelWithCustomTypeVariables), isTrue);
242
+ expect (req.modelType, ModelWithCustomType .classType);
243
+ expect (req.decodePath, decodePath);
244
+ // response asserts
245
+ expect (res.data, isA <ModelWithCustomType >());
246
+ expect (res.data? .id, modelCustomTypeId);
247
+
248
+ final data = res.data! ;
249
+ expect (
250
+ data.customTypeValue,
251
+ equals (modelWithCustomType.customTypeValue),
252
+ );
253
+ expect (
254
+ data.listOfCustomTypeValue,
255
+ equals (modelWithCustomType.listOfCustomTypeValue),
256
+ );
272
257
expect (res.errors, isEmpty);
273
258
});
274
259
});
260
+
275
261
const graphQLDocument = '''subscription MySubscription {
276
262
onCreateBlog {
277
263
id
@@ -550,15 +536,15 @@ void main() {
550
536
final res = await operation.response;
551
537
552
538
const errorExpected = GraphQLResponseError (
553
- message: _errorMessage ,
539
+ message: errorMessage ,
554
540
locations: [
555
541
GraphQLResponseErrorLocation (2 , 3 ),
556
542
GraphQLResponseErrorLocation (4 , 5 ),
557
543
],
558
- path: < dynamic > [..._errorPath ],
559
- extensions: < String , dynamic > {..._errorExtensions },
560
- errorType: _errorType ,
561
- errorInfo: _errorInfo ,
544
+ path: < dynamic > [...errorPath ],
545
+ extensions: < String , dynamic > {...errorExtensions },
546
+ errorType: errorType ,
547
+ errorInfo: errorInfo ,
562
548
);
563
549
564
550
expect (res.data, equals (null ));
@@ -585,7 +571,7 @@ void main() {
585
571
final res = await operation.response;
586
572
587
573
const errorExpected = GraphQLResponseError (
588
- message: _authErrorMessage ,
574
+ message: authErrorMessage ,
589
575
);
590
576
591
577
expect (res.data, equals (null ));
0 commit comments