def post(self, name): _entry = EntryModel.find_by_name(name) competition = CompetitionModel.find_by_name(name) if competition: if competition.isFinished: return {'message': "The competition '{}' is Finished! Can not accept more entries!".format(name)} competition_num_tries = competition.numTrys else: return {'message': "The competition '{}' doesen't exists.".format(name)} data = Entry.parser.parse_args() competition_id = CompetitionModel.find_competition_id(name) athlete_num_tries = EntryModel.find_athlete_tries(competition_id, data['atleta']) if _entry and athlete_num_tries and athlete_num_tries >= competition_num_tries: return {'message': "{} has reached the maximum number of attempts in {} competition.".format(data['atleta'], name)}, 500 competition = CompetitionModel.find_by_name(name) entry = EntryModel(data['atleta'], data['value'], data['unidade'], competition.id) try: entry.save_to_db() except: return {"message": "An error occurred inserting the entry."}, 500 return entry.json(), 201
def test_competition_relationship(self): with self.app_context(): competition = CompetitionModel('test_competition', False, 1) entry = EntryModel('test', 19.99, 'm', 1) competition.save_to_db() entry.save_to_db() self.assertEqual(entry.competition.competicao, 'test_competition')
def test_competition_relationship(self): with self.app_context(): competition = CompetitionModel('test', False, 1) entry = EntryModel('test_entry', 19.99, 'm', 1) competition.save_to_db() entry.save_to_db() self.assertIsNotNone(competition.entrys[0]) self.assertEqual(competition.entrys[0].atleta, 'test_entry')
def test_put_competition(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() r = c.put('/competition/100m', data={'isFinished': False, 'numTrys': 2}) self.assertEqual(r.status_code, 200) self.assertEqual(CompetitionModel.find_by_name('100m').numTrys, 2) self.assertDictEqual(d1={'competicao': '100m', 'entrys': [], 'isFinished': True, 'numTrys': 2}, d2=json.loads(r.data.decode('utf-8')))
def test_competition_json(self): competition = CompetitionModel('test', False, 1) expected = { 'competicao': 'test', 'isFinished': False, 'numTrys': 1, 'entrys': [] } self.assertEqual( competition.json(), expected, "The JSON export of the competition is incorrect. Received {}, expected {}." .format(competition.json(), expected))
def get(self): return { 'competitions': [ competition.json() for competition in CompetitionModel.find_all() ] }
def test_competition_list(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() r = c.get('/competitions') self.assertDictEqual(d1={'competitions': [{'competicao': '100m', 'entrys': [], 'isFinished': False, 'numTrys': 1}]}, d2=json.loads(r.data.decode('utf-8')))
def test_put_update_competition(self): with self.app() as c: with self.app_context(): c.put('/competition/100m', data={'isFinished': False, 'numTrys': 1}) r = c.put('/competition/100m', data={'isFinished': False, 'numTrys': 2}) self.assertEqual(r.status_code, 200) self.assertEqual(CompetitionModel.find_by_name('100m').numTrys, 2)
def test_entry_list(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() r = c.get('/entrys') self.assertDictEqual(d1={'entradas': []}, d2=json.loads(r.data.decode('utf-8')))
def test_delete_competition(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() r = c.delete('/competition/100m') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'message': 'Competition deleted'}, d2=json.loads(r.data.decode('utf-8')))
def test_create_competition(self): with self.app() as c: with self.app_context(): r = c.post('/competition/test', data={'isFinished': False, 'numTrys': 1}) self.assertEqual(r.status_code, 201) self.assertIsNotNone(CompetitionModel.find_by_name('test')) self.assertDictEqual(d1={'competicao': 'test', 'entrys': [], 'isFinished': False, 'numTrys': 1}, d2=json.loads(r.data.decode('utf-8')))
def test_delete_entry(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() EntryModel('Bolt', 9.59, 's', 1).save_to_db() r = c.delete('/entry/100m') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'message': 'Entry deleted'}, d2=json.loads(r.data.decode('utf-8')))
def post(self, name): competition = CompetitionModel.find_by_name(name) if competition is None: return {'message': 'Competition not found'}, 404 else: competition.isFinished = True competition.save_to_db() return competition.json(), 200
def get(self, name): competition = CompetitionModel.find_by_name(name) if competition: competition_id = CompetitionModel.find_competition_id(name) if competition.competicao.find("ardo") != -1: #same as #query = "SELECT atleta FROM entrys WHERE competition_id=? ORDER BY value desc" return [ entry.json() for entry in EntryModel.query.filter_by( competition_id=competition_id).order_by( EntryModel.value.desc()) ] else: #query = "SELECT atleta FROM entrys WHERE competition_id=? ORDER BY value asc" return [ entry.json() for entry in EntryModel.query.filter_by( competition_id=competition_id).order_by('value') ] return {'message': 'Competition not found'}, 404
def test_set_competition_isFinished(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() r = c.post('/finish/100m') self.assertEqual(r.status_code, 200) r = c.get('/competition/100m') self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={'competicao': '100m', 'entrys': [], 'isFinished': True, 'numTrys': 1}, d2=json.loads(r.data.decode('utf-8')))
def test_competition_with_entries_list(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() EntryModel('Bolt', 9.59, 's', 1).save_to_db() r = c.get('/competitions') self.assertDictEqual(d1={'competitions': [{'competicao': '100m', 'entrys': [{'atleta': 'Bolt','unidade': 's','value': 9.59}], 'isFinished': False, 'numTrys': 1}]}, d2=json.loads(r.data.decode('utf-8')))
def test_crud(self): with self.app_context(): competition = CompetitionModel('test', False, 1) competition.save_to_db() entry = EntryModel('test', 19.99, 'm', 1) self.assertIsNone(EntryModel.find_by_name('test').first(), "Found an entry with name 'test' before save_to_db") self.assertEqual(EntryModel.find_athlete_tries(1, 'test'), 0, "The number of tries for the athelte 'test' before save_to_db was not 0") entry.save_to_db() self.assertIsNotNone(EntryModel.find_by_name('test'), "Did not find an entry with name 'test' after save_to_db") self.assertEqual(EntryModel.find_athlete_tries(1, 'test'), 1, "The number of tries for the athelte 'test' after save_to_db was not 1") entry.delete_from_db() self.assertIsNone(EntryModel.find_by_name('test').first(), "Found an entry with name 'test' after delete_from_db")
def test_crud(self): with self.app_context(): competition = CompetitionModel('test', False, 1) self.assertIsNone( CompetitionModel.find_by_name('test'), "Found an competition with name 'test' before save_to_db") competition.save_to_db() self.assertIsNotNone( CompetitionModel.find_by_name('test'), "Did not find a competition with name 'test' after save_to_db") competition.delete_from_db() self.assertIsNone( CompetitionModel.find_by_name('test'), "Found an competition with name 'test' after delete_from_db")
def test_get_competition_ranking_dardo(self): with self.app() as c: with self.app_context(): CompetitionModel('Dardo', False, 1).save_to_db() EntryModel('First', 100.00, 'm', 1).save_to_db() EntryModel('Last', 10.00, 'm', 1).save_to_db() EntryModel('Silver', 50.00, 'm', 1).save_to_db() r = c.get('/finish/Dardo') self.assertEqual(r.status_code, 200) self.assertEqual([{'atleta': 'First', 'value': 100.00, 'unidade': 'm'}, {'atleta': 'Silver', 'value': 50.00, 'unidade': 'm'}, {'atleta': 'Last', 'value': 10.00, 'unidade': 'm'}], json.loads(r.data.decode('utf-8')))
def test_get_competition_ranking_100m(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() EntryModel('Bolt', 9.59, 's', 1).save_to_db() EntryModel('Lazy', 19.59, 's', 1).save_to_db() EntryModel('human', 12.59, 's', 1).save_to_db() r = c.get('/finish/100m') self.assertEqual(r.status_code, 200) self.assertEqual([{'atleta': 'Bolt', 'value': 9.59, 'unidade': 's'}, {'atleta': 'human', 'value': 12.59, 'unidade': 's'}, {'atleta': 'Lazy', 'value': 19.59, 'unidade': 's'}], json.loads(r.data.decode('utf-8')))
def test_entry_list_with_entry(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() EntryModel('Bolt', 9.59, 's', 1).save_to_db() r = c.get('/entrys') self.assertDictEqual(d1={ 'entradas': [{ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.59 }] }, d2=json.loads(r.data.decode('utf-8')))
def test_entry_found(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() EntryModel('Usain Bolt', 9.59, 's', 1).save_to_db() r = c.get('/entry/100m', headers={'Authorization': self.auth_header}) self.assertEqual(r.status_code, 200) self.assertDictEqual(d1={ '100m': [{ 'atleta': 'Usain Bolt', 'unidade': 's', 'value': 9.59 }] }, d2=json.loads(r.data.decode('utf-8')))
def test_create_another_entry_in_the_same_competition(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 2).save_to_db() c.post('/entry/100m', data={ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.59 }) r = c.post('/entry/100m', data={ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.29 }) self.assertEqual(r.status_code, 201)
def test_create_entry(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() r = c.post('/entry/100m', data={ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.59 }) self.assertEqual(r.status_code, 201) #self.assertEqual(EntryModel.find_by_name('Bolt').atleta, 9.59) self.assertDictEqual(d1={ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.59 }, d2=json.loads(r.data.decode('utf-8')))
def test_create_competition(self): competition = CompetitionModel('test', False, 1) self.assertEqual( competition.competicao, 'test', "The name of the competition after creation does not equal the constructor argument." ) self.assertEqual( competition.isFinished, False, "The boolean isFinished of the competition after creation does not equal the constructor argument." ) self.assertEqual( competition.numTrys, 1, "The number of tries of the competition after creation does not equal the constructor argument." ) self.assertListEqual( competition.entrys, [], "The competition's entries length was not 0 even though no entries were added." )
def put(self, name): data = Competition.parser.parse_args() competition = CompetitionModel.find_by_name(name) if competition: competition.name = name competition.isFinished = data['isFinished'] competition.numTrys = data['numTrys'] else: competition = CompetitionModel(name, data['isFinished'], data['numTrys']) competition.save_to_db() return competition.json()
def post(self, name): if CompetitionModel.find_by_name(name): return { 'message': "An competition with name '{}' already exists.".format(name) }, 500 data = Competition.parser.parse_args() competition = CompetitionModel(name, 0, data['numTrys']) try: competition.save_to_db() except: return { "message": "An error occurred inserting the competition." }, 500 return competition.json(), 201
def test_create_another_entry_but_exceeds_number_of_tries(self): with self.app() as c: with self.app_context(): CompetitionModel('100m', False, 1).save_to_db() c.post('/entry/100m', data={ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.59 }) r = c.post('/entry/100m', data={ 'atleta': 'Bolt', 'unidade': 's', 'value': 9.29 }) self.assertDictEqual(d1={ 'message': "{} has reached the maximum number of attempts in {} competition." .format('Bolt', '100m') }, d2=json.loads(r.data.decode('utf-8'))) self.assertEqual(r.status_code, 500)
def get(self, name): competition = CompetitionModel.find_by_name(name) if competition: return competition.json() return {'message': 'Competition not found'}, 404
def delete(self, name): competition = CompetitionModel.find_by_name(name) if competition: competition.delete_from_db() return {'message': 'Competition deleted'}, 200