Skip to content

Commit

Permalink
Begin adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethshackleton committed Jul 18, 2021
1 parent 83c3bc6 commit 02866d2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,78 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bloomberg.selekt.android

import com.bloomberg.selekt.Experimental
import com.bloomberg.selekt.SQLiteJournalMode
import com.bloomberg.selekt.SQLiteTransactionMode
import com.bloomberg.selekt.android.SQLiteDatabase.Companion.deleteDatabase
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import org.junit.After
import org.junit.Test
import java.io.File
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@OptIn(Experimental::class, ExperimentalCoroutinesApi::class)
internal class SQLiteDatabaseWithTransactionTest {
private val dispatcher = TestCoroutineDispatcher()
private val scope = TestCoroutineScope(dispatcher)

private val file = File.createTempFile("test-with-transaction", ".db").also { it.deleteOnExit() }
private val database = SQLiteDatabase.openOrCreateDatabase(file, SQLiteJournalMode.WAL.databaseConfiguration,
ByteArray(32) { 0x42 })

@After
fun tearDown() {
database.run {
try {
close()
assertFalse(isOpen)
} finally {
assertTrue(deleteDatabase(file))
}
}
}

@Test
fun withTransactionExclusively(): Unit = scope.runBlockingTest {
database.withTransaction(SQLiteTransactionMode.EXCLUSIVE, dispatcher) {
assertTrue(isTransactionOpenedByCurrentThread)
}
}

@Test
fun withTransactionImmediately(): Unit = scope.runBlockingTest {
database.withTransaction(SQLiteTransactionMode.IMMEDIATE, dispatcher) {
assertTrue(isTransactionOpenedByCurrentThread)
}
}

@Test(expected = CancellationException::class)
fun withTransactionIsCancellable(): Unit = scope.runBlockingTest {
cancel("Test cancel!")
database.withTransaction(dispatcher = Dispatchers.IO) { }
}

@Test
fun delayTransactionDefault(): Unit = scope.runBlockingTest {
database.withTransaction(dispatcher = dispatcher) {
delayTransaction()
}
}

@Test
fun delayTransaction(): Unit = scope.runBlockingTest {
database.withTransaction(dispatcher = dispatcher) {
delayTransaction(50L)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class SharedCloseable : Closeable {
}
}

internal suspend inline fun <T> withPledge(block: suspend () -> T): T {
internal suspend fun <T> withPledge(block: suspend () -> T): T {
retain()
try {
return block()
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/SelektPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class SelektPlugin : Plugin<Project> {
add(name, kotlin("test-junit", Versions.KOTLIN_TEST.version))
add(name, kotlinX("coroutines-core", Versions.KOTLIN_COROUTINES.version))
add(name, kotlinX("coroutines-jdk8", Versions.KOTLIN_COROUTINES.version))
add(name, kotlinX("coroutines-test", Versions.KOTLIN_COROUTINES_TEST.version))
add(name, "org.assertj:assertj-core:${Versions.ASSERT_J}")
add(name, "org.mockito:mockito-core:${Versions.MOCKITO}")
add(name, "org.mockito.kotlin:mockito-kotlin:${Versions.MOCKITO_KOTLIN}")
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ enum class Versions(
JUNIT5_PLATFORM("1.7.2", URL("https://junit.org/junit5/")),
KOTLIN("1.5.21", URL("https://github.com/JetBrains/kotlin")),
KOTLIN_COROUTINES("1.5.0", URL("https://github.com/Kotlin/kotlinx.coroutines")),
KOTLIN_COROUTINES_TEST("1.5.0", URL("https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/")),
KOTLIN_TEST("1.4.32", URL("https://github.com/JetBrains/kotlin")),
KTLINT("0.41.0", URL("https://github.com/pinterest/ktlint")),
KTLINT_GRADLE_PLUGIN("10.1.0", URL("https://github.com/JLLeitschuh/ktlint-gradle")),
Expand Down

0 comments on commit 02866d2

Please sign in to comment.