Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(datastore): add integration tests for datastore #753

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 'package:amplify_datastore_example/amplifyconfiguration.dart';

import 'save_test.dart' as save_tests;
import 'query_test.dart' as query_tests;

void main() async {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('amplify_datastore', () {
setUpAll(() async {
Jordan-Nelson marked this conversation as resolved.
Show resolved Hide resolved
final dataStorePlugin =
AmplifyDataStore(modelProvider: ModelProvider.instance);
await Amplify.addPlugins([dataStorePlugin]);
await Amplify.configure(amplifyconfig);
});

save_tests.main();
query_tests.main();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('query', () {
setUp(() async {
// 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);
Jordan-Nelson marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
}
Original file line number Diff line number Diff line change
@@ -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';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('save', () {
setUp(() async {
// clear data before each test
await Amplify.DataStore.clear();
});

testWidgets('should save data locally', (WidgetTester tester) async {
final blogName = 'test name';
Blog blog = Blog(
name: blogName,
);
await Amplify.DataStore.save(blog);
var blogs = await Amplify.DataStore.query(Blog.classType);
expect(blogs.length, 1);
expect(blogs[0].name, blogName);
});
});
}
5 changes: 5 additions & 0 deletions packages/amplify_datastore/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -23,6 +24,10 @@ dependencies:
# path: ../../amplify_api
amplify_flutter:
path: ../../amplify_flutter
flutter_driver:
Jordan-Nelson marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> main() => integrationDriver();