|
| 1 | +package pseudoankit.droid.agendamanger.data.repository |
| 2 | + |
| 3 | +import io.mockk.coEvery |
| 4 | +import io.mockk.coVerify |
| 5 | +import io.mockk.mockk |
| 6 | +import kotlinx.coroutines.flow.first |
| 7 | +import kotlinx.coroutines.flow.flowOf |
| 8 | +import kotlinx.coroutines.flow.map |
| 9 | +import kotlinx.coroutines.test.runTest |
| 10 | +import org.junit.Test |
| 11 | +import pseudoankit.droid.agendamanger.data.local.dao.ReminderDao |
| 12 | +import pseudoankit.droid.agendamanger.data.local.entity.ReminderEntity |
| 13 | +import pseudoankit.droid.agendamanger.domain.mapper.ReminderMapper.mapToDomain |
| 14 | +import pseudoankit.droid.agendamanger.domain.mapper.ReminderMapper.sortByAscDateTime |
| 15 | +import pseudoankit.droid.agendamanger.util.reminderEntity |
| 16 | +import java.time.LocalDate |
| 17 | +import java.time.LocalTime |
| 18 | +import kotlin.test.assertEquals |
| 19 | + |
| 20 | +class ReminderRepositoryImplTest { |
| 21 | + |
| 22 | + private val dao = mockk<ReminderDao>() |
| 23 | + private val repository = ReminderRepositoryImpl(dao) |
| 24 | + |
| 25 | + @Test |
| 26 | + fun `getReminders should call correct dao when date is not null`() = runTest { |
| 27 | + val date = LocalDate.MAX |
| 28 | + val reminders = flowOf(listOf(mockk<ReminderEntity>())) |
| 29 | + |
| 30 | + coEvery { dao.getRemindersFlow(date) } returns reminders |
| 31 | + |
| 32 | + repository.getReminders(date) |
| 33 | + |
| 34 | + coVerify(exactly = 1) { dao.getRemindersFlow(date) } |
| 35 | + coVerify(exactly = 0) { dao.getRemindersFlow() } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `getReminders should call correct dao when date is null`() = runTest { |
| 40 | + val date: LocalDate? = null |
| 41 | + val reminders = flowOf(listOf(mockk<ReminderEntity>())) |
| 42 | + |
| 43 | + coEvery { dao.getRemindersFlow() } returns reminders |
| 44 | + |
| 45 | + repository.getReminders(date) |
| 46 | + |
| 47 | + coVerify(exactly = 1) { dao.getRemindersFlow() } |
| 48 | + coVerify(exactly = 0) { dao.getRemindersFlow(any()) } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `getReminders should sort data in asc ordered by dateTime`() = runTest { |
| 53 | + val reminders = flowOf( |
| 54 | + listOf( |
| 55 | + reminderEntity.copy( |
| 56 | + date = LocalDate.of(1011, 12, 14), |
| 57 | + time = LocalTime.of(1, 12, 12) |
| 58 | + ), |
| 59 | + reminderEntity.copy( |
| 60 | + date = LocalDate.of(1011, 12, 2), |
| 61 | + time = LocalTime.of(1, 12, 19) |
| 62 | + ), |
| 63 | + reminderEntity.copy( |
| 64 | + date = LocalDate.of(1011, 12, 19), |
| 65 | + time = LocalTime.of(1, 12, 2) |
| 66 | + ) |
| 67 | + ) |
| 68 | + ) |
| 69 | + coEvery { dao.getRemindersFlow() } returns reminders |
| 70 | + val expected = reminders |
| 71 | + .map { list -> |
| 72 | + list.map { |
| 73 | + it.mapToDomain |
| 74 | + }.sortByAscDateTime |
| 75 | + } |
| 76 | + |
| 77 | + val actual = repository.getReminders(null) |
| 78 | + |
| 79 | + assertEquals(expected.first(), actual.first()) |
| 80 | + } |
| 81 | +} |
0 commit comments