From e20a180155b190f701f57a257e0087b587730b42 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Wed, 3 Feb 2021 15:52:42 +0300 Subject: [PATCH] Add test for Repository#onUpgrade --- .../src/test/assets/config/migrations/1.up.sql | 2 +- .../org/smartregister/TestSyncConfiguration.java | 5 +++++ .../repository/RepositoryRobolectricTest.java | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/opensrp-app/src/test/assets/config/migrations/1.up.sql b/opensrp-app/src/test/assets/config/migrations/1.up.sql index bb90ffd595..b62fc80f1d 100644 --- a/opensrp-app/src/test/assets/config/migrations/1.up.sql +++ b/opensrp-app/src/test/assets/config/migrations/1.up.sql @@ -1 +1 @@ -CREATE IF NOT EXISTS TABLE key_value(key VARCHAR, val VARCHAR); +CREATE IF NOT EXISTS TABLE key_value(key VARCHAR, val VARCHAR); \ No newline at end of file diff --git a/opensrp-app/src/test/java/org/smartregister/TestSyncConfiguration.java b/opensrp-app/src/test/java/org/smartregister/TestSyncConfiguration.java index d018f9a55a..dd60f4a24c 100644 --- a/opensrp-app/src/test/java/org/smartregister/TestSyncConfiguration.java +++ b/opensrp-app/src/test/java/org/smartregister/TestSyncConfiguration.java @@ -72,4 +72,9 @@ public String getOauthClientSecret() { public Class getAuthenticationActivity() { return BaseLoginActivity.class; } + + @Override + public boolean isMigrationsConfigurationEnabled() { + return true; + } } diff --git a/opensrp-app/src/test/java/org/smartregister/repository/RepositoryRobolectricTest.java b/opensrp-app/src/test/java/org/smartregister/repository/RepositoryRobolectricTest.java index f105757ab7..be8924212e 100644 --- a/opensrp-app/src/test/java/org/smartregister/repository/RepositoryRobolectricTest.java +++ b/opensrp-app/src/test/java/org/smartregister/repository/RepositoryRobolectricTest.java @@ -141,4 +141,19 @@ public void canUseThisPasswordShouldCallIsDatabaseWritableAndReturnTrue() { Mockito.doReturn(true).when(repository).isDatabaseWritable(password); Assert.assertTrue(repository.canUseThisPassword(password)); } + + @Test + public void onUpgradeShouldApplyMigrationsFromAssetsFolder() { + Repository repository = Mockito.mock(Repository.class, Mockito.CALLS_REAL_METHODS); + ReflectionHelpers.setField(repository, "context", RuntimeEnvironment.application); + SQLiteDatabase database = Mockito.mock(SQLiteDatabase.class); + + // call the method under test + repository.onUpgrade(database, 0, 3); + + // Verify the migration calls + Mockito.verify(database).execSQL(Mockito.eq("CREATE IF NOT EXISTS TABLE key_value(key VARCHAR, val VARCHAR);")); + Mockito.verify(database).execSQL(Mockito.eq("CREATE IF NOT EXISTS TABLE clients(id INTEGER, full_name VARCHAR, age INTEGER, dob INTEGER);")); + Mockito.verify(database).execSQL(Mockito.eq("CREATE IF NOT EXISTS TABLE events(event_id INTEGER, json VARCHAR, server_version INTEGER);")); + } }