forked from github/wulkanowy-mirror
Fix more than one current semester (#307)
This commit is contained in:
parent
4e3864f26f
commit
e71dd55066
@ -25,6 +25,15 @@ android {
|
||||
fabric_api_key: System.getenv("FABRIC_API_KEY") ?: "null",
|
||||
crashlytics_enabled: project.hasProperty("enableCrashlytics")
|
||||
]
|
||||
javaCompileOptions {
|
||||
annotationProcessorOptions {
|
||||
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
|
||||
}
|
||||
|
||||
signingConfigs {
|
||||
@ -77,7 +86,7 @@ play {
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation('io.github.wulkanowy:api:0.7.3') { exclude module: "threetenbp" }
|
||||
implementation('com.github.wulkanowy:api:f1152ae') { exclude module: "threetenbp" }
|
||||
|
||||
implementation "androidx.legacy:legacy-support-v4:1.0.0"
|
||||
implementation "androidx.appcompat:appcompat:1.0.2"
|
||||
@ -137,6 +146,7 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
|
||||
androidTestImplementation "io.mockk:mockk-android:1.9.2"
|
||||
androidTestImplementation 'org.mockito:mockito-android:2.25.1'
|
||||
androidTestImplementation "androidx.room:room-testing:2.1.0-alpha06"
|
||||
androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
|
||||
}
|
||||
|
||||
|
1325
app/schemas/io.github.wulkanowy.data.db.AppDatabase/11.json
Normal file
1325
app/schemas/io.github.wulkanowy.data.db.AppDatabase/11.json
Normal file
File diff suppressed because it is too large
Load Diff
1332
app/schemas/io.github.wulkanowy.data.db.AppDatabase/12.json
Normal file
1332
app/schemas/io.github.wulkanowy.data.db.AppDatabase/12.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,159 @@
|
||||
package io.github.wulkanowy.data.db.migrations
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.database.sqlite.SQLiteDatabase.CONFLICT_FAIL
|
||||
import androidx.room.Room
|
||||
import androidx.room.testing.MigrationTestHelper
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import io.github.wulkanowy.data.db.AppDatabase
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class Migration12Test {
|
||||
|
||||
private val dbName = "migration-test"
|
||||
|
||||
@get:Rule
|
||||
val helper: MigrationTestHelper = MigrationTestHelper(
|
||||
InstrumentationRegistry.getInstrumentation(),
|
||||
AppDatabase::class.java.canonicalName,
|
||||
FrameworkSQLiteOpenHelperFactory()
|
||||
)
|
||||
|
||||
@Test
|
||||
fun migrate11To12_twoNotRelatedStudents() {
|
||||
helper.createDatabase(dbName, 11).apply {
|
||||
// user 1
|
||||
createStudent(this, 1, true)
|
||||
createSemester(this, 1, false, 5, 1)
|
||||
createSemester(this, 1, true, 5, 2)
|
||||
|
||||
// user 2
|
||||
createStudent(this, 2, true)
|
||||
createSemester(this, 2, false, 6, 1)
|
||||
createSemester(this, 2, true, 6, 2)
|
||||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = db.studentDao.loadAll().blockingGet()
|
||||
|
||||
assertEquals(2, students.size)
|
||||
|
||||
students[0].run {
|
||||
assertEquals(1, studentId)
|
||||
assertEquals(5, classId)
|
||||
}
|
||||
|
||||
students[1].run {
|
||||
assertEquals(2, studentId)
|
||||
assertEquals(6, classId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun migrate11To12_removeStudentsWithoutClassId() {
|
||||
helper.createDatabase(dbName, 11).apply {
|
||||
// user 1
|
||||
createStudent(this, 1, true)
|
||||
createSemester(this, 1, false, 0, 2)
|
||||
createStudent(this, 2, true)
|
||||
createSemester(this, 2, true, 1, 2)
|
||||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = db.studentDao.loadAll().blockingGet()
|
||||
|
||||
assertEquals(1, students.size)
|
||||
|
||||
students[0].run {
|
||||
assertEquals(2, studentId)
|
||||
assertEquals(1, classId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun migrate11To12_ensureThereIsOnlyOneCurrentStudent() {
|
||||
helper.createDatabase(dbName, 11).apply {
|
||||
// user 1
|
||||
createStudent(this, 1, true)
|
||||
createSemester(this, 1, true, 5, 2)
|
||||
createStudent(this, 2, true)
|
||||
createSemester(this, 2, true, 6, 2)
|
||||
createStudent(this, 3, true)
|
||||
createSemester(this, 3, false, 7, 2)
|
||||
close()
|
||||
}
|
||||
|
||||
helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
|
||||
|
||||
val db = getMigratedRoomDatabase()
|
||||
val students = db.studentDao.loadAll().blockingGet()
|
||||
|
||||
assertEquals(3, students.size)
|
||||
|
||||
students[0].run {
|
||||
assertEquals(studentId, 1)
|
||||
assertEquals(false, isCurrent)
|
||||
}
|
||||
students[1].run {
|
||||
assertEquals(studentId, 2)
|
||||
assertEquals(false, isCurrent)
|
||||
}
|
||||
students[2].run {
|
||||
assertEquals(studentId, 3)
|
||||
assertEquals(true, isCurrent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createStudent(db: SupportSQLiteDatabase, studentId: Int, isCurrent: Boolean) {
|
||||
db.insert("Students", CONFLICT_FAIL, ContentValues().apply {
|
||||
put("endpoint", "https://fakelog.cf")
|
||||
put("loginType", "STANDARD")
|
||||
put("email", "jan@fakelog.cf")
|
||||
put("password", "******")
|
||||
put("symbol", "Default")
|
||||
put("student_id", studentId)
|
||||
put("student_name", "Jan Kowalski")
|
||||
put("school_id", "000123")
|
||||
put("school_name", "")
|
||||
put("is_current", isCurrent)
|
||||
put("registration_date", "0")
|
||||
})
|
||||
}
|
||||
|
||||
private fun createSemester(db: SupportSQLiteDatabase, studentId: Int, isCurrent: Boolean, classId: Int, diaryId: Int) {
|
||||
db.insert("Semesters", CONFLICT_FAIL, ContentValues().apply {
|
||||
put("student_id", studentId)
|
||||
put("diary_id", diaryId)
|
||||
put("diary_name", "IA")
|
||||
put("semester_id", diaryId * 5)
|
||||
put("semester_name", "1")
|
||||
put("is_current", isCurrent)
|
||||
put("class_id", classId)
|
||||
put("unit_id", "99")
|
||||
})
|
||||
}
|
||||
|
||||
private fun getMigratedRoomDatabase(): AppDatabase {
|
||||
val database = Room.databaseBuilder(ApplicationProvider.getApplicationContext(),
|
||||
AppDatabase::class.java, dbName)
|
||||
.addMigrations(Migration12())
|
||||
.build()
|
||||
// close the database and release any stream resources when the test finishes
|
||||
helper.closeWhenFinished(database)
|
||||
return database
|
||||
}
|
||||
}
|
@ -42,7 +42,7 @@ class RecipientLocalTest {
|
||||
))
|
||||
|
||||
val recipients = recipientLocal.getRecipients(
|
||||
Student("fakelog.cf", "AUTO", "", "", "", 1, "", "", "", true, LocalDateTime.now()),
|
||||
Student("fakelog.cf", "AUTO", "", "", "", 1, "", "", "", 1, true, LocalDateTime.now()),
|
||||
2,
|
||||
ReportingUnit(1, 4, "", 0, "", emptyList())
|
||||
).blockingGet()
|
||||
|
@ -39,7 +39,7 @@ class StudentLocalTest {
|
||||
|
||||
@Test
|
||||
fun saveAndReadTest() {
|
||||
studentLocal.saveStudent(Student(email = "test", password = "test123", schoolSymbol = "23", endpoint = "fakelog.cf", loginType = "AUTO", isCurrent = true, studentName = "", schoolName = "", studentId = 0, symbol = "", registrationDate = now()))
|
||||
studentLocal.saveStudent(Student(email = "test", password = "test123", schoolSymbol = "23", endpoint = "fakelog.cf", loginType = "AUTO", isCurrent = true, studentName = "", schoolName = "", studentId = 0, classId = 1, symbol = "", registrationDate = now()))
|
||||
.blockingGet()
|
||||
|
||||
val student = studentLocal.getCurrentStudent(true).blockingGet()
|
||||
|
@ -14,6 +14,7 @@ class ApiHelper @Inject constructor(private val api: Api) {
|
||||
symbol = student.symbol
|
||||
schoolSymbol = student.schoolSymbol
|
||||
studentId = student.studentId
|
||||
classId = student.classId
|
||||
host = URL(student.endpoint).run { host + ":$port".removeSuffix(":-1") }
|
||||
ssl = student.endpoint.startsWith("https")
|
||||
loginType = Api.LoginType.valueOf(student.loginType)
|
||||
@ -28,6 +29,7 @@ class ApiHelper @Inject constructor(private val api: Api) {
|
||||
this.symbol = symbol
|
||||
host = URL(endpoint).run { host + ":$port".removeSuffix(":-1") }
|
||||
ssl = endpoint.startsWith("https")
|
||||
useNewStudent = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ import io.github.wulkanowy.data.db.entities.Subject
|
||||
import io.github.wulkanowy.data.db.entities.Timetable
|
||||
import io.github.wulkanowy.data.db.migrations.Migration10
|
||||
import io.github.wulkanowy.data.db.migrations.Migration11
|
||||
import io.github.wulkanowy.data.db.migrations.Migration12
|
||||
import io.github.wulkanowy.data.db.migrations.Migration2
|
||||
import io.github.wulkanowy.data.db.migrations.Migration3
|
||||
import io.github.wulkanowy.data.db.migrations.Migration4
|
||||
@ -74,13 +75,13 @@ import javax.inject.Singleton
|
||||
Recipient::class
|
||||
],
|
||||
version = AppDatabase.VERSION_SCHEMA,
|
||||
exportSchema = false
|
||||
exportSchema = true
|
||||
)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
companion object {
|
||||
const val VERSION_SCHEMA = 11
|
||||
const val VERSION_SCHEMA = 12
|
||||
|
||||
fun newInstance(context: Context): AppDatabase {
|
||||
return Room.databaseBuilder(context, AppDatabase::class.java, "wulkanowy_database")
|
||||
@ -97,7 +98,8 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
Migration8(),
|
||||
Migration9(),
|
||||
Migration10(),
|
||||
Migration11()
|
||||
Migration11(),
|
||||
Migration12()
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
@ -18,6 +18,6 @@ interface SemesterDao {
|
||||
@Delete
|
||||
fun deleteAll(semester: List<Semester>)
|
||||
|
||||
@Query("SELECT * FROM Semesters WHERE student_id = :studentId")
|
||||
fun loadAll(studentId: Int): Maybe<List<Semester>>
|
||||
@Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId")
|
||||
fun loadAll(studentId: Int, classId: Int): Maybe<List<Semester>>
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import androidx.room.PrimaryKey
|
||||
import org.threeten.bp.LocalDateTime
|
||||
import java.io.Serializable
|
||||
|
||||
@Entity(tableName = "Students", indices = [Index(value = ["email", "symbol", "student_id", "school_id"], unique = true)])
|
||||
@Entity(tableName = "Students", indices = [Index(value = ["email", "symbol", "student_id", "school_id", "class_id"], unique = true)])
|
||||
data class Student(
|
||||
|
||||
val endpoint: String,
|
||||
@ -32,6 +32,9 @@ data class Student(
|
||||
@ColumnInfo(name = "school_name")
|
||||
val schoolName: String,
|
||||
|
||||
@ColumnInfo(name = "class_id")
|
||||
val classId: Int,
|
||||
|
||||
@ColumnInfo(name = "is_current")
|
||||
val isCurrent: Boolean,
|
||||
|
||||
|
@ -0,0 +1,69 @@
|
||||
package io.github.wulkanowy.data.db.migrations
|
||||
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration12 : Migration(11, 12) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
createTempStudentsTable(database)
|
||||
replaceStudentTable(database)
|
||||
updateStudentsWithClassId(database, getStudentsIds(database))
|
||||
removeStudentsWithNoClassId(database)
|
||||
ensureThereIsOnlyOneCurrentStudent(database)
|
||||
}
|
||||
|
||||
private fun createTempStudentsTable(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("""
|
||||
CREATE TABLE IF NOT EXISTS Students_tmp (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
endpoint TEXT NOT NULL,
|
||||
loginType TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
student_id INTEGER NOT NULL,
|
||||
student_name TEXT NOT NULL,
|
||||
school_id TEXT NOT NULL,
|
||||
school_name TEXT NOT NULL,
|
||||
is_current INTEGER NOT NULL,
|
||||
registration_date INTEGER NOT NULL,
|
||||
class_id INTEGER NOT NULL
|
||||
)
|
||||
""")
|
||||
database.execSQL("CREATE UNIQUE INDEX index_Students_email_symbol_student_id_school_id_class_id ON Students_tmp (email, symbol, student_id, school_id, class_id)")
|
||||
}
|
||||
|
||||
private fun replaceStudentTable(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE Students ADD COLUMN class_id INTEGER DEFAULT 0 NOT NULL")
|
||||
database.execSQL("INSERT INTO Students_tmp SELECT * FROM Students")
|
||||
database.execSQL("DROP TABLE Students")
|
||||
database.execSQL("ALTER TABLE Students_tmp RENAME TO Students")
|
||||
}
|
||||
|
||||
private fun getStudentsIds(database: SupportSQLiteDatabase): List<Int> {
|
||||
val students = mutableListOf<Int>()
|
||||
val studentsCursor = database.query("SELECT student_id FROM Students")
|
||||
if (studentsCursor.moveToFirst()) {
|
||||
do {
|
||||
students.add(studentsCursor.getInt(0))
|
||||
} while (studentsCursor.moveToNext())
|
||||
}
|
||||
return students
|
||||
}
|
||||
|
||||
private fun updateStudentsWithClassId(database: SupportSQLiteDatabase, students: List<Int>) {
|
||||
students.forEach {
|
||||
database.execSQL("UPDATE Students SET class_id = IFNULL((SELECT class_id FROM Semesters WHERE student_id = $it), 0) WHERE student_id = $it")
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeStudentsWithNoClassId(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("DELETE FROM Students WHERE class_id = 0")
|
||||
}
|
||||
|
||||
private fun ensureThereIsOnlyOneCurrentStudent(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("UPDATE Students SET is_current = 0")
|
||||
database.execSQL("UPDATE Students SET is_current = 1 WHERE id = (SELECT MAX(id) FROM Students)")
|
||||
}
|
||||
}
|
@ -19,6 +19,6 @@ class SemesterLocal @Inject constructor(private val semesterDb: SemesterDao) {
|
||||
}
|
||||
|
||||
fun getSemesters(student: Student): Maybe<List<Semester>> {
|
||||
return semesterDb.loadAll(student.studentId).filter { !it.isEmpty() }
|
||||
return semesterDb.loadAll(student.studentId, student.classId).filter { !it.isEmpty() }
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ class StudentRemote @Inject constructor(private val api: Api) {
|
||||
studentName = student.studentName,
|
||||
schoolSymbol = student.schoolSymbol,
|
||||
schoolName = student.schoolName,
|
||||
classId = student.classId,
|
||||
endpoint = endpoint,
|
||||
loginType = student.loginType.name,
|
||||
isCurrent = false,
|
||||
|
@ -22,7 +22,7 @@ class StudentRemoteTest {
|
||||
|
||||
@Test
|
||||
fun testRemoteAll() {
|
||||
doReturn(Single.just(listOf(Student("", "", 1, "test", "", "", Api.LoginType.AUTO))))
|
||||
doReturn(Single.just(listOf(Student("", "", 1, "test", "", "", 1, Api.LoginType.AUTO))))
|
||||
.`when`(mockApi).getStudents()
|
||||
|
||||
val students = StudentRemote(mockApi).getStudents("", "", "").blockingGet()
|
||||
|
@ -86,7 +86,7 @@ class LoginFormPresenterTest {
|
||||
|
||||
@Test
|
||||
fun loginTest() {
|
||||
val studentTest = Student(email = "test@", password = "123", endpoint = "https://fakelog.cf", loginType = "AUTO", studentName = "", schoolSymbol = "", schoolName = "", studentId = 0, isCurrent = false, symbol = "", registrationDate = now())
|
||||
val studentTest = Student(email = "test@", password = "123", endpoint = "https://fakelog.cf", loginType = "AUTO", studentName = "", schoolSymbol = "", schoolName = "", studentId = 0, classId = 1, isCurrent = false, symbol = "", registrationDate = now())
|
||||
doReturn(Single.just(listOf(studentTest)))
|
||||
.`when`(repository).getStudents(anyString(), anyString(), anyString(), anyString())
|
||||
|
||||
|
@ -32,7 +32,7 @@ class LoginStudentSelectPresenterTest {
|
||||
|
||||
private lateinit var presenter: LoginStudentSelectPresenter
|
||||
|
||||
private val testStudent by lazy { Student(email = "test", password = "test123", endpoint = "https://fakelog.cf", loginType = "AUTO", symbol = "", isCurrent = false, studentId = 0, schoolName = "", schoolSymbol = "", studentName = "", registrationDate = now()) }
|
||||
private val testStudent by lazy { Student(email = "test", password = "test123", endpoint = "https://fakelog.cf", loginType = "AUTO", symbol = "", isCurrent = false, studentId = 0, schoolName = "", schoolSymbol = "", classId = 1, studentName = "", registrationDate = now()) }
|
||||
|
||||
private val testException by lazy { RuntimeException("Problem") }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user