-
Notifications
You must be signed in to change notification settings - Fork 85
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
1 parent
3156af3
commit 3987da9
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
data/src/test/java/com/android/data/source/local/dao/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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.android.data.source.local.dao | ||
|
||
import com.android.data.TestUtil | ||
import com.android.data.source.local.entity.PhotoEntity | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.hamcrest.CoreMatchers | ||
import org.hamcrest.MatcherAssert | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(JUnit4::class) | ||
class PhotoDaoTest { | ||
|
||
private var photoDao = mockk<PhotoDao>() | ||
|
||
@Test | ||
fun isPhotoListEmpty() { | ||
every { photoDao.loadAll() } returns mutableListOf() | ||
Assert.assertEquals(0, photoDao.loadAll().size) | ||
} | ||
|
||
@Test | ||
fun insertPhoto() { | ||
val photoEntity: PhotoEntity = TestUtil.createPhoto(7) | ||
|
||
every { photoDao.insert(photoEntity) } returns 1L | ||
val insertedPhoto = photoDao.insert(photoEntity) | ||
|
||
Assert.assertNotNull(insertedPhoto) | ||
} | ||
|
||
@Test | ||
fun insertPhotoAndLoadByTitle() { | ||
val photoTitle = "Art" | ||
val photoEntity: PhotoEntity = TestUtil.createPhoto(1).apply { | ||
title = photoTitle | ||
} | ||
every { photoDao.insert(photoEntity) } returns 1L | ||
|
||
every { photoDao.loadOneByPhotoTitle(photoTitle) } returns photoEntity | ||
val photoLoadedByTitle = photoDao.loadOneByPhotoTitle(photoTitle) | ||
|
||
MatcherAssert.assertThat(photoLoadedByTitle, CoreMatchers.equalTo(photoEntity)) | ||
} | ||
|
||
@Test | ||
fun retrievesPhotos() { | ||
val photoEntities = TestUtil.makePhotoList(5) | ||
photoEntities.forEach { photoDao.insert(it) } | ||
|
||
every { photoDao.loadAll() } returns mutableListOf() | ||
val loadedPhotos = photoDao.loadAll() | ||
|
||
Assert.assertEquals(photoEntities, loadedPhotos) | ||
} | ||
|
||
@Test | ||
fun deletePhoto() { | ||
val photoId = 8L | ||
val photoEntity = TestUtil.createPhoto(photoId) | ||
|
||
every { photoDao.delete(photoEntity) } returns Unit | ||
photoDao.delete(photoEntity) | ||
|
||
every { photoDao.loadOneByPhotoId(photoId) } returns null | ||
val loadOneByPhotoId = photoDao.loadOneByPhotoId(photoId) | ||
|
||
Assert.assertNull(loadOneByPhotoId) | ||
} | ||
|
||
@Test | ||
fun deleteAllPhotos() { | ||
every { photoDao.deleteAll() } returns Unit | ||
photoDao.deleteAll() | ||
|
||
every { photoDao.loadAll() } returns mutableListOf() | ||
val loadedAllPhotos = photoDao.loadAll() | ||
|
||
assert(loadedAllPhotos.isEmpty()) | ||
} | ||
} |