-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create room db, test DAOs and add android test workflow script
- Loading branch information
1 parent
a6f0c98
commit e8b8f1d
Showing
15 changed files
with
381 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
108 changes: 108 additions & 0 deletions
108
app/src/androidTest/kotlin/data/local/dao/AirportDAOTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package data.local.dao | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.nabilbdev.searchflight.data.local.dao.AirportDAO | ||
import com.nabilbdev.searchflight.data.local.database.SearchFlightDatabase | ||
import com.nabilbdev.searchflight.data.local.entity.Airport | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.After | ||
import org.junit.Assert.* | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.io.IOException | ||
|
||
|
||
private lateinit var airportDAO: AirportDAO | ||
private lateinit var searchFlightDatabase: SearchFlightDatabase | ||
|
||
// Sample data from the actual database | ||
private val airport1 = Airport( | ||
1, | ||
"Francisco Sá Carneiro Airport", | ||
"OPO", | ||
5053134 | ||
) | ||
private val airport2 = Airport( | ||
2, | ||
"Stockholm Arlanda Airport", | ||
"ARN", | ||
7494765 | ||
) | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AirportDAOTest { | ||
|
||
@Before | ||
fun createDB() { | ||
val context: Context = ApplicationProvider.getApplicationContext() | ||
|
||
searchFlightDatabase = | ||
Room.databaseBuilder(context, SearchFlightDatabase::class.java, "flight_search_test") | ||
.createFromAsset("database/flight_search.db") | ||
.allowMainThreadQueries() // Only for testing purposes | ||
.build() | ||
|
||
airportDAO = searchFlightDatabase.airportDAO() | ||
|
||
} | ||
|
||
@After | ||
@Throws(IOException::class) | ||
fun closeDB() { | ||
searchFlightDatabase.close() | ||
} | ||
|
||
@Test | ||
@Throws(IOException::class) | ||
fun daoGetAllAirports_returnAllAirports() = runBlocking { | ||
val allAirports = airportDAO.getAllAirports().first() | ||
|
||
assertTrue(allAirports.isNotEmpty()) | ||
assertEquals(allAirports[0], airport1) | ||
assertEquals(allAirports[1], airport2) | ||
} | ||
|
||
@Test | ||
@Throws(IOException::class) | ||
fun daoGetAirportByCode_returnSingleAirport() = runBlocking { | ||
val airportResultOne = airportDAO.getAirportByCode("OPO") | ||
val airportResultTwo = airportDAO.getAirportByCode("ARN") | ||
|
||
assertEquals(airportResultOne.first(), airport1) | ||
assertEquals(airportResultTwo.first(), airport2) | ||
} | ||
|
||
@Test | ||
@Throws(IOException::class) | ||
fun daoGetAirportExcept_verifyNotExistInReturnedAirports() = runBlocking { | ||
val allAirports = airportDAO.getAllAirportsExcept("OPO").first() | ||
|
||
assertTrue(airport1 !in allAirports) // airport1 is not in allAirports | ||
assertTrue(airport2 in allAirports) | ||
} | ||
|
||
@Test | ||
@Throws(IOException::class) | ||
fun daoGetAirportByQuery_verifyNotReturnAllResultsFromDB() = runBlocking { | ||
val query = "FR" | ||
val allAirportsByQuery = airportDAO.getAirportsByQuery("%$query%").first() | ||
val allAirports = airportDAO.getAllAirports().first() | ||
|
||
assertTrue(allAirports.size > allAirportsByQuery.size) | ||
} | ||
|
||
@Test | ||
@Throws(IOException::class) | ||
fun daoGetAirportByQuery_verifyReturnedAirportsIncludeQuery() = runBlocking { | ||
val query = "FR" | ||
val allAirportsByQuery = airportDAO.getAirportsByQuery("%$query%").first() | ||
|
||
assertTrue(allAirportsByQuery[0].name.contains(query, ignoreCase = true)) | ||
assertTrue(allAirportsByQuery[1].name.contains(query, ignoreCase = true)) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
18 changes: 0 additions & 18 deletions
18
app/src/test/java/com/nabilbdev/searchflight/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
app/src/test/java/com/nabilbdev/searchflight/data/local/fake/FakeAirportDAO.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.nabilbdev.searchflight.data.local.fake | ||
|
||
import com.nabilbdev.searchflight.data.local.dao.AirportDAO | ||
import com.nabilbdev.searchflight.data.local.entity.Airport | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.flow | ||
|
||
class FakeAirportDAO : AirportDAO { | ||
private val airports = FakeDataSource.fakeAirports | ||
|
||
/** | ||
* We mimic the same SQLITE query behaviour exist in the [AirportDAO]. | ||
* | ||
* f.e: LIKE %query% changed to it.name.contains(query) | ||
*/ | ||
override fun getAirportsByQuery(query: String): Flow<List<Airport>> = flow { | ||
|
||
emit( | ||
airports.filter { | ||
it.name.contains(query, ignoreCase = true) || it.iataCode.contains(query) | ||
} | ||
) | ||
} | ||
|
||
override fun getAllAirportsExcept(airportCode: String): Flow<List<Airport>> = flow { | ||
emit(airports.filter { it.iataCode != airportCode }) | ||
} | ||
|
||
override fun getAirportByCode(airportCode: String): Flow<Airport> = flow { | ||
airports.find { it.iataCode == airportCode }?.let { emit(it) } | ||
}.filterNotNull() | ||
|
||
override fun getAllAirports(): Flow<List<Airport>> = flow { | ||
emit(airports) | ||
} | ||
} |
Oops, something went wrong.