4
4
5
5
package dev.gitlive.firebase.firestore
6
6
7
- import dev.gitlive.firebase.*
8
- import kotlinx.serialization.*
9
- import kotlin.test.*
7
+ import dev.gitlive.firebase.Firebase
8
+ import dev.gitlive.firebase.FirebaseOptions
9
+ import dev.gitlive.firebase.apps
10
+ import dev.gitlive.firebase.initialize
11
+ import kotlinx.coroutines.CoroutineScope
12
+ import kotlinx.coroutines.async
13
+ import kotlinx.coroutines.delay
14
+ import kotlinx.coroutines.flow.filter
15
+ import kotlinx.coroutines.flow.first
16
+ import kotlinx.coroutines.withTimeout
17
+ import kotlinx.serialization.Serializable
18
+ import kotlin.random.Random
19
+ import kotlin.test.BeforeTest
20
+ import kotlin.test.Test
21
+ import kotlin.test.assertEquals
22
+ import kotlin.test.assertNotEquals
23
+ import kotlin.test.assertNotNull
24
+ import kotlin.test.assertNull
25
+ import kotlin.test.assertTrue
10
26
11
27
expect val emulatorHost: String
12
28
expect val context: Any
13
- expect fun runTest (test : suspend () -> Unit )
29
+ expect fun runTest (test : suspend CoroutineScope . () -> Unit )
14
30
15
31
class FirebaseFirestoreTest {
16
32
@@ -121,7 +137,73 @@ class FirebaseFirestoreTest {
121
137
122
138
assertNotEquals(FieldValue .serverTimestamp, doc.get().get(" time" ))
123
139
assertNotEquals(FieldValue .serverTimestamp, doc.get().data(FirestoreTest .serializer()).time)
140
+ }
141
+
142
+ @Test
143
+ fun testServerTimestampBehaviorNone () = runTest {
144
+ val doc = Firebase .firestore
145
+ .collection(" testServerTimestampBehaviorNone" )
146
+ .document(" test${Random .nextInt()} " )
147
+
148
+ val deferredPendingWritesSnapshot = async {
149
+ withTimeout(5000 ) {
150
+ doc.snapshots.filter { it.exists }.first()
151
+ }
152
+ }
153
+ delay(100 ) // makes possible to catch pending writes snapshot
154
+
155
+ doc.set(
156
+ FirestoreTest .serializer(),
157
+ FirestoreTest (" ServerTimestampBehavior" , FieldValue .serverTimestamp)
158
+ )
159
+
160
+ val pendingWritesSnapshot = deferredPendingWritesSnapshot.await()
161
+ assertTrue(pendingWritesSnapshot.metadata.hasPendingWrites)
162
+ assertNull(pendingWritesSnapshot.get<Double ?>(" time" , ServerTimestampBehavior .NONE ))
163
+ assertNull(pendingWritesSnapshot.dataMap(ServerTimestampBehavior .NONE )[" time" ])
164
+ }
165
+
166
+ @Test
167
+ fun testServerTimestampBehaviorEstimate () = runTest {
168
+ val doc = Firebase .firestore
169
+ .collection(" testServerTimestampBehaviorEstimate" )
170
+ .document(" test${Random .nextInt()} " )
171
+
172
+ val deferredPendingWritesSnapshot = async {
173
+ withTimeout(5000 ) {
174
+ doc.snapshots.filter { it.exists }.first()
175
+ }
176
+ }
177
+ delay(100 ) // makes possible to catch pending writes snapshot
178
+
179
+ doc.set(FirestoreTest .serializer(), FirestoreTest (" ServerTimestampBehavior" , FieldValue .serverTimestamp))
180
+
181
+ val pendingWritesSnapshot = deferredPendingWritesSnapshot.await()
182
+ assertTrue(pendingWritesSnapshot.metadata.hasPendingWrites)
183
+ assertNotNull(pendingWritesSnapshot.get<Double ?>(" time" , ServerTimestampBehavior .ESTIMATE ))
184
+ assertNotNull(pendingWritesSnapshot.dataMap(ServerTimestampBehavior .ESTIMATE )[" time" ])
185
+ assertNotEquals(0.0 , pendingWritesSnapshot.data(FirestoreTest .serializer(), ServerTimestampBehavior .ESTIMATE ).time)
186
+ }
187
+
188
+ @Test
189
+ fun testServerTimestampBehaviorPrevious () = runTest {
190
+ val doc = Firebase .firestore
191
+ .collection(" testServerTimestampBehaviorPrevious" )
192
+ .document(" test${Random .nextInt()} " )
193
+
194
+ val deferredPendingWritesSnapshot = async {
195
+ withTimeout(5000 ) {
196
+ doc.snapshots.filter { it.exists }.first()
197
+ }
198
+ }
199
+ delay(100 ) // makes possible to catch pending writes snapshot
200
+
201
+ doc.set(FirestoreTest .serializer(), FirestoreTest (" ServerTimestampBehavior" , FieldValue .serverTimestamp))
124
202
203
+ val pendingWritesSnapshot = deferredPendingWritesSnapshot.await()
204
+ assertTrue(pendingWritesSnapshot.metadata.hasPendingWrites)
205
+ assertNull(pendingWritesSnapshot.get<Double ?>(" time" , ServerTimestampBehavior .PREVIOUS ))
206
+ assertNull(pendingWritesSnapshot.dataMap(ServerTimestampBehavior .PREVIOUS )[" time" ])
125
207
}
126
208
127
209
@Test
@@ -169,4 +251,4 @@ class FirebaseFirestoreTest {
169
251
.document(" three" )
170
252
.set(FirestoreTest .serializer(), FirestoreTest (" ccc" ))
171
253
}
172
- }
254
+ }
0 commit comments