From ac4e8b07f50d9635740215627388b4c656bb85b5 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 26 Apr 2012 18:35:05 -0700 Subject: [PATCH] Added unit tests for ab test retention database --- .../todoroo/astrid/dao/ABTestEventDao.java | 7 +- .../astrid/service/ABTestingServiceTest.java | 95 +++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/src/com/todoroo/astrid/service/ABTestingServiceTest.java diff --git a/astrid/src/com/todoroo/astrid/dao/ABTestEventDao.java b/astrid/src/com/todoroo/astrid/dao/ABTestEventDao.java index 30e1fa819..aa860ff13 100644 --- a/astrid/src/com/todoroo/astrid/dao/ABTestEventDao.java +++ b/astrid/src/com/todoroo/astrid/dao/ABTestEventDao.java @@ -34,7 +34,12 @@ public class ABTestEventDao extends DatabaseDao { createNew(event); } - private void createTestEventWithTimeInterval(String testName, int timeInterval) { + /** + * Only public for unit testing--don't use unless you really mean it! + * @param testName + * @param timeInterval + */ + public void createTestEventWithTimeInterval(String testName, int timeInterval) { TodorooCursor existing = query(Query.select(ABTestEvent.PROPERTIES) .where(ABTestEvent.TEST_NAME.eq(testName)).orderBy(Order.asc(ABTestEvent.TIME_INTERVAL))); diff --git a/tests/src/com/todoroo/astrid/service/ABTestingServiceTest.java b/tests/src/com/todoroo/astrid/service/ABTestingServiceTest.java new file mode 100644 index 000000000..b7f194969 --- /dev/null +++ b/tests/src/com/todoroo/astrid/service/ABTestingServiceTest.java @@ -0,0 +1,95 @@ +package com.todoroo.astrid.service; + +import com.todoroo.andlib.data.TodorooCursor; +import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.sql.Order; +import com.todoroo.andlib.sql.Query; +import com.todoroo.andlib.utility.AndroidUtilities; +import com.todoroo.andlib.utility.Preferences; +import com.todoroo.astrid.dao.ABTestEventDao; +import com.todoroo.astrid.data.ABTestEvent; +import com.todoroo.astrid.service.abtesting.ABChooser; +import com.todoroo.astrid.service.abtesting.ABTests; +import com.todoroo.astrid.test.DatabaseTestCase; + +public class ABTestingServiceTest extends DatabaseTestCase { + + @Autowired ABTestEventDao abTestEventDao; + @Autowired ABChooser abChooser; + @Autowired ABTests abTests; + + public void testReportInitialEventNewUser() { + testInitialEvents(true, false); + } + + public void testReportInitialEventExistingUser() { + testInitialEvents(false, true); + } + + private void testInitialEvents(boolean newUser, boolean activatedUser) { + testInterval(newUser, activatedUser, ABTestEvent.TIME_INTERVAL_0); + } + + public void testIntervalEventWithShortIntervalNewUser() { + testIntervalEventWithShortInterval(true, false); + } + + public void testIntervalEventWithShortIntervalExistingUser() { + testIntervalEventWithShortInterval(false, true); + } + + public void testIntervalEventWithLongIntervalNewUser() { + testIntervalEventWithLongInterval(true, false); + } + + public void testIntervalEventWithLongIntervalExistingUser() { + testIntervalEventWithLongInterval(false, true); + } + + private void testIntervalEventWithShortInterval(boolean newUser, boolean activatedUser) { + testInterval(newUser, activatedUser, ABTestEvent.TIME_INTERVAL_3); + } + + private void testIntervalEventWithLongInterval(boolean newUser, boolean activatedUser) { + testInterval(newUser, activatedUser, ABTestEvent.TIME_INTERVAL_14); + } + + private void testInterval(boolean newUser, boolean activatedUser, int testInterval) { + abChooser.makeChoicesForAllTests(newUser, activatedUser); + abTestEventDao.createTestEventWithTimeInterval(TEST_NAME, testInterval); + + TodorooCursor events = abTestEventDao.query( + Query.select(ABTestEvent.PROPERTIES) + .where(ABTestEvent.TEST_NAME.eq(TEST_NAME)) + .orderBy(Order.asc(ABTestEvent.TIME_INTERVAL))); + try { + int maxIntervalIndex = AndroidUtilities.indexOf(ABTestEvent.TIME_INTERVALS, testInterval); + assertEquals(maxIntervalIndex + 1, events.getCount()); + + for (int i = 0; i < events.getCount(); i++) { + events.moveToNext(); + ABTestEvent event = new ABTestEvent(events); + assertExpectedValues(event, newUser, activatedUser, ABTestEvent.TIME_INTERVALS[i]); + } + } finally { + events.close(); + } + } + + private void assertExpectedValues(ABTestEvent event, boolean newUser, boolean activatedUser, int timeInterval) { + assertEquals(TEST_NAME, event.getValue(ABTestEvent.TEST_NAME)); + assertEquals(newUser ? 1 : 0, event.getValue(ABTestEvent.NEW_USER).intValue()); + assertEquals(activatedUser ? 1 : 0, event.getValue(ABTestEvent.ACTIVATED_USER).intValue()); + assertEquals(timeInterval, event.getValue(ABTestEvent.TIME_INTERVAL).intValue()); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + abTests.addTest(TEST_NAME, new int[] { 9, 1 } , new int[] { 1, 9 }, TEST_OPTIONS); + Preferences.clear(TEST_NAME); + } + + private static final String TEST_NAME = "test_experiment"; + private static final String[] TEST_OPTIONS = new String[] { "opt-1", "opt-2" }; +}