1
1
package com.example.database
2
2
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 )
8
20
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()
9
63
64
+ assertEquals(listOf (defaultEntityList[1 ], defaultEntityList[2 ]), result)
65
+ }
10
66
}
0 commit comments