CastStorageTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/database/room/CastStorageTest.kt
1package eu.friedcutlet.yokan.database.room23import androidx.room.Room4import androidx.sqlite.driver.bundled.BundledSQLiteDriver5import androidx.sqlite.execSQL6import eu.friedcutlet.yokan.database.*7import eu.friedcutlet.yokan.domain.TitleCredit8import java.io.File9import java.nio.file.Files10import kotlinx.coroutines.runBlocking11import kotlinx.serialization.json.*12import kotlin.test.*1314class CastStorageTest {15 @Test fun version14UpgradeKeepsProgressAndNormalizedCreditsAvoidProgressWriteAmplification() = runBlocking {16 val directory = Files.createTempDirectory("yokan-cast-storage")17 val path = directory.resolve("yokan.db").toString()18 fun open() = RoomYokanDatabase(Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom())19 fun writes(): Long = BundledSQLiteDriver().open(path).use { connection ->20 connection.prepare("SELECT count FROM test_credit_writes").use { statement ->21 check(statement.step()); statement.getLong(0)22 }23 }24 try {25 val schema = DatabaseJson.parseToJsonElement(File("schemas/eu.friedcutlet.yokan.database.room.YokanRoomDatabase/14.json").readText())26 .jsonObject.getValue("database").jsonObject27 BundledSQLiteDriver().open(path).use { connection ->28 for (entity in schema.getValue("entities").jsonArray) {29 val row = entity.jsonObject30 val table = row.getValue("tableName").jsonPrimitive.content31 connection.execSQL(row.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table))32 row["indices"]?.jsonArray.orEmpty().forEach { connection.execSQL(it.jsonObject.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table)) }33 }34 schema.getValue("setupQueries").jsonArray.forEach { connection.execSQL(it.jsonPrimitive.content) }35 connection.execSQL("INSERT INTO manga(id,name,link,status,itemType,favorite,lastRead,isLocalArchive,updatedAt,privateNote) VALUES(1,'Show','/show','Licensed','Anime',1,123,1,1,'Saved note')")36 connection.execSQL("INSERT INTO chapter(id,mangaId,name,url,dateUpload,scanlator,isBookmarked,isRead,lastPageRead,archivePath,isFiller,updatedAt,fetchedAt) VALUES(2,1,'Episode 1','/episode','','',1,1,'2345','/offline/file',0,1,1)")37 connection.execSQL("PRAGMA user_version = 14")38 }39 val cast = List(500) { TitleCredit("Actor $it", "Voice actor", "Character $it") }40 val first = open()41 try {42 val original = requireNotNull(first.manga(1))43 assertEquals(emptyList(), original.cast)44 assertEquals(DbStatus.Licensed, original.status)45 first.upsertManga(original.copy(cast = cast))46 first.awaitPersistence()47 BundledSQLiteDriver().open(path).use { connection ->48 connection.execSQL("CREATE TABLE test_credit_writes(count INTEGER NOT NULL)")49 connection.execSQL("INSERT INTO test_credit_writes VALUES(0)")50 connection.execSQL("CREATE TRIGGER test_credit_insert AFTER INSERT ON manga_credit BEGIN UPDATE test_credit_writes SET count=count+1; END")51 connection.execSQL("CREATE TRIGGER test_credit_delete AFTER DELETE ON manga_credit BEGIN UPDATE test_credit_writes SET count=count+1; END")52 }53 repeat(3) { first.upsertManga(requireNotNull(first.manga(1)).copy(lastRead = 200L + it)) }54 first.awaitPersistence()55 assertEquals(0L, writes(), "Reading progress must not delete/reinsert 500 credits")56 first.upsertManga(requireNotNull(first.manga(1)).copy(cast = cast.drop(1)))57 first.awaitPersistence()58 assertTrue(writes() > 0)59 } finally { first.close() }60 val second = open()61 try {62 assertEquals(cast.drop(1), second.manga(1)?.cast)63 assertEquals("Saved note", second.manga(1)?.privateNote)64 assertEquals(202L, second.manga(1)?.lastRead)65 assertEquals("2345", second.chapter(2)?.lastPageRead)66 val snapshot = second.snapshot()67 second.replaceAllAtomically(snapshot.copy(mangas = listOf(requireNotNull(second.manga(1)).copy(cast = cast.reversed()))), snapshot)68 } finally { second.close() }69 val third = open()70 try {71 assertEquals(cast.reversed(), third.manga(1)?.cast)72 third.removeManga(1)73 third.awaitPersistence()74 assertEquals(emptyList(), third.room.mangaCreditDao().all(), "Deleting a title removes its owned credits")75 } finally { third.close() }76 } finally { directory.toFile().deleteRecursively() }77 }78}79