Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Last active April 11, 2018 05:17
Show Gist options
  • Save markchristopherng/0e83b55f1ac8993a93244827f238d3cb to your computer and use it in GitHub Desktop.
Save markchristopherng/0e83b55f1ac8993a93244827f238d3cb to your computer and use it in GitHub Desktop.
BranchLocationDaoTest
package au.com.auspost.android.feature.postcode.service
import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import au.com.auspost.android.AuspostDatabase
import au.com.auspost.android.feature.postcode.model.BranchLocation
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class BranchLocationDaoTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
lateinit var db : AuspostDatabase
@Before
fun initDb() {
db = Room.inMemoryDatabaseBuilder(
InstrumentationRegistry.getContext(),
AuspostDatabase::class.java)
.allowMainThreadQueries()
.build()
setupTestData()
}
@After
fun closeDb() {
db.close()
}
@Test
fun testFindAll() {
db.branchLocationDao()
.getAll()
.test()
.assertValue { list: List<BranchLocation> -> list.count() == 3 }
}
@Test
fun testFindByKeyword_fullKeywoard() {
db.branchLocationDao()
.getByKeyword("Highett")
.test()
.assertValue { list: List<BranchLocation> -> list.count() == 1 }
}
@Test
fun testFindByKeyword_partialKeyword() {
db.branchLocationDao()
.getByKeyword("High%")
.test()
.assertValue { list: List<BranchLocation> -> list.count() == 1 }
}
@Test
fun testFindByKeyword_notFound() {
db.branchLocationDao()
.getByKeyword("5000")
.test()
.assertValue { list: List<BranchLocation> -> list.count() == 0 }
}
fun setupTestData() {
var data1 = BranchLocation(id = 1, postcode = "3190", locality = "Highett", state = "VIC", category = "A")
var data2 = BranchLocation(id = 2, postcode = "2000", locality = "Sydney", state = "NSW", category = "A")
var data3 = BranchLocation(id = 3, postcode = "4000", locality = "Brisbane", state = "QLD", category = "A")
db.branchLocationDao().insert(data1)
db.branchLocationDao().insert(data2)
db.branchLocationDao().insert(data3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment