Skip to content

Commit

Permalink
Finished ProjectFilesystemDatasourceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Wavesonics committed Jul 10, 2024
1 parent f8fef68 commit df52844
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,7 @@ class ProjectFilesystemDatasource(
entityType: ApiProjectEntity.Type,
projectDef: ProjectDefinition,
entityId: Int
): Path {
val entityDir = getEntityDirectory(userId, projectDef, fileSystem)
val filename = "$entityId-${getPath(entityType)}.json"
return entityDir / filename
}

private fun getPath(entityType: ApiProjectEntity.Type): String = entityType.name.lowercase()
): Path = getEntityPath(userId, entityType, projectDef, entityId, fileSystem)

companion object {
private val ENTITY_FILENAME_REGEX = Regex("^([0-9]+)-([a-zA-Z_]+).json$")
Expand Down Expand Up @@ -285,5 +279,20 @@ class ProjectFilesystemDatasource(
val dir = getProjectDirectory(userId, projectDef, fileSystem)
return dir / SYNC_DATA_FILE
}

private fun getPathStub(entityType: ApiProjectEntity.Type): String =
entityType.name.lowercase()

fun getEntityPath(
userId: Long,
entityType: ApiProjectEntity.Type,
projectDef: ProjectDefinition,
entityId: Int,
fileSystem: FileSystem,
): Path {
val entityDir = getEntityDirectory(userId, projectDef, fileSystem)
val filename = "$entityId-${getPathStub(entityType)}.json"
return entityDir / filename
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.darkrockstudios.apps.hammer.project.datasource

import PROJECT_1_NAME
import com.darkrockstudios.apps.hammer.base.http.ApiProjectEntity
import com.darkrockstudios.apps.hammer.base.http.ApiSceneType
import com.darkrockstudios.apps.hammer.base.http.createJsonSerializer
import com.darkrockstudios.apps.hammer.base.http.writeJson
import com.darkrockstudios.apps.hammer.project.EntityDefinition
import com.darkrockstudios.apps.hammer.project.EntityNotFound
import com.darkrockstudios.apps.hammer.project.ProjectDefinition
import com.darkrockstudios.apps.hammer.project.ProjectFilesystemDatasource
Expand Down Expand Up @@ -184,6 +186,31 @@ class ProjectFilesystemDatasourceTest : BaseTest() {
assertEquals(ApiProjectEntity.Type.NOTE, entityType)
}

@Test
fun `Find Entity Defs`() = runTest {
setupEntities()

val datasource = ProjectFilesystemDatasource(fileSystem, json)

val entityDefs = datasource.getEntityDefs(userId, projectDef) { true }
val expectedDefs = listOf(
EntityDefinition(1, ApiProjectEntity.Type.SCENE),
EntityDefinition(2, ApiProjectEntity.Type.SCENE),
EntityDefinition(4, ApiProjectEntity.Type.SCENE),
EntityDefinition(5, ApiProjectEntity.Type.SCENE),
EntityDefinition(6, ApiProjectEntity.Type.SCENE_DRAFT),
EntityDefinition(7, ApiProjectEntity.Type.SCENE_DRAFT),
EntityDefinition(10, ApiProjectEntity.Type.TIMELINE_EVENT),
EntityDefinition(12, ApiProjectEntity.Type.ENCYCLOPEDIA_ENTRY),
EntityDefinition(14, ApiProjectEntity.Type.ENCYCLOPEDIA_ENTRY),
EntityDefinition(15, ApiProjectEntity.Type.SCENE),
EntityDefinition(16, ApiProjectEntity.Type.NOTE),
EntityDefinition(20, ApiProjectEntity.Type.NOTE),
)

assertEquals(expectedDefs, entityDefs.sortedBy { it.id })
}

@Test
fun `Load Entity - Decode Scene JSON - SerializationException`() = runTest {
val userId = 1L
Expand Down Expand Up @@ -235,6 +262,82 @@ class ProjectFilesystemDatasourceTest : BaseTest() {
assertEquals(entityId, resultException.id)
}

@Test
fun `Delete Entity`() = runTest {
val entityId = 1
setupEntities()

val path = ProjectFilesystemDatasource.getEntityPath(
userId = userId,
entityType = ApiProjectEntity.Type.SCENE,
projectDef = projectDef,
entityId = entityId,
fileSystem = fileSystem
)
assertTrue(fileSystem.exists(path))

val datasource = ProjectFilesystemDatasource(fileSystem, json)
val result =
datasource.deleteEntity(userId, ApiProjectEntity.Type.SCENE, projectDef, entityId)
assertTrue(result.isSuccess)
assertFalse(fileSystem.exists(path))
}

@Test
fun `Delete Entity - Failure - Wrong Type`() = runTest {
val entityId = 1
setupEntities()

val path = ProjectFilesystemDatasource.getEntityPath(
userId = userId,
entityType = ApiProjectEntity.Type.SCENE,
projectDef = projectDef,
entityId = entityId,
fileSystem = fileSystem
)
assertTrue(fileSystem.exists(path))

val datasource = ProjectFilesystemDatasource(fileSystem, json)
val result =
datasource.deleteEntity(userId, ApiProjectEntity.Type.NOTE, projectDef, entityId)
assertTrue(result.isSuccess)
assertTrue(fileSystem.exists(path))
}

@Test
fun `Store Entity - Scene Entity`() = runTest {
val entityId = 1
val entity = ApiProjectEntity.SceneEntity(
id = entityId,
name = "test",
content = "test content",
order = 0,
path = listOf(0),
sceneType = ApiSceneType.Scene,
outline = "outline",
notes = "notes",
)

val datasource = ProjectFilesystemDatasource(fileSystem, json)
val result = datasource.storeEntity(
userId,
projectDef,
entity,
ApiProjectEntity.Type.SCENE,
ApiProjectEntity.SceneEntity.serializer()
)
assertTrue(result.isSuccess)

val path = ProjectFilesystemDatasource.getEntityPath(
userId = userId,
entityType = ApiProjectEntity.Type.SCENE,
projectDef = projectDef,
entityId = entityId,
fileSystem = fileSystem
)
assertTrue(fileSystem.exists(path))
}

private fun setupEntities() {
val entityDir =
ProjectFilesystemDatasource.getEntityDirectory(userId, projectDef, fileSystem)
Expand Down

0 comments on commit df52844

Please sign in to comment.