예제 #1
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)
예제 #2
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)
예제 #3
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)