Skip to content

Commit

Permalink
Now finally properly closing all open Realm instances after use.
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Feb 8, 2017
1 parent fa937a9 commit f6ca81a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,34 @@ 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.Test

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

lateinit var coffeeEventRepository: RealmCoffeeEventRepository

@Before
fun setUp() {
val realmConfig = RealmConfiguration.Builder()
.name("test.realm")
.inMemory()
.build()
Realm.setDefaultConfiguration(testConfig)

Realm.setDefaultConfiguration(realmConfig)
Realm.getDefaultInstance().executeTransaction(Realm::deleteAll)
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 All @@ -63,15 +71,15 @@ class CoffeeEventRepositoryTest {
coffeeEventRepository.recordBrewingEvent()

val lastEvent = coffeeEventRepository.recordBrewingEvent()
assertThat(coffeeEventRepository.getLastBrewingEvent(), equalTo(lastEvent))
assertThat(coffeeEventRepository.getLastBrewingEvent()!!.id, equalTo(lastEvent.id))
}

@Test
fun getLastBrewingEvent_WhenHavingAccidentsAndSuccessfulEvents_ReturnsOnlyLastBrewingEvent() {
val lastSuccessfulEvent = coffeeEventRepository.recordBrewingEvent()
coffeeEventRepository.recordBrewingAccident(User())

assertThat(coffeeEventRepository.getLastBrewingEvent(), equalTo(lastSuccessfulEvent))
assertThat(coffeeEventRepository.getLastBrewingEvent()!!.user, equalTo(lastSuccessfulEvent.user))
}

@Test
Expand All @@ -81,7 +89,8 @@ class CoffeeEventRepositoryTest {
coffeeEventRepository.recordBrewingAccident(user)

val lastAccident = coffeeEventRepository.recordBrewingAccident(user)
assertThat(coffeeEventRepository.getLastBrewingAccident(), equalTo(lastAccident))
assertThat(coffeeEventRepository.getLastBrewingAccident()!!.id, equalTo(lastAccident.id))
assertThat(coffeeEventRepository.getLastBrewingAccident()!!.user!!.id, equalTo(lastAccident.user!!.id))
}

@Test
Expand All @@ -100,8 +109,12 @@ class CoffeeEventRepositoryTest {
assertThat(coffeeEventRepository.getAccidentCountForUser(user), equalTo(3L))
}

private fun coffeeEventCount() =
Realm.getDefaultInstance().where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", true)
.count()
private fun coffeeEventCount() = with(Realm.getDefaultInstance()) {
val count = where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", true)
.count()
close()

return@with count
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,37 @@ 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.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

@Before
fun setUp() {
val realmConfig = RealmConfiguration.Builder()
.name("test.realm")
.inMemory()
.build()
Realm.setDefaultConfiguration(testConfig)

Realm.setDefaultConfiguration(realmConfig)
Realm.getDefaultInstance().executeTransaction(Realm::deleteAll)
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,58 @@ class RealmCoffeeEventRepository : CoffeeEventRepository {
}
}

val copy = copyFromRealm(event)
close()
return@with event!!
return@with copy!!
}

override fun recordBrewingAccident(user: User) = with(Realm.getDefaultInstance()) {
var event: CoffeeBrewingEvent? = null
var accident: CoffeeBrewingEvent? = null
executeTransaction {
event = newEvent(it).apply {
accident = newEvent(it).apply {
time = System.currentTimeMillis()
isSuccessful = false
this.user = copyToRealmOrUpdate(user)
}
}

val copy = copyFromRealm(accident)
close()
return@with copy!!
}

override fun getAccidentCountForUser(user: User) = with(Realm.getDefaultInstance()) {
val count = where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", false)
.equalTo("user.id", user.id)
.count()

close()
return@with event!!
return@with count
}

override fun getAccidentCountForUser(user: User) =
Realm.getDefaultInstance()
.where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", false)
.equalTo("user.id", user.id)
.count()

override fun getLastBrewingEvent() =
Realm.getDefaultInstance()
.where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", true)
.findAllSorted("time", Sort.ASCENDING)
.lastOrNull()
override fun getLastBrewingEvent() = with(Realm.getDefaultInstance()) {
val lastEvent = where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", true)
.findAllSorted("time", Sort.ASCENDING)
.lastOrNull()
val copy = copyFromRealm(lastEvent)

override fun getLastBrewingAccident() =
Realm.getDefaultInstance()
.where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", false)
.findAllSorted("time", Sort.ASCENDING)
.lastOrNull()
close()
return@with copy
}

override fun getLastBrewingAccident() = with(Realm.getDefaultInstance()) {
val accident = where(CoffeeBrewingEvent::class.java)
.equalTo("isSuccessful", false)
.findAllSorted("time", Sort.ASCENDING)
.lastOrNull()
val copy = copyFromRealm(accident)

close()
return@with copy
}

private fun newEvent(realm: Realm) =
realm.createObject(
Expand Down

0 comments on commit f6ca81a

Please sign in to comment.