Skip to content

Commit f116aed

Browse files
committed
TEST: implement test of data access object (RemindersDao)
1 parent 50970de commit f116aed

File tree

1 file changed

+112
-4
lines changed
  • app/src/androidTest/java/com/udacity/locationreminder/locationreminders/data/local

1 file changed

+112
-4
lines changed
Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,126 @@
11
package com.udacity.locationreminder.locationreminders.data.local
22

3+
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+
import androidx.room.Room
5+
import androidx.test.core.app.ApplicationProvider
36
import androidx.test.ext.junit.runners.AndroidJUnit4
47
import androidx.test.filters.SmallTest
5-
6-
import org.junit.runner.RunWith
7-
8+
import com.udacity.locationreminder.locationreminders.data.dto.ReminderDTO
89
import kotlinx.coroutines.ExperimentalCoroutinesApi
10+
import kotlinx.coroutines.test.runBlockingTest
11+
import org.hamcrest.MatcherAssert.assertThat
12+
import org.hamcrest.Matchers.*
13+
import org.junit.After
14+
import org.junit.Before
15+
import org.junit.Rule
16+
import org.junit.Test
17+
import org.junit.runner.RunWith
918

1019
@ExperimentalCoroutinesApi
1120
@RunWith(AndroidJUnit4::class)
1221
//Unit test the DAO
1322
@SmallTest
1423
class RemindersDaoTest {
1524

16-
// TODO: Add testing implementation to the RemindersDao.kt
25+
@get:Rule
26+
var instantTaskExecutorRule = InstantTaskExecutorRule()
27+
28+
private lateinit var database: RemindersDatabase
29+
30+
@Before
31+
fun initDatabase() {
32+
database = Room.inMemoryDatabaseBuilder(
33+
ApplicationProvider.getApplicationContext(),
34+
RemindersDatabase::class.java
35+
).build()
36+
}
37+
38+
@After
39+
fun closeDatabase() {
40+
database.close()
41+
}
42+
43+
private val reminders = listOf(
44+
ReminderDTO(
45+
title = "Title of reminder 1",
46+
description = "Description of reminder 1",
47+
location = "User defined location",
48+
latitude = 10.0,
49+
longitude = 10.0
50+
),
51+
ReminderDTO(
52+
title = "Title of reminder 2",
53+
description = "Description of reminder 2",
54+
location = "User defined location",
55+
latitude = 20.0,
56+
longitude = 20.0
57+
),
58+
)
59+
60+
@Test
61+
fun getReminders_returnsCorrectData() = runBlockingTest {
62+
database.reminderDao().saveReminder(reminders[0])
63+
database.reminderDao().saveReminder(reminders[1])
64+
65+
val actual = database.reminderDao().getReminders()
66+
67+
assertThat(actual.size, `is`(2))
68+
assertThat(actual, hasItem(reminders[0]))
69+
assertThat(actual, hasItem(reminders[1]))
70+
}
71+
72+
@Test
73+
fun getReminders_noData_returnsNotNull() = runBlockingTest {
74+
val actual = database.reminderDao().getReminders()
75+
assertThat(actual, notNullValue())
76+
assertThat(actual.isEmpty(), `is`(true))
77+
}
78+
79+
@Test
80+
fun getReminderById_returnsCorrectData() = runBlockingTest {
81+
val reminder = reminders[0]
82+
database.reminderDao().saveReminder(reminder)
83+
84+
val actual = database.reminderDao().getReminderById(reminder.id)
85+
86+
assertThat(actual?.id, `is`(reminder.id))
87+
assertThat(actual?.title, `is`(reminder.title))
88+
assertThat(actual?.description, `is`(reminder.description))
89+
assertThat(actual?.location, `is`(reminder.location))
90+
assertThat(actual?.latitude, `is`(reminder.latitude))
91+
assertThat(actual?.longitude, `is`(reminder.longitude))
92+
}
93+
94+
@Test
95+
fun deleteAllReminders_returnsNoData() = runBlockingTest {
96+
database.reminderDao().saveReminder(reminders[0])
97+
database.reminderDao().saveReminder(reminders[1])
98+
99+
var actual = database.reminderDao().getReminders()
100+
assertThat(actual.size, `is`(2))
101+
assertThat(actual, hasItem(reminders[0]))
102+
assertThat(actual, hasItem(reminders[1]))
103+
104+
database.reminderDao().deleteAllReminders()
105+
106+
actual = database.reminderDao().getReminders()
107+
assertThat(actual.isEmpty(), `is`(true))
108+
}
109+
110+
@Test
111+
fun deleteReminderById_returnsNoData() = runBlockingTest {
112+
database.reminderDao().saveReminder(reminders[0])
113+
database.reminderDao().saveReminder(reminders[1])
114+
115+
var actual = database.reminderDao().getReminders()
116+
assertThat(actual.size, `is`(2))
117+
assertThat(actual, hasItem(reminders[0]))
118+
assertThat(actual, hasItem(reminders[1]))
119+
120+
database.reminderDao().deleteReminderById(reminders[0].id)
17121

122+
actual = database.reminderDao().getReminders()
123+
assertThat(actual.size, `is`(1))
124+
assertThat(actual, hasItem(reminders[1]))
125+
}
18126
}

0 commit comments

Comments
 (0)