def test_for_params_control(self): years = ['2010', '2015'] colors = ['black', 'blue'] brands = ['bmw', 'audi'] trans_types = ['manual', 'automatic'] for year, trans, brand, color in zip(years, trans_types, brands, colors): tester = app.test_client(self) response = tester.get( f'{self.main_path}?brand={brand}&year={year}&trans={trans}&extcolor={color}' ) json_data = json.loads(response.data)['cars'] self.assertTrue(json_data, msg='The list is empty') for data in json_data: self.assertEqual( str(data['trans_type']).lower(), str(trans).lower()) self.assertEqual(str(data['year']).lower(), str(year).lower()) self.assertEqual( str(data['brand']).lower(), str(brand).lower()) self.assertEqual( str(data['exterior_color']).lower(), str(color).lower()) self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_return_count(self): tester = app.test_client(self) response = tester.get(f'{self.main_path}') if response.status_code == requests.codes.ok: data = json.loads(response.data)['cars'] self.assertEqual(len(data), 50, msg='cars count is missing') self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_year_control(self): years = ['2020', '1990', '2017'] for year in years: tester = app.test_client(self) response = tester.get(f'{self.main_path}?year={year}') json_data = json.loads(response.data)['cars'] self.assertTrue(json_data, msg='The list is empty') for data in json_data: self.assertEqual(str(data['year']).lower(), str(year).lower()) self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_brand_control(self): brands = ['bmw', 'audi', 'ford'] for brand in brands: tester = app.test_client(self) response = tester.get(f'{self.main_path}?brand={brand}') json_data = json.loads(response.data)['cars'] self.assertTrue(json_data, msg='The list is empty') for data in json_data: self.assertEqual( str(data['brand']).lower(), str(brand).lower()) self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_color_control(self): colors = ['black', 'Black', 'white', 'RED'] for color in colors: tester = app.test_client(self) response = tester.get(f'{self.main_path}?extcolor={color}') json_data = json.loads(response.data)['cars'] self.assertTrue(json_data, msg='The list is empty') for data in json_data: self.assertEqual( str(data['exterior_color']).lower(), str(color).lower()) self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_trans_control(self): trans_types = ['manual', 'automatic'] for trans in trans_types: tester = app.test_client(self) response = tester.get(f'{self.main_path}?trans={trans}') json_data = json.loads(response.data)['cars'] self.assertTrue(json_data, msg='The list is empty') for data in json_data: self.assertEqual( str(data['trans_type']).lower(), str(trans).lower()) self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_two_params_control(self): colors = ['black', 'Black', 'white', 'RED'] brands = ['bmw', 'audi', 'FORD'] for color, brand in zip(colors, brands): tester = app.test_client(self) response = tester.get( f'{self.main_path}?extcolor={color}&brand={brand}') json_data = json.loads(response.data)['cars'] self.assertTrue(json_data, msg='The list is empty') for data in json_data: self.assertEqual( str(data['exterior_color']).lower(), str(color).lower()) self.assertEqual( str(data['brand']).lower(), str(brand).lower()) self.assertEqual(response.status_code, requests.codes.ok, msg=f'status code {response.status_code}')
def test_car_list_multi(self): tester = app.test_client(self) response = tester.get( f'{self.main_path}?trans=automatic&brand=BMW&color=black&year=2020' ) self.assertEqual(response.status_code, requests.codes.ok)
def test_car_list_trans(self): tester = app.test_client(self) response = tester.get(f'{self.main_path}?trans=automatic') self.assertEqual(response.status_code, requests.codes.ok)
def test_car_list_color(self): tester = app.test_client(self) response = tester.get(f'{self.main_path}?color=black') self.assertEqual(response.status_code, requests.codes.ok)
def test_car_list_brand(self): tester = app.test_client(self) response = tester.get(f'{self.main_path}?brand=BMW') self.assertEqual(response.status_code, requests.codes.ok)
def test_car_list_content(self): tester = app.test_client(self) response = tester.get(self.main_path) self.assertEqual(response.content_type, 'application/json')