def test_init(self): ## Should fail if bogus goal is specified with self.assertRaisesRegexp(AssertionError, "Passed goal must be a goal object"): ci = CheckIn("bogus", self.test_tf, 1) ## Should fail if not a timeframe with self.assertRaisesRegexp(AssertionError, "Passed timeframe must be a timeframe object"): ci = CheckIn(self.test_numeric_goal, "bogus", 1) ## Should fail if timeframe doesn't conform: with self.assertRaisesRegexp(AssertionError, "Passed timeframe frequency must match"): ci = CheckIn(self.test_numeric_goal, Timeframe("weekly", datetime.datetime(2016,2,7)), 1) ## Should fail if numeric and a boolean is passed with self.assertRaisesRegexp(AssertionError, "Value must be numeric"): ci = CheckIn(self.test_numeric_goal, self.test_tf, "orange you glad I didn't say banana?'") ## Should fail if binary and a number is passed with self.assertRaisesRegexp(AssertionError, "Value must be a boolean"): ci = CheckIn(self.test_binary_goal, self.test_tf, 30) ## Should actually work if everything is provided correctly ci = CheckIn(self.test_numeric_goal, self.test_tf, 30) self.assertEqual(ci.goal_obj, self.test_numeric_goal) self.assertEqual(ci.goal, self.test_numeric_goal.get_id()) self.assertEqual(ci.timeframe_obj, self.test_tf) self.assertEqual(ci.timeframe, self.test_tf.get_id()) self.assertEqual(ci.value, 30) ci = CheckIn(self.test_binary_goal, self.test_tf, True) self.assertEqual(ci.goal_obj, self.test_binary_goal) self.assertEqual(ci.goal, self.test_binary_goal.get_id()) self.assertEqual(ci.timeframe_obj, self.test_tf) self.assertEqual(ci.timeframe, self.test_tf.get_id()) self.assertEqual(ci.value, 1)
def test_get_id(self): ## Test that a Timeframe that has just been persisted returns an ID tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertIsNotNone(tf.get_id()) ## Test that a timeframe pulled by id returns same id check_tf = Timeframe.pull_by_id(tf.get_id()) self.assertEqual(check_tf.get_id(), tf.get_id()) ## Test that a timeframe created by start / end persistes, then returns an ID tf = Timeframe("monthly", datetime.datetime(2016, 1, 1)) self.assertIsNotNone(tf.get_id()) self.assertTrue(tf.exists()) self.assertNotEqual(tf.get_id(), check_tf.get_id()) ## Test that a timeframe pulled by start / end returns an ID check_tf = Timeframe.pull_by_start_end(tf.start, tf.end) self.assertEqual(check_tf.get_id(), tf.get_id())
def test_pull_by_id(self): ## Test that a timeframe that exists can be pulled by ID self.assertIsNone(Timeframe.pull_by_id(0)) tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertTrue(tf.exists()) check_tf = Timeframe.pull_by_id(tf.get_id()).to_dict() for key, val in tf.to_dict().iteritems(): self.assertEqual(check_tf[key], val)
def test_pull_by_goal_start_end(self): ci = self.get_test_check_in() ci.persist() for x in range(0, 7): tf = ci.timeframe_obj new_tf = Timeframe(tf.frequency_name, tf.start + datetime.timedelta(1)) ci = CheckIn(ci.goal_obj, new_tf, x) ci.persist() cis = CheckIn.pull_by_goal_start_end(ci.goal, datetime.datetime(2016, 1, 1), datetime.datetime(2016, 1, 8)) self.assertEqual(len(cis), 7)
def test_persist(self): ## Test that data persists to the database tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertTrue(tf.exists()) check_tf = Timeframe.pull_by_start_end(tf.start, tf.end).to_dict() for key, val in tf.to_dict().iteritems(): self.assertEqual(check_tf[key], val) ## Test that you can persist the same one twice without duplicate key, etc errors tf.persist()
def test_pull_by_goal_timeframes(self): ci = self.get_test_check_in() timeframes = [ci.timeframe] ci.persist() for x in range(0, 7): tf = ci.timeframe_obj new_tf = Timeframe(tf.frequency_name, tf.start + datetime.timedelta(1)) timeframes.append(new_tf.get_id()) ci = CheckIn(ci.goal_obj, new_tf, x) ci.persist() cis = CheckIn.pull_by_goal_timeframes(ci.goal, timeframes) self.assertEqual(len(cis), 8)
def test_pull_by_start_end(self): ## Pulling by start / end should return None if none exists: self.assertIsNone(Timeframe.pull_by_start_end(datetime.datetime(2016, 1, 1), datetime.datetime(2017, 1, 1))) tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertTrue(tf.exists()) check_tf = Timeframe.pull_by_start_end(tf.start, tf.end).to_dict() for key, val in tf.to_dict().iteritems(): self.assertEqual(check_tf[key], val) ## Should *not* pull if the start is right and end is wrong or vice versa: self.assertIsNone(Timeframe.pull_by_start_end(tf.start, datetime.datetime(2016, 2, 1))) self.assertIsNone(Timeframe.pull_by_start_end(datetime.datetime(2016, 2, 1), tf.end))
def test__calculate_end(self): time = datetime.datetime(2016, 2, 29) ## pretty much as edge case as you can get? tf = Timeframe("daily", time) self.assertEqual(tf._calculate_end(), datetime.datetime(2016, 3, 1)) tf.frequency_name = "weekly" self.assertEqual(tf._calculate_end(), datetime.datetime(2016, 3, 7)) tf.frequency_name = "monthly" self.assertEqual(tf._calculate_end(), datetime.datetime(2016, 3, 29)) tf.frequency_name = "quarterly" self.assertEqual(tf._calculate_end(), datetime.datetime(2016, 5, 29)) tf.frequency_name = "yearly" self.assertEqual(tf._calculate_end(), datetime.datetime(2017, 2, 28))
class TestCheckIn(unittest.TestCase): test_tf = Timeframe("daily", datetime.datetime(2016, 1, 1)) test_user = setup.create_test_user() test_binary_goal = Goal(test_user, "test binary goal", "is this goal a test?", "weekly", 10, "binary", "daily") test_numeric_goal = Goal(test_user, "test numeric goal", "is this goal a test?", "weekly", 10, "numeric", "daily") test_binary_goal.persist() test_numeric_goal.persist() @classmethod def startUpClass(self): Goal.query.delete() db.session.commit() @classmethod def tearDownClass(self): CheckIn.query.delete() Goal.query.delete() db.session.commit() def startUp(self): CheckIn.query.delete() def tearDown(self): CheckIn.query.delete() def test_init(self): ## Should fail if bogus goal is specified with self.assertRaisesRegexp(AssertionError, "Passed goal must be a goal object"): ci = CheckIn("bogus", self.test_tf, 1) ## Should fail if not a timeframe with self.assertRaisesRegexp(AssertionError, "Passed timeframe must be a timeframe object"): ci = CheckIn(self.test_numeric_goal, "bogus", 1) ## Should fail if timeframe doesn't conform: with self.assertRaisesRegexp(AssertionError, "Passed timeframe frequency must match"): ci = CheckIn(self.test_numeric_goal, Timeframe("weekly", datetime.datetime(2016,2,7)), 1) ## Should fail if numeric and a boolean is passed with self.assertRaisesRegexp(AssertionError, "Value must be numeric"): ci = CheckIn(self.test_numeric_goal, self.test_tf, "orange you glad I didn't say banana?'") ## Should fail if binary and a number is passed with self.assertRaisesRegexp(AssertionError, "Value must be a boolean"): ci = CheckIn(self.test_binary_goal, self.test_tf, 30) ## Should actually work if everything is provided correctly ci = CheckIn(self.test_numeric_goal, self.test_tf, 30) self.assertEqual(ci.goal_obj, self.test_numeric_goal) self.assertEqual(ci.goal, self.test_numeric_goal.get_id()) self.assertEqual(ci.timeframe_obj, self.test_tf) self.assertEqual(ci.timeframe, self.test_tf.get_id()) self.assertEqual(ci.value, 30) ci = CheckIn(self.test_binary_goal, self.test_tf, True) self.assertEqual(ci.goal_obj, self.test_binary_goal) self.assertEqual(ci.goal, self.test_binary_goal.get_id()) self.assertEqual(ci.timeframe_obj, self.test_tf) self.assertEqual(ci.timeframe, self.test_tf.get_id()) self.assertEqual(ci.value, 1) def test_persist(self): ## also tests exists, really -- and pull_by_id ci = self.get_test_check_in() self.assertFalse(ci.exists()) ci.persist() self.assertTrue(ci.exists()) self.assertIsNotNone(ci.get_id()) self.assertEqual(ci.to_dict(), CheckIn.pull_by_id(ci.get_id()).to_dict()) ## now if you overwrite that it should still work. ci = CheckIn(self.test_numeric_goal, self.test_tf, 60) self.assertTrue(ci.exists()) ci.persist() test_ci = CheckIn.pull_by_id(ci.get_id()) self.assertEqual(ci.to_dict(), test_ci.to_dict()) self.assertEqual(test_ci.value, 60) def test_pull_by_goal_timeframe(self): ci = CheckIn.pull_by_goal_timeframe(self.test_numeric_goal.get_id(), self.test_tf.get_id()) self.assertIsNone(ci) ci = self.get_test_check_in() ci.persist() test_ci = CheckIn.pull_by_goal_timeframe(self.test_numeric_goal.get_id(), self.test_tf.get_id()) self.assertEqual(ci.to_dict(), test_ci.to_dict()) def test_destroy(self): ci = self.get_test_check_in() ci.persist() self.assertTrue(ci.exists()) ci.destroy() self.assertFalse(ci.exists()) def test_pull_by_goal_start_end(self): ci = self.get_test_check_in() ci.persist() for x in range(0, 7): tf = ci.timeframe_obj new_tf = Timeframe(tf.frequency_name, tf.start + datetime.timedelta(1)) ci = CheckIn(ci.goal_obj, new_tf, x) ci.persist() cis = CheckIn.pull_by_goal_start_end(ci.goal, datetime.datetime(2016, 1, 1), datetime.datetime(2016, 1, 8)) self.assertEqual(len(cis), 7) def test_pull_by_goal_timeframes(self): ci = self.get_test_check_in() timeframes = [ci.timeframe] ci.persist() for x in range(0, 7): tf = ci.timeframe_obj new_tf = Timeframe(tf.frequency_name, tf.start + datetime.timedelta(1)) timeframes.append(new_tf.get_id()) ci = CheckIn(ci.goal_obj, new_tf, x) ci.persist() cis = CheckIn.pull_by_goal_timeframes(ci.goal, timeframes) self.assertEqual(len(cis), 8) def get_test_check_in(self): return CheckIn(self.test_numeric_goal, self.test_tf, 30)
def test_get_by_time(self): tc = self.test_client ## Login required setup.assertRequiresLogin(self, tc.get("/goals/0/check-ins/")) ## Goal must exist self.login() setup.assert404(self, tc.get("/goals/0/check-ins/")) ## User is the goal's owner goal = self.create_test_numeric_goal() goal_id = str(goal.get_id()) self.logout() self.login_other_user() setup.assertInvalidCredentials( self, tc.get("/goals/" + goal_id + "/check-ins/")) ## Start and end are present self.logout() self.login() setup.assertBadData(self, tc.get("/goals/" + goal_id + "/check-ins/"), "start and end") setup.assertBadData( self, tc.get("/goals/" + goal_id + "/check-ins/?start=banana"), "start and end") setup.assertBadData( self, tc.get("/goals/" + goal_id + "/check-ins/?end=banana"), "start and end") ## Start and end in format YYYY-MM-DD HH:mm:ss res = tc.get("/goals/" + goal_id + "/check-ins/?start=2016-01-01 10:00:00&end=banana") setup.assertBadData(self, res, "'banana' does not match format") res = tc.get("/goals/" + goal_id + "/check-ins/?start=strawberry&end=2016-01-01 10:00:00") setup.assertBadData(self, res, "'strawberry' does not match format") ## Returns empty for no check-ins res = tc.get( "/goals/" + goal_id + "/check-ins/?start=2016-01-01 00:00:00&end=2016-01-08 00:00:00") setup.assertOk(self, res) data = json.loads(res.data) self.assertIn("check-ins", data) self.assertEqual(len(data['check-ins']), 0) ## Correctly returns multiple check-ins ci = CheckIn( goal, Timeframe(goal.check_in_frequency_name, datetime.datetime(2015, 12, 31)), 0) ci.persist() for x in range( 0, 8 ): #one preceding and one postceding so we can make sure this limits the results tf = ci.timeframe_obj new_tf = Timeframe(tf.frequency_name, tf.start + datetime.timedelta(1)) ci = CheckIn(ci.goal_obj, new_tf, x) ci.persist() res = tc.get( "/goals/" + goal_id + "/check-ins/?start=2016-01-01 00:00:00&end=2016-01-08 00:00:00") setup.assertOk(self, res) data = json.loads(res.data) self.assertIn("check-ins", data) self.assertEqual(len(data['check-ins']), 7) for index, check_in in enumerate(data['check-ins']): self.assertEqual(index, check_in['value'])
def test_exists(self): tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) self.assertFalse(tf.exists()) tf.persist() self.assertTrue(tf.exists())
def test_init(self): ## Should fail if bogus frequency is specified with self.assertRaisesRegexp(AssertionError, "Frequency must be one of"): tf = Timeframe("bogus", datetime.datetime.now()) ## start date for daily should always be the start of a day with self.assertRaisesRegexp(AssertionError, "Timeframe must begin at midnight"): tf = Timeframe("daily", datetime.datetime(2016, 1, 1, 1, 1, 1)) with self.assertRaisesRegexp(AssertionError, "Timeframe must begin at midnight"): tf = Timeframe("daily", datetime.datetime(2016, 1, 1, 1, 1)) with self.assertRaisesRegexp(AssertionError, "Timeframe must begin at midnight"): tf = Timeframe("daily", datetime.datetime(2016, 1, 1, 1)) tf = Timeframe("daily", datetime.datetime(2016, 1, 1)) self.assertEqual(tf.end, datetime.datetime(2016, 1, 2)) with self.assertRaisesRegexp(AssertionError, "Weekly timeframes must begin on Sunday"): tf = Timeframe("weekly", datetime.datetime(2016, 1, 1)) ## start date for monthly should always be the start of a month with self.assertRaisesRegexp(AssertionError, "Monthly timeframes must begin on the first"): tf = Timeframe("monthly", datetime.datetime(2016, 1, 2)) tf = Timeframe("monthly", datetime.datetime(2016, 1, 1)) self.assertEqual(tf.end, datetime.datetime(2016, 2, 1)) ## start date for quarterly should always be the start of a quarter with self.assertRaisesRegexp(AssertionError, "Quarterly timeframes must begin "): tf = Timeframe("quarterly", datetime.datetime(2016, 2, 1)) tf = Timeframe("quarterly", datetime.datetime(2016, 1, 1)) self.assertEqual(tf.end, datetime.datetime(2016, 4, 1)) ## start date for yearly should always be the start of a year with self.assertRaisesRegexp(AssertionError, "Yearly timeframes must begin on"): tf = Timeframe("yearly", datetime.datetime(2016, 4, 1)) tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) self.assertEqual(tf.end, datetime.datetime(2017, 1, 1)) ## Should create a weekly timeframe if everything's cool now = datetime.datetime(2016, 2, 7) tf = Timeframe("weekly", now) self.assertEqual(tf.start, now) self.assertEqual(tf.end, now + datetime.timedelta(7)) self.assertEqual(tf.frequency_name, "weekly") self.assertIs(type(tf.frequency), long)