Skip to content

Commit

Permalink
Merge branch 'master' into feature/kotlin-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nbransby authored Jun 25, 2024
2 parents a85efaa + f0c0777 commit 5504c0d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 != 0
is String -> value.toBoolean()
else -> throw SerializationException("Expected $value to be boolean")
}

private fun decodeChar(value: Any?) = when(value) {
is Number -> value.toInt().toChar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,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)
Expand Down Expand Up @@ -198,6 +197,29 @@ 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<Boolean>()
val trueValue = trueRef.valueEvents.first().value<Boolean>()
assertFalse(falseValue)
assertTrue(trueValue)
}

@Test
fun testBooleanValueInChild() = runTest {
ensureDatabaseConnected()
val reference = database.reference("FirebaseRealtimeDatabaseBooleanInChildTest")
reference.setValue(FirebaseDatabaseChildTest())
val value = reference.valueEvents.first().value<FirebaseDatabaseChildTest>()
assertEquals(FirebaseDatabaseChildTest(), value)
}

// Ignoring on Android Instrumented Tests due to bug in Firebase: https://github.com/firebase/firebase-android-sdk/issues/5870
@IgnoreForAndroidTest
@Test
Expand Down

0 comments on commit 5504c0d

Please sign in to comment.