-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
103 additions
and
18 deletions.
There are no files selected for viewing
121 changes: 103 additions & 18 deletions
121
app/src/test/java/com/ludovic/vimont/nasaapod/db/PhotoDaoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,132 @@ | ||
package com.ludovic.vimont.nasaapod.db | ||
|
||
import androidx.room.Room | ||
import androidx.test.core.app.ApplicationProvider | ||
import com.ludovic.vimont.nasaapod.MockModel | ||
import com.ludovic.vimont.nasaapod.model.Photo | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.koin.core.context.GlobalContext | ||
import org.koin.test.KoinTest | ||
import org.koin.test.inject | ||
import org.robolectric.RobolectricTestRunner | ||
import kotlin.test.assertEquals | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class PhotoDaoTest : KoinTest { | ||
private val photos = ArrayList<Photo>() | ||
private val photoDao: PhotoDao by inject() | ||
|
||
@Before | ||
fun setUp() { | ||
photos.add(MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-10")) | ||
photos.add(MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-09")) | ||
photos.add(MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-08")) | ||
photos.add(MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-07")) | ||
private val photos = listOf( | ||
MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-10"), | ||
MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-09"), | ||
MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-08"), | ||
MockModel.buildPhoto(mediaType = Photo.IMAGE_MEDIA_TYPE, date = "2020-10-07"), | ||
) | ||
|
||
private val db: PhotoDatabase by lazy { | ||
Room.databaseBuilder( | ||
context = ApplicationProvider.getApplicationContext(), | ||
klass = PhotoDatabase::class.java, | ||
name = "NasaApodDb", | ||
).fallbackToDestructiveMigration().build() | ||
} | ||
|
||
private val photoDao: PhotoDao by lazy { db.photoDao() } | ||
|
||
@After | ||
fun tearDown() { | ||
db.close() | ||
GlobalContext.stopKoin() | ||
} | ||
|
||
@Test | ||
fun testCount() = runBlocking { | ||
Assert.assertEquals(0, photoDao.count()) | ||
fun `count should return stored photos size`() = runBlocking { | ||
// Given | ||
assertEquals( | ||
expected = 0, | ||
actual = photoDao.count(), | ||
) | ||
|
||
// When | ||
photoDao.insert(photos) | ||
Assert.assertEquals(photos.size, photoDao.count()) | ||
|
||
// Then | ||
assertEquals( | ||
expected = photos.size, | ||
actual = photoDao.count(), | ||
) | ||
} | ||
|
||
|
||
@Test | ||
fun testInsert() = runBlocking { | ||
Assert.assertEquals(0, photoDao.getAll().size) | ||
fun `insert should store the given parameter in database`() = runBlocking { | ||
// Given | ||
assertEquals( | ||
expected = 0, | ||
actual = photoDao.getAll().size, | ||
) | ||
|
||
// When | ||
photoDao.insert(photos) | ||
Assert.assertEquals(photos.size, photoDao.getAll().size) | ||
|
||
// Then | ||
assertEquals( | ||
expected = photos.size, | ||
actual = photoDao.getAll().size, | ||
) | ||
} | ||
|
||
@Test | ||
fun `get should return expected photo entity`() = runBlocking { | ||
// Given | ||
val photo = MockModel.buildPhoto() | ||
photoDao.insert(listOf(photo)) | ||
|
||
// When | ||
val result = photoDao.get(photo.date) | ||
|
||
// Then | ||
assertEquals( | ||
expected = photo, | ||
actual = result, | ||
) | ||
} | ||
|
||
@Test | ||
fun `getAll should return all database data`() = runBlocking { | ||
// Given | ||
photoDao.insert(photos) | ||
assertEquals( | ||
expected = photos.size, | ||
actual = photoDao.count(), | ||
) | ||
|
||
// When | ||
val result = photoDao.getAll() | ||
|
||
// Then | ||
assertEquals( | ||
expected = photos, | ||
actual = result, | ||
) | ||
} | ||
|
||
@Test | ||
fun `drop should empty the database data`() = runBlocking { | ||
// Given | ||
photoDao.insert(photos) | ||
assertEquals( | ||
expected = photos.size, | ||
actual = photoDao.count(), | ||
) | ||
|
||
// When | ||
photoDao.drop() | ||
|
||
// Then | ||
assertEquals( | ||
expected = 0, | ||
actual = photoDao.count(), | ||
) | ||
} | ||
|
||
} |