Skip to content

Commit

Permalink
Revert "Fix PreviewsTest to cleanup VMs"
Browse files Browse the repository at this point in the history
This reverts commit 4a6b343.
  • Loading branch information
Daeda88 committed Apr 15, 2024
1 parent 4a6b343 commit 8c3a9d7
Showing 1 changed file with 139 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import dev.gitlive.firebase.initialize
import dev.gitlive.firebase.runBlockingTest
import dev.gitlive.firebase.runTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext
Expand All @@ -22,7 +21,6 @@ import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

expect val emulatorHost: String
expect val context: Any
Expand Down Expand Up @@ -55,9 +53,6 @@ class FirebaseDatabaseTest {

database = Firebase.database(app).apply {
useEmulator(emulatorHost, 9000)
setLoggingEnabled(true)
}.also {
it.goOnline()
}
}

Expand All @@ -68,169 +63,155 @@ class FirebaseDatabaseTest {
}
}

// @Test
// fun testSetValue() = runTest {
// ensureDatabaseConnected()
// val testValue = "test"
// val testReference = database.reference("testPath")
//
// testReference.setValue(testValue)
//
// val testReferenceValue = testReference
// .valueEvents
// .first()
// .value<String>()
//
// assertEquals(testValue, testReferenceValue)
// }
//
// @Test
// fun testChildCount() = runTest {
// setupRealtimeData()
// val dataSnapshot = database
// .reference("FirebaseRealtimeDatabaseTest")
// .valueEvents
// .first()
//
// val firebaseDatabaseChildCount = dataSnapshot.child("values").children.count()
// assertEquals(3, firebaseDatabaseChildCount)
// }
//
// @Test
// fun testBasicIncrementTransaction() = runTest {
// ensureDatabaseConnected()
// val data = DatabaseTest("PostOne", 2)
// val userRef = database.reference("users/user_1/post_id_1")
// setupDatabase(userRef, data, DatabaseTest.serializer())
//
// // Check database before transaction
// val userDocBefore = userRef.valueEvents.first().value(DatabaseTest.serializer())
// assertEquals(data.title, userDocBefore.title)
// assertEquals(data.likes, userDocBefore.likes)
//
// // Run transaction
// val transactionSnapshot = userRef.runTransaction(DatabaseTest.serializer()) { it.copy(likes = it.likes + 1) }
// val userDocAfter = transactionSnapshot.value(DatabaseTest.serializer())
//
// // Check the database after transaction
// assertEquals(data.title, userDocAfter.title)
// assertEquals(data.likes + 1, userDocAfter.likes)
// }
//
// @Test
// fun testBasicDecrementTransaction() = runTest {
// ensureDatabaseConnected()
// val data = DatabaseTest("PostTwo", 2)
// val userRef = database.reference("users/user_1/post_id_2")
// setupDatabase(userRef, data, DatabaseTest.serializer())
//
// // Check database before transaction
// val userDocBefore = userRef.valueEvents.first().value(DatabaseTest.serializer())
// assertEquals(data.title, userDocBefore.title)
// assertEquals(data.likes, userDocBefore.likes)
//
// // Run transaction
// val transactionSnapshot = userRef.runTransaction(DatabaseTest.serializer()) { it.copy(likes = it.likes - 1) }
// val userDocAfter = transactionSnapshot.value(DatabaseTest.serializer())
//
// // Check the database after transaction
// assertEquals(data.title, userDocAfter.title)
// assertEquals(data.likes - 1, userDocAfter.likes)
// }
//
// @Test
// fun testSetServerTimestamp() = runTest {
// ensureDatabaseConnected()
// val testReference = database.reference("testSetServerTimestamp")
//
// testReference.setValue(ServerValue.TIMESTAMP)
//
// val timestamp = testReference
// .valueEvents
// .first()
// .value<Long>()
//
// assertTrue(timestamp > 0)
// }
//
// @Test
// fun testIncrement() = runTest {
// ensureDatabaseConnected()
// val testReference = database.reference("testIncrement")
//
// testReference.setValue(2.0)
//
// val value = testReference
// .valueEvents
// .first()
// .value<Double>()
//
// assertEquals(2.0, value)
//
// testReference.setValue(ServerValue.increment(5.0))
// val updatedValue = testReference
// .valueEvents
// .first()
// .value<Double>()
//
// assertEquals(7.0, updatedValue)
// }
//
// @Test
// fun testBreakRules() = runTest {
// ensureDatabaseConnected()
// val reference = database
// .reference("FirebaseRealtimeDatabaseTest")
// val child = reference.child("lastActivity")
// assertFailsWith<DatabaseException> {
// child.setValue("stringNotAllowed")
// }
// child.setValue(2)
// assertFailsWith<DatabaseException> {
// reference.updateChildren(mapOf("lastActivity" to "stringNotAllowed"))
// }
// }
//
// @Test
// fun testUpdateChildren() = runTest {
// setupRealtimeData()
// val reference = database
// .reference("FirebaseRealtimeDatabaseTest")
// val valueEvents = reference.child("lastActivity").valueEvents
// assertTrue(valueEvents.first().exists)
// reference.updateChildren(mapOf("test" to false, "nested" to mapOf("lastActivity" to null), "lastActivity" to null))
// assertFalse(valueEvents.first().exists)
// }
@Test
fun testSetValue() = runTest {
ensureDatabaseConnected()
val testValue = "test"
val testReference = database.reference("testPath")

testReference.setValue(testValue)

val testReferenceValue = testReference
.valueEvents
.first()
.value<String>()

assertEquals(testValue, testReferenceValue)
}

@Test
fun testUpdateChildrenOnDisconnect() = runBlockingTest {
fun testChildCount() = runTest {
setupRealtimeData()
withTimeout(120.seconds) {
val reference = database
.reference("FirebaseRealtimeDatabaseTest")
val valueEvents = reference.child("lastActivity").valueEvents
assertTrue(valueEvents.first().exists)
reference.onDisconnect().updateChildren(
mapOf(
"test" to false,
"nested" to mapOf("lastActivity" to null),
"lastActivity" to null
)
)
database.goOffline()
val dataSnapshot = database
.reference("FirebaseRealtimeDatabaseTest")
.valueEvents
.first()

val firebaseDatabaseChildCount = dataSnapshot.child("values").children.count()
assertEquals(3, firebaseDatabaseChildCount)
}

@Test
fun testBasicIncrementTransaction() = runTest {
ensureDatabaseConnected()
val data = DatabaseTest("PostOne", 2)
val userRef = database.reference("users/user_1/post_id_1")
setupDatabase(userRef, data, DatabaseTest.serializer())

// Check database before transaction
val userDocBefore = userRef.valueEvents.first().value(DatabaseTest.serializer())
assertEquals(data.title, userDocBefore.title)
assertEquals(data.likes, userDocBefore.likes)

// Run transaction
val transactionSnapshot = userRef.runTransaction(DatabaseTest.serializer()) { it.copy(likes = it.likes + 1) }
val userDocAfter = transactionSnapshot.value(DatabaseTest.serializer())

// Check the database after transaction
assertEquals(data.title, userDocAfter.title)
assertEquals(data.likes + 1, userDocAfter.likes)
}

ensureDatabaseDisconnected()
@Test
fun testBasicDecrementTransaction() = runTest {
ensureDatabaseConnected()
val data = DatabaseTest("PostTwo", 2)
val userRef = database.reference("users/user_1/post_id_2")
setupDatabase(userRef, data, DatabaseTest.serializer())

// Check database before transaction
val userDocBefore = userRef.valueEvents.first().value(DatabaseTest.serializer())
assertEquals(data.title, userDocBefore.title)
assertEquals(data.likes, userDocBefore.likes)

// Run transaction
val transactionSnapshot = userRef.runTransaction(DatabaseTest.serializer()) { it.copy(likes = it.likes - 1) }
val userDocAfter = transactionSnapshot.value(DatabaseTest.serializer())

// Check the database after transaction
assertEquals(data.title, userDocAfter.title)
assertEquals(data.likes - 1, userDocAfter.likes)
}

@Test
fun testSetServerTimestamp() = runTest {
ensureDatabaseConnected()
val testReference = database.reference("testSetServerTimestamp")

testReference.setValue(ServerValue.TIMESTAMP)

val timestamp = testReference
.valueEvents
.first()
.value<Long>()

assertTrue(timestamp > 0)
}

@Test
fun testIncrement() = runTest {
ensureDatabaseConnected()
val testReference = database.reference("testIncrement")

delay(10000)
testReference.setValue(2.0)

// database.goOnline()
// ensureDatabaseConnected()
assertFalse(valueEvents.first().exists)
val value = testReference
.valueEvents
.first()
.value<Double>()

assertEquals(2.0, value)

testReference.setValue(ServerValue.increment(5.0))
val updatedValue = testReference
.valueEvents
.first()
.value<Double>()

assertEquals(7.0, updatedValue)
}

@Test
fun testBreakRules() = runTest {
ensureDatabaseConnected()
val reference = database
.reference("FirebaseRealtimeDatabaseTest")
val child = reference.child("lastActivity")
assertFailsWith<DatabaseException> {
child.setValue("stringNotAllowed")
}
child.setValue(2)
assertFailsWith<DatabaseException> {
reference.updateChildren(mapOf("lastActivity" to "stringNotAllowed"))
}
}

@Test
fun testUpdateChildren() = runTest {
setupRealtimeData()
val reference = database
.reference("FirebaseRealtimeDatabaseTest")
val valueEvents = reference.child("lastActivity").valueEvents
assertTrue(valueEvents.first().exists)
reference.updateChildren(mapOf("test" to false, "nested" to mapOf("lastActivity" to null), "lastActivity" to null))
assertFalse(valueEvents.first().exists)
}

@Test
fun testUpdateChildrenOnDisconnect() = runTest {
setupRealtimeData()
val reference = database
.reference("FirebaseRealtimeDatabaseTest")
val valueEvents = reference.child("lastActivity").valueEvents
assertTrue(valueEvents.first().exists)
reference.onDisconnect().updateChildren(mapOf("test" to false, "nested" to mapOf("lastActivity" to null), "lastActivity" to null))
database.goOffline()

database.goOnline()
ensureDatabaseConnected()
assertFalse(valueEvents.first().exists)
}

private suspend fun setupRealtimeData() {
ensureDatabaseConnected()
val firebaseDatabaseTestReference = database
Expand Down Expand Up @@ -259,14 +240,8 @@ class FirebaseDatabaseTest {
}

private suspend fun ensureDatabaseConnected() = withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(10.seconds) {
withTimeout(2.minutes) {
database.reference(".info/connected").valueEvents.first { it.value() }
}
}

private suspend fun ensureDatabaseDisconnected() = withContext(Dispatchers.Default.limitedParallelism(1)) {
withTimeout(10.seconds) {
database.reference(".info/connected").valueEvents.first { !it.value<Boolean>() }
}
}
}

0 comments on commit 8c3a9d7

Please sign in to comment.