From 7b353d8754eb362bd0786857a7fe0331ed338836 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Mon, 24 Jun 2024 22:27:24 +0300 Subject: [PATCH 1/4] Add test --- .../dev/gitlive/firebase/database/database.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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 4c1c9a754..5b8d0ea2f 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 @@ -8,7 +8,6 @@ import dev.gitlive.firebase.runBlockingTest import dev.gitlive.firebase.runTest import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.map import kotlinx.coroutines.withContext import kotlinx.coroutines.withTimeout import kotlinx.serialization.Serializable @@ -198,6 +197,20 @@ class FirebaseDatabaseTest { assertFalse(valueEvents.first().exists) } + @Test + fun testBooleanValue() = runTest { + ensureDatabaseConnected() + val reference = database.reference("FirebaseRealtimeDatabaseBooleanTest") + val falseRef = reference.child("false") + val trueRef = reference.child("true") + falseRef.setValue(false) + trueRef.setValue(true) + val falseValue = falseRef.valueEvents.first().value() + val trueValue = trueRef.valueEvents.first().value() + assertFalse(falseValue) + assertTrue(trueValue) + } + // Ignoring on Android Instrumented Tests due to bug in Firebase: https://github.com/firebase/firebase-android-sdk/issues/5870 @IgnoreForAndroidTest @Test From 2da8cac57071959289065907bc9253bf41f65ee3 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Mon, 24 Jun 2024 22:28:04 +0300 Subject: [PATCH 2/4] Re-work boolean decoder --- .../kotlin/dev/gitlive/firebase/internal/decoders.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt b/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt index bdac459bd..52fd3fa26 100644 --- a/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt +++ b/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt @@ -217,7 +217,12 @@ private fun decodeShort(value: Any?) = when(value) { else -> throw SerializationException("Expected $value to be short") } -private fun decodeBoolean(value: Any?) = value as Boolean +private fun decodeBoolean(value: Any?) = when (value) { + is Boolean -> value + is Number -> value.toInt() != 0 + is String -> value.toBoolean() + else -> throw SerializationException("Expected $value to be boolean") +} private fun decodeChar(value: Any?) = when(value) { is Number -> value.toChar() From 053c6fc83469a07a04749b30720e5521ec39d505 Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Mon, 24 Jun 2024 22:41:19 +0300 Subject: [PATCH 3/4] Add one more test --- .../kotlin/dev/gitlive/firebase/database/database.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 5b8d0ea2f..e8e559827 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 @@ -32,7 +32,7 @@ class FirebaseDatabaseTest { lateinit var database: FirebaseDatabase @Serializable - data class FirebaseDatabaseChildTest(val prop1: String? = null, val time: Double = 0.0) + data class FirebaseDatabaseChildTest(val prop1: String? = null, val time: Double = 0.0, val boolean: Boolean = true) @Serializable data class DatabaseTest(val title: String, val likes: Int = 0) @@ -211,6 +211,15 @@ class FirebaseDatabaseTest { assertTrue(trueValue) } + @Test + fun testBooleanValueInChild() = runTest { + ensureDatabaseConnected() + val reference = database.reference("FirebaseRealtimeDatabaseBooleanInChildTest") + reference.setValue(FirebaseDatabaseChildTest()) + val value = reference.valueEvents.first().value() + assertEquals(FirebaseDatabaseChildTest(), value) + } + // Ignoring on Android Instrumented Tests due to bug in Firebase: https://github.com/firebase/firebase-android-sdk/issues/5870 @IgnoreForAndroidTest @Test From 1ab5cb55dcee9a002cc70c8ff94404d1ce09fb6e Mon Sep 17 00:00:00 2001 From: Alex Shepeliev Date: Mon, 24 Jun 2024 22:57:24 +0300 Subject: [PATCH 4/4] Remove redundant type conversion --- .../commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt b/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt index 52fd3fa26..ca9d0ed31 100644 --- a/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt +++ b/firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/decoders.kt @@ -219,7 +219,7 @@ private fun decodeShort(value: Any?) = when(value) { private fun decodeBoolean(value: Any?) = when (value) { is Boolean -> value - is Number -> value.toInt() != 0 + is Number -> value != 0 is String -> value.toBoolean() else -> throw SerializationException("Expected $value to be boolean") }