SeasonRecordsMigrationTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/database/room/SeasonRecordsMigrationTest.kt
1package eu.friedcutlet.yokan.database.room23import androidx.room.Room4import androidx.sqlite.driver.bundled.BundledSQLiteDriver5import androidx.sqlite.execSQL6import eu.friedcutlet.yokan.database.*7import java.io.File8import java.nio.file.Files9import kotlinx.coroutines.runBlocking10import kotlinx.serialization.json.*11import kotlin.test.*1213class SeasonRecordsMigrationTest {14 @Test fun version15UpgradeDefaultsToEpisodeTitlesAndSeasonLinksAndFillerMarksSurviveReopen() {15 val directory = Files.createTempDirectory("yokan-season-migration")16 val path = directory.resolve("yokan.db").toString()17 try {18 val schema = DatabaseJson.parseToJsonElement(File("schemas/eu.friedcutlet.yokan.database.room.YokanRoomDatabase/15.json").readText())19 .jsonObject.getValue("database").jsonObject20 BundledSQLiteDriver().open(path).use { connection ->21 schema.getValue("entities").jsonArray.forEach { value ->22 val entity = value.jsonObject23 val table = entity.getValue("tableName").jsonPrimitive.content24 connection.execSQL(entity.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table))25 entity["indices"]?.jsonArray.orEmpty().forEach { index ->26 connection.execSQL(index.jsonObject.getValue("createSql").jsonPrimitive.content.replace("\${TABLE_NAME}", table))27 }28 }29 schema.getValue("setupQueries").jsonArray.forEach { connection.execSQL(it.jsonPrimitive.content) }30 connection.execSQL("INSERT INTO manga(id,name,link,status,itemType,favorite,lastRead,isLocalArchive,updatedAt,privateNote) VALUES(1,'Show','/show','Ongoing','Anime',1,123,0,321,'Saved note')")31 connection.execSQL("INSERT INTO manga(id,name,link,status,itemType,favorite,lastRead,isLocalArchive,updatedAt) VALUES(2,'Season 2','/show/2','Ongoing','Anime',0,0,0,322)")32 connection.execSQL("INSERT INTO manga_credit(mangaId,position,name) VALUES(1,0,'Actor')")33 connection.execSQL("INSERT INTO chapter(id,mangaId,name,url,dateUpload,scanlator,isBookmarked,isRead,lastPageRead,archivePath,isFiller,updatedAt,fetchedAt) VALUES(3,2,'Episode 1','/ep','2026-01-01','',1,1,'900','',1,456,789)")34 connection.execSQL("PRAGMA user_version = 15")35 }36 val room = Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom()37 try {38 runBlocking(roomPersistenceDispatcher) {39 val titles = room.mangaDao().all().map { it.toDb() }.associateBy { it.id }40 assertTrue(titles.values.all { it.parentId == null && it.fetchType == DbFetchType.Episodes && it.seasonNumber == null })41 assertEquals("Saved note", titles.getValue(1).privateNote)42 val chapter = room.chapterDao().all().single().toDb()43 assertNull(chapter.fillerOverride)44 assertTrue(chapter.effectiveFiller && chapter.isRead && chapter.isBookmarked)45 assertEquals("900", chapter.lastPageRead)46 room.mangaDao().upsert(titles.getValue(1).copy(fetchType = DbFetchType.Seasons).toRow())47 room.mangaDao().upsert(titles.getValue(2).copy(parentId = 1, seasonNumber = 2.0).toRow())48 room.chapterDao().upsert(chapter.copy(fillerOverride = false).toRow())49 }50 } finally { room.close() }51 val reopened = Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom()52 try {53 runBlocking(roomPersistenceDispatcher) {54 val titles = reopened.mangaDao().all().map { it.toDb() }.associateBy { it.id }55 assertEquals(DbFetchType.Seasons, titles.getValue(1).fetchType)56 assertEquals(1L, titles.getValue(2).parentId)57 assertEquals(2.0, titles.getValue(2).seasonNumber)58 assertEquals(listOf(1L), titles.values.filter { it.favorite && it.parentId == null }.map { it.id })59 val chapter = reopened.chapterDao().all().single().toDb()60 assertEquals(false, chapter.fillerOverride)61 assertTrue(chapter.isFiller)62 assertFalse(chapter.effectiveFiller)63 assertEquals("900", chapter.lastPageRead)64 }65 } finally { reopened.close() }66 } finally { directory.toFile().deleteRecursively() }67 }68}69