Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdetilly committed Dec 1, 2023
1 parent a9f46a3 commit c15b1a8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions trikot-datasources/datasources-flow/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kotlin {
implementation("org.jetbrains.kotlin:kotlin-test-common")
implementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.KOTLINX_COROUTINES}")
implementation("com.squareup.okio:okio-fakefilesystem:${Versions.OKIO}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class FileSystemDataSource<R : ExpiringFlowDataSourceRequest, T>(
private val fileManager: FileSystem = NativeFileSystem.fileSystem
) : BaseExpiringExecutableFlowDataSource<R, T>() {

override suspend fun internalRead(request: R): FlowDataSourceExpiringValue<T> {
public override suspend fun internalRead(request: R): FlowDataSourceExpiringValue<T> {
return withContext(DataSourceDispatchers.IO) {
val filePath = buildFilePath(request)
try {
val data = json.decodeFromString(dataSerializer, getFileAsString(filePath))
FlowDataSourceExpiringValue(
value = data,
expiredEpoch = (NativeFileSystem.fileSystem.metadata(filePath).lastModifiedAtMillis ?: 0) + request.expiredInMilliseconds
expiredEpoch = lastModifiedAtMillisMetadata(filePath) + request.expiredInMilliseconds
)
} catch (exception: SerializationException) {
fileManager.deleteRecursively(filePath)
Expand Down Expand Up @@ -65,5 +65,11 @@ class FileSystemDataSource<R : ExpiringFlowDataSourceRequest, T>(
}
}

private fun lastModifiedAtMillisMetadata(filePath: Path) = try {
fileManager.metadata(filePath).lastModifiedAtMillis ?: 0
} catch (e: FileNotFoundException) {
0
}

private fun buildFilePath(request: R) = "$diskCachePath/${request.cacheableId}.json".toPath()
}
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
}

0 comments on commit c15b1a8

Please sign in to comment.