Skip to content

Commit a791bfe

Browse files
author
sitatec
committed
Test MockDatabaseReference
1 parent f317aa5 commit a791bfe

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import 'package:firebase_database_mocks/src/mock_database_reference.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
MockDatabaseReference databaseReference;
6+
setUp(() {
7+
databaseReference = MockDatabaseReference();
8+
});
9+
10+
group('Node path handling : ', () {
11+
test('Should work with slash as prefix', () {
12+
expect(
13+
databaseReference.child('/test').path,
14+
equals(MockDatabaseReference().child('test').path),
15+
);
16+
});
17+
test('Should work with slash as suffix', () {
18+
expect(
19+
databaseReference.child('test/').path,
20+
equals(MockDatabaseReference().child('test').path),
21+
);
22+
});
23+
test('Should work with slash as suffix', () {
24+
expect(
25+
databaseReference.child('/test/').path,
26+
equals(MockDatabaseReference().child('test').path),
27+
);
28+
});
29+
test('Should work with nested nodes', () {
30+
expect(
31+
databaseReference.child('test').child('other').path,
32+
equals(MockDatabaseReference().child('test/other').path),
33+
);
34+
expect(
35+
databaseReference
36+
.child('path')
37+
.child('mock')
38+
.child('test')
39+
.child('other')
40+
.path,
41+
equals(MockDatabaseReference().child('path/mock/test/other').path),
42+
);
43+
});
44+
test('Should return null when a nonexistent path is given', () async {
45+
expect(
46+
(await databaseReference.child('fghgg').once()).value,
47+
isNull,
48+
);
49+
expect(
50+
(await databaseReference.child('o_/therèè_/Test').once()).value,
51+
isNull,
52+
);
53+
});
54+
55+
group('Should return null when a nonexistent path that', () {
56+
test('starts with existent node path is given', () async {
57+
await databaseReference.child('existing_path').set('value');
58+
expect(
59+
(await databaseReference.child('existing_path/therèè_/Test').once())
60+
.value,
61+
isNull,
62+
);
63+
});
64+
test('wrap existent node path is given', () async {
65+
await databaseReference.child('existing_path').set('value');
66+
expect(
67+
(await databaseReference.child('any/existing_path/thè_/Tt').once())
68+
.value,
69+
isNull,
70+
);
71+
await databaseReference.child('tteesstt').set({'key': 'value'});
72+
expect(
73+
(await databaseReference.child('tttttst/path/tteesstt/Test').once())
74+
.value,
75+
isNull,
76+
reason: 'With Map as value',
77+
);
78+
});
79+
test('end with existent node path is given', () async {
80+
await databaseReference.child('end').set('value');
81+
expect(
82+
(await databaseReference.child('any/existing_path/Test/end').once())
83+
.value,
84+
isNull,
85+
);
86+
});
87+
88+
// Todo put any expect satement inside test function.
89+
});
90+
91+
// test('Should work when wrapped in two slash or more', () {
92+
// expect(
93+
// databaseReference.child('//test//').path,
94+
// equals(MockDatabaseReference().child('test').path),
95+
// );
96+
// expect(
97+
// databaseReference.child('//test///').path,
98+
// equals(MockDatabaseReference().child('test').path),
99+
// );
100+
// });
101+
});
102+
group('Set any type of data : ', () {
103+
test('Should set String', () async {
104+
await databaseReference.child('test').set('value');
105+
expect(
106+
(await databaseReference.child('test').once()).value,
107+
equals('value'),
108+
);
109+
await databaseReference.child('otherTest').set('otherValue');
110+
expect(
111+
(await databaseReference.child('otherTest').once()).value,
112+
equals('otherValue'),
113+
);
114+
});
115+
// await databaseReference.child('test').set({'key': 'value'});
116+
// expect((await databaseReference.child('test').once()).value,
117+
// equals({'key': 'value'}));
118+
});
119+
120+
// group('Set data at any node reference :', null);
121+
122+
group('Data persistence : ', () {
123+
test('Should persist data while test running', () async {
124+
var databaseReference = MockDatabaseReference();
125+
await databaseReference.child('test1').set('value1');
126+
await databaseReference.child('test2/test2').set('value2');
127+
await databaseReference.child('test1/test_one').set('value3');
128+
databaseReference = null;
129+
expect(databaseReference, isNull);
130+
131+
var newDatabaseReference = MockDatabaseReference();
132+
expect(
133+
(await newDatabaseReference.child('test1').once()).value,
134+
equals('value1'),
135+
);
136+
expect(
137+
(await newDatabaseReference.child('test2/test2').once()).value,
138+
equals('value2'),
139+
);
140+
expect(
141+
(await newDatabaseReference.child('test1/test_one').once()).value,
142+
equals('value3'),
143+
);
144+
});
145+
});
146+
// Todo implement all dataSnapshot, dbReference and fbDatabase getters and setters if possible.
147+
// test(
148+
// 'Should not persist data if setting "persistData" of MockFirebaseData is false',
149+
// () async {
150+
// MockFirebaseDatabase.settings(persistData: false);
151+
// var databaseReference = MockDatabaseReference();
152+
// var test = databaseReference.child('test1');
153+
// await test.set('value1');
154+
// await databaseReference.child('test2/test2').set('value2');
155+
// await databaseReference.child('test1/test_one').set('value3');
156+
// expect(
157+
// (await test.once()).value,
158+
// equals('value1'),
159+
// );
160+
// databaseReference = null;
161+
// expect(databaseReference, isNull);
162+
163+
// var newDatabaseReference = MockDatabaseReference();
164+
// expect(
165+
// (await newDatabaseReference.child('test1').once()).value,
166+
// isNull,
167+
// );
168+
// expect(
169+
// (await newDatabaseReference.child('test2/test2').once()).value,
170+
// isNull,
171+
// );
172+
// expect(
173+
// (await newDatabaseReference.child('test1/test_one').once()).value,
174+
// isNull,
175+
// );
176+
// });
177+
//Todo test nonexistent data should'nt return any data.
178+
}

0 commit comments

Comments
 (0)