forked from github/wulkanowy-mirror
Merge branch 'hotfix/1.8.2' into develop
This commit is contained in:
commit
09c968f273
20 changed files with 3166 additions and 179 deletions
|
@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.migrations
|
|||
import android.content.Context
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.room.Room
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.room.testing.MigrationTestHelper
|
||||
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
|
@ -16,7 +17,7 @@ abstract class AbstractMigrationTest {
|
|||
|
||||
val dbName = "migration-test"
|
||||
|
||||
val context: Context get() = ApplicationProvider.getApplicationContext()
|
||||
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
||||
|
||||
@get:Rule
|
||||
val helper: MigrationTestHelper = MigrationTestHelper(
|
||||
|
@ -25,6 +26,10 @@ abstract class AbstractMigrationTest {
|
|||
FrameworkSQLiteOpenHelperFactory()
|
||||
)
|
||||
|
||||
fun runMigrationsAndValidate(migration: Migration) {
|
||||
helper.runMigrationsAndValidate(dbName, migration.endVersion, true, migration).close()
|
||||
}
|
||||
|
||||
fun getMigratedRoomDatabase(): AppDatabase {
|
||||
val database = Room.databaseBuilder(
|
||||
ApplicationProvider.getApplicationContext(),
|
||||
|
|
|
@ -33,7 +33,7 @@ class Migration12Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
|
||||
runMigrationsAndValidate(Migration12())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -49,6 +49,7 @@ class Migration12Test : AbstractMigrationTest() {
|
|||
assertEquals(2, studentId)
|
||||
assertEquals(6, classId)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -62,7 +63,7 @@ class Migration12Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
|
||||
runMigrationsAndValidate(Migration12())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -73,6 +74,7 @@ class Migration12Test : AbstractMigrationTest() {
|
|||
assertEquals(2, studentId)
|
||||
assertEquals(1, classId)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -88,7 +90,7 @@ class Migration12Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
|
||||
runMigrationsAndValidate(Migration12())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -107,6 +109,7 @@ class Migration12Test : AbstractMigrationTest() {
|
|||
assertEquals(studentId, 3)
|
||||
assertEquals(true, isCurrent)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
private fun createStudent(db: SupportSQLiteDatabase, studentId: Int, isCurrent: Boolean) {
|
||||
|
|
|
@ -57,6 +57,8 @@ class Migration13Test : AbstractMigrationTest() {
|
|||
assertEquals("C", className)
|
||||
assertEquals("Publiczna szkoła Wulkanowego-fejka nr 2 w fakelog.cf", schoolName)
|
||||
}
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -85,6 +87,8 @@ class Migration13Test : AbstractMigrationTest() {
|
|||
assertEquals("", className)
|
||||
assertEquals("Publiczna szkoła Wulkanowego-fejka nr 1 w fakelog.cf", schoolName)
|
||||
}
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -148,6 +152,7 @@ class Migration13Test : AbstractMigrationTest() {
|
|||
assertFalse(semesters[2].second)
|
||||
assertTrue(semesters[3].second)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
private fun getSemesters(db: SupportSQLiteDatabase, query: String): List<Pair<Semester, Boolean>> {
|
||||
|
|
|
@ -27,7 +27,7 @@ class Migration27Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 27, true, Migration27())
|
||||
runMigrationsAndValidate(Migration27())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -39,6 +39,8 @@ class Migration27Test : AbstractMigrationTest() {
|
|||
assertEquals(123, userLoginId)
|
||||
assertEquals("Student Jan", userName)
|
||||
}
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -49,7 +51,7 @@ class Migration27Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 27, true, Migration27())
|
||||
runMigrationsAndValidate(Migration27())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -61,6 +63,8 @@ class Migration27Test : AbstractMigrationTest() {
|
|||
assertEquals(2, userLoginId)
|
||||
assertEquals("Unit Jan", userName)
|
||||
}
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -73,7 +77,7 @@ class Migration27Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 27, true, Migration27())
|
||||
runMigrationsAndValidate(Migration27())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -90,6 +94,8 @@ class Migration27Test : AbstractMigrationTest() {
|
|||
assertEquals(333, userLoginId)
|
||||
assertEquals("Unit Tomasz", userName)
|
||||
}
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
private fun createStudent(db: SupportSQLiteDatabase, id: Long, userLoginId: Int, studentName: String) {
|
||||
|
|
|
@ -29,7 +29,7 @@ class Migration35Test : AbstractMigrationTest() {
|
|||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 35, true, Migration35(AppInfo()))
|
||||
runMigrationsAndValidate(Migration35(AppInfo()))
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = runBlocking { db.studentDao.loadAll() }
|
||||
|
@ -38,6 +38,8 @@ class Migration35Test : AbstractMigrationTest() {
|
|||
|
||||
assertTrue { students[0].avatarColor in AppInfo().defaultColorsForAvatar }
|
||||
assertTrue { students[1].avatarColor in AppInfo().defaultColorsForAvatar }
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
private fun createStudent(db: SupportSQLiteDatabase, id: Long) {
|
||||
|
|
|
@ -0,0 +1,130 @@
|
|||
package io.github.wulkanowy.data.db.migrations
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.os.Build
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import dagger.hilt.android.testing.HiltAndroidTest
|
||||
import dagger.hilt.android.testing.HiltTestApplication
|
||||
import io.github.wulkanowy.sdk.Sdk
|
||||
import io.github.wulkanowy.sdk.Sdk.ScrapperLoginType.*
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import kotlin.random.Random
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@HiltAndroidTest
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@Config(sdk = [Build.VERSION_CODES.O_MR1], application = HiltTestApplication::class)
|
||||
class Migration54Test : AbstractMigrationTest() {
|
||||
|
||||
@Test
|
||||
fun `don't touch unrelated students`() = runTest {
|
||||
with(helper.createDatabase(dbName, 53)) {
|
||||
createStudent(1, STANDARD, "vulcan.net.pl", "rzeszow", "Jan Michniewicz")
|
||||
createStudent(2, ADFSLight, "umt.tarnow.pl", "tarnow", "Joanna Marcinkiewicz")
|
||||
close()
|
||||
}
|
||||
|
||||
runMigrationsAndValidate(Migration54())
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = db.studentDao.loadAll()
|
||||
|
||||
assertEquals(2, students.size)
|
||||
with(students[0]) {
|
||||
assertEquals(STANDARD.name, loginType)
|
||||
assertEquals("https://vulcan.net.pl", scrapperBaseUrl)
|
||||
assertEquals("rzeszow", symbol)
|
||||
}
|
||||
with(students[1]) {
|
||||
assertEquals(ADFSLight.name, loginType)
|
||||
assertEquals("https://umt.tarnow.pl", scrapperBaseUrl)
|
||||
assertEquals("tarnow", symbol)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `remove tomaszow mazowiecki students`() = runTest {
|
||||
with(helper.createDatabase(dbName, 53)) {
|
||||
createStudent(1, STANDARD, "vulcan.net.pl", "rzeszow", "Jan Michniewicz")
|
||||
createStudent(2, STANDARD, "vulcan.net.pl", "tomaszowmazowiecki", "Joanna Stec")
|
||||
createStudent(3, STANDARD, "vulcan.net.pl", "tomaszowmazowiecki", "Kacper Morawiecki")
|
||||
close()
|
||||
}
|
||||
|
||||
runMigrationsAndValidate(Migration54())
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = db.studentDao.loadAll()
|
||||
assertEquals(1, students.size)
|
||||
with(students[0]) {
|
||||
assertEquals("rzeszow", symbol)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `migrate resman students`() = runTest {
|
||||
with(helper.createDatabase(dbName, 53)) {
|
||||
createStudent(1, ADFSLight, "resman.pl", "rzeszow", "Joanna Stec")
|
||||
createStudent(2, ADFSLight, "resman.pl", "rzeszow", "Kacper Morawiecki")
|
||||
createStudent(3, STANDARD, "vulcan.net.pl", "rzeszow", "Jan Michniewicz")
|
||||
close()
|
||||
}
|
||||
runMigrationsAndValidate(Migration54())
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = db.studentDao.loadAll()
|
||||
assertEquals(3, students.size)
|
||||
with(students[0]) {
|
||||
assertEquals(ADFSLightScoped.name, loginType)
|
||||
assertEquals("https://vulcan.net.pl", scrapperBaseUrl)
|
||||
assertEquals("rzeszowprojekt", symbol)
|
||||
}
|
||||
with(students[1]) {
|
||||
assertEquals(ADFSLightScoped.name, loginType)
|
||||
assertEquals("https://vulcan.net.pl", scrapperBaseUrl)
|
||||
assertEquals("rzeszowprojekt", symbol)
|
||||
}
|
||||
db.close()
|
||||
}
|
||||
|
||||
private fun SupportSQLiteDatabase.createStudent(
|
||||
id: Long,
|
||||
loginType: Sdk.ScrapperLoginType,
|
||||
host: String,
|
||||
symbol: String,
|
||||
studentName: String,
|
||||
) {
|
||||
insert("Students", SQLiteDatabase.CONFLICT_FAIL, ContentValues().apply {
|
||||
put("scrapper_base_url", "https://$host")
|
||||
put("mobile_base_url", "")
|
||||
put("login_type", loginType.name)
|
||||
put("login_mode", "SCRAPPER")
|
||||
put("certificate_key", "")
|
||||
put("private_key", "")
|
||||
put("is_parent", false)
|
||||
put("email", "jan@fakelog.cf")
|
||||
put("password", "******")
|
||||
put("symbol", symbol)
|
||||
put("student_id", Random.nextInt())
|
||||
put("user_login_id", id)
|
||||
put("user_name", studentName)
|
||||
put("student_name", studentName)
|
||||
put("school_id", "123")
|
||||
put("school_short", "")
|
||||
put("school_name", "")
|
||||
put("class_name", "")
|
||||
put("class_id", Random.nextInt())
|
||||
put("is_current", false)
|
||||
put("registration_date", "0")
|
||||
put("id", id)
|
||||
put("nick", "")
|
||||
put("avatar_color", "")
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue