Пример #1
0
 def test_get_by_name(self):
     ## Fails on bogus name:
     with self.assertRaisesRegexp(AssertionError, "Frequency must be one of"):
         Frequency.get_id_by_name("Bogus")
     
     id = Frequency.get_id_by_name('weekly')
     self.assertEqual(Frequency.get_name_by_id(id), 'weekly')
Пример #2
0
    def validate_frequency(self, key, freq):
        freq = freq.strip()
        freq_id = Frequency.get_id_by_name(freq, key) ## Assertion exists here

        if (key == 'frequency'): 
            if (self.check_in_frequency and self.check_in_frequency_name):
                assert Frequency.conforms(freq, self.check_in_frequency_name), "Check-in frequency must conform to frequency!"
            self.frequency_name = freq
        elif (key == 'check_in_frequency'):
            if (self.frequency and self.frequency_name):
                assert Frequency.conforms(self.frequency_name, freq), "Check-in frequency must conform to frequency!"
            self.check_in_frequency_name = freq

    
        return freq_id
Пример #3
0
    def test_create(self):
        # Requires login:
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        setup.assertRequiresLogin(self, res)

        self.login()

        ## Test that it rejects bad inputs
        bad_goal = self.new_goal.copy()
        bad_goal['name'] = None
        res = self.test_client.post("/goals/create/", data=bad_goal)
        setup.assertInvalid(self, res, "name")

        bad_goal['name'] = self.new_goal['name']
        bad_goal['prompt'] = "" 
        res = self.test_client.post("/goals/create/", data=bad_goal)

        setup.assertBadData(self, res, "Prompt must be between 0 and 255 characters")

        ## Test that it actually registers at all :-)
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        setup.assertOk(self, res, 201)
        data = json.loads(res.data)
        self.assertIn('id', data)
        goal = Goal.pull_by_id(data['id'])
        self.assertIsNotNone(goal)
        for key, val in self.new_goal.iteritems():
            self.assertEqual(val, data[key])
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(val, Frequency.get_name_by_id(getattr(goal, key)))
            else:
                self.assertEqual(val, getattr(goal, key))

        ## Shouldn't be able to register something that already exists
        res = self.test_client.post("/goals/create/", data=self.new_goal)
        self.assertEqual(res.status_code, 409)
        data = json.loads(res.data)
        self.assertIn('detail', data)
        self.assertIn('Test goal already exists', data['detail'])
Пример #4
0
 def reconstruct(self):
     self.frequency_name = Frequency.get_name_by_id(self.frequency)
     self.check_in_frequency_name = Frequency.get_name_by_id(self.check_in_frequency)
Пример #5
0
    def test_update(self):
        ## Requires login
        res = self.test_client.post("/goals/update/", data={'id': 0})
        setup.assertRequiresLogin(self, res)

        ## Validates form
        self.login()
        res = self.test_client.post("/goals/update/", data={'favorite_muppet': 'kermit'})
        setup.assertInvalid(self, res, 'id')        
        
        ## 404's
        res = self.test_client.post("/goals/update/", data={'id': 0, 'name': 'test goal 2.0'})
        setup.assert404(self, res)

        ## Actually works
        goal = self.create_test_goal()
        data = {'id': goal['id']}
        data['name'] = "Test Goal 2.0"
        data['prompt'] = "Guess how many eggs I had for breakfast today!"
        data['frequency'] = "monthly"
        data['target'] = 100
        data['input_type'] = "binary"
        data['active'] = False
        data['public'] = True
        data['check_in_frequency'] = "daily"
        
        res = self.test_client.post("/goals/update/", data=data)
        setup.assertOk(self, res, 200)

        updated_goal = Goal.pull_by_id(goal['id'])
        for key, val in data.iteritems():
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(Frequency.get_name_by_id(getattr(updated_goal, key)), val)
            else:
                self.assertEqual(getattr(updated_goal, key), val)

        ## If any part is invalid, no part should go through
        bad_data = {'id': goal['id']}
        bad_data['name'] = "Test Goal 3.0"
        bad_data['prompt'] = "How many jars of sand did you collect?"
        bad_data['frequency'] = "daily"
        bad_data['target'] = 1000
        bad_data['input_type'] = "numeric"
        bad_data['active'] = True
        bad_data['public'] = "banana"
        bad_data['check_in_frequency'] = "daily"

        res = self.test_client.post("/goals/update/", data=bad_data)
        setup.assertBadData(self, res, "Public must be a boolean")
        updated_goal = Goal.pull_by_id(goal['id'])
        for key, val in data.iteritems():
            if (key == 'frequency' or key == 'check_in_frequency'):
                self.assertEqual(Frequency.get_name_by_id(getattr(updated_goal, key)), val)
            else:
                self.assertEqual(getattr(updated_goal, key), val)

        
        ## Can't update someone else's goal
        self.logout()
        self.login_other_user()
        res = self.test_client.post("/goals/update/", data=bad_data) ## using bad_data here ensures we don't update
        setup.assertInvalidCredentials(self, res)
Пример #6
0
    def validate_frequency(self, key, freq):
        freq = freq.strip()
        self.frequency_name = freq
        freq = Frequency.get_id_by_name(freq) ## Assertion exists here

        return freq