This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ColumnInfo(name = "address") | |
var address: String? = null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Database( | |
entities = [User::class], | |
version = 3, | |
autoMigrations = [ | |
AutoMigration( | |
from = 2, to = 3, | |
spec = UserDatabase.RenameFromUserAddressToAddress::class | |
), | |
], | |
exportSchema = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface MovieDao { | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
suspend fun addMovies(movies: List<Movie>) | |
@Query("SELECT * FROM movies") | |
fun getAllMovies(): PagingSource<Int, Movie> | |
@Query("SELECT * FROM movies WHERE movieId = :movieId") | |
fun getMovie(movieId: Int): Flow<Movie> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Dao | |
interface MovieRemoteKeysDao { | |
@Query("SELECT * FROM movie_remote_keys WHERE id = :movieId") | |
suspend fun getMovieRemoteKeys(movieId: Int): MovieRemoteKeys? | |
@Insert(onConflict = OnConflictStrategy.REPLACE) | |
suspend fun addAllMovieRemoteKeys(movieRemoteKeys : List<MovieRemoteKeys>) | |
@Query("DELETE FROM movie_remote_keys") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface MovieLocalDataSource { | |
fun getMoviesFromDB(movieId : Int): Flow<Movie> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MovieRemoteDataSourceImpl(private val movieApi: MovieApi, private val movieDB: MovieDB) : | |
MovieRemoteDataSource { | |
private val movieDao = movieDB.movieDao() | |
@OptIn(ExperimentalPagingApi::class) | |
override fun getPopularMovies(): Flow<PagingData<Movie>> { | |
val pagingSourceFactory = { movieDao.getAllMovies() } | |
return Pager( | |
config = PagingConfig(pageSize = 20), | |
remoteMediator = MovieRemoteMediator( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ImageApiService { | |
@GET("v1/search") | |
suspend fun getSearchedImage( | |
@Query("query") | |
searchQuery:String, | |
@Query("per_page") | |
page:Int | |
): Response<APIResponse> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lateinit var service: ImageApiService | |
private lateinit var server: MockWebServer | |
@Before | |
fun setUp() { | |
server = MockWebServer() | |
service = Retrofit.Builder() | |
.baseUrl(server.url(""))//We will use MockWebServers url | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun getSearchedResult_sentRequest_receivedExpected() { | |
runBlocking { | |
// Prepare fake response | |
enqueueMockResponse("ImageResponse.json") | |
//Send Request to the MockServer | |
val responseBody = service.getSearchedImage("nature", 5).body() | |
//Request received by the mock server | |
val request = server.takeRequest() | |
assertThat(responseBody).isNotNull() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Mock Server | |
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.0' | |
//Truth | |
testImplementation "com.google.truth:truth:1.1" | |
//okhttp | |
implementation 'com.squareup.okhttp3:okhttp:4.9.1' | |
testImplementation 'junit:junit:4.13.2' |