Skip to content

Commit 13b510c

Browse files
committed
fix firebase-app tests
fix minor compiler warnings rename EncodersTests to work on android
1 parent 642f7d5 commit 13b510c

File tree

11 files changed

+104
-32
lines changed
  • firebase-app/src
  • firebase-common/src/androidTest/kotlin/dev/gitlive/firebase
  • firebase-database/src
    • androidMain/kotlin/dev/gitlive/firebase/database
    • jsMain/kotlin/dev/gitlive/firebase/database
  • firebase-firestore/src

11 files changed

+104
-32
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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
7+
8+
import androidx.test.platform.app.InstrumentationRegistry
9+
import kotlinx.coroutines.runBlocking
10+
11+
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
12+
13+
actual fun runTest(test: suspend () -> Unit) = runBlocking { test() }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.gitlive.firebase
2+
3+
import dev.gitlive.firebase.Firebase
4+
import dev.gitlive.firebase.initialize
5+
import kotlin.test.Test
6+
7+
expect val context: Any
8+
expect fun runTest(test: suspend () -> Unit)
9+
10+
class FirebaseAppTest {
11+
@Test
12+
fun testInitialize() {
13+
Firebase.initialize(
14+
context,
15+
FirebaseOptions(
16+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
17+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
18+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
19+
storageBucket = "fir-kotlin-sdk.appspot.com",
20+
projectId = "fir-kotlin-sdk"
21+
)
22+
)
23+
}
24+
25+
}

firebase-app/src/commonTest/kotlin/dev/teamhub/firebase/firebase.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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
6+
7+
import kotlinx.coroutines.*
8+
import platform.Foundation.*
9+
10+
actual val context: Any = Unit
11+
12+
actual fun runTest(test: suspend () -> Unit) = runBlocking {
13+
val testRun = MainScope().async { test() }
14+
while (testRun.isActive) {
15+
NSRunLoop.mainRunLoop.runMode(
16+
NSDefaultRunLoopMode,
17+
beforeDate = NSDate.create(timeInterval = 1.0, sinceDate = NSDate())
18+
)
19+
yield()
20+
}
21+
testRun.await()
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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
6+
7+
import kotlinx.coroutines.GlobalScope
8+
import kotlinx.coroutines.promise
9+
10+
actual val context: Any = Unit
11+
12+
actual fun runTest(test: suspend () -> Unit) = GlobalScope.promise { test() }.unsafeCast<Unit>()

firebase-common/src/androidTest/kotlin/dev/gitlive/firebase/EncodersTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ data class TestData(val map: Map<String, String>, val bool: Boolean = false)
1515

1616
class EncodersTest {
1717
@Test
18-
fun `encode a map`() {
19-
val encoded = encode(mapOf("key" to "value"))
18+
fun encodeMap() {
19+
val encoded = encode(mapOf("key" to "value"), shouldEncodeElementDefault = true)
2020
assertEquals(mapOf("key" to "value"), encoded)
2121
}
2222

2323
@Test
24-
fun `encode a class`() {
25-
val encoded = encode<TestData>(TestData::class.serializer(), TestData(mapOf("key" to "value"), true))
24+
fun encodeObject() {
25+
val encoded = encode<TestData>(TestData::class.serializer(), TestData(mapOf("key" to "value"), true), shouldEncodeElementDefault = true)
2626
assertEquals(mapOf("map" to mapOf("key" to "value"), "bool" to true), encoded)
2727
}
2828

2929
@Test
30-
fun `decode a class`() {
30+
fun decodeObject() {
3131
val decoded = decode<TestData>(TestData::class.serializer(), mapOf("map" to mapOf("key" to "value")))
3232
assertEquals(TestData(mapOf("key" to "value"), false), decoded)
3333
}
3434

3535
@Test
36-
fun `decode a list of class`() {
36+
fun decodeListOfObjects() {
3737
val decoded = decode(ListSerializer(TestData::class.serializer()), listOf(mapOf("map" to mapOf("key" to "value"))))
3838
assertEquals(listOf(TestData(mapOf("key" to "value"), false)), decoded)
3939
}

firebase-database/src/androidMain/kotlin/dev/gitlive/firebase/database/database.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import dev.gitlive.firebase.Firebase
1313
import dev.gitlive.firebase.FirebaseApp
1414
import dev.gitlive.firebase.database.ChildEvent.Type
1515
import dev.gitlive.firebase.decode
16+
import kotlinx.coroutines.FlowPreview
1617
import kotlinx.coroutines.channels.awaitClose
1718
import kotlinx.coroutines.coroutineScope
19+
import kotlinx.coroutines.flow.Flow
1820
import kotlinx.coroutines.flow.callbackFlow
21+
import kotlinx.coroutines.flow.filter
22+
import kotlinx.coroutines.flow.produceIn
1923
import kotlinx.coroutines.selects.select
2024
import kotlinx.coroutines.tasks.asDeferred
2125
import kotlinx.coroutines.tasks.await
@@ -28,6 +32,7 @@ fun encode(value: Any?, shouldEncodeElementDefault: Boolean) =
2832
fun <T> encode(strategy: SerializationStrategy<T> , value: T, shouldEncodeElementDefault: Boolean): Any? =
2933
dev.gitlive.firebase.encode(strategy, value, shouldEncodeElementDefault, ServerValue.TIMESTAMP)
3034

35+
@OptIn(FlowPreview::class)
3136
suspend fun <T> Task<T>.awaitWhileOnline(): T = coroutineScope {
3237

3338
val notConnected = Firebase.database
@@ -82,7 +87,8 @@ actual open class Query internal constructor(
8287

8388
actual fun startAt(value: Boolean, key: String?) = Query(android.startAt(value, key), persistenceEnabled)
8489

85-
actual val valueEvents get() = callbackFlow {
90+
actual val valueEvents: Flow<DataSnapshot>
91+
get() = callbackFlow {
8692
val listener = object : ValueEventListener {
8793
override fun onDataChange(snapshot: com.google.firebase.database.DataSnapshot) {
8894
offer(DataSnapshot(snapshot))
@@ -96,7 +102,7 @@ actual open class Query internal constructor(
96102
awaitClose { android.removeEventListener(listener) }
97103
}
98104

99-
actual fun childEvents(vararg types: Type) = callbackFlow {
105+
actual fun childEvents(vararg types: Type): Flow<ChildEvent> = callbackFlow {
100106
val listener = object : ChildEventListener {
101107

102108
val moved by lazy { types.contains(Type.MOVED) }

firebase-database/src/jsMain/kotlin/dev/gitlive/firebase/database/database.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ actual class DataSnapshot internal constructor(val js: firebase.database.DataSna
110110
actual inline fun <reified T> value() =
111111
rethrow { decode<T>(value = js.`val`()) }
112112

113-
actual inline fun <T> value(strategy: DeserializationStrategy<T>) =
113+
actual fun <T> value(strategy: DeserializationStrategy<T>) =
114114
rethrow { decode(strategy, js.`val`()) }
115115

116116
actual val exists get() = rethrow { js.exists() }

firebase-firestore/src/androidMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,11 @@ actual class DocumentReference(val android: com.google.firebase.firestore.Docume
218218
android.set(encode(strategy, data, encodeDefaults)!!, SetOptions.mergeFieldPaths(mergeFieldPaths.toList()))
219219
.await().run { Unit }
220220

221+
@Suppress("UNCHECKED_CAST")
221222
actual suspend fun update(data: Any, encodeDefaults: Boolean) =
222223
android.update(encode(data, encodeDefaults) as Map<String, Any>).await().run { Unit }
223224

225+
@Suppress("UNCHECKED_CAST")
224226
actual suspend fun <T> update(strategy: SerializationStrategy<T>, data: T, encodeDefaults: Boolean) =
225227
android.update(encode(strategy, data, encodeDefaults) as Map<String, Any>).await().run { Unit }
226228

@@ -281,17 +283,17 @@ actual open class Query(open val android: com.google.firebase.firestore.Query) {
281283
}
282284

283285
internal actual fun _where(field: String, lessThan: Any?, greaterThan: Any?, arrayContains: Any?) = Query(
284-
(lessThan?.let { android.whereLessThan(field, it) } ?: android).let { android ->
285-
(greaterThan?.let { android.whereGreaterThan(field, it) } ?: android).let { android ->
286-
arrayContains?.let { android.whereArrayContains(field, it) } ?: android
286+
(lessThan?.let { android.whereLessThan(field, it) } ?: android).let { android2 ->
287+
(greaterThan?.let { android2.whereGreaterThan(field, it) } ?: android2).let { android3 ->
288+
arrayContains?.let { android3.whereArrayContains(field, it) } ?: android3
287289
}
288290
}
289291
)
290292

291293
internal actual fun _where(path: FieldPath, lessThan: Any?, greaterThan: Any?, arrayContains: Any?) = Query(
292-
(lessThan?.let { android.whereLessThan(path, it) } ?: android).let { android ->
293-
(greaterThan?.let { android.whereGreaterThan(path, it) } ?: android).let { android ->
294-
arrayContains?.let { android.whereArrayContains(path, it) } ?: android
294+
(lessThan?.let { android.whereLessThan(path, it) } ?: android).let { android2 ->
295+
(greaterThan?.let { android2.whereGreaterThan(path, it) } ?: android2).let { android3 ->
296+
arrayContains?.let { android3.whereArrayContains(path, it) } ?: android3
295297
}
296298
}
297299
)

firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ expect class CollectionReference : Query {
134134

135135
expect class FirebaseFirestoreException : FirebaseException
136136

137+
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
137138
expect val FirebaseFirestoreException.code: FirestoreExceptionCode
138139

139140
expect enum class FirestoreExceptionCode {

0 commit comments

Comments
 (0)