From 20ef6500dc0b6d84905d92d8469feb6ff0ac502d Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 30 May 2017 17:26:14 -0700 Subject: migrate to dbflow --- .../kotlin/com/pitchedapps/frost/BaseUnitTest.kt | 25 ++++++++++++++ .../kotlin/com/pitchedapps/frost/DbFlowTestRule.kt | 38 ++++++++++++++++++++++ .../frost/ImmediateTransactionManager.kt | 38 ++++++++++++++++++++++ .../kotlin/com/pitchedapps/frost/TestDatabase.kt | 21 ++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 app/src/test/kotlin/com/pitchedapps/frost/BaseUnitTest.kt create mode 100644 app/src/test/kotlin/com/pitchedapps/frost/DbFlowTestRule.kt create mode 100644 app/src/test/kotlin/com/pitchedapps/frost/ImmediateTransactionManager.kt create mode 100644 app/src/test/kotlin/com/pitchedapps/frost/TestDatabase.kt (limited to 'app/src/test/kotlin/com/pitchedapps') diff --git a/app/src/test/kotlin/com/pitchedapps/frost/BaseUnitTest.kt b/app/src/test/kotlin/com/pitchedapps/frost/BaseUnitTest.kt new file mode 100644 index 00000000..55208aae --- /dev/null +++ b/app/src/test/kotlin/com/pitchedapps/frost/BaseUnitTest.kt @@ -0,0 +1,25 @@ +package com.pitchedapps.frost + +import android.content.Context +import android.os.Build +import org.junit.Rule +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +/** + * Created by Allan Wang on 2017-05-30. + */ +@RunWith(RobolectricTestRunner::class) +@Config(constants = BuildConfig::class, sdk = intArrayOf(Build.VERSION_CODES.LOLLIPOP), + assetDir = "build/intermediates/classes/test/") +abstract class BaseUnitTest { + + @JvmField + @Rule + var dblflowTestRule = DBFlowTestRule.create() + + val context: Context + get() = RuntimeEnvironment.application +} diff --git a/app/src/test/kotlin/com/pitchedapps/frost/DbFlowTestRule.kt b/app/src/test/kotlin/com/pitchedapps/frost/DbFlowTestRule.kt new file mode 100644 index 00000000..aaf38a0f --- /dev/null +++ b/app/src/test/kotlin/com/pitchedapps/frost/DbFlowTestRule.kt @@ -0,0 +1,38 @@ +package com.pitchedapps.frost + +import com.raizlabs.android.dbflow.config.DatabaseConfig +import com.raizlabs.android.dbflow.config.FlowConfig +import com.raizlabs.android.dbflow.config.FlowManager +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement +import org.robolectric.RuntimeEnvironment + +/** + * Created by Allan Wang on 2017-05-30. + */ + +class DBFlowTestRule : TestRule { + + override fun apply(base: Statement, description: Description): Statement { + return object : Statement() { + + @Throws(Throwable::class) + override fun evaluate() { + FlowManager.init(FlowConfig.Builder(RuntimeEnvironment.application) + .addDatabaseConfig(DatabaseConfig.Builder(TestDatabase::class.java) + .transactionManagerCreator(::ImmediateTransactionManager) + .build()).build()) + try { + base.evaluate() + } finally { + FlowManager.destroy() + } + } + } + } + + companion object { + fun create() = DBFlowTestRule() + } +} \ No newline at end of file diff --git a/app/src/test/kotlin/com/pitchedapps/frost/ImmediateTransactionManager.kt b/app/src/test/kotlin/com/pitchedapps/frost/ImmediateTransactionManager.kt new file mode 100644 index 00000000..f3d17e76 --- /dev/null +++ b/app/src/test/kotlin/com/pitchedapps/frost/ImmediateTransactionManager.kt @@ -0,0 +1,38 @@ +package com.pitchedapps.frost + +import com.raizlabs.android.dbflow.config.DatabaseDefinition +import com.raizlabs.android.dbflow.runtime.BaseTransactionManager +import com.raizlabs.android.dbflow.structure.database.transaction.ITransactionQueue +import com.raizlabs.android.dbflow.structure.database.transaction.Transaction +/** + * Created by Allan Wang on 2017-05-30. + */ +class ImmediateTransactionManager(databaseDefinition: DatabaseDefinition) + : BaseTransactionManager(ImmediateTransactionQueue(), databaseDefinition) + + +class ImmediateTransactionQueue : ITransactionQueue { + + override fun add(transaction: Transaction?) { + if (transaction != null) { + transaction.newBuilder() + .runCallbacksOnSameThread(true) + .build() + .executeSync() + } + } + + override fun cancel(transaction: Transaction?) { + + } + + override fun startIfNotAlive() { + } + + override fun cancel(name: String?) { + } + + override fun quit() { + } + +} \ No newline at end of file diff --git a/app/src/test/kotlin/com/pitchedapps/frost/TestDatabase.kt b/app/src/test/kotlin/com/pitchedapps/frost/TestDatabase.kt new file mode 100644 index 00000000..328233ac --- /dev/null +++ b/app/src/test/kotlin/com/pitchedapps/frost/TestDatabase.kt @@ -0,0 +1,21 @@ +package com.pitchedapps.frost + +import com.raizlabs.android.dbflow.annotation.Database +import com.raizlabs.android.dbflow.annotation.Migration +import com.raizlabs.android.dbflow.sql.migration.UpdateTableMigration + +/** + * Created by Allan Wang on 2017-05-30. + */ + +/** + * Description: + */ +@Database(version = TestDatabase.VERSION, name = TestDatabase.NAME) +object TestDatabase { + + const val VERSION = 1 + + const val NAME = "TestDatabase" + +} \ No newline at end of file -- cgit v1.2.3