From 040857ba20ce34aef3aa5cbda7f59db172f5a385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 19 Mar 2019 13:23:52 +0100 Subject: [PATCH] Change grade weightValue type to double (#285) --- .travis.yml | 6 ++-- app/build.gradle | 2 +- .../data/repositories/grade/GradeLocalTest.kt | 6 ++-- .../repositories/grade/GradeRepositoryTest.kt | 22 ++++++------ .../grade/TestGradeEntityCreator.kt | 4 +-- .../github/wulkanowy/data/db/AppDatabase.kt | 6 ++-- .../wulkanowy/data/db/entities/Grade.kt | 2 +- .../data/db/migrations/Migration11.kt | 34 +++++++++++++++++++ .../wulkanowy/utils/GradeExtensionTest.kt | 24 ++++++------- 9 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration11.kt diff --git a/.travis.yml b/.travis.yml index e1d86ee37..8920018ed 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,9 +11,9 @@ cache: - $HOME/.gradle/caches/ - $HOME/.gradle/wrapper/ -#branches: -# only: -# - master +branches: + only: + - master android: licenses: diff --git a/app/build.gradle b/app/build.gradle index b18a22cc4..f7fd82c0e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -77,7 +77,7 @@ play { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation('io.github.wulkanowy:api:0.7.0') { exclude module: "threetenbp" } + implementation('com.github.wulkanowy:api:0aea843800') { exclude module: "threetenbp" } implementation "androidx.legacy:legacy-support-v4:1.0.0" implementation "androidx.appcompat:appcompat:1.0.2" diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt index e50a52fff..36238f1b4 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt @@ -34,9 +34,9 @@ class GradeLocalTest { @Test fun saveAndReadTest() { gradeLocal.saveGrades(listOf( - createGradeLocal(5, 3, LocalDate.of(2018, 9, 10), "", 1), - createGradeLocal(4, 4, LocalDate.of(2019, 2, 27), "", 2), - createGradeLocal(3, 5, LocalDate.of(2019, 2, 28), "", 2) + createGradeLocal(5, 3.0, LocalDate.of(2018, 9, 10), "", 1), + createGradeLocal(4, 4.0, LocalDate.of(2019, 2, 27), "", 2), + createGradeLocal(3, 5.0, LocalDate.of(2019, 2, 28), "", 2) )) val grades = gradeLocal diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt index 7da2d4553..17e788fc0 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt @@ -71,10 +71,10 @@ class GradeRepositoryTest { @Test fun markOlderThanRegisterDateAsRead() { every { mockApi.getGrades(1) } returns Single.just(listOf( - createGradeApi(5, 4, of(2019, 2, 25), "Ocena pojawiła się"), - createGradeApi(5, 4, of(2019, 2, 26), "przed zalogowanie w aplikacji"), - createGradeApi(5, 4, of(2019, 2, 27), "Ocena z dnia logowania"), - createGradeApi(5, 4, of(2019, 2, 28), "Ocena jeszcze nowsza") + createGradeApi(5, 4.0, of(2019, 2, 25), "Ocena pojawiła się"), + createGradeApi(5, 4.0, of(2019, 2, 26), "przed zalogowanie w aplikacji"), + createGradeApi(5, 4.0, of(2019, 2, 27), "Ocena z dnia logowania"), + createGradeApi(5, 4.0, of(2019, 2, 28), "Ocena jeszcze nowsza") )) val grades = GradeRepository(settings, gradeLocal, gradeRemote) @@ -89,16 +89,16 @@ class GradeRepositoryTest { @Test fun mitigateOldGradesNotifications() { gradeLocal.saveGrades(listOf( - createGradeLocal(5, 3, of(2019, 2, 25), "Jedna ocena"), - createGradeLocal(4, 4, of(2019, 2, 26), "Druga"), - createGradeLocal(3, 5, of(2019, 2, 27), "Trzecia") + createGradeLocal(5, 3.0, of(2019, 2, 25), "Jedna ocena"), + createGradeLocal(4, 4.0, of(2019, 2, 26), "Druga"), + createGradeLocal(3, 5.0, of(2019, 2, 27), "Trzecia") )) every { mockApi.getGrades(1) } returns Single.just(listOf( - createGradeApi(5, 2, of(2019, 2, 25), "Ocena ma datę, jest inna, ale nie zostanie powiadomiona"), - createGradeApi(4, 3, of(2019, 2, 26), "starszą niż ostatnia lokalnie"), - createGradeApi(3, 4, of(2019, 2, 27), "Ta jest z tego samego dnia co ostatnia lokalnie"), - createGradeApi(2, 5, of(2019, 2, 28), "Ta jest już w ogóle nowa") + createGradeApi(5, 2.0, of(2019, 2, 25), "Ocena ma datę, jest inna, ale nie zostanie powiadomiona"), + createGradeApi(4, 3.0, of(2019, 2, 26), "starszą niż ostatnia lokalnie"), + createGradeApi(3, 4.0, of(2019, 2, 27), "Ta jest z tego samego dnia co ostatnia lokalnie"), + createGradeApi(2, 5.0, of(2019, 2, 28), "Ta jest już w ogóle nowa") )) val grades = GradeRepository(settings, gradeLocal, gradeRemote) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt index 485e56922..e0fd05a82 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt @@ -5,7 +5,7 @@ import org.threeten.bp.LocalDate import io.github.wulkanowy.api.grades.Grade as GradeRemote import io.github.wulkanowy.data.db.entities.Grade as GradeLocal -fun createGradeLocal(value: Int, weight: Int, date: LocalDate, desc: String, semesterId: Int = 1): GradeLocal { +fun createGradeLocal(value: Int, weight: Double, date: LocalDate, desc: String, semesterId: Int = 1): GradeLocal { return GradeLocal( semesterId = semesterId, studentId = 1, @@ -24,7 +24,7 @@ fun createGradeLocal(value: Int, weight: Int, date: LocalDate, desc: String, sem ) } -fun createGradeApi(value: Int, weight: Int, date: LocalDate, desc: String): GradeRemote { +fun createGradeApi(value: Int, weight: Double, date: LocalDate, desc: String): GradeRemote { return GradeRemote().apply { this.value = value this.weightValue = weight diff --git a/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt b/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt index 1601c91a3..4fefd9c90 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt @@ -41,6 +41,7 @@ import io.github.wulkanowy.data.db.entities.Student 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.Migration2 import io.github.wulkanowy.data.db.migrations.Migration3 import io.github.wulkanowy.data.db.migrations.Migration4 @@ -79,7 +80,7 @@ import javax.inject.Singleton abstract class AppDatabase : RoomDatabase() { companion object { - const val VERSION_SCHEMA = 10 + const val VERSION_SCHEMA = 11 fun newInstance(context: Context): AppDatabase { return Room.databaseBuilder(context, AppDatabase::class.java, "wulkanowy_database") @@ -95,7 +96,8 @@ abstract class AppDatabase : RoomDatabase() { Migration7(), Migration8(), Migration9(), - Migration10() + Migration10(), + Migration11() ) .build() } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt index fdf25c9ea..1221a7aab 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt @@ -34,7 +34,7 @@ data class Grade( val weight: String, - val weightValue: Int, + val weightValue: Double, val date: LocalDate, diff --git a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration11.kt b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration11.kt new file mode 100644 index 000000000..cb437c0ee --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration11.kt @@ -0,0 +1,34 @@ +package io.github.wulkanowy.data.db.migrations + +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase + +class Migration11 : Migration(10, 11) { + + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL(""" + CREATE TABLE IF NOT EXISTS Grades_temp ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + is_read INTEGER NOT NULL, + is_notified INTEGER NOT NULL, + semester_id INTEGER NOT NULL, + student_id INTEGER NOT NULL, + subject TEXT NOT NULL, + entry TEXT NOT NULL, + value INTEGER NOT NULL, + modifier REAL NOT NULL, + comment TEXT NOT NULL, + color TEXT NOT NULL, + grade_symbol TEXT NOT NULL, + description TEXT NOT NULL, + weight TEXT NOT NULL, + weightValue REAL NOT NULL, + date INTEGER NOT NULL, + teacher TEXT NOT NULL + ) + """) + database.execSQL("INSERT INTO Grades_temp SELECT * FROM Grades") + database.execSQL("DROP TABLE Grades") + database.execSQL("ALTER TABLE Grades_temp RENAME TO Grades") + } +} diff --git a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt index 12a086edd..3b1ab648c 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt @@ -22,11 +22,11 @@ class GradeExtensionTest { @Test fun calcWeightedAverage() { assertEquals(3.47, listOf( - createGrade(5, 6, 0.33), - createGrade(5, 5, -0.33), - createGrade(4, 1, 0.0), - createGrade(1, 9, 0.5), - createGrade(0, 0, 0.0) + createGrade(5, 6.0, 0.33), + createGrade(5, 5.0, -0.33), + createGrade(4, 1.0, 0.0), + createGrade(1, 9.0, 0.5), + createGrade(0, .0, 0.0) ).calcAverage(), 0.005) } @@ -42,23 +42,23 @@ class GradeExtensionTest { @Test fun changeModifier_default() { - assertEquals(.33, createGrade(5, 0, .33).changeModifier(.0, .0).modifier, .0) - assertEquals(-.33, createGrade(5, 0, -.33).changeModifier(.0, .0).modifier, .0) + assertEquals(.33, createGrade(5, .0, .33).changeModifier(.0, .0).modifier, .0) + assertEquals(-.33, createGrade(5, .0, -.33).changeModifier(.0, .0).modifier, .0) } @Test fun changeModifier_plus() { - assertEquals(.33, createGrade(5, 0, .25).changeModifier(.33, .50).modifier, .0) - assertEquals(.25, createGrade(5, 0, .33).changeModifier(.25, .0).modifier, .0) + assertEquals(.33, createGrade(5, .0, .25).changeModifier(.33, .50).modifier, .0) + assertEquals(.25, createGrade(5, .0, .33).changeModifier(.25, .0).modifier, .0) } @Test fun changeModifier_minus() { - assertEquals(-.33, createGrade(5, 0, -.25).changeModifier(.25, .33).modifier, .0) - assertEquals(-.25, createGrade(5, 0, -.33).changeModifier(.0, .25).modifier, .0) + assertEquals(-.33, createGrade(5, .0, -.25).changeModifier(.25, .33).modifier, .0) + assertEquals(-.25, createGrade(5, .0, -.33).changeModifier(.0, .25).modifier, .0) } - private fun createGrade(value: Int, weightValue: Int = 0, modifier: Double = 0.25): Grade { + private fun createGrade(value: Int, weightValue: Double = .0, modifier: Double = 0.25): Grade { return Grade( semesterId = 1, studentId = 1,