Skip to content

Commit

Permalink
Add attempt at Espresso test
Browse files Browse the repository at this point in the history
The goal would be to use the last assert in the test method but it always fails, claiming that the table is unmodified from its original state. The tmp table does have the intended columns and contents.
  • Loading branch information
lognaturel committed Jul 30, 2019
1 parent db4dd85 commit 3709176
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 16 deletions.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.odk.collect.android.database.helpers;

import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.utilities.FileUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;

@RunWith(Parameterized.class)
public class InstanceDatabaseHelperTest {
@Parameterized.Parameter
public String description;

@Parameterized.Parameter(1)
public String dbFilename;

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
{"Downgrading from version with extra column drops that column", "instances_v7000_added_fakeColumn.db"},
{"Downgrading from version with missing column adds that column", "instances_v7000_removed_jrVersion.db"}
});
}

private static final String DATABASE_PATH = Collect.METADATA_PATH + File.separator + InstancesDatabaseHelper.DATABASE_NAME;
private static final String TEMPORARY_EXTENSION = ".real";

@Test
public void testDowngrade() throws IOException {
saveRealDb();

writeDatabaseFile("database" + File.separator + dbFilename);
InstancesDatabaseHelper databaseHelper = new InstancesDatabaseHelper();

SQLiteDatabase db = databaseHelper.getReadableDatabase();
assertThat(db.getVersion(), is(InstancesDatabaseHelper.DATABASE_VERSION));

List<String> newColumnNames = InstancesDatabaseHelper.getInstancesColumnNames(db);

try (Cursor c = db.query("instances_tmp", null, null, null, null, null, null)) {
assertThat(Arrays.asList(c.getColumnNames()), contains(InstancesDatabaseHelper.CURRENT_VERSION_COLUMN_NAMES));
}

//assertThat(newColumnNames, contains(InstancesDatabaseHelper.CURRENT_VERSION_COLUMN_NAMES));

restoreRealDb();
}

private void writeDatabaseFile(String dbPath) throws IOException {
AssetManager assetManager = InstrumentationRegistry.getInstrumentation().getContext().getAssets();
try (InputStream input = assetManager.open(dbPath);
OutputStream output = new FileOutputStream(DATABASE_PATH)) {
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
}
}

private void saveRealDb() {
FileUtils.copyFile(new File(DATABASE_PATH), new File(DATABASE_PATH + TEMPORARY_EXTENSION));
}

private void restoreRealDb() {
FileUtils.copyFile(new File(DATABASE_PATH + TEMPORARY_EXTENSION), new File(DATABASE_PATH));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@
* This class helps open, create, and upgrade the database file.
*/
public class InstancesDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "instances.db";
static final String DATABASE_NAME = "instances.db";
public static final String INSTANCES_TABLE_NAME = "instances";

private static final int DATABASE_VERSION = 5;
static final int DATABASE_VERSION = 5;

private final String[] instancesTableColumnsInVersion5 = new String[] {_ID, DISPLAY_NAME, SUBMISSION_URI, CAN_EDIT_WHEN_COMPLETE,
private static final String[] COLUMN_NAMES_V5 = new String[] {_ID, DISPLAY_NAME, SUBMISSION_URI, CAN_EDIT_WHEN_COMPLETE,
INSTANCE_FILE_PATH, JR_FORM_ID, JR_VERSION, STATUS, LAST_STATUS_CHANGE_DATE, DELETED_DATE};
public static final String[] CURRENT_VERSION_COLUMN_NAMES = COLUMN_NAMES_V5;

public InstancesDatabaseHelper() {
super(new DatabaseContext(Collect.METADATA_PATH), DATABASE_NAME, null, DATABASE_VERSION);
Expand Down Expand Up @@ -134,7 +135,7 @@ private void upgradeToVersion4(SQLiteDatabase db) {
* removing a column. See https://sqlite.org/lang_altertable.html
*/
private void moveInstancesTableToVersion5(SQLiteDatabase db) {
List<String> columnNamesPrev = getColumnNames(db);
List<String> columnNamesPrev = getInstancesColumnNames(db);

String temporaryTableName = INSTANCES_TABLE_NAME + "_tmp";

Expand All @@ -149,7 +150,7 @@ private void moveInstancesTableToVersion5(SQLiteDatabase db) {
createInstancesTableV5(db, temporaryTableName);

// Only select columns from the existing table that are also relevant to v5
columnNamesPrev.retainAll(new ArrayList<>(Arrays.asList(instancesTableColumnsInVersion5)));
columnNamesPrev.retainAll(new ArrayList<>(Arrays.asList(COLUMN_NAMES_V5)));

CustomSQLiteQueryBuilder
.begin(db)
Expand All @@ -160,16 +161,16 @@ private void moveInstancesTableToVersion5(SQLiteDatabase db) {
.from(INSTANCES_TABLE_NAME)
.end();

CustomSQLiteQueryBuilder
.begin(db)
.dropIfExists(INSTANCES_TABLE_NAME)
.end();

CustomSQLiteQueryBuilder
.begin(db)
.renameTable(temporaryTableName)
.to(INSTANCES_TABLE_NAME)
.end();
// CustomSQLiteQueryBuilder
// .begin(db)
// .dropIfExists(INSTANCES_TABLE_NAME)
// .end();
//
// CustomSQLiteQueryBuilder
// .begin(db)
// .renameTable(temporaryTableName)
// .to(INSTANCES_TABLE_NAME)
// .end();
}

private void createInstancesTableV5(SQLiteDatabase db, String name) {
Expand All @@ -186,7 +187,7 @@ private void createInstancesTableV5(SQLiteDatabase db, String name) {
+ DELETED_DATE + " date );");
}

private List<String> getColumnNames(SQLiteDatabase db) {
static List<String> getInstancesColumnNames(SQLiteDatabase db) {
String[] columnNames;
try (Cursor c = db.query(INSTANCES_TABLE_NAME, null, null, null, null, null, null)) {
columnNames = c.getColumnNames();
Expand Down

0 comments on commit 3709176

Please sign in to comment.