-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #491 from Qw4z1/set-source
Adds an optional Source argument to DocumentReference and Query
- Loading branch information
Showing
6 changed files
with
174 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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
117 changes: 117 additions & 0 deletions
117
...ase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/FirestoreSourceTest.kt
This file contains 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package dev.gitlive.firebase.firestore | ||
|
||
import dev.gitlive.firebase.* | ||
import kotlin.test.* | ||
|
||
/** | ||
* These tests are separated from other tests because | ||
* testing Firestore Source requires toggling persistence settings per test. | ||
*/ | ||
class FirestoreSourceTest { | ||
lateinit var firestore: FirebaseFirestore | ||
|
||
companion object { | ||
val testDoc = FirebaseFirestoreTest.FirestoreTest( | ||
"aaa", | ||
0.0, | ||
1, | ||
listOf("a", "aa", "aaa"), | ||
"notNull", | ||
) | ||
} | ||
|
||
private suspend fun setDoc() { | ||
firestore.collection("testFirestoreQuerying").document("one").set(testDoc) | ||
} | ||
|
||
private fun initializeFirebase(persistenceEnabled: Boolean = false) { | ||
val app = Firebase.apps(context).firstOrNull() ?: Firebase.initialize( | ||
context, | ||
FirebaseOptions( | ||
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a", | ||
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0", | ||
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com", | ||
storageBucket = "fir-kotlin-sdk.appspot.com", | ||
projectId = "fir-kotlin-sdk", | ||
gcmSenderId = "846484016111" | ||
) | ||
) | ||
|
||
firestore = Firebase.firestore(app).apply { | ||
useEmulator(emulatorHost, 8080) | ||
setSettings(persistenceEnabled = persistenceEnabled) | ||
} | ||
} | ||
|
||
@AfterTest | ||
fun deinitializeFirebase() = runBlockingTest { | ||
Firebase.apps(context).forEach { | ||
it.delete() | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetFromServer_withPersistence() = runTest { | ||
initializeFirebase(persistenceEnabled = true) | ||
setDoc() | ||
val doc = firestore.collection("testFirestoreQuerying").document("one").get(Source.SERVER) | ||
assertTrue(doc.exists) | ||
assertFalse(doc.native.metadata.isFromCache) | ||
} | ||
|
||
@Test | ||
fun testGetFromServer_withoutPersistence() = runTest { | ||
initializeFirebase(persistenceEnabled = false) | ||
setDoc() | ||
val doc = firestore.collection("testFirestoreQuerying").document("one").get(Source.SERVER) | ||
assertTrue(doc.exists) | ||
assertFalse(doc.native.metadata.isFromCache) | ||
} | ||
|
||
@Test | ||
fun testGetFromCache() = runTest { | ||
initializeFirebase(persistenceEnabled = true) | ||
|
||
// Warm up cache by setting a document | ||
setDoc() | ||
|
||
val cachedDoc = firestore.collection("testFirestoreQuerying").document("one").get(Source.CACHE) | ||
assertTrue(cachedDoc.exists) | ||
assertTrue(cachedDoc.native.metadata.isFromCache) | ||
} | ||
|
||
@Test | ||
fun testGetFromCache_withoutPersistence() = runTest { | ||
initializeFirebase(persistenceEnabled = false) | ||
setDoc() | ||
assertFailsWith(FirebaseFirestoreException::class) { | ||
firestore.collection("testFirestoreQuerying").document("one").get(Source.CACHE) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetDefault_withPersistence() = runTest { | ||
initializeFirebase(persistenceEnabled = false) | ||
val doc = firestore.collection("testFirestoreQuerying").document("one").get(Source.DEFAULT) | ||
assertTrue(doc.exists) | ||
assertFalse(doc.native.metadata.isFromCache) | ||
} | ||
@Test | ||
fun testGet() = runTest { | ||
initializeFirebase(persistenceEnabled = false) | ||
val doc = firestore.collection("testFirestoreQuerying").document("one").get() | ||
assertTrue(doc.exists) | ||
assertFalse(doc.native.metadata.isFromCache) | ||
} | ||
|
||
@Test | ||
fun testGetDefault_withoutPersistence() = runTest { | ||
initializeFirebase(persistenceEnabled = true) | ||
setDoc() | ||
val doc = firestore.collection("testFirestoreQuerying").document("one").get(Source.DEFAULT) | ||
assertTrue(doc.exists) | ||
// Firebase defaults to first fetching from server | ||
assertFalse(doc.native.metadata.isFromCache) | ||
} | ||
|
||
} |
This file contains 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 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 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