|
| 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.mockImageToImagePayload |
| 6 | +import com.shifthackz.aisdv1.data.mocks.mockTextToImagePayload |
| 7 | +import com.shifthackz.aisdv1.domain.datasource.GenerationResultDataSource |
| 8 | +import com.shifthackz.aisdv1.domain.datasource.HordeGenerationDataSource |
| 9 | +import com.shifthackz.aisdv1.domain.entity.HordeProcessStatus |
| 10 | +import com.shifthackz.aisdv1.domain.gateway.MediaStoreGateway |
| 11 | +import com.shifthackz.aisdv1.domain.preference.PreferenceManager |
| 12 | +import io.mockk.every |
| 13 | +import io.mockk.mockk |
| 14 | +import io.reactivex.rxjava3.core.BackpressureStrategy |
| 15 | +import io.reactivex.rxjava3.core.Completable |
| 16 | +import io.reactivex.rxjava3.core.Flowable |
| 17 | +import io.reactivex.rxjava3.core.Single |
| 18 | +import io.reactivex.rxjava3.subjects.BehaviorSubject |
| 19 | +import org.junit.Before |
| 20 | +import org.junit.Test |
| 21 | + |
| 22 | +class HordeGenerationRepositoryImplTest { |
| 23 | + |
| 24 | + private val stubException = Throwable("Something went wrong.") |
| 25 | + private val stubStatus = BehaviorSubject.create<HordeProcessStatus>() |
| 26 | + private val stubMediaStoreGateway = mockk<MediaStoreGateway>() |
| 27 | + private val stubBase64ToBitmapConverter = mockk<Base64ToBitmapConverter>() |
| 28 | + private val stubLocalDataSource = mockk<GenerationResultDataSource.Local>() |
| 29 | + private val stubPreferenceManager = mockk<PreferenceManager>() |
| 30 | + private val stubRemoteDataSource = mockk<HordeGenerationDataSource.Remote>() |
| 31 | + private val stubStatusSource = mockk<HordeGenerationDataSource.StatusSource>() |
| 32 | + |
| 33 | + private val repository = HordeGenerationRepositoryImpl( |
| 34 | + mediaStoreGateway = stubMediaStoreGateway, |
| 35 | + base64ToBitmapConverter = stubBase64ToBitmapConverter, |
| 36 | + localDataSource = stubLocalDataSource, |
| 37 | + preferenceManager = stubPreferenceManager, |
| 38 | + remoteDataSource = stubRemoteDataSource, |
| 39 | + statusSource = stubStatusSource, |
| 40 | + ) |
| 41 | + |
| 42 | + @Before |
| 43 | + fun initialize() { |
| 44 | + every { |
| 45 | + stubStatusSource.observe() |
| 46 | + } returns stubStatus.toFlowable(BackpressureStrategy.LATEST) |
| 47 | + |
| 48 | + every { |
| 49 | + stubPreferenceManager.autoSaveAiResults |
| 50 | + } returns false |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `given attempt to observe status, status source emits two values, expected valid values in same order`() { |
| 55 | + val stubObserver = repository.observeStatus().test() |
| 56 | + |
| 57 | + stubStatus.onNext(HordeProcessStatus(5598, 1504)) |
| 58 | + |
| 59 | + stubObserver |
| 60 | + .assertNoErrors() |
| 61 | + .assertValueAt(0, HordeProcessStatus(5598, 1504)) |
| 62 | + |
| 63 | + stubStatus.onNext(HordeProcessStatus(0, 0)) |
| 64 | + |
| 65 | + stubObserver |
| 66 | + .assertNoErrors() |
| 67 | + .assertValueAt(1, HordeProcessStatus(0, 0)) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `given attempt to observe status, status source throws exception, expected error value`() { |
| 72 | + every { |
| 73 | + stubStatusSource.observe() |
| 74 | + } returns Flowable.error(stubException) |
| 75 | + |
| 76 | + repository |
| 77 | + .observeStatus() |
| 78 | + .test() |
| 79 | + .assertError(stubException) |
| 80 | + .assertNoValues() |
| 81 | + .await() |
| 82 | + .assertNotComplete() |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `given attempt to validate api key, remote returns true, expected true value`() { |
| 87 | + every { |
| 88 | + stubRemoteDataSource.validateApiKey() |
| 89 | + } returns Single.just(true) |
| 90 | + |
| 91 | + repository |
| 92 | + .validateApiKey() |
| 93 | + .test() |
| 94 | + .assertNoErrors() |
| 95 | + .assertValue(true) |
| 96 | + .await() |
| 97 | + .assertComplete() |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `given attempt to validate api key, remote returns false, expected false value`() { |
| 102 | + every { |
| 103 | + stubRemoteDataSource.validateApiKey() |
| 104 | + } returns Single.just(false) |
| 105 | + |
| 106 | + repository |
| 107 | + .validateApiKey() |
| 108 | + .test() |
| 109 | + .assertNoErrors() |
| 110 | + .assertValue(false) |
| 111 | + .await() |
| 112 | + .assertComplete() |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `given attempt to validate api key, remote throws exception, expected error value`() { |
| 117 | + every { |
| 118 | + stubRemoteDataSource.validateApiKey() |
| 119 | + } returns Single.error(stubException) |
| 120 | + |
| 121 | + repository |
| 122 | + .validateApiKey() |
| 123 | + .test() |
| 124 | + .assertError(stubException) |
| 125 | + .assertNoValues() |
| 126 | + .await() |
| 127 | + .assertNotComplete() |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + fun `given attempt to generate from text, remote returns result, expected valid domain model value`() { |
| 132 | + every { |
| 133 | + stubRemoteDataSource.textToImage(any()) |
| 134 | + } returns Single.just(mockAiGenerationResult) |
| 135 | + |
| 136 | + repository |
| 137 | + .generateFromText(mockTextToImagePayload) |
| 138 | + .test() |
| 139 | + .assertNoErrors() |
| 140 | + .assertValue(mockAiGenerationResult) |
| 141 | + .await() |
| 142 | + .assertComplete() |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun `given attempt to generate from text, remote throws exception, expected error value`() { |
| 147 | + every { |
| 148 | + stubRemoteDataSource.textToImage(any()) |
| 149 | + } returns Single.error(stubException) |
| 150 | + |
| 151 | + repository |
| 152 | + .generateFromText(mockTextToImagePayload) |
| 153 | + .test() |
| 154 | + .assertError(stubException) |
| 155 | + .assertNoValues() |
| 156 | + .await() |
| 157 | + .assertNotComplete() |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + fun `given attempt to generate from image, remote returns result, expected valid domain model value`() { |
| 162 | + every { |
| 163 | + stubRemoteDataSource.imageToImage(any()) |
| 164 | + } returns Single.just(mockAiGenerationResult) |
| 165 | + |
| 166 | + repository |
| 167 | + .generateFromImage(mockImageToImagePayload) |
| 168 | + .test() |
| 169 | + .assertNoErrors() |
| 170 | + .assertValue(mockAiGenerationResult) |
| 171 | + .await() |
| 172 | + .assertComplete() |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + fun `given attempt to generate from image, remote throws exception, expected error value`() { |
| 177 | + every { |
| 178 | + stubRemoteDataSource.imageToImage(any()) |
| 179 | + } returns Single.error(stubException) |
| 180 | + |
| 181 | + repository |
| 182 | + .generateFromImage(mockImageToImagePayload) |
| 183 | + .test() |
| 184 | + .assertError(stubException) |
| 185 | + .assertNoValues() |
| 186 | + .await() |
| 187 | + .assertNotComplete() |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + fun `given attempt to interrupt generation, remote completes, expected complete value`() { |
| 192 | + every { |
| 193 | + stubRemoteDataSource.interruptGeneration() |
| 194 | + } returns Completable.complete() |
| 195 | + |
| 196 | + repository |
| 197 | + .interruptGeneration() |
| 198 | + .test() |
| 199 | + .assertNoErrors() |
| 200 | + .await() |
| 201 | + .assertComplete() |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + fun `given attempt to interrupt generation, remote throws exception, expected error value`() { |
| 206 | + every { |
| 207 | + stubRemoteDataSource.interruptGeneration() |
| 208 | + } returns Completable.error(stubException) |
| 209 | + |
| 210 | + repository |
| 211 | + .interruptGeneration() |
| 212 | + .test() |
| 213 | + .assertError(stubException) |
| 214 | + .await() |
| 215 | + .assertNotComplete() |
| 216 | + } |
| 217 | +} |
0 commit comments