Skip to content

Commit

Permalink
test: Added FakeBooksRepository and FakeBookUiState
Browse files Browse the repository at this point in the history
  • Loading branch information
donald-okara committed Jan 21, 2025
1 parent d5ba918 commit 3b50829
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BooksRepositoryImpl(
}


private fun updateBookState(newState: BookUiState) {
override fun updateBookState(newState: BookUiState) {
_bookState.update { newState }
}

Expand All @@ -62,7 +62,8 @@ class BooksRepositoryImpl(
response.body()?.let { volumeData ->
// Extract the highest image URL from the book details
val highestImageUrl = volumeData.volumeInfo.imageLinks.let {
it.extraLarge ?: it.large ?: it.medium ?: it.small ?: it.thumbnail ?: it.smallThumbnail
it.extraLarge ?: it.large ?: it.medium ?: it.small ?: it.thumbnail
?: it.smallThumbnail
}?.replace("http", "https")


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.don.preface.domain.repositories

import com.don.preface.data.model.BookListItemResponse
import com.don.preface.data.model.BookDetailsResponse
import com.don.preface.domain.states.BookUiState
import kotlinx.coroutines.flow.StateFlow
import retrofit2.Response
Expand All @@ -13,5 +12,7 @@ interface BooksRepository {

suspend fun getBookDetails(bookId: String)

fun updateBookState(newState: BookUiState)

}

21 changes: 21 additions & 0 deletions app/src/test/java/com/don/preface/fake/data/FakeBookUiState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.don.preface.fake.data

import com.don.preface.domain.states.BookUiState
import com.don.preface.domain.states.ResultState
import com.don.preface.domain.utils.color_utils.model.ColorPallet
import com.don.preface.fake.data.FakeBookDetailsDataSource.fakeBookDetailsResponse

object FakeBookUiState {

val fakeBookUiStateSuccess: BookUiState =
BookUiState(
bookDetails = fakeBookDetailsResponse,
resultState = ResultState.Success,
highestImageUrl = "https://example.com/highest_image.jpg",
colorPallet = ColorPallet()
)
val fakeBookUiStateError: BookUiState =
BookUiState(
resultState = ResultState.Error("Error message")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import com.don.preface.network.GoogleBooksApi
import retrofit2.Response

class FakeBookApiService : GoogleBooksApi {
override suspend fun searchBooks(query: String): Response<BookListItemResponse> = FakeBooksDataSource.fakeBookList
override suspend fun searchBooks(
query: String,
apiKey: String
): Response<BookListItemResponse> = FakeBooksDataSource.fakeBookList

override suspend fun getBookDetails(bookId: String): Response<BookDetailsResponse> = FakeBookDetailsDataSource.fakeBookDetails
override suspend fun getBookDetails(
bookId: String,
apiKey: String
): Response<BookDetailsResponse> = FakeBookDetailsDataSource.fakeBookDetails

override suspend fun getUserLibrary(accessToken: String, apiKey: String): LibraryResponse{
TODO()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.don.preface.fake.repository

import com.don.preface.data.model.BookListItemResponse
import com.don.preface.domain.repositories.BooksRepository
import com.don.preface.domain.states.BookUiState
import com.don.preface.fake.data.FakeBookUiState.fakeBookUiStateError
import com.don.preface.fake.data.FakeBookUiState.fakeBookUiStateSuccess
import com.don.preface.fake.data.FakeBooksDataSource
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update
import retrofit2.Response

class FakeBooksRepository: BooksRepository {
private val _bookState = MutableStateFlow(BookUiState())
override val bookUiState: StateFlow<BookUiState> = _bookState

override suspend fun searchBooks(query: String): Response<BookListItemResponse> {
return FakeBooksDataSource.fakeBookList
}

override suspend fun getBookDetails(bookId: String) {
if (bookId == "5cu7sER89nwC") {
_bookState.value = fakeBookUiStateSuccess
}else if(bookId == "exampleErrorId"){
_bookState.value = fakeBookUiStateError

}
}

override fun updateBookState(newState: BookUiState) {
_bookState.update {
newState
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.don.preface.tests.repositories

class BooksRepositoryTest {
}

0 comments on commit 3b50829

Please sign in to comment.