def test_get(self): tc = self.test_client ## Login required setup.assertRequiresLogin(self, tc.get("/goals/0/check-ins/0/")) ## Goal exists self.login() setup.assert404(self, tc.get("/goals/0/check-ins/0/")) ## user is goal 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/0/")) ## Check in must exist self.logout() self.login() setup.assert404(self, tc.get("/goals/" + goal_id + "/check-ins/0/")) ## Returns correctly ci = CheckIn( goal, Timeframe.get_current_timeframe(goal.check_in_frequency_name), 1) ci.persist() tfid = str(ci.timeframe) res = tc.get("/goals/" + goal_id + "/check-ins/" + tfid + "/") setup.assertOk(self, res, 200) self.assertEqual(json.loads(res.data), ci.to_dict())
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_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 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 check_in(id): validate_form(request.form, ["value"]) goal = Goal.pull_by_id(id) if (not goal): raise NotFoundError() if goal.user != current_user.get_id(): raise UnauthorizedError if ('timeframe' in request.form): timeframe = Timeframe.pull_by_id(request.form['timeframe']) else: timeframe = Timeframe.get_current_timeframe(goal.check_in_frequency_name) check_in = CheckIn(goal, timeframe, request.form['value']) return_code = 200 if check_in.exists() else 201 check_in.persist() return check_in.to_json(), return_code
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'])