|
| 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