Repository navigation
feat(cloud_firestore): experimental setIndexConfiguration() API
#9928
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8ddbe8
feat(firestore): setIndexConfiguration() API
russellwheatley ee7b2a0
fix analyse issues
russellwheatley 786b913
format
russellwheatley ab9e2af
revert main.dart
russellwheatley 89f7670
format
russellwheatley 0f2cb0b
fix(firestore, web): pass app into setIndexConfiguration()
russellwheatley 8435c49
docs(firestore, web): docs for setIndexConfiguration()
russellwheatley 04b2420
chore(firestore): make setIndexConfiguration() experimental
russellwheatley 7cf40da
chore(firestore): capitalise expected values
russellwheatley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,98 +18,106 @@ void runInstanceTests() { | |
| firestore = FirebaseFirestore.instance; | ||
| }); | ||
|
|
||
| test('snapshotsInSync()', () async { | ||
| DocumentReference<Map<String, dynamic>> documentReference = | ||
| firestore.doc('flutter-tests/insync'); | ||
|
|
||
| // Ensure deleted | ||
| await documentReference.delete(); | ||
|
|
||
| StreamController controller = StreamController(); | ||
| StreamSubscription insync; | ||
| StreamSubscription snapshots; | ||
|
|
||
| int inSyncCount = 0; | ||
|
|
||
| insync = firestore.snapshotsInSync().listen((_) { | ||
| controller.add('insync=$inSyncCount'); | ||
| inSyncCount++; | ||
| }); | ||
|
|
||
| snapshots = documentReference.snapshots().listen((ds) { | ||
| controller.add('snapshot-exists=${ds.exists}'); | ||
| }); | ||
|
|
||
| // Allow the snapshots to trigger... | ||
| await Future.delayed(const Duration(seconds: 1)); | ||
|
|
||
| await documentReference.set({'foo': 'bar'}); | ||
|
|
||
| await expectLater( | ||
| controller.stream, | ||
| emitsInOrder([ | ||
| 'insync=0', // No other snapshots | ||
| 'snapshot-exists=false', | ||
| 'insync=1', | ||
| 'snapshot-exists=true', | ||
| 'insync=2', | ||
| ]), | ||
| ); | ||
|
|
||
| await controller.close(); | ||
| await insync.cancel(); | ||
| await snapshots.cancel(); | ||
| }); | ||
|
|
||
| test('enableNetwork()', () async { | ||
| if (kIsWeb) return; | ||
|
|
||
| // Write some data while online | ||
| await firestore.enableNetwork(); | ||
| DocumentReference<Map<String, dynamic>> documentReference = | ||
| firestore.doc('flutter-tests/enable-network'); | ||
| await documentReference.set({'foo': 'bar'}); | ||
|
|
||
| // Disable the network | ||
| await firestore.disableNetwork(); | ||
|
|
||
| StreamController controller = StreamController(); | ||
|
|
||
| // Set some data while offline | ||
| // ignore: unawaited_futures | ||
| documentReference.set({'foo': 'baz'}).then((_) async { | ||
| // Only when back online will this trigger | ||
| controller.add(true); | ||
| }); | ||
| test( | ||
| 'snapshotsInSync()', | ||
| () async { | ||
| DocumentReference<Map<String, dynamic>> documentReference = | ||
| firestore.doc('flutter-tests/insync'); | ||
|
|
||
| // Ensure deleted | ||
| await documentReference.delete(); | ||
|
|
||
| StreamController controller = StreamController(); | ||
| StreamSubscription insync; | ||
| StreamSubscription snapshots; | ||
|
|
||
| int inSyncCount = 0; | ||
|
|
||
| insync = firestore.snapshotsInSync().listen((_) { | ||
| controller.add('insync=$inSyncCount'); | ||
| inSyncCount++; | ||
| }); | ||
|
|
||
| snapshots = documentReference.snapshots().listen((ds) { | ||
| controller.add('snapshot-exists=${ds.exists}'); | ||
| }); | ||
|
|
||
| // Allow the snapshots to trigger... | ||
| await Future.delayed(const Duration(seconds: 1)); | ||
|
|
||
| await documentReference.set({'foo': 'bar'}); | ||
|
|
||
| await expectLater( | ||
| controller.stream, | ||
| emitsInOrder([ | ||
| 'insync=0', // No other snapshots | ||
| 'snapshot-exists=false', | ||
| 'insync=1', | ||
| 'snapshot-exists=true', | ||
| 'insync=2', | ||
| ]), | ||
| ); | ||
|
|
||
| await controller.close(); | ||
| await insync.cancel(); | ||
| await snapshots.cancel(); | ||
| }, | ||
| skip: kIsWeb, | ||
| ); | ||
|
|
||
| // Go back online | ||
| await firestore.enableNetwork(); | ||
| test( | ||
| 'enableNetwork()', | ||
| () async { | ||
| // Write some data while online | ||
| await firestore.enableNetwork(); | ||
| DocumentReference<Map<String, dynamic>> documentReference = | ||
| firestore.doc('flutter-tests/enable-network'); | ||
| await documentReference.set({'foo': 'bar'}); | ||
|
|
||
| await expectLater(controller.stream, emits(true)); | ||
| await controller.close(); | ||
| }); | ||
| // Disable the network | ||
| await firestore.disableNetwork(); | ||
|
|
||
| test('disableNetwork()', () async { | ||
| if (kIsWeb) return; | ||
| StreamController controller = StreamController(); | ||
|
|
||
| // Write some data while online | ||
| await firestore.enableNetwork(); | ||
| DocumentReference<Map<String, dynamic>> documentReference = | ||
| firestore.doc('flutter-tests/disable-network'); | ||
| await documentReference.set({'foo': 'bar'}); | ||
| // Set some data while offline | ||
| // ignore: unawaited_futures | ||
| documentReference.set({'foo': 'baz'}).then((_) async { | ||
| // Only when back online will this trigger | ||
| controller.add(true); | ||
| }); | ||
|
|
||
| // Disable the network | ||
| await firestore.disableNetwork(); | ||
| // Go back online | ||
| await firestore.enableNetwork(); | ||
|
|
||
| // Get data from cache | ||
| DocumentSnapshot<Map<String, dynamic>> documentSnapshot = | ||
| await documentReference.get(); | ||
| expect(documentSnapshot.metadata.isFromCache, isTrue); | ||
| expect(documentSnapshot.data()!['foo'], equals('bar')); | ||
| await expectLater(controller.stream, emits(true)); | ||
| await controller.close(); | ||
| }, | ||
| skip: kIsWeb, | ||
| ); | ||
|
|
||
| // Go back online once test complete | ||
| await firestore.enableNetwork(); | ||
| }); | ||
| test( | ||
| 'disableNetwork()', | ||
| () async { | ||
| // Write some data while online | ||
| await firestore.enableNetwork(); | ||
| DocumentReference<Map<String, dynamic>> documentReference = | ||
| firestore.doc('flutter-tests/disable-network'); | ||
| await documentReference.set({'foo': 'bar'}); | ||
|
|
||
| // Disable the network | ||
| await firestore.disableNetwork(); | ||
|
|
||
| // Get data from cache | ||
| DocumentSnapshot<Map<String, dynamic>> documentSnapshot = | ||
| await documentReference.get(); | ||
| expect(documentSnapshot.metadata.isFromCache, isTrue); | ||
| expect(documentSnapshot.data()!['foo'], equals('bar')); | ||
|
|
||
| // Go back online once test complete | ||
| await firestore.enableNetwork(); | ||
| }, | ||
| skip: kIsWeb, | ||
| ); | ||
|
|
||
| test( | ||
| 'waitForPendingWrites()', | ||
|
|
@@ -119,24 +127,108 @@ void runInstanceTests() { | |
| skip: kIsWeb, | ||
| ); | ||
|
|
||
| test('terminate() / clearPersistence()', () async { | ||
| // Since the firestore instance has already been used, | ||
| // calling `clearPersistence` will throw a native error. | ||
| // We first check it does throw as expected, then terminate | ||
| // the instance, and then check whether clearing succeeds. | ||
| try { | ||
| test( | ||
| 'terminate() / clearPersistence()', | ||
| () async { | ||
| // Since the firestore instance has already been used, | ||
| // calling `clearPersistence` will throw a native error. | ||
| // We first check it does throw as expected, then terminate | ||
| // the instance, and then check whether clearing succeeds. | ||
| try { | ||
| await firestore.clearPersistence(); | ||
| fail('Should have thrown'); | ||
| } on FirebaseException catch (e) { | ||
| expect(e.code, equals('failed-precondition')); | ||
| } catch (e) { | ||
| fail('$e'); | ||
| } | ||
|
|
||
| await firestore.terminate(); | ||
| await firestore.clearPersistence(); | ||
| fail('Should have thrown'); | ||
| } on FirebaseException catch (e) { | ||
| expect(e.code, equals('failed-precondition')); | ||
| } catch (e) { | ||
| fail('$e'); | ||
| } | ||
|
|
||
| await firestore.terminate(); | ||
| await firestore.clearPersistence(); | ||
| }, | ||
| skip: kIsWeb, | ||
| ); | ||
|
|
||
| test('setIndexConfiguration()', () async { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the integration test for |
||
| Index index1 = Index( | ||
| collectionGroup: 'bar', | ||
| queryScope: QueryScope.collectionGroup, | ||
| fields: [ | ||
| IndexField( | ||
| fieldPath: 'fieldPath', | ||
| order: Order.ascending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ) | ||
| ], | ||
| ); | ||
|
|
||
| Index index2 = Index( | ||
| collectionGroup: 'baz', | ||
| queryScope: QueryScope.collection, | ||
| fields: [ | ||
| IndexField( | ||
| fieldPath: 'foo', | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| IndexField( | ||
| fieldPath: 'bar', | ||
| order: Order.descending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| IndexField( | ||
| fieldPath: 'baz', | ||
| order: Order.descending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| ], | ||
| ); | ||
|
|
||
| FieldOverrides fieldOverride1 = FieldOverrides( | ||
| fieldPath: 'fieldPath', | ||
| indexes: [ | ||
| FieldOverrideIndex( | ||
| queryScope: 'foo', | ||
| order: Order.ascending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| FieldOverrideIndex( | ||
| queryScope: 'bar', | ||
| order: Order.descending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| FieldOverrideIndex( | ||
| queryScope: 'baz', | ||
| order: Order.descending, | ||
| ), | ||
| ], | ||
| collectionGroup: 'bar', | ||
| ); | ||
| FieldOverrides fieldOverride2 = FieldOverrides( | ||
| fieldPath: 'anotherField', | ||
| indexes: [ | ||
| FieldOverrideIndex( | ||
| queryScope: 'foo', | ||
| order: Order.ascending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| FieldOverrideIndex( | ||
| queryScope: 'bar', | ||
| order: Order.descending, | ||
| arrayConfig: ArrayConfig.contains, | ||
| ), | ||
| FieldOverrideIndex( | ||
| queryScope: 'baz', | ||
| order: Order.descending, | ||
| ), | ||
| ], | ||
| collectionGroup: 'collectiongroup', | ||
| ); | ||
|
|
||
| await firestore.setIndexConfiguration( | ||
| indexes: [index1, index2], | ||
| fieldOverrides: [fieldOverride1, fieldOverride2], | ||
| ); | ||
| }); | ||
| }, | ||
| skip: kIsWeb, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this file so that web is skipped for each individual e2e test rather than for the entire file hence why it looks like a lot of changes.