|
| 1 | +package com.shifthackz.aisdv1.data.repository |
| 2 | + |
| 3 | +import com.shifthackz.aisdv1.data.mocks.mockStableDiffusionEmbeddings |
| 4 | +import com.shifthackz.aisdv1.domain.datasource.StableDiffusionEmbeddingsDataSource |
| 5 | +import io.mockk.every |
| 6 | +import io.mockk.mockk |
| 7 | +import io.reactivex.rxjava3.core.Completable |
| 8 | +import io.reactivex.rxjava3.core.Single |
| 9 | +import org.junit.Test |
| 10 | + |
| 11 | +class StableDiffusionEmbeddingsRepositoryImplTest { |
| 12 | + |
| 13 | + private val stubException = Throwable("Something went wrong.") |
| 14 | + private val stubRemoteDataSource = mockk<StableDiffusionEmbeddingsDataSource.Remote>() |
| 15 | + private val stubLocalDataSource = mockk<StableDiffusionEmbeddingsDataSource.Local>() |
| 16 | + |
| 17 | + private val repository = StableDiffusionEmbeddingsRepositoryImpl( |
| 18 | + remoteDataSource = stubRemoteDataSource, |
| 19 | + localDataSource = stubLocalDataSource, |
| 20 | + ) |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `given attempt to fetch embeddings, remote returns data, local insert success, expected complete value`() { |
| 24 | + every { |
| 25 | + stubRemoteDataSource.fetchEmbeddings() |
| 26 | + } returns Single.just(mockStableDiffusionEmbeddings) |
| 27 | + |
| 28 | + every { |
| 29 | + stubLocalDataSource.insertEmbeddings(any()) |
| 30 | + } returns Completable.complete() |
| 31 | + |
| 32 | + repository |
| 33 | + .fetchEmbeddings() |
| 34 | + .test() |
| 35 | + .assertNoErrors() |
| 36 | + .await() |
| 37 | + .assertComplete() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `given attempt to fetch embeddings, remote throws exception, local insert success, expected error value`() { |
| 42 | + every { |
| 43 | + stubRemoteDataSource.fetchEmbeddings() |
| 44 | + } returns Single.error(stubException) |
| 45 | + |
| 46 | + every { |
| 47 | + stubLocalDataSource.insertEmbeddings(any()) |
| 48 | + } returns Completable.complete() |
| 49 | + |
| 50 | + repository |
| 51 | + .fetchEmbeddings() |
| 52 | + .test() |
| 53 | + .assertError(stubException) |
| 54 | + .await() |
| 55 | + .assertNotComplete() |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `given attempt to fetch embeddings, remote returns data, local insert fails, expected error value`() { |
| 60 | + every { |
| 61 | + stubRemoteDataSource.fetchEmbeddings() |
| 62 | + } returns Single.just(mockStableDiffusionEmbeddings) |
| 63 | + |
| 64 | + every { |
| 65 | + stubLocalDataSource.insertEmbeddings(any()) |
| 66 | + } returns Completable.error(stubException) |
| 67 | + |
| 68 | + repository |
| 69 | + .fetchEmbeddings() |
| 70 | + .test() |
| 71 | + .assertError(stubException) |
| 72 | + .await() |
| 73 | + .assertNotComplete() |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `given attempt to get embeddings, local data source returns list, expected valid domain models list value`() { |
| 78 | + every { |
| 79 | + stubLocalDataSource.getEmbeddings() |
| 80 | + } returns Single.just(mockStableDiffusionEmbeddings) |
| 81 | + |
| 82 | + repository |
| 83 | + .getEmbeddings() |
| 84 | + .test() |
| 85 | + .assertNoErrors() |
| 86 | + .assertValue(mockStableDiffusionEmbeddings) |
| 87 | + .await() |
| 88 | + .assertComplete() |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun `given attempt to get embeddings, local data source returns empty list, expected empty domain models list value`() { |
| 93 | + every { |
| 94 | + stubLocalDataSource.getEmbeddings() |
| 95 | + } returns Single.just(emptyList()) |
| 96 | + |
| 97 | + repository |
| 98 | + .getEmbeddings() |
| 99 | + .test() |
| 100 | + .assertNoErrors() |
| 101 | + .assertValue(emptyList()) |
| 102 | + .await() |
| 103 | + .assertComplete() |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `given attempt to get embeddings, local data source throws exception, expected error value`() { |
| 108 | + every { |
| 109 | + stubLocalDataSource.getEmbeddings() |
| 110 | + } returns Single.error(stubException) |
| 111 | + |
| 112 | + repository |
| 113 | + .getEmbeddings() |
| 114 | + .test() |
| 115 | + .assertError(stubException) |
| 116 | + .assertNoValues() |
| 117 | + .await() |
| 118 | + .assertNotComplete() |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun `given attempt to fetch and get embeddings, remote returns data, local returns data, expected valid domain models list value`() { |
| 123 | + every { |
| 124 | + stubRemoteDataSource.fetchEmbeddings() |
| 125 | + } returns Single.just(mockStableDiffusionEmbeddings) |
| 126 | + |
| 127 | + every { |
| 128 | + stubLocalDataSource.insertEmbeddings(any()) |
| 129 | + } returns Completable.complete() |
| 130 | + |
| 131 | + every { |
| 132 | + stubLocalDataSource.getEmbeddings() |
| 133 | + } returns Single.just(mockStableDiffusionEmbeddings) |
| 134 | + |
| 135 | + repository |
| 136 | + .fetchAndGetEmbeddings() |
| 137 | + .test() |
| 138 | + .assertNoErrors() |
| 139 | + .assertValue(mockStableDiffusionEmbeddings) |
| 140 | + .await() |
| 141 | + .assertComplete() |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun `given attempt to fetch and get embeddings, remote fails, local returns data, expected valid domain models list value`() { |
| 146 | + every { |
| 147 | + stubRemoteDataSource.fetchEmbeddings() |
| 148 | + } returns Single.error(stubException) |
| 149 | + |
| 150 | + every { |
| 151 | + stubLocalDataSource.insertEmbeddings(any()) |
| 152 | + } returns Completable.complete() |
| 153 | + |
| 154 | + every { |
| 155 | + stubLocalDataSource.getEmbeddings() |
| 156 | + } returns Single.just(mockStableDiffusionEmbeddings) |
| 157 | + |
| 158 | + repository |
| 159 | + .fetchAndGetEmbeddings() |
| 160 | + .test() |
| 161 | + .assertNoErrors() |
| 162 | + .assertValue(mockStableDiffusionEmbeddings) |
| 163 | + .await() |
| 164 | + .assertComplete() |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + fun `given attempt to fetch and get embeddings, remote fails, local fails, expected valid error value`() { |
| 169 | + every { |
| 170 | + stubRemoteDataSource.fetchEmbeddings() |
| 171 | + } returns Single.error(stubException) |
| 172 | + |
| 173 | + every { |
| 174 | + stubLocalDataSource.getEmbeddings() |
| 175 | + } returns Single.error(stubException) |
| 176 | + |
| 177 | + repository |
| 178 | + .fetchAndGetEmbeddings() |
| 179 | + .test() |
| 180 | + .assertError(stubException) |
| 181 | + .assertNoValues() |
| 182 | + .await() |
| 183 | + .assertNotComplete() |
| 184 | + } |
| 185 | +} |
0 commit comments