From b559b999b9a787840074f357a184e7da8a76244d Mon Sep 17 00:00:00 2001 From: Joey Heck Date: Mon, 18 Oct 2021 11:26:43 -0700 Subject: [PATCH 1/2] setup unit testing for javascript in database --- firebase-database/build.gradle.kts | 7 ++++ .../dev/gitlive/firebase/database/database.kt | 15 ++++++++ .../dev.gitlive.firebase.database/database.kt | 37 +++++++++++++++++++ .../dev/gitlive/firebase/database/database.kt | 24 ++++++++++++ .../dev.gitlive.firebase.database/database.kt | 31 ++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 firebase-database/src/androidAndroidTest/kotlin/dev/gitlive/firebase/database/database.kt create mode 100644 firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt create mode 100644 firebase-database/src/iosTest/kotlin/dev/gitlive/firebase/database/database.kt create mode 100644 firebase-database/src/jsTest/kotlin/dev.gitlive.firebase.database/database.kt diff --git a/firebase-database/build.gradle.kts b/firebase-database/build.gradle.kts index 8b5c08b7d..fbceae27b 100644 --- a/firebase-database/build.gradle.kts +++ b/firebase-database/build.gradle.kts @@ -10,6 +10,7 @@ version = project.property("firebase-database.version") as String plugins { id("com.android.library") kotlin("multiplatform") + kotlin("plugin.serialization") version "1.5.31" } repositories { @@ -22,11 +23,17 @@ android { defaultConfig { minSdk = property("minSdkVersion") as Int targetSdk = property("targetSdkVersion") as Int + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + multiDexEnabled = true } sourceSets { getByName("main") { manifest.srcFile("src/androidMain/AndroidManifest.xml") } + getByName("androidTest"){ + java.srcDir(file("src/androidAndroidTest/kotlin")) + manifest.srcFile("src/androidAndroidTest/AndroidManifest.xml") + } } testOptions { unitTests.apply { diff --git a/firebase-database/src/androidAndroidTest/kotlin/dev/gitlive/firebase/database/database.kt b/firebase-database/src/androidAndroidTest/kotlin/dev/gitlive/firebase/database/database.kt new file mode 100644 index 000000000..ebe4d120b --- /dev/null +++ b/firebase-database/src/androidAndroidTest/kotlin/dev/gitlive/firebase/database/database.kt @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:JvmName("tests") +package dev.gitlive.firebase.database + +import androidx.test.platform.app.InstrumentationRegistry +import kotlinx.coroutines.runBlocking + +actual val emulatorHost: String = "10.0.2.2" + +actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext + +actual fun runTest(test: suspend () -> Unit) = runBlocking { test() } diff --git a/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt b/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt new file mode 100644 index 000000000..79caf2da4 --- /dev/null +++ b/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.gitlive.firebase.database + +import dev.gitlive.firebase.* +import kotlinx.coroutines.flow.first +import kotlinx.serialization.* +import kotlin.test.* + +expect val emulatorHost: String +expect val context: Any +expect fun runTest(test: suspend () -> Unit) + +class database { + + @BeforeTest + fun initializeFirebase() { + Firebase + .takeIf { Firebase.apps(context).isEmpty() } + ?.apply { + 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" + ) + ) + Firebase.database.useEmulator(emulatorHost, 8080) + } + } +} \ No newline at end of file diff --git a/firebase-database/src/iosTest/kotlin/dev/gitlive/firebase/database/database.kt b/firebase-database/src/iosTest/kotlin/dev/gitlive/firebase/database/database.kt new file mode 100644 index 000000000..b1c213873 --- /dev/null +++ b/firebase-database/src/iosTest/kotlin/dev/gitlive/firebase/database/database.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.gitlive.firebase.database + +import kotlinx.coroutines.* +import platform.Foundation.* + +actual val emulatorHost: String = "localhost" + +actual val context: Any = Unit + +actual fun runTest(test: suspend () -> Unit) = runBlocking { + val testRun = MainScope().async { test() } + while (testRun.isActive) { + NSRunLoop.mainRunLoop.runMode( + NSDefaultRunLoopMode, + beforeDate = NSDate.create(timeInterval = 1.0, sinceDate = NSDate()) + ) + yield() + } + testRun.await() +} diff --git a/firebase-database/src/jsTest/kotlin/dev.gitlive.firebase.database/database.kt b/firebase-database/src/jsTest/kotlin/dev.gitlive.firebase.database/database.kt new file mode 100644 index 000000000..e0854b0f2 --- /dev/null +++ b/firebase-database/src/jsTest/kotlin/dev.gitlive.firebase.database/database.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.gitlive.firebase.database + +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.promise + +actual val emulatorHost: String = "localhost" + +actual val context: Any = Unit + +actual fun runTest(test: suspend () -> Unit) = GlobalScope + .promise { + try { + test() + } catch (e: Throwable) { + e.log() + throw e + } + }.asDynamic() + +internal fun Throwable.log() { + console.error(this) + cause?.let { + console.error("Caused by:") + it.log() + } +} + From 165f6c93147e0d71fe4c0dc89845445cfdc5eaa0 Mon Sep 17 00:00:00 2001 From: Joey Heck Date: Mon, 18 Oct 2021 11:38:45 -0700 Subject: [PATCH 2/2] Issue-241 added test to validate setValue works --- .../dev.gitlive.firebase.database/database.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt b/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt index 79caf2da4..5b437036c 100644 --- a/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt +++ b/firebase-database/src/commonTest/kotlin/dev.gitlive.firebase.database/database.kt @@ -34,4 +34,19 @@ class database { Firebase.database.useEmulator(emulatorHost, 8080) } } + + @Test + fun testSetValue() = runTest { + val testValue = "test" + val testReference = Firebase.database.reference("testPath") + + testReference.setValue(testValue) + + val testReferenceValue = testReference + .valueEvents + .first() + .value() + + assertEquals(testValue, testReferenceValue) + } } \ No newline at end of file