Ver Fonte

Add test for DB 67 -> 68 migration

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey há 2 anos atrás
pai
commit
9053bc96a7

+ 7 - 1
app/build.gradle

@@ -222,6 +222,10 @@ android {
         htmlReport true
     }
 
+    sourceSets {
+        // Adds exported schema location as test app assets.
+        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
+    }
 }
 
 dependencies {
@@ -232,7 +236,8 @@ dependencies {
         exclude group: 'org.ogce', module: 'xpp3' // unused in Android and brings wrong Junit version
     }
 
-    compileOnly 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' // remove after entire switch to lib v2
+    compileOnly 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
+    // remove after entire switch to lib v2
     implementation "commons-httpclient:commons-httpclient:3.1@jar" // remove after entire switch to lib v2
     implementation 'org.apache.jackrabbit:jackrabbit-webdav:2.13.5' // remove after entire switch to lib v2
     implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
@@ -366,6 +371,7 @@ dependencies {
 
     implementation "androidx.room:room-runtime:$roomVersion"
     kapt "androidx.room:room-compiler:$roomVersion"
+    androidTestImplementation "androidx.room:room-testing:$roomVersion"
 
 }
 

+ 97 - 0
app/src/androidTest/java/com/nextcloud/client/database/migrations/MigrationTest.kt

@@ -0,0 +1,97 @@
+/*
+ * Nextcloud Android client application
+ *
+ *  @author Álvaro Brey
+ *  Copyright (C) 2023 Álvaro Brey
+ *  Copyright (C) 2023 Nextcloud GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.nextcloud.client.database.migrations
+
+import androidx.room.testing.MigrationTestHelper
+import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import androidx.test.platform.app.InstrumentationRegistry
+import com.nextcloud.client.database.NextcloudDatabase
+import org.junit.Assert.assertEquals
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+import java.io.IOException
+
+@RunWith(AndroidJUnit4::class)
+class MigrationTest {
+
+    @get:Rule
+    val helper: MigrationTestHelper = MigrationTestHelper(
+        InstrumentationRegistry.getInstrumentation(),
+        NextcloudDatabase::class.java.canonicalName,
+        FrameworkSQLiteOpenHelperFactory()
+    )
+
+    @Test
+    @Throws(IOException::class)
+    fun migrate67to68() {
+        val nullId = 6
+        val notNullId = 7
+        val notNullLocalIdValue = 1234
+
+        var db = helper.createDatabase(TEST_DB, 67)
+
+        // create some data
+        db.apply {
+            execSQL(
+                "INSERT INTO filelist VALUES($nullId,'foo.zip','foo.zip','/foo.zip','/foo.zip',1,1643648081," +
+                    "1643648081000,'application/zip',178382355,NULL,'test@nextcloud',1674554955638,0,0,''," +
+                    "'f45028679b68652c6b345b5d8c9a5d63',0,'RGDNVW','00014889ocb5tqw7y2f3',NULL,0,0,0,0,NULL,0,NULL," +
+                    "0,0,'test','test','','[]',NULL,'null',0,-1,NULL,NULL,NULL,0,0,NULL);"
+            )
+            execSQL(
+                "INSERT INTO filelist VALUES($notNullId,'foo.zip','foo.zip','/foo.zip','/foo.zip',1,1643648081," +
+                    "1643648081000,'application/zip',178382355,NULL,'test@nextcloud',1674554955638,0,0,''," +
+                    "'f45028679b68652c6b345b5d8c9a5d63',0,'RGDNVW','00014889ocb5tqw7y2f3',NULL,0,0,0,0,NULL,0,NULL," +
+                    "0,0,'test','test','','[]',NULL,'null',0,-1,NULL,NULL,NULL,0,0,NULL);"
+            )
+            execSQL("UPDATE filelist SET local_id = NULL WHERE _id = $nullId")
+            execSQL("UPDATE filelist SET local_id = $notNullLocalIdValue WHERE _id = $notNullId")
+
+            close()
+        }
+
+        // run migration and validate schema matches
+        db = helper.runMigrationsAndValidate(TEST_DB, 68, true, Migration67to68())
+
+        // check values are correct
+        db.query("SELECT local_id FROM filelist WHERE _id=$nullId").use { cursor ->
+            cursor.moveToFirst()
+            val localId = cursor.getInt(cursor.getColumnIndex("local_id"))
+            assertEquals("NULL localId is not -1 after migration", -1, localId)
+        }
+
+        db.query("SELECT local_id FROM filelist WHERE _id=$notNullId").use { cursor ->
+            cursor.moveToFirst()
+            val localId = cursor.getInt(cursor.getColumnIndex("local_id"))
+            assertEquals("Not null localId is not the same after migration", notNullLocalIdValue, localId)
+        }
+
+        db.close()
+    }
+
+    companion object {
+        private const val TEST_DB = "migration-test"
+    }
+}