Skip to content

Commit

Permalink
Stabilize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Nov 14, 2023
1 parent ccb5561 commit ed98066
Show file tree
Hide file tree
Showing 62 changed files with 470 additions and 631 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ import androidx.test.platform.app.InstrumentationRegistry

actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ import org.junit.Ignore

actual val context: Any = ""

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
actual typealias IgnoreForAndroidUnitTest = Ignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.gitlive.firebase

import kotlin.test.Test
import kotlin.test.assertEquals

expect val context: Any
expect fun runTest(test: suspend () -> Unit)
expect annotation class IgnoreForAndroidUnitTest()

class FirebaseAppTest {

@IgnoreForAndroidUnitTest
@Test
fun testInitialize() {
fun testInitialize() = runTest {
Firebase.initialize(
context,
FirebaseOptions(
Expand All @@ -22,6 +22,12 @@ class FirebaseAppTest {
gcmSenderId = "846484016111"
)
)

assertEquals(1, Firebase.apps(context).size)

Firebase.apps(context).forEach {
it.delete()
}
}

}
21 changes: 0 additions & 21 deletions firebase-app/src/iosTest/kotlin/dev/gitlive/firebase/firebase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,7 @@

package dev.gitlive.firebase

import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.yield
import platform.Foundation.NSDate
import platform.Foundation.NSDefaultRunLoopMode
import platform.Foundation.NSRunLoop
import platform.Foundation.create
import platform.Foundation.runMode

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()
}
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@

package dev.gitlive.firebase

import kotlinx.coroutines.test.runTest
import kotlin.time.Duration.Companion.minutes

actual val context: Any = Unit

actual fun runTest(test: suspend () -> Unit) {
runTest(timeout = 5.minutes) { test() }
}
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ package dev.gitlive.firebase

actual val context: Any = Unit

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ actual val emulatorHost: String = "10.0.2.2"

actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ actual val emulatorHost: String = "10.0.2.2"

actual val context: Any = ""

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
actual typealias IgnoreForAndroidUnitTest = Ignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,56 @@ import kotlin.test.*

expect val emulatorHost: String
expect val context: Any
expect fun runTest(test: suspend () -> Unit): TestResult
expect annotation class IgnoreForAndroidUnitTest()

@IgnoreForAndroidUnitTest
class FirebaseAuthTest {

lateinit var auth: FirebaseAuth

@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.auth.useEmulator(emulatorHost, 9099)
}
val app = Firebase.apps(context).firstOrNull() ?: Firebase.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"
)
)

auth = Firebase.auth(app).apply {
useEmulator(emulatorHost, 9099)
}
}

@AfterTest
fun deinitializeFirebase() = runBlockingTest {
Firebase.apps(context).forEach {
it.delete()
}
}

@Test
fun testSignInWithUsernameAndPassword() = runTest {
val uid = getTestUid("[email protected]", "test123")
val result = Firebase.auth.signInWithEmailAndPassword("[email protected]", "test123")
val result = auth.signInWithEmailAndPassword("[email protected]", "test123")
assertEquals(uid, result.user!!.uid)
}

@Test
fun testCreateUserWithEmailAndPassword() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
val createResult = Firebase.auth.createUserWithEmailAndPassword(email, "test123")
val createResult = auth.createUserWithEmailAndPassword(email, "test123")
assertNotEquals(null, createResult.user?.uid)
assertEquals(null, createResult.user?.displayName)
assertEquals(null, createResult.user?.phoneNumber)
assertEquals(email, createResult.user?.email)

val signInResult = Firebase.auth.signInWithEmailAndPassword(email, "test123")
val signInResult = auth.signInWithEmailAndPassword(email, "test123")
assertEquals(createResult.user?.uid, signInResult.user?.uid)

signInResult.user!!.delete()
Expand All @@ -62,19 +69,19 @@ class FirebaseAuthTest {
@Test
fun testFetchSignInMethods() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
var signInMethodResult = Firebase.auth.fetchSignInMethodsForEmail(email)
var signInMethodResult = auth.fetchSignInMethodsForEmail(email)
assertEquals(emptyList(), signInMethodResult)
Firebase.auth.createUserWithEmailAndPassword(email, "test123")
signInMethodResult = Firebase.auth.fetchSignInMethodsForEmail(email)
auth.createUserWithEmailAndPassword(email, "test123")
signInMethodResult = auth.fetchSignInMethodsForEmail(email)
assertEquals(listOf("password"), signInMethodResult)

Firebase.auth.signInWithEmailAndPassword(email, "test123").user!!.delete()
auth.signInWithEmailAndPassword(email, "test123").user!!.delete()
}

@Test
fun testSendEmailVerification() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
val createResult = Firebase.auth.createUserWithEmailAndPassword(email, "test123")
val createResult = auth.createUserWithEmailAndPassword(email, "test123")
assertNotEquals(null, createResult.user?.uid)
createResult.user!!.sendEmailVerification()

Expand All @@ -84,10 +91,10 @@ class FirebaseAuthTest {
@Test
fun sendPasswordResetEmail() = runTest {
val email = "test+${Random.nextInt(100000)}@test.com"
val createResult = Firebase.auth.createUserWithEmailAndPassword(email, "test123")
val createResult = auth.createUserWithEmailAndPassword(email, "test123")
assertNotEquals(null, createResult.user?.uid)

Firebase.auth.sendPasswordResetEmail(email)
auth.sendPasswordResetEmail(email)

createResult.user!!.delete()
}
Expand All @@ -96,16 +103,16 @@ class FirebaseAuthTest {
fun testSignInWithCredential() = runTest {
val uid = getTestUid("[email protected]", "test123")
val credential = EmailAuthProvider.credential("[email protected]", "test123")
val result = Firebase.auth.signInWithCredential(credential)
val result = auth.signInWithCredential(credential)
assertEquals(uid, result.user!!.uid)
}

@Test
fun testIsSignInWithEmailLink() {
val validLink = "http://localhost:9099/emulator/action?mode=signIn&lang=en&oobCode=_vr0QcFcxcVeLZbrcU-GpTaZiuxlHquqdC8MSy0YM_vzWCTAQgV9Jq&apiKey=fake-api-key&continueUrl=https%3A%2F%2Fexample.com%2Fsignin"
val invalidLink = "http://localhost:9099/emulator/action?mode=signIn&lang=en&&apiKey=fake-api-key&continueUrl=https%3A%2F%2Fexample.com%2Fsignin"
assertTrue(Firebase.auth.isSignInWithEmailLink(validLink))
assertFalse(Firebase.auth.isSignInWithEmailLink(invalidLink))
assertTrue(auth.isSignInWithEmailLink(validLink))
assertFalse(auth.isSignInWithEmailLink(invalidLink))
}

@Test
Expand All @@ -117,7 +124,7 @@ class FirebaseAuthTest {
}

private suspend fun getTestUid(email: String, password: String): String {
val uid = Firebase.auth.let {
val uid = auth.let {
val user = try {
it.createUserWithEmailAndPassword(email, password).user
} catch (e: FirebaseAuthUserCollisionException) {
Expand All @@ -127,9 +134,9 @@ class FirebaseAuthTest {
user!!.uid
}

check(Firebase.auth.currentUser != null)
Firebase.auth.signOut()
check(Firebase.auth.currentUser == null)
check(auth.currentUser != null)
auth.signOut()
check(auth.currentUser == null)

return uid
}
Expand Down
14 changes: 0 additions & 14 deletions firebase-auth/src/iosTest/kotlin/dev/gitlive/firebase/auth/auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,9 @@

package dev.gitlive.firebase.auth

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()
}
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

package dev.gitlive.firebase.auth

import kotlin.time.Duration.Companion.minutes


actual val emulatorHost: String = "localhost"

actual val context: Any = Unit

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest(timeout = 5.minutes) { test() }

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ package dev.gitlive.firebase.auth
actual val emulatorHost: String = "10.0.2.2"

actual val context: Any = Unit

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
2 changes: 1 addition & 1 deletion firebase-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ kotlin {

getByName("jsMain") {
dependencies {
api(npm("firebase", "10.4.0"))
api(npm("firebase", "10.6.0"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ import androidx.test.platform.app.InstrumentationRegistry

actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ import org.junit.Ignore

actual val context: Any = ""

actual fun runTest(test: suspend () -> Unit) = kotlinx.coroutines.test.runTest { test() }
actual typealias IgnoreForAndroidUnitTest = Ignore
Loading

0 comments on commit ed98066

Please sign in to comment.