Skip to content

Commit 324ebad

Browse files
test(DataStore): add additional local integration tests for datastore (#844)
1 parent 00127b7 commit 324ebad

File tree

14 files changed

+1367
-3
lines changed

14 files changed

+1367
-3
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import 'package:amplify_datastore_example/models/ModelProvider.dart';
17+
import 'package:integration_test/integration_test.dart';
18+
import 'package:flutter_test/flutter_test.dart';
19+
import 'package:amplify_flutter/amplify.dart';
20+
21+
import 'utils/setup_utils.dart';
22+
23+
void main() {
24+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
25+
26+
group('clear', () {
27+
setUp(() async {
28+
await configureDataStore();
29+
await clearDataStore();
30+
});
31+
32+
testWidgets('should clear the store', (WidgetTester tester) async {
33+
Blog blog = Blog(name: 'blog');
34+
await Amplify.DataStore.save(blog);
35+
var resultOne = await Amplify.DataStore.query(Blog.classType);
36+
expect(resultOne, isNotEmpty);
37+
await Amplify.DataStore.clear();
38+
var resultTwo = await Amplify.DataStore.query(Blog.classType);
39+
expect(resultTwo, isEmpty);
40+
});
41+
});
42+
}

packages/amplify_datastore/example/integration_test/main_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import 'save_test.dart' as save_tests;
2020
import 'query_test/query_test.dart' as query_test;
2121
import 'model_type_test.dart' as model_type_tests;
2222
import 'relationship_type_test.dart' as relationship_type_tests;
23+
import 'observe_test.dart' as observe_tests;
24+
import 'clear_test.dart' as clear_tests;
2325

2426
import 'utils/setup_utils.dart';
2527

@@ -35,5 +37,7 @@ void main() async {
3537
query_test.main();
3638
model_type_tests.main();
3739
relationship_type_tests.main();
40+
observe_tests.main();
41+
clear_tests.main();
3842
});
3943
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import 'package:amplify_datastore/amplify_datastore.dart';
17+
import 'package:amplify_datastore_example/models/ModelProvider.dart';
18+
import 'package:integration_test/integration_test.dart';
19+
import 'package:flutter_test/flutter_test.dart';
20+
import 'package:amplify_flutter/amplify.dart';
21+
22+
import 'utils/setup_utils.dart';
23+
24+
void main() {
25+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
26+
27+
group('observe', () {
28+
setUp(() async {
29+
await configureDataStore();
30+
await clearDataStore();
31+
});
32+
33+
testWidgets('should broadcast events for create, update, and delete',
34+
(WidgetTester tester) async {
35+
Blog blog = Blog(name: 'blog');
36+
Blog updatedBlog = blog.copyWith(name: 'updated blog');
37+
38+
var eventTypeStream = Amplify.DataStore.observe(Blog.classType)
39+
.map((event) => event.eventType);
40+
expectLater(
41+
eventTypeStream,
42+
emitsInOrder(
43+
[
44+
EventType.create,
45+
EventType.update,
46+
EventType.delete,
47+
],
48+
),
49+
);
50+
await Amplify.DataStore.save(blog);
51+
await Amplify.DataStore.save(updatedBlog);
52+
await Amplify.DataStore.delete(updatedBlog);
53+
});
54+
55+
testWidgets(
56+
'should broadcast events with the model that is created, update, or deleted',
57+
(WidgetTester tester) async {
58+
Blog blog = Blog(name: 'blog');
59+
Blog updatedBlog = blog.copyWith(name: 'updated blog');
60+
61+
var eventItemStream =
62+
Amplify.DataStore.observe(Blog.classType).map((event) => event.item);
63+
expectLater(
64+
eventItemStream,
65+
emitsInOrder(
66+
[
67+
blog,
68+
updatedBlog,
69+
updatedBlog,
70+
],
71+
),
72+
);
73+
74+
await Amplify.DataStore.save(blog);
75+
await Amplify.DataStore.save(updatedBlog);
76+
await Amplify.DataStore.delete(updatedBlog);
77+
});
78+
});
79+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
import 'package:amplify_datastore/amplify_datastore.dart';
17+
18+
import 'package:amplify_datastore_example/models/ModelProvider.dart';
19+
import 'package:integration_test/integration_test.dart';
20+
import 'package:flutter_test/flutter_test.dart';
21+
import 'package:amplify_flutter/amplify.dart';
22+
23+
import '../utils/setup_utils.dart';
24+
25+
void main() {
26+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
27+
28+
group('pagination', () {
29+
var models = List.generate(1000, (i) => Blog(name: 'blog $i'));
30+
var sortedModels = models..sort((a, b) => a.name.compareTo(b.name));
31+
setUpAll(() async {
32+
await configureDataStore();
33+
await clearDataStore();
34+
for (var model in models) {
35+
await Amplify.DataStore.save(model);
36+
}
37+
});
38+
testWidgets('should return the models for the given page and limit',
39+
(WidgetTester tester) async {
40+
// page 0
41+
var pageZeroBlogs = await Amplify.DataStore.query(
42+
Blog.classType,
43+
pagination: QueryPagination(page: 0, limit: 10),
44+
sortBy: [Blog.NAME.ascending()],
45+
);
46+
var expectedPageZeroBlogs = sortedModels.getRange(0, 10).toList();
47+
expect(pageZeroBlogs.length, 10);
48+
expect(pageZeroBlogs, orderedEquals(expectedPageZeroBlogs));
49+
50+
// page 1
51+
var pageOneBlogs = await Amplify.DataStore.query(
52+
Blog.classType,
53+
pagination: QueryPagination(page: 1, limit: 10),
54+
sortBy: [Blog.NAME.ascending()],
55+
);
56+
var expectedPageOneBlogs = sortedModels.getRange(10, 20).toList();
57+
expect(pageOneBlogs.length, 10);
58+
expect(pageOneBlogs, orderedEquals(expectedPageOneBlogs));
59+
60+
// final page
61+
var finalPageBlogs = await Amplify.DataStore.query(
62+
Blog.classType,
63+
pagination: QueryPagination(page: 99, limit: 10),
64+
sortBy: [Blog.NAME.ascending()],
65+
);
66+
var expectedFinalPageBlogs = sortedModels.getRange(990, 1000).toList();
67+
expect(pageOneBlogs.length, 10);
68+
expect(finalPageBlogs, orderedEquals(expectedFinalPageBlogs));
69+
});
70+
71+
testWidgets('should return no models for an out of range page/limit combo',
72+
(WidgetTester tester) async {
73+
var blogs = await Amplify.DataStore.query(Blog.classType,
74+
pagination: QueryPagination(page: 1000, limit: 10));
75+
expect(blogs, isEmpty);
76+
});
77+
78+
testWidgets('should default to a page size of 100',
79+
(WidgetTester tester) async {
80+
var blogs = await Amplify.DataStore.query(
81+
Blog.classType,
82+
pagination: QueryPagination(page: 0),
83+
sortBy: [Blog.NAME.ascending()],
84+
);
85+
var expectedBlogs = sortedModels.getRange(0, 100).toList();
86+
expect(blogs.length, 100);
87+
expect(blogs, orderedEquals(expectedBlogs));
88+
});
89+
90+
testWidgets('should work with a descending sort order',
91+
(WidgetTester tester) async {
92+
var blogs = await Amplify.DataStore.query(
93+
Blog.classType,
94+
pagination: QueryPagination(page: 0, limit: 10),
95+
sortBy: [Blog.NAME.descending()],
96+
);
97+
var expectedBlogs = models.getRange(990, 1000).toList().reversed.toList();
98+
expect(blogs.length, 10);
99+
expect(blogs, orderedEquals(expectedBlogs));
100+
});
101+
102+
testWidgets('should work with a query predicate',
103+
(WidgetTester tester) async {
104+
var blogs = await Amplify.DataStore.query(
105+
Blog.classType,
106+
pagination: QueryPagination(page: 0, limit: 10),
107+
sortBy: [Blog.NAME.ascending()],
108+
where: Blog.NAME.beginsWith('blog 1'),
109+
);
110+
var expectedBlogs = sortedModels
111+
.where((blog) => blog.name.startsWith('blog 1'))
112+
.toList()
113+
.getRange(0, 10)
114+
.toList();
115+
expect(blogs.length, 10);
116+
expect(blogs, orderedEquals(expectedBlogs));
117+
});
118+
});
119+
}

0 commit comments

Comments
 (0)