def test_user_story_4_airport_capacity_changeable(self): # As the system designer # So that the software can be used for many different airports # I would like a default airport capacity that can be overridden as appropriate self.airport = Airport(self.weather, 5) for number in range(1, 6): self.diff_plane = Plane() self.weather.is_stormy = MagicMock(return_value=False) self.airport.instruct_to_land(self.diff_plane) print self.airport.planes with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane)
def populate_table(filename): """Populate table (airports) by data read from csv. Parameters ---------- filename : str Path to csv file. """ conn = mysql.connector.connect(user=Connection.MYSQL_USER, password=Connection.MYSQL_PASSWORD, host=Connection.MYSQL_HOST, database=Connection.MYSQL_DB) cursor = conn.cursor() with open(filename, "r") as f_input: csv_input = csv.DictReader(f_input) for row in csv_input: if row['iata_code'] != '': airport = Airport(row['name'].replace("'", "\\'"), row['iata_code'].replace("'", "\\'"), row['ident'].replace("'", "\\'"), row['municipality'].replace("'", "\\'"), row['iso_country'].replace("'", "\\'"), row['latitude_deg'].replace("'", "\\'"), row['longitude_deg'].replace("'", "\\'")) query = f'INSERT INTO airports (name, iata, icao, city, country, latitude, longitude) VALUES (\'{airport.name}\', \'{airport.iata}\', \'{airport.icao}\', \'{airport.city}\', \'{airport.country}\', {airport.latitude}, {airport.longitude});' cursor.execute(query) conn.commit() cursor.close() conn.close()
def test_user_story_4_airport_capacity_changeable(self): # As the system designer # So that the software can be used for many different airports # I would like a default airport capacity that can be overridden as appropriate self.airport = Airport(self.weather, 5) for number in range(1,6): self.diff_plane = Plane() self.weather.is_stormy = MagicMock(return_value=False) self.airport.instruct_to_land(self.diff_plane) print self.airport.planes with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane)
def name_search(): """Endpoint to search airports by name Parameters ---------- name : str Airport name or its part. Output: Array of jsons with found airports. """ if not request.args.get('name'): raise ValueError('Missing name') else: _name = request.args.get('name') conn = mysql.connector.connect(host=Connection.MYSQL_HOST, user=Connection.MYSQL_USER, password=Connection.MYSQL_PASSWORD, db=Connection.MYSQL_DB) cursor = conn.cursor() cursor.execute("SELECT * FROM airports WHERE name LIKE " + "'" + '%' + _name + '%' + "'") airports_data = cursor.fetchall() found_airports = [] if airports_data is not None: for record in airports_data: airport = Airport(record[0], record[1], record[2], record[3], record[4], record[5], record[6]) found_airports.append({ 'name': airport.name, 'iata': airport.iata, 'icao': airport.icao, 'city': airport.city, 'country': airport.country, 'latitude': airport.latitude, 'longitude': airport.longitude }) cursor.close() conn.close() return json.dumps(found_airports)
def test_airport_default_capacity(self): airport = Airport() self.assertEqual(airport.capacity, 1)
def test_airport_does_not_let_plane_take_off_when_stormy(self): airport = Airport(Weather(1)) airport.landPlane("Plane") airport.updateWeather(Weather(99)) with self.assertRaises(RuntimeError): airport.takeOffPlane("Plane")
def test_airport_does_not_let_plane_land_when_stormy(self): airport = Airport(Weather(99)) with self.assertRaises(RuntimeError): airport.landPlane("Plane")
def test_airport_lets_plane_take_off_when_sunny(self): airport = Airport() airport.landPlane("Plane") airport.takeOffPlane("Plane") self.assertEqual(airport.hanger.count("Plane"), 0)
def test_airport_lets_plane_land_when_sunny(self): airport = Airport() airport.landPlane("Plane") self.assertEqual(airport.hanger.count("Plane"), 1)
def setUp(self): self.weather = Weather() self.airport = Airport(self.weather) self.plane = Plane() self.weather.is_stormy = MagicMock(return_value=False)
class TestAirport(unittest.TestCase): def setUp(self): self.plane = MagicMock() self.weather = MagicMock() self.weather.is_stormy = MagicMock(return_value=False) self.airport = Airport(self.weather) def test_0_defaults(self): """airport is empty""" self.assertEqual(self.airport.planes, []) def test_0a_defaults(self): """airport has default capacity""" self.assertEqual(self.airport.capacity, 20) def test_0b_defaults(self): """airport can change capacity""" self.airport = Airport(self.weather, 5) self.assertEqual(self.airport.capacity, 5) def test_1a_instruct_to_land(self): """plane in airport after landing""" self.airport.instruct_to_land(self.plane) number_of_planes = self.airport.planes self.assertEqual(1, len(number_of_planes)) def test_1b_instruct_to_land(self): """aiport hanger filled with one plane""" self.airport.instruct_to_land(self.plane) self.assertIn(self.plane, self.airport.planes) def test_1c_instruct_to_land(self): """aiport instruct_to_land calls land on plane""" self.airport.instruct_to_land(self.plane) self.plane.land.assert_called_with() def test_1d_instruct_to_land(self): """plane cannot land if already in airport""" self.airport.instruct_to_land(self.plane) with self.assertRaisesRegexp(Exception, 'Plane already at airport'): self.airport.instruct_to_land(self.plane) def test_1e_instruct_to_land(self): """plane cannot land if airport is full""" for number in range(1,21): self.diff_plane = MagicMock() self.airport.instruct_to_land(self.diff_plane) with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane) def test_1f_instruct_to_land(self): """plane cannot land if stormy weather""" self.weather.is_stormy = MagicMock(return_value=True) with self.assertRaisesRegexp(Exception, 'Plane cannot land: weather is stormy'): self.airport.instruct_to_land(self.plane) def test_2a_instruct_take_off(self): """plane is not in airport after take off""" self.airport.instruct_to_land(self.plane) self.airport.instruct_take_off(self.plane) number_of_planes = self.airport.planes self.assertEqual(0, len(number_of_planes)) def test_2b_instruct_take_off(self): """aiport instruct_take_off calls take_off on plane""" self.airport.instruct_to_land(self.plane) self.airport.instruct_take_off(self.plane) self.plane.take_off.assert_called_with() def test_2c_instruct_take_off(self): """plane cannot take off if not at airport""" with self.assertRaisesRegexp(Exception, 'Plane not at airport: land plane first'): self.airport.instruct_take_off(self.plane) def test_2d_instruct_take_off(self): """plane cannot take off if stormy weather""" self.airport.instruct_to_land(self.plane) self.weather.is_stormy = MagicMock(return_value=True) with self.assertRaisesRegexp(Exception, 'Plane cannot take off: weather is stormy'): self.airport.instruct_take_off(self.plane)
class TestUserStory(unittest.TestCase): def setUp(self): self.weather = Weather() self.airport = Airport(self.weather) self.plane = Plane() self.weather.is_stormy = MagicMock(return_value=False) def test_user_story_1_plane_lands(self): # As an air traffic controller # So I can get passengers to a destination # I want to instruct a plane to land at an airport and confirm that it has landed self.airport.instruct_to_land(self.plane) self.assertIn(self.plane, self.airport.planes) self.assertTrue(self.plane.is_at_airport()) def test_user_story_2_plane_takes_off(self): # As an air traffic controller # So I can get passengers on the way to their destination # I want to instruct a plane to take off from an airport and confirm that it is no longer in the airport self.airport.instruct_to_land(self.plane) self.airport.instruct_take_off(self.plane) self.assertNotIn(self.plane, self.airport.planes) self.assertFalse(self.plane.is_at_airport()) def test_user_story_3_plane_lands_when_airport_empty(self): # As an air traffic controller # To ensure safety # I want to prevent landing when the airport is full for number in range(1,21): self.diff_plane = Plane() self.airport.instruct_to_land(self.diff_plane) with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane) def test_user_story_4_airport_capacity_changeable(self): # As the system designer # So that the software can be used for many different airports # I would like a default airport capacity that can be overridden as appropriate self.airport = Airport(self.weather, 5) for number in range(1,6): self.diff_plane = Plane() self.weather.is_stormy = MagicMock(return_value=False) self.airport.instruct_to_land(self.diff_plane) print self.airport.planes with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane) def test_user_story_5_no_take_off_when_stormy(self): # As an air traffic controller # To ensure safety # I want to prevent takeoff when weather is stormy self.airport.instruct_to_land(self.plane) self.weather.is_stormy = MagicMock(return_value=True) with self.assertRaisesRegexp(Exception, 'Plane cannot take off: weather is stormy'): self.airport.instruct_take_off(self.plane) def test_user_story_6_no_landing_when_stormy(self): # As an air traffic controller # To ensure safety # I want to prevent landing when weather is stormy self.weather.is_stormy = MagicMock(return_value=True) with self.assertRaisesRegexp(Exception, 'Plane cannot land: weather is stormy'): self.airport.instruct_to_land(self.plane)
def test_airport_set_capacity(self): airport = Airport(capacity=5) self.assertEqual(airport.capacity, 5)
class TestUserStory(unittest.TestCase): def setUp(self): self.weather = Weather() self.airport = Airport(self.weather) self.plane = Plane() self.weather.is_stormy = MagicMock(return_value=False) def test_user_story_1_plane_lands(self): # As an air traffic controller # So I can get passengers to a destination # I want to instruct a plane to land at an airport and confirm that it has landed self.airport.instruct_to_land(self.plane) self.assertIn(self.plane, self.airport.planes) self.assertTrue(self.plane.is_at_airport()) def test_user_story_2_plane_takes_off(self): # As an air traffic controller # So I can get passengers on the way to their destination # I want to instruct a plane to take off from an airport and confirm that it is no longer in the airport self.airport.instruct_to_land(self.plane) self.airport.instruct_take_off(self.plane) self.assertNotIn(self.plane, self.airport.planes) self.assertFalse(self.plane.is_at_airport()) def test_user_story_3_plane_lands_when_airport_empty(self): # As an air traffic controller # To ensure safety # I want to prevent landing when the airport is full for number in range(1, 21): self.diff_plane = Plane() self.airport.instruct_to_land(self.diff_plane) with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane) def test_user_story_4_airport_capacity_changeable(self): # As the system designer # So that the software can be used for many different airports # I would like a default airport capacity that can be overridden as appropriate self.airport = Airport(self.weather, 5) for number in range(1, 6): self.diff_plane = Plane() self.weather.is_stormy = MagicMock(return_value=False) self.airport.instruct_to_land(self.diff_plane) print self.airport.planes with self.assertRaisesRegexp(Exception, 'Airport is full: Take off plane'): self.airport.instruct_to_land(self.plane) def test_user_story_5_no_take_off_when_stormy(self): # As an air traffic controller # To ensure safety # I want to prevent takeoff when weather is stormy self.airport.instruct_to_land(self.plane) self.weather.is_stormy = MagicMock(return_value=True) with self.assertRaisesRegexp( Exception, 'Plane cannot take off: weather is stormy'): self.airport.instruct_take_off(self.plane) def test_user_story_6_no_landing_when_stormy(self): # As an air traffic controller # To ensure safety # I want to prevent landing when weather is stormy self.weather.is_stormy = MagicMock(return_value=True) with self.assertRaisesRegexp(Exception, 'Plane cannot land: weather is stormy'): self.airport.instruct_to_land(self.plane)
def test_airport_does_not_allow_plane_to_land_when_at_capacity(self): airport = Airport() airport.landPlane("Plane") with self.assertRaises(RuntimeError): airport.landPlane("Plane2")
def test_0b_defaults(self): """airport can change capacity""" self.airport = Airport(self.weather, 5) self.assertEqual(self.airport.capacity, 5)