Skip to content

Commit 3e62016

Browse files
committed
[ADD]
- TestCollectionData.kt [Commit] - MapperUnitTest.kt - PlaceRepoUnitTest.kt - DataBaseUnitTest.kt [Modify] - EatDao.kt, DatabaseRepositoryImpl.kt - gradle.properties - build.gradle.kts
1 parent 11962fa commit 3e62016

File tree

10 files changed

+130
-44
lines changed

10 files changed

+130
-44
lines changed

core/data/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ dependencies {
2626
implementation(project(Module.model))
2727

2828
testImplementation(Dependencies.junit)
29-
androidTestImplementation(Dependencies.androidx_junit)
3029

30+
androidTestImplementation(Dependencies.androidx_junit)
3131

3232
implementation(Dependencies.hilt)
3333
ksp(Dependencies.hilt_compiler)

core/data/src/main/java/com/example/data/repository/DatabaseRepositoryImpl.kt

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.example.data.util.entityToModelMapper
55
import com.example.data.util.modelToEntityMapper
66
import com.example.model.feature.CollectionModel
77
import com.example.database.dao.EatDao
8-
import com.example.database.model.CollectionEntity
98
import kotlinx.coroutines.CoroutineDispatcher
109
import kotlinx.coroutines.flow.Flow
1110
import kotlinx.coroutines.flow.catch
@@ -44,9 +43,7 @@ class DatabaseRepositoryImpl @Inject constructor(
4443
override fun saveUserCollection(
4544
content: CollectionModel
4645
): Flow<Result<Unit>> = flow {
47-
val entity: CollectionEntity = modelToEntityMapper(content)
48-
49-
eatDao.saveUserCollection(entity)
46+
eatDao.saveUserCollection(modelToEntityMapper(content))
5047
emit(Result.success(Unit))
5148
}.catch { exception ->
5249
when (exception) {
@@ -60,9 +57,7 @@ class DatabaseRepositoryImpl @Inject constructor(
6057
override fun deleteUserCollection(
6158
content: CollectionModel
6259
): Flow<Result<Unit>> = flow {
63-
val entity: CollectionEntity = modelToEntityMapper(content)
64-
65-
eatDao.deleteUserCollection(entity)
60+
eatDao.deleteUserCollection(modelToEntityMapper(content))
6661
emit(Result.success(Unit))
6762
}.catch { exception ->
6863
when (exception) {
@@ -72,5 +67,4 @@ class DatabaseRepositoryImpl @Inject constructor(
7267
else -> emit(Result.failure(exception))
7368
}
7469
}.flowOn(ioDispatcher)
75-
7670
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
package com.example.data
22

3-
import org.junit.Before
3+
import com.example.data.util.entityToModelMapper
4+
import com.example.data.util.modelToEntityMapper
5+
import com.example.database.model.CollectionEntity
6+
import com.example.model.feature.CollectionModel
7+
import org.junit.Test
8+
import org.junit.Assert.assertEquals
49

510
/**
611
* Mapper local unit test
712
*/
813
class MapperUnitTest {
14+
private val collectionEntity: CollectionEntity = CollectionEntity(
15+
placeID = "dummy",
16+
placeName = "dummy",
17+
placeLatLng = "dummy",
18+
placeImgUrl = "dummy"
19+
)
20+
private val collectionModel: CollectionModel = CollectionModel(
21+
id = "dummy",
22+
name = "dummy",
23+
latLng = "dummy",
24+
imgUrl = "dummy"
25+
)
926

10-
@Before
11-
fun init() {
1227

28+
@Test
29+
fun mapping_ENTITY_TO_MODEL() {
30+
val mappingResult = entityToModelMapper(collectionEntity)
31+
assertEquals(collectionModel, mappingResult)
32+
}
33+
34+
@Test
35+
fun mapping_MODEL_TO_ENTITY() {
36+
val mappingResult = modelToEntityMapper(collectionModel)
37+
assertEquals(collectionEntity, mappingResult)
1338
}
1439
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
package com.example.data
22

3+
4+
/**
5+
* PlaceRepo local unit test
6+
*/
37
class PlaceRepoUnitTest {
8+
49
}

core/database/build.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ android {
1515
minSdk = AppConfig.minSdk
1616
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1717
}
18+
19+
testOptions.unitTests.isIncludeAndroidResources = true
1820
}
1921

2022
dependencies {
@@ -28,5 +30,9 @@ dependencies {
2830
ksp(Dependencies.hilt_compiler)
2931

3032
testImplementation(Dependencies.junit)
33+
testImplementation("androidx.test.ext:junit-ktx:1.1.5")
34+
testImplementation("androidx.test:core-ktx:1.5.0")
35+
testImplementation("org.robolectric:robolectric:4.11.1")
36+
testImplementation(Dependencies.coroutine_test)
3137
androidTestImplementation(Dependencies.androidx_junit)
3238
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +0,0 @@
1-
package com.example.database
2-
3-
import androidx.test.platform.app.InstrumentationRegistry
4-
import androidx.test.ext.junit.runners.AndroidJUnit4
5-
6-
import org.junit.Test
7-
import org.junit.runner.RunWith
8-
9-
import org.junit.Assert.*
10-
11-
/**
12-
* Instrumented test, which will execute on an Android device.
13-
*
14-
* See [testing documentation](http://d.android.com/tools/testing).
15-
*/
16-
@RunWith(AndroidJUnit4::class)
17-
class ExampleInstrumentedTest {
18-
@Test
19-
fun useAppContext() {
20-
// Context of the app under test.
21-
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22-
assertEquals("com.example.database.test", appContext.packageName)
23-
}
24-
}

core/database/src/main/java/com/example/database/dao/EatDao.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ interface EatDao {
1717
fun readCollectionEntity(placeID: String): Flow<CollectionEntity>
1818

1919
@Insert(onConflict = OnConflictStrategy.REPLACE)
20-
fun saveUserCollection(content: CollectionEntity)
20+
suspend fun saveUserCollection(content: CollectionEntity)
2121

2222
@Delete(entity = CollectionEntity::class)
23-
fun deleteUserCollection(content: CollectionEntity)
23+
suspend fun deleteUserCollection(content: CollectionEntity)
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,66 @@
11
package com.example.database
22

3-
/**
4-
* Example local unit test, which will execute on the development machine (host).
5-
*
6-
* See [testing documentation](http://d.android.com/tools/testing).
7-
*/
3+
import android.content.Context
4+
import androidx.room.Room
5+
import androidx.test.core.app.ApplicationProvider
6+
import androidx.test.ext.junit.runners.AndroidJUnit4
7+
import com.example.database.dao.EatDao
8+
import com.example.database.data.defaultEntityList
9+
import kotlinx.coroutines.flow.first
10+
import kotlinx.coroutines.test.runTest
11+
import org.junit.After
12+
import org.junit.Assert.*
13+
import org.junit.Before
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import java.lang.Exception
17+
import kotlin.jvm.Throws
18+
19+
@RunWith(AndroidJUnit4::class)
820
class DataBaseUnitTest {
21+
private lateinit var eatDAO: EatDao
22+
private lateinit var eatDataBase: EatDataBase
23+
24+
@Before
25+
fun createEatDB() {
26+
val context: Context = ApplicationProvider.getApplicationContext()
27+
28+
eatDataBase = Room.inMemoryDatabaseBuilder(
29+
context = context,
30+
klass = EatDataBase::class.java
31+
).build()
32+
33+
eatDAO = eatDataBase.eatDao()
34+
}
35+
36+
@After
37+
fun closeEatDB() {
38+
eatDataBase.close()
39+
}
40+
41+
@Test
42+
@Throws(Exception::class)
43+
fun save_COLLECTION_INTO_TEST_DATABASE() = runTest {
44+
eatDAO.saveUserCollection(defaultEntityList[0])
45+
eatDAO.saveUserCollection(defaultEntityList[1])
46+
eatDAO.saveUserCollection(defaultEntityList[2])
47+
48+
val result = eatDAO.readCollectionEntity(defaultEntityList[0].placeID).first()
49+
50+
assertEquals(defaultEntityList[0], result)
51+
}
52+
53+
@Test
54+
@Throws(Exception::class)
55+
fun delete_COLLECTION_INTO_TEST_DATABASE() = runTest {
56+
eatDAO.saveUserCollection(defaultEntityList[0])
57+
eatDAO.saveUserCollection(defaultEntityList[1])
58+
eatDAO.saveUserCollection(defaultEntityList[2])
59+
60+
eatDAO.deleteUserCollection(defaultEntityList[0])
61+
62+
val result = eatDAO.readAllCollectionEntities().first()
963

64+
assertEquals(listOf(defaultEntityList[1], defaultEntityList[2]), result)
65+
}
1066
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.database.data
2+
3+
import com.example.database.model.CollectionEntity
4+
5+
val defaultEntityList = listOf(
6+
CollectionEntity(
7+
placeID = "1",
8+
placeName = "default",
9+
placeLatLng = "default",
10+
placeImgUrl = "default"
11+
),
12+
CollectionEntity(
13+
placeID = "2",
14+
placeName = "default",
15+
placeLatLng = "default",
16+
placeImgUrl = "default"
17+
),
18+
CollectionEntity(
19+
placeID = "3",
20+
placeName = "default",
21+
placeLatLng = "default",
22+
placeImgUrl = "default"
23+
)
24+
)

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
android.enableJetifier=true
22
android.nonFinalResIds=false
3-
android.nonTransitiveRClass=false
3+
android.nonTransitiveRClass=true
44
android.useAndroidX=true
55
kotlin.code.style=official
66
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding\=UTF-8

0 commit comments

Comments
 (0)