def create_test_binary_goal(self): goal = Goal(setup.get_test_user(), "Test Binary goal", "Have you created a goal today?", "weekly", 7, "binary", "daily") goal.persist() return goal
def create_test_numeric_goal(self): goal = Goal(setup.get_test_user(), "Test Numeric goal", "How many goals have you created today?", "weekly", 7, "numeric", "daily") goal.persist() return goal
def create_test_goals(): user = create_test_user() for x in range(ord('a'), ord('a') + 25): x = chr(x) name = "test goal " + str(x) new_goal = Goal(user, name, "Is this a test?", "daily", 10, "numeric", "daily") new_goal.created = new_goal.created - datetime.timedelta(hours=30-(ord(x)-ord('a'))) db.session.add(new_goal) db.session.flush() db.session.commit()
def create_goal(): validate_form(request.form, [ "name", "prompt", "frequency", "target", "input_type", "check_in_frequency" ]) goal = Goal(current_user, request.form['name'], request.form['prompt'], request.form['frequency'], request.form['target'], request.form['input_type'], request.form['check_in_frequency']) goal.persist() return goal.to_json(), 201
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_init(self): ## Test that all of the validators work! fake_user = User("notreal", "notreal", "notreal", "notreal") with self.assertRaisesRegexp(AssertionError, "user does not exist"): goal = Goal(fake_user, "test name", "test prompt", "weekly", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "user must be a User"): goal = Goal("Jimmy", "test", "test prompt", "weekly", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "Name must be between 0 and 50"): goal = Goal(self.test_user, " ", "test prompt", "weekly", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "Name must be between 0 and 50"): goal = Goal(self.test_user, "a" * 51, "test prompt", "weekly", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "Prompt must be between 0 and 255"): goal = Goal(self.test_user, "test", " ", "weekly", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "Prompt must be between 0 and 255"): goal = Goal(self.test_user, "test", "a" * 256, "weekly", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "frequency must be one of "): goal = Goal(self.test_user, "test", "test prompt", "not-an-option", 10, "binary", "daily") with self.assertRaisesRegexp(AssertionError, "Target must be an integer"): goal = Goal(self.test_user, "test", "test prompt", "weekly", "banana", "binary", "daily") with self.assertRaisesRegexp(AssertionError, "Input type must be binary or numeric"): goal = Goal(self.test_user, "test", "test prompt", "weekly", 10, "banana", "daily") with self.assertRaisesRegexp(AssertionError, "check_in_frequency must be one of"): goal = Goal(self.test_user, "test", "test prompt", "weekly", 10, "numeric", "only on fudge sundaes") with self.assertRaisesRegexp( AssertionError, "Check-in frequency must conform to frequency"): goal = Goal(self.test_user, "test", "test prompt", "weekly", 10, "numeric", "monthly") goal = Goal(self.test_user, "test", "test prompt", "weekly", "10", "binary", "daily") self.assertIsInstance(goal, Goal) self.assertTrue(goal.active) self.assertFalse(goal.public) with self.assertRaisesRegexp(AssertionError, "Active must be a boolean"): goal.active = "fish" with self.assertRaisesRegexp(AssertionError, "Public must be a boolean"): goal.public = "filet"
def create_test_goal(self): goal = Goal(self.test_user, "test goal", "is this goal a test?", "weekly", 10, "binary", "daily") goal.persist() return goal