|
1 | 1 | package com.udacity.locationreminder.locationreminders.data.local |
2 | 2 |
|
| 3 | +import androidx.arch.core.executor.testing.InstantTaskExecutorRule |
| 4 | +import androidx.room.Room |
| 5 | +import androidx.test.core.app.ApplicationProvider |
3 | 6 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
4 | 7 | import androidx.test.filters.SmallTest |
5 | | - |
6 | | -import org.junit.runner.RunWith |
7 | | - |
| 8 | +import com.udacity.locationreminder.locationreminders.data.dto.ReminderDTO |
8 | 9 | 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 |
9 | 18 |
|
10 | 19 | @ExperimentalCoroutinesApi |
11 | 20 | @RunWith(AndroidJUnit4::class) |
12 | 21 | //Unit test the DAO |
13 | 22 | @SmallTest |
14 | 23 | class RemindersDaoTest { |
15 | 24 |
|
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) |
17 | 121 |
|
| 122 | + actual = database.reminderDao().getReminders() |
| 123 | + assertThat(actual.size, `is`(1)) |
| 124 | + assertThat(actual, hasItem(reminders[1])) |
| 125 | + } |
18 | 126 | } |
0 commit comments