Skip to content

Commit

Permalink
[Tests] split out db initialization during tests
Browse files Browse the repository at this point in the history
- instead of initing all dbs every test.
  • Loading branch information
agrosner committed Feb 18, 2021
1 parent 12ad960 commit bd5305e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class DatabaseHelperDelegate(
context.assets.open(prepackagedName)
}
writeDB(dbPath, inputStream)
databaseDefinition.reopen()
} catch (e: IOException) {
FlowLog.log(FlowLog.Level.W, "Failed to open file", e)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.dbflow5

import com.dbflow5.config.DBFlowDatabase
import com.dbflow5.config.FlowConfig
import com.dbflow5.config.FlowLog
import com.dbflow5.config.FlowManager
import com.dbflow5.config.flowConfig
import com.dbflow5.contentobserver.ContentObserverDatabase
import com.dbflow5.database.AndroidSQLiteOpenHelper
import com.dbflow5.prepackaged.PrepackagedDB
import com.dbflow5.provider.ContentDatabase
import com.dbflow5.runtime.ContentResolverNotifier
import com.dbflow5.sqlcipher.CipherDatabase
import com.dbflow5.sqlcipher.SQLCipherOpenHelper
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement


class DBFlowInstrumentedTestRule : TestRule {
class DBFlowInstrumentedTestRule(private val dbConfigBlock: FlowConfig.Builder.() -> Unit) : TestRule {

override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
Expand All @@ -24,22 +21,10 @@ class DBFlowInstrumentedTestRule : TestRule {
override fun evaluate() {
FlowLog.setMinimumLoggingLevel(FlowLog.Level.V)
FlowManager.init(DemoApp.context) {
database<AppDatabase>({
modelNotifier(ContentResolverNotifier(DemoApp.context, "com.grosner.content"))
transactionManagerCreator { databaseDefinition: DBFlowDatabase ->
ImmediateTransactionManager(databaseDefinition)
}
}, AndroidSQLiteOpenHelper.createHelperCreator(DemoApp.context))
database<PrepackagedDB>({
databaseName("prepackaged")
}, AndroidSQLiteOpenHelper.createHelperCreator(DemoApp.context))
database<CipherDatabase>(openHelperCreator = SQLCipherOpenHelper.createHelperCreator(DemoApp.context, "dbflow-rules"))
database<ContentDatabase>({
databaseName("content")
}, AndroidSQLiteOpenHelper.createHelperCreator(DemoApp.context))
database<TestDatabase>({
transactionManagerCreator(::ImmediateTransactionManager)
}, AndroidSQLiteOpenHelper.createHelperCreator(DemoApp.context))
dbConfigBlock()
}
try {
base.evaluate()
Expand All @@ -51,6 +36,6 @@ class DBFlowInstrumentedTestRule : TestRule {
}

companion object {
fun create() = DBFlowInstrumentedTestRule()
fun create(dbConfigBlock: FlowConfig.Builder.() -> Unit = {}) = DBFlowInstrumentedTestRule(dbConfigBlock)
}
}
3 changes: 2 additions & 1 deletion tests/src/androidTest/java/com/dbflow5/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package com.dbflow5
import com.dbflow5.annotation.Column
import com.dbflow5.annotation.PrimaryKey
import com.dbflow5.annotation.Table
import com.dbflow5.contentobserver.ContentObserverDatabase

@Table(database = AppDatabase::class, name = "User2")
@Table(database = ContentObserverDatabase::class, name = "User2")
class User(@PrimaryKey var id: Int = 0,
@Column var firstName: String? = null,
@Column var lastName: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dbflow5
package com.dbflow5.contentobserver

import com.dbflow5.annotation.Database
import com.dbflow5.config.DBFlowDatabase

@Database(version = 1)
abstract class AppDatabase : DBFlowDatabase()
abstract class ContentObserverDatabase : DBFlowDatabase()
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
package com.dbflow5.contentobserver

import android.net.Uri
import com.dbflow5.BaseUnitTest
import androidx.test.core.app.ApplicationProvider
import com.dbflow5.DBFlowInstrumentedTestRule
import com.dbflow5.DemoApp
import com.dbflow5.ImmediateTransactionManager
import com.dbflow5.TABLE_QUERY_PARAM
import com.dbflow5.config.DBFlowDatabase
import com.dbflow5.config.database
import com.dbflow5.config.databaseForTable
import com.dbflow5.config.modelAdapter
import com.dbflow5.config.tableName
import com.dbflow5.contentobserver.User_Table.id
import com.dbflow5.contentobserver.User_Table.name
import com.dbflow5.database.AndroidSQLiteOpenHelper
import com.dbflow5.database.DatabaseWrapper
import com.dbflow5.getNotificationUri
import com.dbflow5.query.SQLOperator
import com.dbflow5.query.delete
import com.dbflow5.runtime.ContentResolverNotifier
import com.dbflow5.runtime.FlowContentObserver
import com.dbflow5.structure.ChangeAction
import com.dbflow5.structure.delete
import com.dbflow5.structure.insert
import com.dbflow5.structure.save
import com.dbflow5.structure.update
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.CountDownLatch

class ContentObserverTest : BaseUnitTest() {
class ContentObserverTest {

@JvmField
@Rule
var dblflowTestRule = DBFlowInstrumentedTestRule.create {
database<ContentObserverDatabase>({
modelNotifier(ContentResolverNotifier(DemoApp.context, "com.grosner.content"))
transactionManagerCreator { databaseDefinition: DBFlowDatabase ->
ImmediateTransactionManager(databaseDefinition)
}
}, AndroidSQLiteOpenHelper.createHelperCreator(ApplicationProvider.getApplicationContext()))
}

val contentUri = "com.grosner.content"

private lateinit var user: User

@Before
fun setupUser() {
databaseForTable<User> { dbFlowDatabase ->
database<ContentObserverDatabase> { dbFlowDatabase ->
delete<User>().execute(dbFlowDatabase)
}
user = User(5, "Something", 55)
Expand Down Expand Up @@ -60,7 +74,7 @@ class ContentObserverTest : BaseUnitTest() {

@Test
fun testSpecificUrlUpdate() {
// assertProperConditions(ChangeAction.UPDATE) { user, db -> user.apply { age = 56 }.update(db) }
// assertProperConditions(ChangeAction.UPDATE) { user, db -> user.apply { age = 56 }.update(db) }

}

Expand All @@ -72,8 +86,8 @@ class ContentObserverTest : BaseUnitTest() {

@Test
fun testSpecificUrlDelete() {
// user.save(databaseForTable<User>())
// assertProperConditions(ChangeAction.DELETE) { user, db -> user.delete(db) }
// user.save(databaseForTable<User>())
// assertProperConditions(ChangeAction.DELETE) { user, db -> user.delete(db) }
}

private fun assertProperConditions(action: ChangeAction, userFunc: (User, DatabaseWrapper) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.dbflow5.contentobserver

import com.dbflow5.AppDatabase
import com.dbflow5.annotation.Column
import com.dbflow5.annotation.PrimaryKey
import com.dbflow5.annotation.Table

@Table(database = AppDatabase::class)
@Table(database = ContentObserverDatabase::class)
class User(@PrimaryKey var id: Int = 0, @PrimaryKey var name: String = "", @Column var age: Int = 0)
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package com.dbflow5.provider

import android.content.ContentResolver
import androidx.test.core.app.ApplicationProvider
import androidx.test.rule.provider.ProviderTestRule
import com.dbflow5.BaseUnitTest
import com.dbflow5.DBFlowInstrumentedTestRule
import com.dbflow5.config.FlowManager
import com.dbflow5.config.database
import com.dbflow5.database.AndroidSQLiteOpenHelper
import com.dbflow5.query.select
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test

/**
* Description:
*/
class ContentProviderTests : BaseUnitTest() {
class ContentProviderTests {

@JvmField
@Rule
var dblflowTestRule = DBFlowInstrumentedTestRule.create {
database<ContentDatabase>({
databaseName("content")
}, AndroidSQLiteOpenHelper.createHelperCreator(ApplicationProvider.getApplicationContext()))
}

@get:Rule
val contentProviderRule = ProviderTestRule.Builder(TestContentProvider_Provider::class.java, TestContentProvider.AUTHORITY).build()
Expand Down
24 changes: 16 additions & 8 deletions tests/src/androidTest/java/com/dbflow5/sqlcipher/CipherTest.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package com.dbflow5.sqlcipher

import com.dbflow5.BaseUnitTest
import com.dbflow5.DBFlowInstrumentedTestRule
import com.dbflow5.DemoApp
import com.dbflow5.config.database
import com.dbflow5.query.delete
import com.dbflow5.query.select
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test

/**
* Description: Ensures we can use SQLCipher
*/
class CipherTest : BaseUnitTest() {
class CipherTest {

@JvmField
@Rule
var dblflowTestRule = DBFlowInstrumentedTestRule.create {
database<CipherDatabase>(openHelperCreator = SQLCipherOpenHelper.createHelperCreator(DemoApp.context, "dbflow-rules"))
}

@Test
fun testCipherModel() {
database(CipherDatabase::class) { t ->
(delete() from CipherModel::class).execute(t)
database<CipherDatabase> { db ->
(delete() from CipherModel::class).execute(db)
val model = CipherModel(name = "name")
model.save(t)
assertTrue(model.exists(t))
model.save(db)
assertTrue(model.exists(db))

val retrieval = (select from CipherModel::class
where CipherModel_Table.name.eq("name"))
.querySingle(t)
.querySingle(db)
assertTrue(retrieval!!.id == model.id)
(delete() from CipherModel::class).execute(t)
(delete() from CipherModel::class).execute(db)
}
}
}

0 comments on commit bd5305e

Please sign in to comment.