예제 #1
0
 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())
예제 #2
0
    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())
예제 #3
0
    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())
예제 #4
0
    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)
예제 #5
0
    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)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
0
def get_check_in(id, tfid):
    """Get the check-in for a given timeframe.  Returns 404 if there is no check-in for that timeframe"""
    goal = Goal.pull_by_id(id)
    if (not goal):
        raise NotFoundError()
    
    if goal.user != current_user.get_id():
        raise UnauthorizedError
    
    check_in = CheckIn.pull_by_goal_timeframe(id, tfid)
    if (not check_in):
        raise NotFoundError()

    return check_in.to_json(), 200    
예제 #9
0
def get_current_check_in(id):
    """Get the current check-in for a given goal.  Returns 404 if the current check-in or goal does not exist"""
    goal = Goal.pull_by_id(id)
    if (not goal):
        raise NotFoundError()
    
    if goal.user != current_user.get_id():
        raise UnauthorizedError
    
    check_in = CheckIn.pull_by_goal_timeframe(id, Timeframe.get_current_timeframe(goal.check_in_frequency_name).get_id())
    if (not check_in):
        raise NotFoundError()

    return check_in.to_json(), 200
예제 #10
0
    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'])
예제 #11
0
def get_by_time(id):
    goal = Goal.pull_by_id(id)
    if (not goal):
        raise NotFoundError()
    
    if goal.user != current_user.get_id():
        raise UnauthorizedError

    if (not 'start' in request.args or not 'end' in request.args):
        raise InvalidRequest(["start and end are required parameters"])

    try:
        start = datetime.datetime.strptime(request.args['start'], "%Y-%m-%d %H:%M:%S")
        end = datetime.datetime.strptime(request.args['end'], "%Y-%m-%d %H:%M:%S")
    except ValueError as e:
        raise InvalidRequest([e.message])

    check_ins = CheckIn.pull_by_goal_start_end(id, start, end)

    return jsonify({"check-ins": [check_in.to_dict() for check_in in check_ins]}), 200
예제 #12
0
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
예제 #13
0
 def get_test_check_in(self):
     return CheckIn(self.test_numeric_goal, self.test_tf, 30)
예제 #14
0
    def test_check_in(self):
        tc = self.test_client

        # Requires Login:
        res = tc.post("/goals/1/check-ins/", data={"value": 1})
        setup.assertRequiresLogin(self, res)

        ## Requires "value"
        self.login()
        res = tc.post("/goals/1/check-ins/", data={"not-value": 1})
        setup.assertInvalid(self, res, "value")

        ## Must be a goal that exists
        res = tc.post("/goals/0/check-ins/", data={"value": 1})
        setup.assert404(self, res)        
        
        ## User must own the goal in question
        numeric_goal = self.create_test_numeric_goal()
        numeric_goal_id = str(numeric_goal.get_id()) ## You have to do this before you log out, for some reason?
        binary_goal = self.create_test_binary_goal()
        binary_goal_id = str(binary_goal.get_id())
        
        self.logout()
        self.login_other_user()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1})
        setup.assertInvalidCredentials(self, res)
        
        ## Check-in must conform (true / false vs. numeric)
        self.logout()
        self.login()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": "true"})
        setup.assertBadData(self, res, "Value must be numeric")

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/", data={"value": 10})
        setup.assertBadData(self, res, "Value must be a boolean")        

        ## Check-in is returned with 201 if no timeframe is given, returning current timeframe (both numeric & binary)
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/", data={"value": True})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), binary_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## An updated check-in is returned with 200 if no timeframe is given, returning current timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 3})
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'], Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## Check-in is returned with 201 if timeframe ID is given, returning correct timeframe
        tf = Timeframe.get_timeframe("daily", datetime.datetime(2016, 1, 1))
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 1, "timeframe": tf.get_id()})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)
        
        ## Updated check-in is returned with 200 if Timeframe ID is given, returning correct timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/", data={"value": 3, "timeframe": tf.get_id()})
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)
예제 #15
0
    def test_check_in(self):
        tc = self.test_client

        # Requires Login:
        res = tc.post("/goals/1/check-ins/", data={"value": 1})
        setup.assertRequiresLogin(self, res)

        ## Requires "value"
        self.login()
        res = tc.post("/goals/1/check-ins/", data={"not-value": 1})
        setup.assertInvalid(self, res, "value")

        ## Must be a goal that exists
        res = tc.post("/goals/0/check-ins/", data={"value": 1})
        setup.assert404(self, res)

        ## User must own the goal in question
        numeric_goal = self.create_test_numeric_goal()
        numeric_goal_id = str(numeric_goal.get_id(
        ))  ## You have to do this before you log out, for some reason?
        binary_goal = self.create_test_binary_goal()
        binary_goal_id = str(binary_goal.get_id())

        self.logout()
        self.login_other_user()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": 1})
        setup.assertInvalidCredentials(self, res)

        ## Check-in must conform (true / false vs. numeric)
        self.logout()
        self.login()
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": "true"})
        setup.assertBadData(self, res, "Value must be numeric")

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/",
                      data={"value": 10})
        setup.assertBadData(self, res, "Value must be a boolean")

        ## Check-in is returned with 201 if no timeframe is given, returning current timeframe (both numeric & binary)
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": 1})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'],
                         Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        res = tc.post("/goals/" + binary_goal_id + "/check-ins/",
                      data={"value": True})
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), binary_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'],
                         Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## An updated check-in is returned with 200 if no timeframe is given, returning current timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={"value": 3})
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'],
                         Timeframe.get_current_timeframe('daily').to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## Check-in is returned with 201 if timeframe ID is given, returning correct timeframe
        tf = Timeframe.get_timeframe("daily", datetime.datetime(2016, 1, 1))
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={
                          "value": 1,
                          "timeframe": tf.get_id()
                      })
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 1)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)

        ## Updated check-in is returned with 200 if Timeframe ID is given, returning correct timeframe
        res = tc.post("/goals/" + numeric_goal_id + "/check-ins/",
                      data={
                          "value": 3,
                          "timeframe": tf.get_id()
                      })
        setup.assertOk(self, res, 200)
        data = json.loads(res.data)
        self.assertIn("id", data)
        self.assertEqual(str(data['goal']), numeric_goal_id)
        self.assertEqual(data['value'], 3)
        self.assertEqual(data['timeframe'], tf.to_dict())
        self.assertEqual(CheckIn.pull_by_id(data['id']).to_dict(), data)
예제 #16
0
    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'])