Skip to content

Commit 604c08c

Browse files
chore(datastore): add integration tests for datastore (#753)
* chore: add integration tests for datastore * chore: address pr comments * chore: configure datastore before each test * chore: refactor save test * chore: fix formatting * chore: update name to configureDataStore
1 parent c0ccdfd commit 604c08c

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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:integration_test/integration_test.dart';
17+
import 'package:flutter_test/flutter_test.dart';
18+
19+
import 'save_test.dart' as save_tests;
20+
import 'query_test.dart' as query_tests;
21+
import 'utils/setup_utils.dart';
22+
23+
void main() async {
24+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
25+
26+
group('amplify_datastore', () {
27+
setUpAll(() async {
28+
await configureDataStore();
29+
});
30+
31+
save_tests.main();
32+
query_tests.main();
33+
});
34+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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('query', () {
28+
setUp(() async {
29+
await configureDataStore();
30+
// clear data before each test
31+
await Amplify.DataStore.clear();
32+
});
33+
34+
testWidgets('should return all data by default',
35+
(WidgetTester tester) async {
36+
Blog testBlog1 = Blog(name: 'blog one');
37+
Blog testBlog2 = Blog(name: 'blog two');
38+
Blog testBlog3 = Blog(name: 'blog three');
39+
await Amplify.DataStore.save(testBlog1);
40+
await Amplify.DataStore.save(testBlog2);
41+
await Amplify.DataStore.save(testBlog3);
42+
var blogs = await Amplify.DataStore.query(Blog.classType);
43+
expect(blogs.length, 3);
44+
expect(blogs.contains(testBlog1), isTrue);
45+
expect(blogs.contains(testBlog2), isTrue);
46+
expect(blogs.contains(testBlog3), isTrue);
47+
});
48+
49+
testWidgets('should return the correct record when queried by id',
50+
(WidgetTester tester) async {
51+
Blog testBlog1 = Blog(name: 'blog one');
52+
Blog testBlog2 = Blog(name: 'blog two');
53+
Blog testBlog3 = Blog(name: 'blog three');
54+
await Amplify.DataStore.save(testBlog1);
55+
await Amplify.DataStore.save(testBlog2);
56+
await Amplify.DataStore.save(testBlog3);
57+
var blogs = await Amplify.DataStore.query(Blog.classType,
58+
where: Blog.ID.eq(testBlog1.id));
59+
expect(blogs.length, 1);
60+
var blog = blogs[0];
61+
expect(blog, testBlog1);
62+
});
63+
64+
testWidgets('should return the ID of nested objects',
65+
(WidgetTester tester) async {
66+
Blog testBlog = Blog(name: 'test blog');
67+
await Amplify.DataStore.save(testBlog);
68+
Post testPost = Post(
69+
title: 'test post',
70+
blog: testBlog,
71+
created: TemporalDateTime(DateTime.now()),
72+
rating: 10,
73+
);
74+
await Amplify.DataStore.save(testPost);
75+
var posts = await Amplify.DataStore.query(Post.classType);
76+
expect(posts.length, 1);
77+
var post = posts[0];
78+
expect(post.blog!.id, testBlog.id);
79+
});
80+
});
81+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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('save', () {
27+
setUp(() async {
28+
await configureDataStore();
29+
// clear data before each test
30+
await Amplify.DataStore.clear();
31+
});
32+
33+
testWidgets('should save data locally', (WidgetTester tester) async {
34+
Blog testBlog = Blog(name: 'test blog');
35+
await Amplify.DataStore.save(testBlog);
36+
var blogs = await Amplify.DataStore.query(Blog.classType);
37+
expect(blogs.length, 1);
38+
expect(blogs.contains(testBlog), isTrue);
39+
});
40+
});
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:amplify_datastore/amplify_datastore.dart';
2+
import 'package:amplify_datastore_example/amplifyconfiguration.dart';
3+
import 'package:amplify_datastore_example/models/ModelProvider.dart';
4+
import 'package:amplify_flutter/amplify.dart';
5+
6+
Future<void> configureDataStore() async {
7+
if (!Amplify.isConfigured) {
8+
final dataStorePlugin =
9+
AmplifyDataStore(modelProvider: ModelProvider.instance);
10+
await Amplify.addPlugins([dataStorePlugin]);
11+
await Amplify.configure(amplifyconfig);
12+
}
13+
}

packages/amplify_datastore/example/pubspec.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
77

88
environment:
99
sdk: ">=2.12.0 <3.0.0"
10+
flutter: ">=2.2.0"
1011

1112
dependencies:
1213
flutter:
@@ -23,6 +24,10 @@ dependencies:
2324
# path: ../../amplify_api
2425
amplify_flutter:
2526
path: ../../amplify_flutter
27+
flutter_driver:
28+
sdk: flutter
29+
integration_test:
30+
sdk: flutter
2631

2732
# The following adds the Cupertino Icons font to your application.
2833
# Use with the CupertinoIcons class for iOS style icons.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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:integration_test/integration_test_driver.dart';
17+
18+
Future<void> main() => integrationDriver();

0 commit comments

Comments
 (0)