|
| 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('observeQuery', () { |
| 27 | + setUp(() async { |
| 28 | + await configureDataStore(); |
| 29 | + await clearDataStore(); |
| 30 | + }); |
| 31 | + |
| 32 | + testWidgets( |
| 33 | + 'should return an initial result set that is consistent with query()', |
| 34 | + (WidgetTester tester) async { |
| 35 | + List<Blog> blogs = |
| 36 | + List.generate(10, (index) => Blog(name: 'blog $index')); |
| 37 | + |
| 38 | + for (var blog in blogs) { |
| 39 | + await Amplify.DataStore.save(blog); |
| 40 | + } |
| 41 | + var queryBlogs = await Amplify.DataStore.query(Blog.classType); |
| 42 | + |
| 43 | + var observeQueryBlogs = |
| 44 | + (await Amplify.DataStore.observeQuery(Blog.classType).first).items; |
| 45 | + |
| 46 | + expect(observeQueryBlogs.length, 10); |
| 47 | + expect(observeQueryBlogs, orderedEquals(queryBlogs)); |
| 48 | + }); |
| 49 | + |
| 50 | + testWidgets('should emit new snapshots with updated items', |
| 51 | + (WidgetTester tester) async { |
| 52 | + List<Blog> blogs = List.generate(3, (index) => Blog(name: 'blog $index')); |
| 53 | + |
| 54 | + for (var blog in blogs) { |
| 55 | + await Amplify.DataStore.save(blog); |
| 56 | + } |
| 57 | + |
| 58 | + var observeQueryItemStream = Amplify.DataStore.observeQuery( |
| 59 | + Blog.classType, |
| 60 | + ).map((event) => event.items); |
| 61 | + |
| 62 | + Blog newBlog1 = Blog(name: 'new blog 1'); |
| 63 | + Blog newBlog2 = Blog(name: 'new blog 2'); |
| 64 | + Blog newBlog1Copy = newBlog1.copyWith(name: 'new name'); |
| 65 | + |
| 66 | + expectLater( |
| 67 | + observeQueryItemStream, |
| 68 | + emitsInOrder([ |
| 69 | + orderedEquals([...blogs]), |
| 70 | + orderedEquals([...blogs, newBlog1]), |
| 71 | + orderedEquals([...blogs, newBlog1, newBlog2]), |
| 72 | + orderedEquals([...blogs, newBlog1Copy, newBlog2]), |
| 73 | + orderedEquals([...blogs, newBlog2]), |
| 74 | + ]), |
| 75 | + ); |
| 76 | + |
| 77 | + await Amplify.DataStore.save(newBlog1); |
| 78 | + await Amplify.DataStore.save(newBlog2); |
| 79 | + await Amplify.DataStore.save(newBlog1Copy); |
| 80 | + await Amplify.DataStore.delete(newBlog1Copy); |
| 81 | + }); |
| 82 | + |
| 83 | + testWidgets('should respect query predicates', (WidgetTester tester) async { |
| 84 | + List<Blog> blogs = List.generate(3, (index) => Blog(name: 'blog $index')); |
| 85 | + |
| 86 | + for (var blog in blogs) { |
| 87 | + await Amplify.DataStore.save(blog); |
| 88 | + } |
| 89 | + |
| 90 | + var observeQueryItemStream = Amplify.DataStore.observeQuery( |
| 91 | + Blog.classType, |
| 92 | + where: Blog.NAME.contains('blog'), |
| 93 | + ).map((event) => event.items); |
| 94 | + |
| 95 | + Blog newBlog1 = Blog(name: 'new blog 1'); |
| 96 | + Blog newBlog2 = Blog(name: 'new blog 2'); |
| 97 | + Blog newBlog3 = Blog(name: 'new 3'); |
| 98 | + Blog newBlog1Copy = newBlog1.copyWith(name: 'new name'); |
| 99 | + |
| 100 | + expectLater( |
| 101 | + observeQueryItemStream, |
| 102 | + emitsInOrder([ |
| 103 | + orderedEquals([...blogs]), |
| 104 | + orderedEquals([...blogs, newBlog1]), |
| 105 | + orderedEquals([...blogs, newBlog1, newBlog2]), |
| 106 | + orderedEquals([...blogs, newBlog2]), |
| 107 | + ]), |
| 108 | + ); |
| 109 | + |
| 110 | + await Amplify.DataStore.save(newBlog1); |
| 111 | + await Amplify.DataStore.save(newBlog2); |
| 112 | + await Amplify.DataStore.save(newBlog3); |
| 113 | + await Amplify.DataStore.save(newBlog1Copy); |
| 114 | + }); |
| 115 | + |
| 116 | + testWidgets('should respect sort orders', (WidgetTester tester) async { |
| 117 | + List<Blog> blogs = List.generate(3, (index) => Blog(name: 'blog $index')); |
| 118 | + |
| 119 | + for (var blog in blogs) { |
| 120 | + await Amplify.DataStore.save(blog); |
| 121 | + } |
| 122 | + |
| 123 | + var observeQueryItemStream = Amplify.DataStore.observeQuery( |
| 124 | + Blog.classType, |
| 125 | + sortBy: [Blog.NAME.ascending()], |
| 126 | + ).map((event) => event.items); |
| 127 | + |
| 128 | + Blog newBlog1 = Blog(name: 'aaa blog'); |
| 129 | + Blog newBlog2 = Blog(name: 'ccc blog'); |
| 130 | + Blog newBlog2Copy = newBlog2.copyWith(name: 'azz blog'); |
| 131 | + |
| 132 | + expectLater( |
| 133 | + observeQueryItemStream, |
| 134 | + emitsInOrder([ |
| 135 | + orderedEquals([...blogs]), |
| 136 | + orderedEquals([newBlog1, ...blogs]), |
| 137 | + orderedEquals([newBlog1, ...blogs, newBlog2]), |
| 138 | + orderedEquals([newBlog1, newBlog2Copy, ...blogs]), |
| 139 | + ]), |
| 140 | + ); |
| 141 | + |
| 142 | + await Amplify.DataStore.save(newBlog1); |
| 143 | + await Amplify.DataStore.save(newBlog2); |
| 144 | + await Amplify.DataStore.save(newBlog2Copy); |
| 145 | + }); |
| 146 | + }); |
| 147 | +} |
0 commit comments