def test_get_all_counters(self): """ Ensure that we can get all counters. """ counter1 = Counter("test1") counter1.count = 1 counter2 = Counter("test2") counter2.count = 2 db.session.add(counter1) db.session.add(counter2) db.session.commit() with self.client: response = self.client.get('/counters', content_type='application/json') counters = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(counters), 2) self.assertDictEqual(counters[0], { 'id': 1, 'title': "test1", 'count': 1 }) self.assertDictEqual(counters[1], { 'id': 2, 'title': "test2", 'count': 2 })
def test_get_counter_by_id(self): """ Ensure that we can get a counter by it's id. """ counter = Counter("test") counter.add_and_commit() counter = Counter.get_by_id(counter.id) self.assertEqual(counter.id, 1)
def test_get_all_counters_from_database(self): """ Ensure that we can get all counters from the database. """ counter1 = Counter("test1") counter2 = Counter("test2") db.session.add(counter1) db.session.add(counter2) db.session.commit() counters = Counter.get_all() self.assertEqual(len(counters), 2) self.assertEqual(counters[0].title, "test1") self.assertEqual(counters[1].title, "test2")
def test_new_counter(self): """ Ensure that a new counter is given a title, and an initial count equal to zero. """ counter = Counter("test") self.assertEqual(counter.title, "test") self.assertEqual(counter.count, 0)
def test_convert_counter_to_dict(self): """ Ensure that we can convert the counter to a dictionary representation. """ counter = Counter("test") counter.add_and_commit() counter_dict = counter.to_dict() self.assertDictEqual(counter_dict, {'id': 1, 'title': "test", 'count': 0})
def get_counters(): """ Return JSON representation of all existing counters. :return: JSON representation of all existing counters (status code 200) """ counters = Counter.get_all() response_object = [counter.to_dict() for counter in counters] return jsonify(response_object), 200
def decorated_function(*args, **kwargs): id = kwargs['id'] counter = Counter.get_by_id(id) if not counter: return jsonify({ 'status': 'error', 'reason': 'Resource not found.' }), 404 return f(counter, *args, **kwargs)
def test_increment_counter(self): """ Ensure that we can increment a counter, and that we get back all counters. """ counter1 = Counter("test1") counter2 = Counter("test2") counter2.count = 2 db.session.add(counter1) db.session.add(counter2) db.session.commit() with self.client: response = self.client.post(f'/counters/{counter1.id}/increment', content_type='application/json') counters = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(counters), 2) self.assertEqual(counters[0]['count'], 1)
def test_delete_counter_from_database(self): """ Ensure that we can delete a counter from the database. """ counter = Counter("test") counter.add_and_commit() counter.delete_and_commit() counter = db.session.query(Counter).filter(Counter.id == counter.id).first() self.assertIsNone(counter)
def test_add_new_counter_to_database(self): """ Ensure that we can persist a new counter to the database. """ counter = Counter("test") counter.add_and_commit() counter = db.session.query(Counter).filter(Counter.id == counter.id).first() self.assertEqual(counter.title, "test") self.assertEqual(counter.count, 0)
def add_counter(): """ Add a new counter with title as provided in payload. If title is not given in payload the decorator required_fields will return with 400. Empty title is not allowed. :return: JSON representation of all existing counters (status code 201) """ title = request.get_json()['title'] if len(title) == 0: return jsonify({'status': 'error', 'reason': 'Invalid payload.'}), 400 counter = Counter(title) counter.add_and_commit() counters = Counter.get_all() response_object = [counter.to_dict() for counter in counters] return jsonify(response_object), 201
def test_delete_counter(self): """ Ensure that we can delete a counter, and that we get back all other counters. """ counter1 = Counter("test1") counter1.count = 0 counter2 = Counter("test2") counter2.count = 2 db.session.add(counter1) db.session.add(counter2) db.session.commit() with self.client: response = self.client.delete(f'/counters/{counter1.id}', content_type='application/json') counters = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertEqual(len(counters), 1) self.assertEqual(counters[0]['title'], "test2")
def delete_counter(counter, id): """ Delete a counter. If id is not a valid id the validate_counter will return 404. :param counter: provided by validate_counter decorator :param id: provided by route decorator :return: JSON representation of all existing counters (status code 200) """ counter.delete_and_commit() counters = Counter.get_all() response_object = [counter.to_dict() for counter in counters] return jsonify(response_object), 200
def test_update_counter_to_database(self): """ Ensure that we can update any changes to the counter to the database. """ counter = Counter("test") counter.add_and_commit() counter.title = "test2" counter.count += 1 counter.commit() counter = db.session.query(Counter).filter(Counter.id == counter.id).first() self.assertEqual(counter.title, "test2") self.assertEqual(counter.count, 1)
def test_add_counter(self): """ Ensure that we can add a new counter which is given an initial count of 0, and that we get back all other counters. """ counter1 = Counter("test1") counter1.count = 0 db.session.add(counter1) db.session.commit() with self.client: response = self.client.post(f'/counters', data=json.dumps({'title': 'test2'}), content_type='application/json') counters = json.loads(response.data.decode()) self.assertEqual(response.status_code, 201) self.assertEqual(len(counters), 2) self.assertEqual(counters[1]['title'], "test2") self.assertEqual(counters[1]['count'], 0)