parent
c43600ba25
commit
58bf62f8db
@ -0,0 +1,113 @@ |
|||||||
|
package net.pokeranalytics.android.migrations |
||||||
|
|
||||||
|
import android.os.Environment |
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4 |
||||||
|
import androidx.test.platform.app.InstrumentationRegistry |
||||||
|
import androidx.test.rule.GrantPermissionRule |
||||||
|
import io.realm.Realm |
||||||
|
import io.realm.RealmConfiguration |
||||||
|
import io.realm.kotlin.where |
||||||
|
import net.pokeranalytics.android.model.migrations.PokerAnalyticsMigration |
||||||
|
import net.pokeranalytics.android.model.realm.Session |
||||||
|
import org.junit.Before |
||||||
|
import org.junit.Rule |
||||||
|
import org.junit.Test |
||||||
|
import org.junit.runner.RunWith |
||||||
|
import timber.log.Timber |
||||||
|
import java.io.File |
||||||
|
import java.io.IOException |
||||||
|
import java.io.InputStream |
||||||
|
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class) |
||||||
|
class MigrationsInstrumentedUnitTest { |
||||||
|
|
||||||
|
@get:Rule |
||||||
|
var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) |
||||||
|
|
||||||
|
private lateinit var mockRealm: Realm |
||||||
|
private lateinit var directory: File |
||||||
|
private lateinit var dbFileName: String |
||||||
|
|
||||||
|
@Before |
||||||
|
fun setup() { |
||||||
|
|
||||||
|
Timber.d("*** Setup realm migration") |
||||||
|
|
||||||
|
directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) |
||||||
|
dbFileName = "schema_0.realm" |
||||||
|
|
||||||
|
val context = InstrumentationRegistry.getInstrumentation().context |
||||||
|
val inputStream = context.assets.open(dbFileName) |
||||||
|
copyBundledRealmFile(inputStream, dbFileName) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
// TODO: Uncomment this when we'll have migrations to execute (should crash if we don't apply the migration) |
||||||
|
//@Test(expected = io.realm.exceptions.RealmMigrationNeededException::class) |
||||||
|
fun testMigrationNeeded() { |
||||||
|
|
||||||
|
Timber.d("*** testMigrationNeeded") |
||||||
|
|
||||||
|
val config0 = RealmConfiguration.Builder() |
||||||
|
.directory(directory) |
||||||
|
.name(dbFileName) |
||||||
|
.schemaVersion(0) |
||||||
|
.build() |
||||||
|
|
||||||
|
Realm.migrateRealm(config0, PokerAnalyticsMigration()) |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
fun testMigrationSuccess() { |
||||||
|
|
||||||
|
Timber.d("*** testMigrationSuccess") |
||||||
|
|
||||||
|
val config1 = RealmConfiguration.Builder() |
||||||
|
.directory(directory) |
||||||
|
.name(dbFileName) |
||||||
|
// TODO: Set the correct schema version when we'll have migration to avoid crash |
||||||
|
.schemaVersion(0) |
||||||
|
.build() |
||||||
|
|
||||||
|
Realm.migrateRealm(config1, PokerAnalyticsMigration()) |
||||||
|
|
||||||
|
this.mockRealm = Realm.getInstance(config1) |
||||||
|
|
||||||
|
val sessions = this.mockRealm.where<Session>().findAll() |
||||||
|
|
||||||
|
Timber.d("*** Sessions: ${sessions.size}") |
||||||
|
this.mockRealm.close() |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Copy [inputStream] to [outFileName] |
||||||
|
*/ |
||||||
|
private fun copyBundledRealmFile(inputStream: InputStream, outFileName: String): String? { |
||||||
|
try { |
||||||
|
|
||||||
|
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), outFileName) |
||||||
|
val outputStream = file.outputStream() |
||||||
|
|
||||||
|
inputStream.use { input -> |
||||||
|
outputStream.use { fileOut -> |
||||||
|
input.copyTo(fileOut) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inputStream.close() |
||||||
|
outputStream.close() |
||||||
|
|
||||||
|
Timber.d("File: ${file.length()}") |
||||||
|
|
||||||
|
return file.absolutePath |
||||||
|
} catch (e: IOException) { |
||||||
|
e.printStackTrace() |
||||||
|
} |
||||||
|
|
||||||
|
return null |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue