Skip to content

Commit

Permalink
Add AssetMigrationSource test
Browse files Browse the repository at this point in the history
- Fix AssetMigrationSource regex pattern
  • Loading branch information
ekigamba committed Feb 3, 2021
1 parent fdc7abd commit fdcec93
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public HashMap<Integer, ArrayList<Migration>> getMigrations(int fromDbVersion) {
try {
String[] migrationFileNames = context.getAssets().list("config/migrations");

String regex = "(/d)/.(up|down)/.sql";
String regex = "(\\d)\\.(up|down)\\.sql";
Pattern filePattern = Pattern.compile(regex);

if (migrationFileNames != null) {
Expand All @@ -46,7 +46,7 @@ public HashMap<Integer, ArrayList<Migration>> getMigrations(int fromDbVersion) {
Matcher fileNameMatcher = filePattern.matcher(migrationFile);
if (fileNameMatcher.matches()) {
String versionString = fileNameMatcher.group(1);
String migrationType = fileNameMatcher.group(2);
String migrationType = fileNameMatcher.group(2).toUpperCase();

int version = Integer.parseInt(versionString);

Expand Down
1 change: 1 addition & 0 deletions opensrp-app/src/test/assets/config/migrations/1.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE IF NOT EXISTS TABLE key_value(key VARCHAR, val VARCHAR);
1 change: 1 addition & 0 deletions opensrp-app/src/test/assets/config/migrations/2.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE IF NOT EXISTS TABLE clients(id INTEGER, full_name VARCHAR, age INTEGER, dob INTEGER);
1 change: 1 addition & 0 deletions opensrp-app/src/test/assets/config/migrations/3.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE IF NOT EXISTS TABLE events(event_id INTEGER, json VARCHAR, server_version INTEGER);
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.smartregister.repository.dao;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.RuntimeEnvironment;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.repository.contract.MigrationSource;

import java.util.ArrayList;
import java.util.HashMap;

import static org.junit.Assert.*;

/**
* Created by Ephraim Kigamba - [email protected] on 02-02-2021.
*/
public class AssetMigrationSourceTest extends BaseRobolectricUnitTest {

private AssetMigrationSource assetMigrationSource;

@Before
public void setUp() throws Exception {
assetMigrationSource = new AssetMigrationSource(RuntimeEnvironment.application);
}

@After
public void tearDown() throws Exception {
}

@Test
public void getMigrations() {
HashMap<Integer, ArrayList<MigrationSource.Migration>> migrations =
assetMigrationSource.getMigrations();

assertEquals(3, migrations.size());
assertEquals(MigrationSource.Migration.MigrationType.UP, migrations.get(1).get(0).getMigrationType());
assertEquals(MigrationSource.Migration.MigrationType.UP, migrations.get(2).get(0).getMigrationType());
assertEquals("CREATE IF NOT EXISTS TABLE clients(id INTEGER, full_name VARCHAR, age INTEGER, dob INTEGER);"
, migrations.get(2).get(0).getUpMigrationQueries()[0]);
assertEquals(MigrationSource.Migration.MigrationType.UP, migrations.get(3).get(0).getMigrationType());
}

@Test
public void testGetMigrations() {

HashMap<Integer, ArrayList<MigrationSource.Migration>> migrations =
assetMigrationSource.getMigrations(2);

assertEquals(2, migrations.size());
assertEquals(MigrationSource.Migration.MigrationType.UP, migrations.get(2).get(0).getMigrationType());
assertEquals("CREATE IF NOT EXISTS TABLE clients(id INTEGER, full_name VARCHAR, age INTEGER, dob INTEGER);"
, migrations.get(2).get(0).getUpMigrationQueries()[0]);
assertEquals(MigrationSource.Migration.MigrationType.UP, migrations.get(3).get(0).getMigrationType());

}
}

0 comments on commit fdcec93

Please sign in to comment.