|
| 1 | +package com.shifthackz.aisdv1.data.repository |
| 2 | + |
| 3 | +import com.shifthackz.aisdv1.core.imageprocessing.Base64ToBitmapConverter |
| 4 | +import com.shifthackz.aisdv1.data.mocks.mockAiGenerationResult |
| 5 | +import com.shifthackz.aisdv1.data.mocks.mockAiGenerationResults |
| 6 | +import com.shifthackz.aisdv1.domain.datasource.GenerationResultDataSource |
| 7 | +import com.shifthackz.aisdv1.domain.entity.MediaStoreInfo |
| 8 | +import com.shifthackz.aisdv1.domain.gateway.MediaStoreGateway |
| 9 | +import com.shifthackz.aisdv1.domain.preference.PreferenceManager |
| 10 | +import io.mockk.every |
| 11 | +import io.mockk.mockk |
| 12 | +import io.reactivex.rxjava3.core.Completable |
| 13 | +import io.reactivex.rxjava3.core.Single |
| 14 | +import org.junit.Test |
| 15 | + |
| 16 | +class GenerationResultRepositoryImplTest { |
| 17 | + |
| 18 | + private val stubException = Throwable("Something went wrong.") |
| 19 | + private val stubPreferenceManager = mockk<PreferenceManager>() |
| 20 | + private val stubMediaStoreGateway = mockk<MediaStoreGateway>() |
| 21 | + private val stubBase64ToBitmapConverter = mockk<Base64ToBitmapConverter>() |
| 22 | + private val stubLocalDataSource = mockk<GenerationResultDataSource.Local>() |
| 23 | + |
| 24 | + private val repository = GenerationResultRepositoryImpl( |
| 25 | + preferenceManager = stubPreferenceManager, |
| 26 | + mediaStoreGateway = stubMediaStoreGateway, |
| 27 | + base64ToBitmapConverter = stubBase64ToBitmapConverter, |
| 28 | + localDataSource = stubLocalDataSource, |
| 29 | + ) |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `given attempt to get all, local returns data, expected valid domain model list value`() { |
| 33 | + every { |
| 34 | + stubLocalDataSource.queryAll() |
| 35 | + } returns Single.just(mockAiGenerationResults) |
| 36 | + |
| 37 | + repository |
| 38 | + .getAll() |
| 39 | + .test() |
| 40 | + .assertNoErrors() |
| 41 | + .assertValue(mockAiGenerationResults) |
| 42 | + .await() |
| 43 | + .assertComplete() |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `given attempt to get all, local returns empty data, expected empty domain model list value`() { |
| 48 | + every { |
| 49 | + stubLocalDataSource.queryAll() |
| 50 | + } returns Single.just(emptyList()) |
| 51 | + |
| 52 | + repository |
| 53 | + .getAll() |
| 54 | + .test() |
| 55 | + .assertNoErrors() |
| 56 | + .assertValue(emptyList()) |
| 57 | + .await() |
| 58 | + .assertComplete() |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `given attempt to get all, local throws exception, expected error value`() { |
| 63 | + every { |
| 64 | + stubLocalDataSource.queryAll() |
| 65 | + } returns Single.error(stubException) |
| 66 | + |
| 67 | + repository |
| 68 | + .getAll() |
| 69 | + .test() |
| 70 | + .assertError(stubException) |
| 71 | + .assertNoValues() |
| 72 | + .await() |
| 73 | + .assertNotComplete() |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `given attempt to get page, local returns data, expected valid domain model list value`() { |
| 78 | + every { |
| 79 | + stubLocalDataSource.queryPage(any(), any()) |
| 80 | + } returns Single.just(mockAiGenerationResults) |
| 81 | + |
| 82 | + repository |
| 83 | + .getPage(20, 0) |
| 84 | + .test() |
| 85 | + .assertNoErrors() |
| 86 | + .assertValue(mockAiGenerationResults) |
| 87 | + .await() |
| 88 | + .assertComplete() |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun `given attempt to get page, local returns empty data, expected empty domain model list value`() { |
| 93 | + every { |
| 94 | + stubLocalDataSource.queryPage(any(), any()) |
| 95 | + } returns Single.just(emptyList()) |
| 96 | + |
| 97 | + repository |
| 98 | + .getPage(20, 0) |
| 99 | + .test() |
| 100 | + .assertNoErrors() |
| 101 | + .assertValue(emptyList()) |
| 102 | + .await() |
| 103 | + .assertComplete() |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `given attempt to get page, local throws exception, expected error value`() { |
| 108 | + every { |
| 109 | + stubLocalDataSource.queryPage(any(), any()) |
| 110 | + } returns Single.error(stubException) |
| 111 | + |
| 112 | + repository |
| 113 | + .getPage(20, 0) |
| 114 | + .test() |
| 115 | + .assertError(stubException) |
| 116 | + .assertNoValues() |
| 117 | + .await() |
| 118 | + .assertNotComplete() |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun `given attempt to get media store info, gateway returned data, expected valid media store info value`() { |
| 123 | + every { |
| 124 | + stubMediaStoreGateway.getInfo() |
| 125 | + } returns MediaStoreInfo() |
| 126 | + |
| 127 | + repository |
| 128 | + .getMediaStoreInfo() |
| 129 | + .test() |
| 130 | + .assertNoErrors() |
| 131 | + .assertValue(MediaStoreInfo()) |
| 132 | + .await() |
| 133 | + .assertComplete() |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + fun `given attempt to get media store info, gateway throws exception, expected error value`() { |
| 138 | + every { |
| 139 | + stubMediaStoreGateway.getInfo() |
| 140 | + } throws stubException |
| 141 | + |
| 142 | + repository |
| 143 | + .getMediaStoreInfo() |
| 144 | + .test() |
| 145 | + .assertError(stubException) |
| 146 | + .assertNoValues() |
| 147 | + .await() |
| 148 | + .assertNotComplete() |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `given attempt to get by id, local returns data, expected valid domain model value`() { |
| 153 | + every { |
| 154 | + stubLocalDataSource.queryById(any()) |
| 155 | + } returns Single.just(mockAiGenerationResult) |
| 156 | + |
| 157 | + repository |
| 158 | + .getById(5598L) |
| 159 | + .test() |
| 160 | + .assertNoErrors() |
| 161 | + .assertValue(mockAiGenerationResult) |
| 162 | + .await() |
| 163 | + .assertComplete() |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + fun `given attempt to get by id, local throws exception, expected error value`() { |
| 168 | + every { |
| 169 | + stubLocalDataSource.queryById(any()) |
| 170 | + } returns Single.error(stubException) |
| 171 | + |
| 172 | + repository |
| 173 | + .getById(5598L) |
| 174 | + .test() |
| 175 | + .assertError(stubException) |
| 176 | + .assertNoValues() |
| 177 | + .await() |
| 178 | + .assertNotComplete() |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + fun `given attempt to delete by id, local delete success, expected complete value`() { |
| 183 | + every { |
| 184 | + stubLocalDataSource.deleteById(any()) |
| 185 | + } returns Completable.complete() |
| 186 | + |
| 187 | + repository |
| 188 | + .deleteById(5598L) |
| 189 | + .test() |
| 190 | + .assertNoErrors() |
| 191 | + .await() |
| 192 | + .assertComplete() |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + fun `given attempt to delete by id, local delete fails, expected error value`() { |
| 197 | + every { |
| 198 | + stubLocalDataSource.deleteById(any()) |
| 199 | + } returns Completable.error(stubException) |
| 200 | + |
| 201 | + repository |
| 202 | + .deleteById(5598L) |
| 203 | + .test() |
| 204 | + .assertError(stubException) |
| 205 | + .await() |
| 206 | + .assertNotComplete() |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + fun `given attempt to delete all, local delete success, expected complete value`() { |
| 211 | + every { |
| 212 | + stubLocalDataSource.deleteAll() |
| 213 | + } returns Completable.complete() |
| 214 | + |
| 215 | + repository |
| 216 | + .deleteAll() |
| 217 | + .test() |
| 218 | + .assertNoErrors() |
| 219 | + .await() |
| 220 | + .assertComplete() |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + fun `given attempt to delete all, local delete fails, expected complete value`() { |
| 225 | + every { |
| 226 | + stubLocalDataSource.deleteAll() |
| 227 | + } returns Completable.error(stubException) |
| 228 | + |
| 229 | + repository |
| 230 | + .deleteAll() |
| 231 | + .test() |
| 232 | + .assertError(stubException) |
| 233 | + .await() |
| 234 | + .assertNotComplete() |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + fun `given attempt to insert data, local insert success, expected id of inserted model value`() { |
| 239 | + every { |
| 240 | + stubPreferenceManager.saveToMediaStore |
| 241 | + } returns false |
| 242 | + |
| 243 | + every { |
| 244 | + stubLocalDataSource.insert(any()) |
| 245 | + } returns Single.just(mockAiGenerationResult.id) |
| 246 | + |
| 247 | + repository |
| 248 | + .insert(mockAiGenerationResult) |
| 249 | + .test() |
| 250 | + .assertNoErrors() |
| 251 | + .assertValue(5598L) |
| 252 | + .await() |
| 253 | + .assertComplete() |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + fun `given attempt to insert data, local insert fails, expected error value`() { |
| 258 | + every { |
| 259 | + stubPreferenceManager.saveToMediaStore |
| 260 | + } returns false |
| 261 | + |
| 262 | + every { |
| 263 | + stubLocalDataSource.insert(any()) |
| 264 | + } returns Single.error(stubException) |
| 265 | + |
| 266 | + repository |
| 267 | + .insert(mockAiGenerationResult) |
| 268 | + .test() |
| 269 | + .assertError(stubException) |
| 270 | + .assertNoValues() |
| 271 | + .await() |
| 272 | + .assertNotComplete() |
| 273 | + } |
| 274 | +} |
0 commit comments