Skip to content

Commit f85e46a

Browse files
authored
Merge pull request #237 from thejoeba/add-database-testing
Issue-236 added unit test for database DataSnapshot children count
2 parents 9604d00 + 6a72c90 commit f85e46a

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

firebase-database/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ version = project.property("firebase-database.version") as String
1010
plugins {
1111
id("com.android.library")
1212
kotlin("multiplatform")
13+
kotlin("plugin.serialization") version "1.5.31"
1314
}
1415

1516
repositories {
@@ -22,11 +23,17 @@ android {
2223
defaultConfig {
2324
minSdk = property("minSdkVersion") as Int
2425
targetSdk = property("targetSdkVersion") as Int
26+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
27+
multiDexEnabled = true
2528
}
2629
sourceSets {
2730
getByName("main") {
2831
manifest.srcFile("src/androidMain/AndroidManifest.xml")
2932
}
33+
getByName("androidTest"){
34+
java.srcDir(file("src/androidAndroidTest/kotlin"))
35+
manifest.srcFile("src/androidAndroidTest/AndroidManifest.xml")
36+
}
3037
}
3138
testOptions {
3239
unitTests.apply {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:JvmName("tests")
6+
package dev.gitlive.firebase.database
7+
8+
import androidx.test.platform.app.InstrumentationRegistry
9+
import kotlinx.coroutines.runBlocking
10+
11+
actual val emulatorHost: String = "10.0.2.2"
12+
13+
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
14+
15+
actual fun runTest(test: suspend () -> Unit) = runBlocking { test() }
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase.database
6+
7+
import dev.gitlive.firebase.*
8+
import kotlinx.coroutines.flow.first
9+
import kotlinx.serialization.*
10+
import kotlin.test.*
11+
12+
expect val emulatorHost: String
13+
expect val context: Any
14+
expect fun runTest(test: suspend () -> Unit)
15+
16+
class database {
17+
18+
@Serializable
19+
data class FirebaseDatabaseChildTest(val prop1: String? = null, val time: Double = 0.0)
20+
21+
@BeforeTest
22+
fun initializeFirebase() {
23+
Firebase
24+
.takeIf { Firebase.apps(context).isEmpty() }
25+
?.apply {
26+
initialize(
27+
context,
28+
FirebaseOptions(
29+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
30+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
31+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
32+
storageBucket = "fir-kotlin-sdk.appspot.com",
33+
projectId = "fir-kotlin-sdk",
34+
gcmSenderId = "846484016111"
35+
)
36+
)
37+
Firebase.database.useEmulator(emulatorHost, 9000)
38+
}
39+
}
40+
41+
@Test
42+
fun testChildCount() = runTest {
43+
setupRealtimeData()
44+
val dataSnapshot = Firebase.database
45+
.reference("FirebaseRealtimeDatabaseTest")
46+
.valueEvents
47+
.first()
48+
49+
val firebaseDatabaseChildCount = dataSnapshot.children.count()
50+
assertEquals(3, firebaseDatabaseChildCount)
51+
}
52+
53+
private suspend fun setupRealtimeData() {
54+
val firebaseDatabaseTestReference = Firebase.database
55+
.reference("FirebaseRealtimeDatabaseTest")
56+
57+
val firebaseDatabaseChildTest1 = FirebaseDatabaseChildTest("aaa")
58+
val firebaseDatabaseChildTest2 = FirebaseDatabaseChildTest("bbb")
59+
val firebaseDatabaseChildTest3 = FirebaseDatabaseChildTest("ccc")
60+
61+
firebaseDatabaseTestReference.child("1").setValue(firebaseDatabaseChildTest1)
62+
firebaseDatabaseTestReference.child("2").setValue(firebaseDatabaseChildTest2)
63+
firebaseDatabaseTestReference.child("3").setValue(firebaseDatabaseChildTest3)
64+
}
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase.database
6+
7+
import kotlinx.coroutines.*
8+
import platform.Foundation.*
9+
10+
actual val emulatorHost: String = "localhost"
11+
12+
actual val context: Any = Unit
13+
14+
actual fun runTest(test: suspend () -> Unit) = runBlocking {
15+
val testRun = MainScope().async { test() }
16+
while (testRun.isActive) {
17+
NSRunLoop.mainRunLoop.runMode(
18+
NSDefaultRunLoopMode,
19+
beforeDate = NSDate.create(timeInterval = 1.0, sinceDate = NSDate())
20+
)
21+
yield()
22+
}
23+
testRun.await()
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase.database
6+
7+
import kotlinx.coroutines.GlobalScope
8+
import kotlinx.coroutines.promise
9+
10+
actual val emulatorHost: String = "localhost"
11+
12+
actual val context: Any = Unit
13+
14+
actual fun runTest(test: suspend () -> Unit) = GlobalScope
15+
.promise {
16+
try {
17+
test()
18+
} catch (e: Throwable) {
19+
e.log()
20+
throw e
21+
}
22+
}.asDynamic()
23+
24+
internal fun Throwable.log() {
25+
console.error(this)
26+
cause?.let {
27+
console.error("Caused by:")
28+
it.log()
29+
}
30+
}
31+

test/database.rules.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
".read": true,
4+
".write": true
5+
}
6+
}

test/firebase.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
"firestore": {
33
"rules": "firestore.rules"
44
},
5+
"database": {
6+
"rules": "database.rules.json"
7+
},
58
"emulators": {
69
"auth": {
710
"port": 9099
811
},
912
"firestore": {
1013
"port": 8080
14+
},
15+
"database": {
16+
"port": 9000
1117
}
1218
}
1319
}

0 commit comments

Comments
 (0)