Skip to content

Commit

Permalink
Exclude Room metadata when clearing DB (#343)
Browse files Browse the repository at this point in the history
* -provide Room metadata table name and method to exclude it from clearing DB process
-implement test case: doesNotDeleteRoomMetadata

* -replace deprecated method: InstrumentationRegistry.getTargetContext()

* -fix to exclude always Room metadata from clearing

Co-authored-by: Javier Marsicano <[email protected]>
  • Loading branch information
jmarsican and Javier Marsicano authored May 7, 2020
1 parent ea4593f commit 7e7df53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ClearDatabaseRule(private val databaseOperations: DatabaseOperations = Dat
companion object {
@JvmField
internal val UNWANTED_FILENAME_SUFFIXES = arrayOf("-journal", "-shm", "-uid", "-wal")
const val ROOM_METADATA = "room_master_table"
}

private var excludeTablesRegex: Regex? = null
Expand Down Expand Up @@ -42,6 +43,7 @@ class ClearDatabaseRule(private val databaseOperations: DatabaseOperations = Dat
.use { database ->
getTableNames(database)
.filterNot { excludeTablesRegex?.matches(it) ?: false }
.filterNot { it == ROOM_METADATA }
.forEach { tableName ->
deleteTableContent(database, tableName)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.schibsted.spain.barista.rule.cleardata.internal

import android.database.sqlite.SQLiteDatabase
import androidx.test.InstrumentationRegistry
import androidx.test.platform.app.InstrumentationRegistry
import java.io.File
import java.util.ArrayList

class DatabaseOperations {

fun getAllDatabaseFiles(): List<File> {
return InstrumentationRegistry.getTargetContext()
return InstrumentationRegistry.getInstrumentation().targetContext
.let { context ->
context.databaseList()
.map { context.getDatabasePath(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ public void doesNotDeleteTable_whenNameMatchesRegex() throws Throwable {
verify(operations, atLeastOnce()).deleteTableContent(DB_1, "some_table");
}

@Test
public void doesNotDeleteRoomMetadata() throws Throwable {
String givenTableName = "some_table";
given(operations.getAllDatabaseFiles()).willReturn(singletonList(DB_1_FILE));
given(operations.getTableNames(DB_1)).willReturn(asList(givenTableName, ClearDatabaseRule.ROOM_METADATA));

ClearDatabaseRule rule = new ClearDatabaseRule(operations);
executeRule(rule);

verify(operations, never()).deleteTableContent(DB_1, ClearDatabaseRule.ROOM_METADATA);
verify(operations, atLeastOnce()).deleteTableContent(DB_1, givenTableName);
}

private void executeRule(ClearDatabaseRule rule) throws Throwable {
rule.apply(dummyStatement, dummyDescription).evaluate();
}
Expand Down

0 comments on commit 7e7df53

Please sign in to comment.