Yokan / rebuild
Course 1 8624561

EpisodeNumberMigrationTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/database/room/EpisodeNumberMigrationTest.kt

test / fixture65 lines
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 EpisodeNumberMigrationTest {14    @Test fun version13UpgradeKeepsProgressAndNullableNumberingSurvivesReopen() {15        val directory = Files.createTempDirectory("yokan-episode-number-migration")16        val path = directory.resolve("yokan.db").toString()17        try {18            val schema = DatabaseJson.parseToJsonElement(File("schemas/eu.friedcutlet.yokan.database.room.YokanRoomDatabase/13.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','/title','Ongoing','Anime',1,123,1,321,'Saved note')")31                connection.execSQL("INSERT INTO chapter(id,mangaId,name,url,dateUpload,scanlator,isBookmarked,isRead,lastPageRead,archivePath,isFiller,updatedAt,fetchedAt) VALUES(2,1,'Episode 12.5','/episode','2026-01-01','Group',1,1,'1200','/offline/file',1,456,789)")32                connection.execSQL("INSERT INTO settings(id,json) VALUES(1,'{\"mangaGridSize\":4}')")33                connection.execSQL("PRAGMA user_version = 13")34            }35            val room = Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom()36            try {37                runBlocking(roomPersistenceDispatcher) {38                    val chapter = room.chapterDao().all().single().toDb()39                    assertNull(chapter.number)40                    assertNull(chapter.season)41                    assertNull(chapter.numberingName)42                    assertEquals("1200", chapter.lastPageRead)43                    assertTrue(chapter.isRead && chapter.isBookmarked && chapter.isFiller)44                    assertEquals("/offline/file", chapter.archivePath)45                    assertEquals(789L, chapter.fetchedAt)46                    assertEquals("Saved note", room.mangaDao().all().single().toDb().privateNote)47                    assertEquals(4, room.settingsDao().get()?.toDb()?.mangaGridSize)48                    room.chapterDao().upsert(chapter.copy(name = "A custom title", number = 12.5, season = 2, numberingName = "Episode 12.5 [en]").toRow())49                }50            } finally { room.close() }51            val reopened = Room.databaseBuilder<YokanRoomDatabase>(name = path).buildYokanRoom()52            try {53                runBlocking(roomPersistenceDispatcher) {54                    val chapter = reopened.chapterDao().all().single().toDb()55                    assertEquals(12.5, chapter.number)56                    assertEquals(2, chapter.season)57                    assertEquals("Episode 12.5 [en]", chapter.numberingName)58                    assertEquals("A custom title", chapter.name)59                    assertEquals("1200", chapter.lastPageRead)60                    assertTrue(chapter.isRead && chapter.isBookmarked && chapter.isFiller)61                }62            } finally { reopened.close() }63        } finally { directory.toFile().deleteRecursively() }64    }65}66

Search chapters, reference and source filenames.