1+ package dev.gitlive.firebase.firestore
2+
3+ import dev.gitlive.firebase.*
4+ import kotlin.test.*
5+
6+ /* *
7+ * These tests are separated from other tests because
8+ * testing Firestore Source requires toggling persistence settings per test.
9+ */
10+ class FirestoreSourceTest {
11+ lateinit var firestore: FirebaseFirestore
12+
13+ companion object {
14+ val testDoc = FirebaseFirestoreTest .FirestoreTest (
15+ " aaa" ,
16+ 0.0 ,
17+ 1 ,
18+ listOf (" a" , " aa" , " aaa" ),
19+ " notNull" ,
20+ )
21+ }
22+
23+ private suspend fun setDoc () {
24+ firestore.collection(" testFirestoreQuerying" ).document(" one" ).set(testDoc)
25+ }
26+
27+ private fun initializeFirebase (persistenceEnabled : Boolean = false) {
28+ val app = Firebase .apps(context).firstOrNull() ? : Firebase .initialize(
29+ context,
30+ FirebaseOptions (
31+ applicationId = " 1:846484016111:ios:dd1f6688bad7af768c841a" ,
32+ apiKey = " AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0" ,
33+ databaseUrl = " https://fir-kotlin-sdk.firebaseio.com" ,
34+ storageBucket = " fir-kotlin-sdk.appspot.com" ,
35+ projectId = " fir-kotlin-sdk" ,
36+ gcmSenderId = " 846484016111"
37+ )
38+ )
39+
40+ firestore = Firebase .firestore(app).apply {
41+ useEmulator(emulatorHost, 8080 )
42+ setSettings(persistenceEnabled = persistenceEnabled)
43+ }
44+ }
45+
46+ @AfterTest
47+ fun deinitializeFirebase () = runBlockingTest {
48+ Firebase .apps(context).forEach {
49+ it.delete()
50+ }
51+ }
52+
53+ @Test
54+ fun testGetFromServer_withPersistence () = runTest {
55+ initializeFirebase(persistenceEnabled = true )
56+ setDoc()
57+ val doc = firestore.collection(" testFirestoreQuerying" ).document(" one" ).get(Source .SERVER )
58+ assertTrue(doc.exists)
59+ assertFalse(doc.native.metadata.isFromCache)
60+ }
61+
62+ @Test
63+ fun testGetFromServer_withoutPersistence () = runTest {
64+ initializeFirebase(persistenceEnabled = false )
65+ setDoc()
66+ val doc = firestore.collection(" testFirestoreQuerying" ).document(" one" ).get(Source .SERVER )
67+ assertTrue(doc.exists)
68+ assertFalse(doc.native.metadata.isFromCache)
69+ }
70+
71+ @Test
72+ fun testGetFromCache () = runTest {
73+ initializeFirebase(persistenceEnabled = true )
74+
75+ // Warm up cache by setting a document
76+ setDoc()
77+
78+ val cachedDoc = firestore.collection(" testFirestoreQuerying" ).document(" one" ).get(Source .CACHE )
79+ assertTrue(cachedDoc.exists)
80+ assertTrue(cachedDoc.native.metadata.isFromCache)
81+ }
82+
83+ @Test
84+ fun testGetFromCache_withoutPersistence () = runTest {
85+ initializeFirebase(persistenceEnabled = false )
86+ setDoc()
87+ assertFailsWith(FirebaseFirestoreException ::class ) {
88+ firestore.collection(" testFirestoreQuerying" ).document(" one" ).get(Source .CACHE )
89+ }
90+ }
91+
92+ @Test
93+ fun testGetDefault_withPersistence () = runTest {
94+ initializeFirebase(persistenceEnabled = false )
95+ val doc = firestore.collection(" testFirestoreQuerying" ).document(" one" ).get(Source .DEFAULT )
96+ assertTrue(doc.exists)
97+ assertFalse(doc.native.metadata.isFromCache)
98+ }
99+ @Test
100+ fun testGet () = runTest {
101+ initializeFirebase(persistenceEnabled = false )
102+ val doc = firestore.collection(" testFirestoreQuerying" ).document(" one" ).get()
103+ assertTrue(doc.exists)
104+ assertFalse(doc.native.metadata.isFromCache)
105+ }
106+
107+ @Test
108+ fun testGetDefault_withoutPersistence () = runTest {
109+ initializeFirebase(persistenceEnabled = true )
110+ setDoc()
111+ val doc = firestore.collection(" testFirestoreQuerying" ).document(" one" ).get(Source .DEFAULT )
112+ assertTrue(doc.exists)
113+ // Firebase defaults to first fetching from server
114+ assertFalse(doc.native.metadata.isFromCache)
115+ }
116+
117+ }
0 commit comments