diff --git a/packages/amplify_datastore/example/integration_test/main_test.dart b/packages/amplify_datastore/example/integration_test/main_test.dart new file mode 100644 index 0000000000..1889e8bec0 --- /dev/null +++ b/packages/amplify_datastore/example/integration_test/main_test.dart @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +import 'package:integration_test/integration_test.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'save_test.dart' as save_tests; +import 'query_test.dart' as query_tests; +import 'utils/setup_utils.dart'; + +void main() async { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + group('amplify_datastore', () { + setUpAll(() async { + await configureDataStore(); + }); + + save_tests.main(); + query_tests.main(); + }); +} diff --git a/packages/amplify_datastore/example/integration_test/query_test.dart b/packages/amplify_datastore/example/integration_test/query_test.dart new file mode 100644 index 0000000000..619b189ffc --- /dev/null +++ b/packages/amplify_datastore/example/integration_test/query_test.dart @@ -0,0 +1,81 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +import 'package:amplify_datastore/amplify_datastore.dart'; +import 'package:amplify_datastore_example/models/ModelProvider.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:amplify_flutter/amplify.dart'; + +import 'utils/setup_utils.dart'; + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + group('query', () { + setUp(() async { + await configureDataStore(); + // clear data before each test + await Amplify.DataStore.clear(); + }); + + testWidgets('should return all data by default', + (WidgetTester tester) async { + Blog testBlog1 = Blog(name: 'blog one'); + Blog testBlog2 = Blog(name: 'blog two'); + Blog testBlog3 = Blog(name: 'blog three'); + await Amplify.DataStore.save(testBlog1); + await Amplify.DataStore.save(testBlog2); + await Amplify.DataStore.save(testBlog3); + var blogs = await Amplify.DataStore.query(Blog.classType); + expect(blogs.length, 3); + expect(blogs.contains(testBlog1), isTrue); + expect(blogs.contains(testBlog2), isTrue); + expect(blogs.contains(testBlog3), isTrue); + }); + + testWidgets('should return the correct record when queried by id', + (WidgetTester tester) async { + Blog testBlog1 = Blog(name: 'blog one'); + Blog testBlog2 = Blog(name: 'blog two'); + Blog testBlog3 = Blog(name: 'blog three'); + await Amplify.DataStore.save(testBlog1); + await Amplify.DataStore.save(testBlog2); + await Amplify.DataStore.save(testBlog3); + var blogs = await Amplify.DataStore.query(Blog.classType, + where: Blog.ID.eq(testBlog1.id)); + expect(blogs.length, 1); + var blog = blogs[0]; + expect(blog, testBlog1); + }); + + testWidgets('should return the ID of nested objects', + (WidgetTester tester) async { + Blog testBlog = Blog(name: 'test blog'); + await Amplify.DataStore.save(testBlog); + Post testPost = Post( + title: 'test post', + blog: testBlog, + created: TemporalDateTime(DateTime.now()), + rating: 10, + ); + await Amplify.DataStore.save(testPost); + var posts = await Amplify.DataStore.query(Post.classType); + expect(posts.length, 1); + var post = posts[0]; + expect(post.blog!.id, testBlog.id); + }); + }); +} diff --git a/packages/amplify_datastore/example/integration_test/save_test.dart b/packages/amplify_datastore/example/integration_test/save_test.dart new file mode 100644 index 0000000000..a22f8ff1d2 --- /dev/null +++ b/packages/amplify_datastore/example/integration_test/save_test.dart @@ -0,0 +1,41 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +import 'package:amplify_datastore_example/models/ModelProvider.dart'; +import 'package:integration_test/integration_test.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:amplify_flutter/amplify.dart'; + +import 'utils/setup_utils.dart'; + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + group('save', () { + setUp(() async { + await configureDataStore(); + // clear data before each test + await Amplify.DataStore.clear(); + }); + + testWidgets('should save data locally', (WidgetTester tester) async { + Blog testBlog = Blog(name: 'test blog'); + await Amplify.DataStore.save(testBlog); + var blogs = await Amplify.DataStore.query(Blog.classType); + expect(blogs.length, 1); + expect(blogs.contains(testBlog), isTrue); + }); + }); +} diff --git a/packages/amplify_datastore/example/integration_test/utils/setup_utils.dart b/packages/amplify_datastore/example/integration_test/utils/setup_utils.dart new file mode 100644 index 0000000000..f5d5004365 --- /dev/null +++ b/packages/amplify_datastore/example/integration_test/utils/setup_utils.dart @@ -0,0 +1,13 @@ +import 'package:amplify_datastore/amplify_datastore.dart'; +import 'package:amplify_datastore_example/amplifyconfiguration.dart'; +import 'package:amplify_datastore_example/models/ModelProvider.dart'; +import 'package:amplify_flutter/amplify.dart'; + +Future configureDataStore() async { + if (!Amplify.isConfigured) { + final dataStorePlugin = + AmplifyDataStore(modelProvider: ModelProvider.instance); + await Amplify.addPlugins([dataStorePlugin]); + await Amplify.configure(amplifyconfig); + } +} diff --git a/packages/amplify_datastore/example/pubspec.yaml b/packages/amplify_datastore/example/pubspec.yaml index 2d2bc27697..9b1a249c6f 100644 --- a/packages/amplify_datastore/example/pubspec.yaml +++ b/packages/amplify_datastore/example/pubspec.yaml @@ -7,6 +7,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev environment: sdk: ">=2.12.0 <3.0.0" + flutter: ">=2.2.0" dependencies: flutter: @@ -23,6 +24,10 @@ dependencies: # path: ../../amplify_api amplify_flutter: path: ../../amplify_flutter + flutter_driver: + sdk: flutter + integration_test: + sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. diff --git a/packages/amplify_datastore/example/test_driver/integration_test.dart b/packages/amplify_datastore/example/test_driver/integration_test.dart new file mode 100644 index 0000000000..dce7b9cc2a --- /dev/null +++ b/packages/amplify_datastore/example/test_driver/integration_test.dart @@ -0,0 +1,18 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +import 'package:integration_test/integration_test_driver.dart'; + +Future main() => integrationDriver();