From dae1caf7078bdd3e425e25cbfd5a37eb2309e0e6 Mon Sep 17 00:00:00 2001 From: Ngewi Fet Date: Thu, 15 Sep 2016 11:57:46 +0200 Subject: [PATCH] Fix breaking tests - Incomplete scheduled transactions do not throw exceptions when processed - Set end time for a scheduled transaction to make test reliable over time (in terms of number of created transactions) --- .../gnucash/android/service/ScheduledActionService.java | 8 +++++--- .../test/unit/service/ScheduledActionServiceTest.java | 9 ++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/gnucash/android/service/ScheduledActionService.java b/app/src/main/java/org/gnucash/android/service/ScheduledActionService.java index be88db56b..a608592ab 100644 --- a/app/src/main/java/org/gnucash/android/service/ScheduledActionService.java +++ b/app/src/main/java/org/gnucash/android/service/ScheduledActionService.java @@ -142,10 +142,11 @@ private static void executeScheduledEvent(ScheduledAction scheduledAction, SQLit ContentValues contentValues = new ContentValues(); contentValues.put(DatabaseSchema.ScheduledActionEntry.COLUMN_LAST_RUN, System.currentTimeMillis()); contentValues.put(DatabaseSchema.ScheduledActionEntry.COLUMN_EXECUTION_COUNT, executionCount); - new ScheduledActionDbAdapter(db, new RecurrenceDbAdapter(db)).updateRecord(scheduledAction.getUID(), contentValues); + db.update(DatabaseSchema.ScheduledActionEntry.TABLE_NAME, contentValues, + DatabaseSchema.ScheduledActionEntry.COLUMN_UID + "=?", new String[]{scheduledAction.getUID()}); - //set the values in the object because they will be checked for the next iteration in the calling loop - scheduledAction.setExecutionCount(executionCount); + //set the execution count in the object because it will be checked for the next iteration in the calling loop + scheduledAction.setExecutionCount(executionCount); //this call is important, do not remove!! } /** @@ -191,6 +192,7 @@ private static int executeTransactions(ScheduledAction scheduledAction, SQLiteDa try { trxnTemplate = transactionsDbAdapter.getRecord(actionUID); } catch (IllegalArgumentException ex){ //if the record could not be found, abort + Log.e(LOG_TAG, "Scheduled action with UID " + actionUID + " could not be found in the db with path " + db.getPath()); return executionCount; } diff --git a/app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java b/app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java index 08e5f3a50..03d7de562 100644 --- a/app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java +++ b/app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java @@ -16,6 +16,7 @@ package org.gnucash.android.test.unit.service; import android.database.sqlite.SQLiteDatabase; +import android.support.annotation.NonNull; import org.gnucash.android.BuildConfig; import org.gnucash.android.R; @@ -187,6 +188,9 @@ public void missedScheduledTransactions_shouldBeGenerated(){ ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.TRANSACTION); DateTime startTime = new DateTime(2016, 6, 6, 9, 0); scheduledAction.setStartTime(startTime.getMillis()); + DateTime endTime = new DateTime(2016, 9, 12, 8, 0); //end just before last appointment + scheduledAction.setEndTime(endTime.getMillis()); + scheduledAction.setActionUID(mActionUID); scheduledAction.setRecurrence(PeriodType.WEEK, 2); @@ -257,7 +261,7 @@ public void scheduledTransactionsWithEndTimeInPast_shouldBeExecuted(){ /** * Test that only scheduled actions with action UIDs are processed */ - @Test(expected = IllegalArgumentException.class) + @Test //(expected = IllegalArgumentException.class) public void recurringTransactions_shouldHaveScheduledActionUID(){ ScheduledAction scheduledAction = new ScheduledAction(ScheduledAction.ActionType.TRANSACTION); DateTime startTime = new DateTime(2016, 7, 4, 12 ,0); @@ -270,6 +274,9 @@ public void recurringTransactions_shouldHaveScheduledActionUID(){ List actions = new ArrayList<>(); actions.add(scheduledAction); ScheduledActionService.processScheduledActions(actions, mDb); + + //no change in the database since no action UID was specified + assertThat(transactionsDbAdapter.getRecordsCount()).isZero(); } @Test