Skip to content

Commit 4a1ec23

Browse files
test(database): fix tests that were failing frequently on web e2e test run (#12857)
1 parent 40966c7 commit 4a1ec23

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

.github/workflows/scripts/database.rules.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
".indexOn": [
2828
"value"
2929
]
30+
},
31+
"children": {
32+
".read": true,
33+
".write": true,
34+
".indexOn": [".value"]
3035
}
3136
}
3237
}

tests/integration_test/firebase_database/data_snapshot_e2e.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import 'package:flutter_test/flutter_test.dart';
88
void setupDataSnapshotTests() {
99
group('DataSnapshot', () {
1010
late DatabaseReference ref;
11+
DatabaseReference getRef(String path) {
12+
return FirebaseDatabase.instance.ref('tests').child(path);
13+
}
1114

1215
setUp(() async {
1316
ref = FirebaseDatabase.instance.ref('tests');
@@ -17,35 +20,41 @@ void setupDataSnapshotTests() {
1720
});
1821

1922
test('it returns the correct key', () async {
23+
final ref = getRef('key');
2024
final s = await ref.get();
21-
expect(s.key, 'tests');
25+
expect(s.key, 'key');
2226
});
2327

2428
test('it returns a string value', () async {
29+
final ref = getRef('string');
2530
await ref.set('foo');
2631
final s = await ref.get();
2732
expect(s.value, 'foo');
2833
});
2934

3035
test('it returns a number value', () async {
36+
final ref = getRef('num');
3137
await ref.set(123);
3238
final s = await ref.get();
3339
expect(s.value, 123);
3440
});
3541

3642
test('it returns a bool value', () async {
43+
final ref = getRef('bool');
3744
await ref.set(false);
3845
final s = await ref.get();
3946
expect(s.value, false);
4047
});
4148

4249
test('it returns a null value', () async {
50+
final ref = getRef('null');
4351
await ref.set(null);
4452
final s = await ref.get();
4553
expect(s.value, isNull);
4654
});
4755

4856
test('it returns a List value', () async {
57+
final ref = getRef('list');
4958
final data = [
5059
'a',
5160
2,
@@ -71,49 +80,58 @@ void setupDataSnapshotTests() {
7180
});
7281

7382
test('it returns a Map value', () async {
83+
final ref = getRef('map');
7484
final data = {'foo': 'bar'};
85+
7586
await ref.set(data);
7687
final s = await ref.get();
7788
expect(s.value, equals(data));
7889
});
7990

8091
test('non-string Map keys are converted to strings', () async {
92+
final ref = getRef('converted');
8193
final data = {1: 'foo', 2: 'bar', 'foo': 'bar'};
8294
await ref.set(data);
8395
final s = await ref.get();
8496
expect(s.value, equals({'1': 'foo', '2': 'bar', 'foo': 'bar'}));
8597
});
8698

8799
test('setWithPriority returns the correct priority', () async {
100+
final ref = getRef('priority');
88101
await ref.setWithPriority('foo', 1);
89102
final s = await ref.get();
90103
expect(s.priority, 1);
91104
});
92105

93106
test('setPriority returns the correct priority', () async {
107+
final ref = getRef('priority-2');
94108
await ref.set('foo');
95109
await ref.setPriority(2);
96110
final s = await ref.get();
97111
expect(s.priority, 2);
98112
});
99113

100114
test('exists returns true', () async {
115+
final ref = getRef('exists');
101116
await ref.set('foo');
102117
final s = await ref.get();
103118
expect(s.exists, isTrue);
104119
});
105120

106121
test('exists returns false', () async {
122+
final ref = getRef('exists-not-here');
107123
final s = await ref.get();
108124
expect(s.exists, isFalse);
109125
});
110126

111127
test('hasChild returns false', () async {
128+
final ref = getRef('has-child');
112129
final s = await ref.get();
113130
expect(s.hasChild('bar'), isFalse);
114131
});
115132

116133
test('hasChild returns true', () async {
134+
final ref = getRef('has-no-child');
117135
await ref.set({
118136
'foo': {'bar': 'baz'},
119137
});
@@ -122,13 +140,15 @@ void setupDataSnapshotTests() {
122140
});
123141

124142
test('child returns the correct snapshot for lists', () async {
143+
final ref = getRef('list-snap');
125144
await ref.set([0, 1]);
126145
final s = await ref.get();
127146
expect(s.child('1'), isA<DataSnapshot>());
128147
expect(s.child('1').value, 1);
129148
});
130149

131150
test('child returns the correct snapshot', () async {
151+
final ref = getRef('child-snap');
132152
await ref.set({
133153
'foo': {'bar': 'baz'},
134154
});
@@ -138,6 +158,7 @@ void setupDataSnapshotTests() {
138158
});
139159

140160
test('children returns the children in order', () async {
161+
final ref = getRef('children');
141162
await ref.set({
142163
'a': 3,
143164
'b': 2,

tests/integration_test/firebase_database/query_e2e.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,22 @@ void setupQueryTests() {
432432
});
433433

434434
group('onChildChanged', () {
435+
// Create own reference as this clashed with previous tests on web platform
436+
late DatabaseReference childRef;
437+
setUp(() async {
438+
childRef = FirebaseDatabase.instance.ref('tests').child('child-ref');
439+
// Wipe the database before each test
440+
await childRef.remove();
441+
});
442+
435443
test(
436444
'emits an event when a child is changed',
437445
() async {
438-
await ref.child('foo').set('foo');
439-
await ref.child('bar').set('bar');
446+
await childRef.child('foo').set('foo');
447+
await childRef.child('bar').set('bar');
440448

441449
expect(
442-
ref.onChildChanged,
450+
childRef.onChildChanged,
443451
emitsInOrder([
444452
isA<DatabaseEvent>()
445453
.having((s) => s.snapshot.key, 'key', 'bar')
@@ -462,8 +470,8 @@ void setupQueryTests() {
462470
// Give time for listen to be registered on native.
463471
// TODO is there a better way to do this?
464472
await Future.delayed(const Duration(seconds: 1));
465-
await ref.child('bar').set('baz');
466-
await ref.child('foo').set('bar');
473+
await childRef.child('bar').set('baz');
474+
await childRef.child('foo').set('bar');
467475
},
468476
retry: 2,
469477
);

0 commit comments

Comments
 (0)