/
SeasonsEpisodesRepositoryTest.kt
231 lines (195 loc) · 8.74 KB
/
SeasonsEpisodesRepositoryTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.tivi.data.repositories
import app.tivi.data.RoomTransactionRunner
import app.tivi.data.daos.EntityInserter
import app.tivi.data.daos.EpisodeWatchEntryDao
import app.tivi.data.daos.EpisodesDao
import app.tivi.data.daos.SeasonsDao
import app.tivi.data.entities.Success
import app.tivi.data.repositories.episodes.EpisodeDataSource
import app.tivi.data.repositories.episodes.LocalSeasonsEpisodesStore
import app.tivi.data.repositories.episodes.SeasonsEpisodesDataSource
import app.tivi.data.repositories.episodes.SeasonsEpisodesRepository
import app.tivi.trakt.TraktAuthState
import app.tivi.utils.BaseDatabaseTest
import app.tivi.utils.insertShow
import app.tivi.utils.s1
import app.tivi.utils.s1_episodes
import app.tivi.utils.s1_id
import app.tivi.utils.s1e1
import app.tivi.utils.s1e1w
import app.tivi.utils.s1e1w2
import app.tivi.utils.s2
import app.tivi.utils.s2_episodes
import app.tivi.utils.s2_id
import app.tivi.utils.s2e1
import app.tivi.utils.showId
import app.tivi.utils.testCoroutineDispatchers
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.`is`
import org.junit.Assert.assertThat
import org.junit.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import javax.inject.Provider
class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
private lateinit var episodeWatchDao: EpisodeWatchEntryDao
private lateinit var seasonsDao: SeasonsDao
private lateinit var episodesDao: EpisodesDao
private lateinit var traktSeasonsDataSource: SeasonsEpisodesDataSource
private lateinit var traktEpisodeDataSource: EpisodeDataSource
private lateinit var tmdbEpisodeDataSource: EpisodeDataSource
private lateinit var localStore: LocalSeasonsEpisodesStore
private lateinit var repository: SeasonsEpisodesRepository
private var loggedInState = TraktAuthState.LOGGED_IN
override fun setup() {
super.setup()
episodeWatchDao = db.episodeWatchesDao()
episodesDao = db.episodesDao()
seasonsDao = db.seasonsDao()
traktSeasonsDataSource = mock(SeasonsEpisodesDataSource::class.java)
traktEpisodeDataSource = mock(EpisodeDataSource::class.java)
tmdbEpisodeDataSource = mock(EpisodeDataSource::class.java)
val txRunner = RoomTransactionRunner(db)
localStore = LocalSeasonsEpisodesStore(EntityInserter(txRunner), txRunner,
seasonsDao, episodesDao, episodeWatchDao, db.lastRequestDao())
repository = SeasonsEpisodesRepository(
testCoroutineDispatchers,
localStore,
traktSeasonsDataSource,
traktEpisodeDataSource,
tmdbEpisodeDataSource,
Provider { loggedInState }
)
// We'll assume that there's a show in the db
insertShow(db)
}
@Test
fun testSyncEpisodeWatches() = runBlocking {
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
// Return a response with 2 items
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId)).thenReturn(
Success(listOf(s1e1 to s1e1w, s1e1 to s1e1w2))
)
// Sync
repository.syncEpisodeWatchesForShow(showId)
// Assert that both are in the db
assertThat(localStore.getEpisodeWatchesForShow(showId), `is`(listOf(s1e1w, s1e1w2)))
}
@Test
fun testEpisodeWatches_sameEntries() = runBlocking {
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
// Insert both the watches
episodeWatchDao.insertAll(s1e1w, s1e1w2)
// Return a response with the same items
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId))
.thenReturn(Success(listOf(s1e1 to s1e1w, s1e1 to s1e1w2)))
// Now re-sync with the same response
repository.syncEpisodeWatchesForShow(showId)
// Assert that both are in the db
assertThat(localStore.getEpisodeWatchesForShow(showId), `is`(listOf(s1e1w, s1e1w2)))
}
@Test
fun testEpisodeWatches_deletesMissing() = runBlocking {
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
// Insert both the watches
episodeWatchDao.insertAll(s1e1w, s1e1w2)
// Return a response with just the second item
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId))
.thenReturn(Success(listOf(s1e1 to s1e1w2)))
// Now re-sync
repository.syncEpisodeWatchesForShow(showId)
// Assert that only the second is in the db
assertThat(localStore.getEpisodeWatchesForShow(showId), `is`(listOf(s1e1w2)))
}
@Test
fun testEpisodeWatches_emptyResponse() = runBlocking {
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
// Insert both the watches
episodeWatchDao.insertAll(s1e1w, s1e1w2)
// Return a empty response
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId)).thenReturn(Success(emptyList()))
// Now re-sync
repository.syncEpisodeWatchesForShow(showId)
// Assert that the database is empty
assertThat(localStore.getEpisodeWatchesForShow(showId), `is`(emptyList()))
}
@Test
fun testSyncSeasonsEpisodes() = runBlocking {
// Return a response with 2 items
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to s1_episodes)))
repository.updateSeasonsEpisodes(showId)
// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1)))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(s1_episodes))
}
@Test
fun testSyncSeasonsEpisodes_sameEntries() = runBlocking {
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
// Return a response with the same items
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to s1_episodes)))
repository.updateSeasonsEpisodes(showId)
// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1)))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(s1_episodes))
}
@Test
fun testSyncSeasonsEpisodes_emptyResponse() = runBlocking {
db.seasonsDao().insert(s1)
db.episodesDao().insertAll(s1_episodes)
// Return an empty response
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId)).thenReturn(Success(emptyList()))
repository.updateSeasonsEpisodes(showId)
// Assert the database is empty
assertThat(seasonsDao.seasonsForShowId(showId), `is`(emptyList()))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(emptyList()))
}
@Test
fun testSyncSeasonsEpisodes_deletesMissingSeasons() = runBlocking {
db.seasonsDao().insertAll(s1, s2)
db.episodesDao().insertAll(s1_episodes)
db.episodesDao().insertAll(s2_episodes)
// Return a response with just the first season
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to s1_episodes)))
repository.updateSeasonsEpisodes(showId)
// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1)))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(s1_episodes))
}
@Test
fun testSyncSeasonsEpisodes_deletesMissingEpisodes() = runBlocking {
db.seasonsDao().insertAll(s1, s2)
db.episodesDao().insertAll(s1_episodes)
db.episodesDao().insertAll(s2_episodes)
// Return a response with both seasons, but just a single episodes in each
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to listOf(s1e1), s2 to listOf(s2e1))))
repository.updateSeasonsEpisodes(showId)
// Assert that both are in the db
assertThat(seasonsDao.seasonsForShowId(showId), `is`(listOf(s1, s2)))
assertThat(episodesDao.episodesWithSeasonId(s1_id), `is`(listOf(s1e1)))
assertThat(episodesDao.episodesWithSeasonId(s2_id), `is`(listOf(s2e1)))
}
}