-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from RotBolt/task/55-tests-for-flaker-data-module
Task/55 tests for flaker data module
- Loading branch information
Showing
23 changed files
with
271 additions
and
33 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
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
8 changes: 8 additions & 0 deletions
8
flaker-data/src/androidUnitTest/kotlin/io/rotlabs/flakerdb/TestDbDriverFactory.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,8 @@ | ||
package io.rotlabs.flakerdb | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver | ||
|
||
internal actual fun testDbDriverFactory(): SqlDriver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY).also { | ||
FlakerDatabase.Schema.create(it) | ||
} |
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
5 changes: 3 additions & 2 deletions
5
...networkrequest/data/NetworkRequestRepo.kt → ...erdb/networkrequest/NetworkRequestRepo.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package io.rotlabs.flakerdb.networkrequest.data | ||
package io.rotlabs.flakerdb.networkrequest | ||
|
||
import io.rotlabs.flakedomain.networkrequest.NetworkRequest | ||
import io.rotlabs.flakerprefs.RetentionPolicy | ||
import io.rotlabs.flakedomain.prefs.RetentionPolicy | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface NetworkRequestRepo { | ||
suspend fun selectAll(): List<NetworkRequest> | ||
suspend fun insert(networkRequest: NetworkRequest) | ||
fun observeAll(): Flow<List<NetworkRequest>> | ||
suspend fun deleteExpiredData(retentionPolicy: RetentionPolicy) | ||
suspend fun deleteAll() | ||
} |
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
2 changes: 1 addition & 1 deletion
2
flaker-data/src/commonMain/kotlin/io/rotlabs/flakerprefs/PrefDataStore.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
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
5 changes: 5 additions & 0 deletions
5
flaker-data/src/commonTest/kotlin/io/rotlabs/flakerdb/TestDbDriverFactory.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,5 @@ | ||
package io.rotlabs.flakerdb | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
|
||
internal expect fun testDbDriverFactory(): SqlDriver |
152 changes: 152 additions & 0 deletions
152
...r-data/src/commonTest/kotlin/io/rotlabs/flakerdb/networkrequest/NetworkRequestRepoTest.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,152 @@ | ||
package io.rotlabs.flakerdb.networkrequest | ||
|
||
import app.cash.turbine.test | ||
import io.rotlabs.flakedomain.networkrequest.NetworkRequest | ||
import io.rotlabs.flakedomain.prefs.RetentionPolicy | ||
import io.rotlabs.flakerdb.testDbDriverFactory | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class NetworkRequestRepoTest { | ||
|
||
private lateinit var networkRequestRepo: NetworkRequestRepo | ||
|
||
private val testDispatcher = UnconfinedTestDispatcher() | ||
|
||
private suspend fun NetworkRequestRepo.seed() { | ||
insert( | ||
NetworkRequest( | ||
host = "https://jsonplaceholder.typicode.com", | ||
path = "/todos/1", | ||
method = "GET", | ||
requestTime = 1693127126000, | ||
responseCode = 200, | ||
responseTimeTaken = 100, | ||
isFailedByFlaker = false, | ||
createdAt = 1693127139000 | ||
) | ||
) | ||
} | ||
|
||
@BeforeTest | ||
fun setup() = runBlocking { | ||
networkRequestRepo = NetworkRequestRepoImpl( | ||
sqlDriver = testDbDriverFactory(), | ||
dispatcher = testDispatcher | ||
) | ||
|
||
networkRequestRepo.deleteAll() | ||
networkRequestRepo.seed() | ||
} | ||
|
||
@Test | ||
fun `test selectAll`() = runBlocking { | ||
val networkRequests = networkRequestRepo.selectAll() | ||
assertTrue(networkRequests.isNotEmpty()) | ||
assertEquals(1, networkRequests.size) | ||
} | ||
|
||
@Test | ||
fun `test observeAll`() = runBlocking { | ||
val networkRequests = networkRequestRepo.observeAll() | ||
networkRequests.test { | ||
assertEquals(1, awaitItem().size) | ||
networkRequestRepo.insert( | ||
NetworkRequest( | ||
host = "https://jsonplaceholder.typicode.com", | ||
path = "/todos/1", | ||
method = "GET", | ||
requestTime = 1693127126000, | ||
responseCode = 500, | ||
responseTimeTaken = 100, | ||
isFailedByFlaker = true, | ||
createdAt = 1693127139000 | ||
) | ||
) | ||
val items = awaitItem() | ||
assertEquals(2, items.size) | ||
assertEquals(500, items[1].responseCode) | ||
assertEquals(true, items[1].isFailedByFlaker) | ||
} | ||
} | ||
|
||
@Test | ||
fun `test deleteAll`() = runBlocking { | ||
assertEquals(1, networkRequestRepo.selectAll().size) | ||
networkRequestRepo.deleteAll() | ||
assertEquals(0, networkRequestRepo.selectAll().size) | ||
} | ||
|
||
@Test | ||
fun `test deleteExpiredData`() = runBlocking { | ||
// insert 2 days before time | ||
val networkRequest0 = NetworkRequest( | ||
host = "https://jsonplaceholder.typicode.com", | ||
path = "/todos/1", | ||
method = "GET", | ||
requestTime = 1692955276000, | ||
responseCode = 200, | ||
responseTimeTaken = 100, | ||
isFailedByFlaker = false, | ||
createdAt = 1692955276000 | ||
) | ||
networkRequestRepo.insert(networkRequest0) | ||
assertEquals(2, networkRequestRepo.selectAll().size) | ||
networkRequestRepo.deleteExpiredData(RetentionPolicy.ONE_DAY) | ||
assertEquals(1, networkRequestRepo.selectAll().size) | ||
|
||
// insert 7 days before time | ||
val networkRequest1 = NetworkRequest( | ||
host = "https://jsonplaceholder.typicode.com", | ||
path = "/todos/1", | ||
method = "GET", | ||
requestTime = 1692523289000, | ||
responseCode = 200, | ||
responseTimeTaken = 100, | ||
isFailedByFlaker = false, | ||
createdAt = 1692523289000 | ||
) | ||
networkRequestRepo.insert(networkRequest1) | ||
assertEquals(2, networkRequestRepo.selectAll().size) | ||
networkRequestRepo.deleteExpiredData(RetentionPolicy.SEVEN_DAYS) | ||
assertEquals(1, networkRequestRepo.selectAll().size) | ||
|
||
// insert 15 days before time | ||
val networkRequest2 = NetworkRequest( | ||
host = "https://jsonplaceholder.typicode.com", | ||
path = "/todos/1", | ||
method = "GET", | ||
requestTime = 1690968089000, | ||
responseCode = 200, | ||
responseTimeTaken = 100, | ||
isFailedByFlaker = false, | ||
createdAt = 1690968089000 | ||
) | ||
networkRequestRepo.insert(networkRequest2) | ||
assertEquals(2, networkRequestRepo.selectAll().size) | ||
networkRequestRepo.deleteExpiredData(RetentionPolicy.FIFTEEN_DAYS) | ||
assertEquals(1, networkRequestRepo.selectAll().size) | ||
|
||
// insert 30 days before time | ||
val networkRequest3 = NetworkRequest( | ||
host = "https://jsonplaceholder.typicode.com", | ||
path = "/todos/1", | ||
method = "GET", | ||
requestTime = 1688256089000, | ||
responseCode = 200, | ||
responseTimeTaken = 100, | ||
isFailedByFlaker = false, | ||
createdAt = 1688256089000 | ||
) | ||
networkRequestRepo.insert(networkRequest3) | ||
assertEquals(2, networkRequestRepo.selectAll().size) | ||
networkRequestRepo.deleteExpiredData(RetentionPolicy.THIRTY_DAYS) | ||
assertEquals(1, networkRequestRepo.selectAll().size) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
flaker-data/src/commonTest/kotlin/io/rotlabs/flakerprefs/PrefsDataStoreTest.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,40 @@ | ||
package io.rotlabs.flakerprefs | ||
|
||
import io.rotlabs.flakedomain.prefs.FlakerPrefs | ||
import io.rotlabs.flakedomain.prefs.RetentionPolicy | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class PrefsDataStoreTest { | ||
|
||
private lateinit var prefDataStore: PrefDataStore | ||
|
||
@BeforeTest | ||
fun setup() { | ||
val dataStore = createDataStore { DATASTORE_FILE_NAME } | ||
prefDataStore = PrefDataStoreImpl(dataStore) | ||
} | ||
|
||
@Test | ||
fun `test save and get prefs`() = runBlocking { | ||
prefDataStore.savePrefs( | ||
FlakerPrefs( | ||
shouldIntercept = true, | ||
delay = 1000, | ||
failPercent = 10, | ||
variancePercent = 10, | ||
retentionPolicy = RetentionPolicy.ONE_DAY | ||
) | ||
) | ||
val prefs = prefDataStore.getPrefs().first() | ||
assertTrue(prefs.shouldIntercept) | ||
assertEquals(1000, prefs.delay) | ||
assertEquals(10, prefs.failPercent) | ||
assertEquals(10, prefs.variancePercent) | ||
assertEquals(prefs.retentionPolicy, RetentionPolicy.ONE_DAY) | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
flaker-data/src/iosTest/kotlin/io/rotlabs/flakerdb/TestDbDriverFactory.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,7 @@ | ||
package io.rotlabs.flakerdb | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.native.inMemoryDriver | ||
import io.rotlabs.flakerdb.FlakerDatabase | ||
|
||
internal actual fun testDbDriverFactory(): SqlDriver = inMemoryDriver(FlakerDatabase.Schema) |
4 changes: 1 addition & 3 deletions
4
...io/rotlabs/flakerprefs/dto/FlakerPrefs.kt → .../rotlabs/flakedomain/prefs/FlakerPrefs.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
2 changes: 1 addition & 1 deletion
2
...io/rotlabs/flakerprefs/RetentionPolicy.kt → ...labs/flakedomain/prefs/RetentionPolicy.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
2 changes: 1 addition & 1 deletion
2
flaker-okhttp-core/src/main/java/io/rotlabs/flakerretrofit/FlakerInterceptor.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
Oops, something went wrong.