-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
2 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
76 changes: 76 additions & 0 deletions
76
...commonTest/kotlin/com/mirego/trikot/datasources/flow/generic/FileSystemDataSourceTests.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,76 @@ | ||
package com.mirego.trikot.datasources.flow.generic | ||
|
||
import com.mirego.trikot.datasources.flow.ExpiringFlowDataSourceRequest | ||
import com.mirego.trikot.datasources.flow.FlowDataSourceExpiringValue | ||
import com.mirego.trikot.datasources.flow.FlowDataSourceRequest | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.datetime.Clock | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import okio.FileNotFoundException | ||
import okio.Path.Companion.toPath | ||
import okio.fakefilesystem.FakeFileSystem | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertTrue | ||
|
||
class FileSystemDataSourceTests { | ||
private val diskCachePath = "testDiskCachePath" | ||
private val json = Json | ||
private val dataSerializer = TestData.serializer() | ||
|
||
private val fileSystem = FakeFileSystem() | ||
private val fileSystemDataSource = FileSystemDataSource<BasicRequest, TestData>(json, dataSerializer, diskCachePath, fileSystem) | ||
|
||
@Test | ||
fun test_read_valid_data() = runTest { | ||
val testRequest = BasicRequest("test", 1000L, FlowDataSourceRequest.Type.REFRESH_CACHE) | ||
val testData = TestData("value") | ||
val testDataJson = "{\"value\": \"value\"}" | ||
val filePath = "$diskCachePath/${testRequest.cacheableId}.json".toPath() | ||
|
||
val parentDirectory = filePath.parent ?: throw AssertionError("Parent directory should not be null") | ||
fileSystem.createDirectories(parentDirectory) | ||
fileSystem.write(filePath) { | ||
writeUtf8(testDataJson) | ||
} | ||
|
||
val result = fileSystemDataSource.internalRead(testRequest) | ||
|
||
assertEquals(testData, result.value) | ||
} | ||
|
||
@Test | ||
fun test_read_nonexistent_file() = runTest { | ||
val testRequest = BasicRequest("nonexistent", 1000L, FlowDataSourceRequest.Type.REFRESH_CACHE) | ||
|
||
assertFailsWith<FileNotFoundException> { | ||
fileSystemDataSource.internalRead(testRequest) | ||
} | ||
} | ||
|
||
@Test | ||
fun test_save_data() = runTest { | ||
val testRequest = BasicRequest("saveTest", 1000L, FlowDataSourceRequest.Type.REFRESH_CACHE) | ||
val testData = TestData("savedValue") | ||
val filePath = "$diskCachePath/${testRequest.cacheableId}.json".toPath() | ||
|
||
fileSystemDataSource.save(testRequest, FlowDataSourceExpiringValue(testData, Clock.System.now().toEpochMilliseconds())) | ||
|
||
assertTrue(fileSystem.exists(filePath)) | ||
val savedContent = fileSystem.read(filePath) { | ||
readUtf8() | ||
} | ||
assertEquals("{\"value\":\"savedValue\"}", savedContent) | ||
} | ||
|
||
@Serializable | ||
private data class TestData(val value: String) | ||
|
||
private data class BasicRequest( | ||
override val cacheableId: String, | ||
override val expiredInMilliseconds: Long = 0, | ||
override val requestType: FlowDataSourceRequest.Type | ||
) : ExpiringFlowDataSourceRequest | ||
} |