def test_team_relationship(self): TeamModel('test team', 'test', 'test', 'test', 'test', 0, 0).save_to_db() DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() driver = DriverModel.find_by_id(1) self.assertEqual('test team', driver.team.name)
def test_team_list_with_drivers(self): TeamModel('test team', 'test', 'test', 'test', 'test', 0, 0).save_to_db() DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() response = self.client.get('/teams') self.assertEqual(200, response.status_code) expected = { 'teams': [ { 'id': 1, 'name': 'test team', 'base': 'test', 'chief': 'test', 'car': 'test', 'power_unit': 'test', 'since': '0', 'championships': 0, 'drivers': [ {'id': 1, 'first_name': 'test fn', 'last_name': 'test ln', 'number': 0, 'points': 0} ] } ] } self.assertDictEqual(expected, json.loads(response.data))
def test_get_driver_with_auth(self): headers = {'Authorization': self.token} DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() response = self.client.get('/drivers/1', headers=headers) self.assertEqual(response.status_code, 200) expected = { 'id': 1, 'first_name': 'test fn', 'last_name': 'test ln', 'number': 0, 'team': {'base': None, 'car': None, 'championships': 0, 'chief': None, 'id': 0, 'name': None, 'power_unit': None, 'since': None}, 'country': 'test', 'podiums': 0, 'points': 0, 'championships': 0, 'birthday': 'Fri, 25 Aug 1995 00:00:00 -0000', } self.assertDictEqual(expected, json.loads(response.data))
def test_driver_put(self): DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() headers = {'Authorization': self.token, 'Content-Type': 'application/json'} data = json.dumps({ "first_name": "new test fn", "last_name": "new test ln", "number": 1, "team_id": 1, "country": "new test" }) response = self.client.put('/drivers/1', data=data, headers=headers) self.assertEqual(response.status_code, 202) expected = { 'id': 1, 'first_name': 'new test fn', 'last_name': 'new test ln', 'number': 1, 'team': {'base': None, 'car': None, 'championships': 0, 'chief': None, 'id': 0, 'name': None, 'power_unit': None, 'since': None}, 'country': 'new test', 'podiums': 0, 'points': 0, 'championships': 0, 'birthday': 'Fri, 25 Aug 1995 00:00:00 -0000', } self.assertDictEqual(expected, json.loads(response.data))
def test_driver_json(self): driver = DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)) driver.save_to_db() expected = { 'id': 1, 'name': "test fn test ln", 'number': 0, 'team_id': 1, 'country': 'test', 'podiums': 0, 'points': 0, 'championships': 0, 'birthday': '8/25/1995', } self.assertDictEqual(driver.json(), expected)
def test_driver_relationship(self): TeamModel('test team', 'test', 'test', 'test', 'test', 0, 0).save_to_db() DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() team = TeamModel.find_by_name('test team') self.assertEqual(team.drivers.count(), 1) self.assertEqual(team.drivers.first().first_name, 'test fn')
def test_delete_driver(self): DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() headers = {'Authorization': self.token} response = self.client.delete('/drivers/1', headers=headers) self.assertEqual(response.status_code, 202) self.assertDictEqual({'message': "DriverId: 1 deleted."}, json.loads(response.data))
def test_team_json_with_driver(self): team = TeamModel('test team', 'test', 'test', 'test', 'test', 0, 0) driver = DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)) team.save_to_db() driver.save_to_db() expected = { 'id': 1, 'name': 'test team', 'base': 'test', 'chief': 'test', 'car': 'test', 'power_unit': 'test', 'founded': 0, 'championships': 0, 'drivers': [driver.json()], } self.assertEqual(team.json(), expected)
def test_driver_crud(self): TeamModel('test', 'test', 'test', 'test', 'test', 0, 0).save_to_db() driver = DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)) self.assertIsNone(DriverModel.find_by_id(1), "Found an driver with id: 1, but expected not to.") driver.save_to_db() self.assertIsNotNone( DriverModel.find_by_id(1), "Did not find an item with id: 1, but expected to.") driver.delete_from_db() self.assertIsNone(DriverModel.find_by_id(1), "Found an driver with id: 1, but expected not to.")
def test_get_driver_no_auth(self): DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)).save_to_db() response = self.client.get('/drivers/1') self.assertEqual(response.status_code, 200) expected = { "first_name": "test fn", "last_name": "test ln", "number": 0, "message": "more information if logged in." } self.assertDictEqual(expected, json.loads(response.data))
def test_create_driver(self): driver = DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)) self.assertIsNotNone(driver) self.assertEqual(driver.first_name, 'test fn') self.assertEqual(driver.last_name, 'test ln') self.assertEqual(driver.number, 0) self.assertEqual(driver.team_id, 1) self.assertEqual(driver.country, 'test') self.assertEqual(driver.podium, 0) self.assertEqual(driver.points, 0) self.assertEqual(driver.championships, 0) self.assertEqual(driver.birthday, date(1995, 8, 25))
def test_get_driver_list_no_auth(self): driver1 = DriverModel('test fn', 'test ln', 0, 1, 'test', 0, 0, 0, date(1995, 8, 25)) driver2 = DriverModel('test fn2', 'test ln2', 1, 1, 'test', 0, 0, 0, date(1995, 8, 25)) driver1.save_to_db() driver2.save_to_db() response = self.client.get('/drivers') self.assertEqual(response.status_code, 200) expected = { 'drivers': [ {'first_name': 'test fn', 'last_name': 'test ln', 'number': 0 }, {'first_name': 'test fn2', 'last_name': 'test ln2', 'number': 1} ], 'message': 'More information if logged in.' } self.assertDictEqual(expected, json.loads(response.data))
def add_drivers(): for driver in DRIVERS: d = DriverModel(*driver) d.save_to_db() print('drivers added to db')