Skip to content

Commit

Permalink
Extracted the duplicate test Realm setup to a test rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Feb 8, 2017
1 parent f6ca81a commit cbf72dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,23 @@ package com.codemate.koffeemate.data.local
import com.codemate.koffeemate.data.models.CoffeeBrewingEvent
import com.codemate.koffeemate.data.models.User
import io.realm.Realm
import io.realm.RealmConfiguration
import org.hamcrest.core.IsEqual.equalTo
import org.junit.After
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class CoffeeEventRepositoryTest {
val testConfig: RealmConfiguration = RealmConfiguration.Builder()
.name("test.realm")
.build()

lateinit var coffeeEventRepository: RealmCoffeeEventRepository

@Rule @JvmField
val realmRule: RealmTestRule = RealmTestRule()

@Before
fun setUp() {
Realm.setDefaultConfiguration(testConfig)

val realm = Realm.getDefaultInstance()
realm.executeTransaction(Realm::deleteAll)
realm.close()

coffeeEventRepository = RealmCoffeeEventRepository()
}

@After
fun tearDown() {
Realm.deleteRealm(testConfig)
}

@Test
fun recordBrewingEvent_PersistsEventsInDatabase() {
coffeeEventRepository.recordBrewingEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MigrationTest {
@Test
fun testMigrationFromVersionZeroToOne() {
val config = RealmConfiguration.Builder()
.name("migration-test.realm")
.name("test.realm")
.schemaVersion(1)
.migration(Migration())
.build()
Expand Down Expand Up @@ -82,6 +82,7 @@ class MigrationTest {
assertThat(brewingEventWithUserId.user!!.last_updated, equalTo(brewingAccident.user!!.last_updated))

realm.close()
Realm.deleteRealm(config)
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.codemate.koffeemate.data.local

import io.realm.Realm
import io.realm.RealmConfiguration
import org.junit.rules.ExternalResource
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
* A JUnit Rule that sets up a test Realm database before each test,
* clears all data from it, and deletes the whole database file alltogether
* after the test completes.
*
* Usage:
*
* @Rule @JvmField
* val realmRule: RealmTestRule = RealmTestRule()
*
* The above assumes that your application logic that uses Realm always uses
* the default instance. Meaning that your code calls Realm.getDefaultInstance()
* and uses that for all database logic.
*/
class RealmTestRule : ExternalResource() {
val testConfig: RealmConfiguration = RealmConfiguration.Builder()
.name("test.realm")
.build()

override fun before() {
Realm.setDefaultConfiguration(testConfig)

with (Realm.getDefaultInstance()) {
executeTransaction(Realm::deleteAll)
close()
}
}

override fun apply(base: Statement?, description: Description?): Statement {
return super.apply(base, description)
}

override fun after() {
Realm.deleteRealm(testConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,26 @@
package com.codemate.koffeemate.data.local

import com.codemate.koffeemate.data.models.User
import io.realm.Realm
import io.realm.RealmConfiguration
import org.hamcrest.core.IsEqual.equalTo
import org.junit.After
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class UserRepositoryTest {
val testConfig: RealmConfiguration = RealmConfiguration.Builder()
.name("test.realm")
.build()

val TEST_USERS_UNIQUE = listOf(User(id = "abc123"), User(id = "123abc"), User(id = "a1b2c3"))
val TEST_USERS_DUPLICATE = listOf(User(id = "abc123"), User(id = "abc123"), User(id = "abc123"))

lateinit var userRepository: UserRepository

@Rule @JvmField
val realmRule = RealmTestRule()

@Before
fun setUp() {
Realm.setDefaultConfiguration(testConfig)

val realm = Realm.getDefaultInstance()
realm.executeTransaction(Realm::deleteAll)
realm.close()

userRepository = RealmUserRepository()
}

@After
fun tearDown() {
Realm.deleteRealm(testConfig)
}

@Test
fun addAll_WhenUsersAreUnique_PersistsAllInDatabase() {
userRepository.addAll(TEST_USERS_UNIQUE)
Expand Down

0 comments on commit cbf72dd

Please sign in to comment.