def test_return_the_found_flight(self, database_session): # Given flight_model = FlightModel() flight_model.identifier = '3558' flight_model.status = 'ongoing' flight_model.start_time = datetime.now() flight_model.duration = 257 flight_model.position = "40.714,-74.006" database_session.add(flight_model) flight_datasource = FlightDatasource(database_session) # When flight = flight_datasource.get_one_flight(Identifier('3558')) # Then assert flight assert isinstance(flight, Flight) assert flight.status == 'ongoing' assert flight.duration == 257 assert isinstance(flight.position, Position) assert flight.position.latitude == 40.714 assert flight.position.longitude == -74.006 assert isinstance(flight.identifier, Identifier) assert flight.identifier.value == '3558' assert flight.start_time == datetime.now()
def test_return_an_unregistered_plane_message_when_plane_is_not_found(self): # Given plane_identifier = PlaneIdentifier('YGUB') plane_repository = PlaneRepository() plane_repository.get_one_plane = Mock(return_value=None) flight_repository = FlightRepository() flight_repository.save_new_flight = Mock() flight = Flight( status='ongoing', duration=267, start_time=datetime(2020, 4, 5, 15, 25, 16), identifier=Identifier('0937'), position=None ) # When result = declare_new_flight( plane_identifier=plane_identifier, flight=flight, plane_repository=plane_repository, flight_repository=flight_repository ) # Then assert isinstance(result, UnregisteredPlane) plane_repository.get_one_plane.assert_called_with(plane_identifier)
def test_save_a_new_flight_when_plane_exists(self): # Given plane_identifier = PlaneIdentifier('YGUB') plane = Plane(identifier=plane_identifier, number_of_places=50) plane_repository = PlaneRepository() plane_repository.get_one_plane = Mock(return_value=plane) flight_repository = FlightRepository() flight_repository.save_new_flight = Mock() flight = Flight( status='ongoing', duration=267, start_time=datetime(2020, 4, 5, 15, 25, 16), identifier=Identifier('0937'), position=None ) # When result = declare_new_flight( plane_identifier=plane_identifier, flight=flight, plane_repository=plane_repository, flight_repository=flight_repository ) # Then assert result is None flight_repository.save_new_flight.assert_called_with(plane=plane, flight=flight)
def get_one_flight(self, identifier: Identifier) -> Union[Flight, None]: flight = self.session \ .query(FlightModel) \ .filter(FlightModel.identifier == identifier.value) \ .first() if not flight: return None if flight.position: latitude, longitude = flight.position.split(',') position = Position( latitude=float(latitude), longitude=float(longitude) ) else: position = None return Flight( status=flight.status, duration=flight.duration, start_time=flight.start_time, identifier=Identifier(flight.identifier), position=position )
def test_save_flight_when_plane_exists(self, database_session): # Given identifier_model = IdentifierModel() identifier_model.code = 'FDY-198' database_session.add(identifier_model) plane_model = PlaneModel() plane_model.places = 145 plane_model.identifier = identifier_model database_session.add(plane_model) flight = Flight(identifier=Identifier('2VG5'), status='ongoing', start_time=datetime.now(), duration=937, position=None) plane = Plane(identifier=PlaneIdentifier('FDY-198'), number_of_places=23) flight_datasource = FlightDatasource(database_session) # When flight_datasource.save_new_flight(flight, plane) # Then assert database_session.query(FlightModel).count() == 1 saved_flight = database_session.query(FlightModel).first() assert saved_flight.plane.identifier.code == 'FDY-198'
def test_attach_a_new_position_for_a_flight(self, database_session): # Given position = Position(latitude=40.714, longitude=-74.006) flight_model = FlightModel() flight_model.identifier = '2VG5' flight_model.status = 'ongoing' flight_model.start_time = datetime.now() flight_model.duration = 257 database_session.add(flight_model) flight = Flight(identifier=Identifier('2VG5'), status='ongoing', start_time=datetime.now(), duration=937, position=None) flight_datasource = FlightDatasource(database_session) # When flight_datasource.save_new_position(flight, position) # Then flight = database_session.query(FlightModel) \ .filter(FlightModel.identifier == '2VG5').first() assert flight is not None assert flight.position == "40.714,-74.006"
def from_json(flight_payload) -> Flight: return Flight( status=flight_payload['status'], duration=flight_payload['duration'], start_time=flight_payload['start_time'], identifier=Identifier(flight_payload['identifier']), position=None, )
def get_one_flight(flight_id): flight_datasource = FlightDatasource(session=db.session) flight = get_flight_details(repository=flight_datasource, identifier=Identifier(flight_id)) if isinstance(flight, UndeclaredFlight): return {}, 404 return jsonify(FlightSerializer.to_json(flight))
def test_save_the_new_position(self): # Given position = Position(latitude=40.714, longitude=-74.006) flight_repository = FlightRepository() flight = Flight(status='ongoing', duration=1456, start_time=datetime(2020, 4, 5, 15, 25, 16), identifier=Identifier('0937'), position=None) flight_repository.get_one_flight = Mock(return_value=flight) flight_repository.save_new_position = Mock() # When add_new_position_for_a_flight(flight_repository, position, identifier=Identifier('DYFGU')) # Then flight_repository.save_new_position.assert_called_once_with( flight, position)
def test_does_not_return_undeclared_flight_when_new_position_is_saved( self): # Given position = Position(latitude=40.714, longitude=-74.006) flight_repository = FlightRepository() flight = Flight(status='ongoing', duration=1456, start_time=datetime(2020, 4, 5, 15, 25, 16), identifier=Identifier('0937'), position=None) flight_repository.get_one_flight = Mock(return_value=flight) flight_repository.save_new_position = Mock(return_value=None) # When result = add_new_position_for_a_flight(flight_repository, position, identifier=Identifier('DYFGU')) # Then assert result is None
def test_should_return_an_undeclared_flight_message_when_flight_does_not_exist(self): # Given flight_repository = FlightRepository() flight_repository.get_one_flight = Mock(return_value=None) identifier = Identifier('DYFGU') # When result = get_flight_details(repository=flight_repository, identifier=identifier) # Then flight_repository.get_one_flight.assert_called_once_with(identifier=identifier) assert isinstance(result, UndeclaredFlight)
def add_a_plane(flight_id): flight_repository = FlightDatasource(session=db.session) payload = request.json result = add_new_position_for_a_flight(flight_repository=flight_repository, position=Position( latitude=payload['latitude'], longitude=payload['longitude']), identifier=Identifier(flight_id)) if isinstance(result, UndeclaredFlight): return {}, 404 return {}, 200
def test_should_return_an_undeclared_plane_message_when_plane_does_not_exist( self): # Given position = Position(latitude=40.714, longitude=-74.006) flight_repository = FlightRepository() flight_repository.get_one_flight = Mock(return_value=None) identifier = Identifier('DYFGU') # When result = add_new_position_for_a_flight(flight_repository, position, identifier=identifier) # Then assert isinstance(result, UndeclaredFlight) flight_repository.get_one_flight.assert_called_once_with(identifier)
def test_return_none_when_the_flight_does_not_exist( self, database_session): # Given flight_model = FlightModel() flight_model.identifier = 3558 flight_model.status = 'ongoing' flight_model.start_time = datetime.now() flight_model.duration = 257 database_session.add(flight_model) flight_datasource = FlightDatasource(database_session) # When flight = flight_datasource.get_one_flight(Identifier('2456')) # Then assert flight is None
def test_should_return_the_json_version_of_a_flight(self): # Given flight = Flight(status='ongoing', duration=267, start_time=datetime(2020, 4, 5, 15, 25, 16), identifier=Identifier('0937'), position=None) # When json = FlightSerializer.to_json(flight) # Then assert json == { 'status': 'ongoing', 'duration': 267, 'start_time': '2020-04-05T15:25:16', 'identifier': '0937', 'position': None }
def get_all_flights(self) -> List[Flight]: flights = self.session.query(FlightModel).all() list_of_flights = [] for flight in flights: if flight.position: latitude, longitude = flight.position.split(',') position = Position( latitude=float(latitude), longitude=float(longitude) ) else: position = None flight = Flight( status=flight.status, duration=flight.duration, start_time=flight.start_time, identifier=Identifier(flight.identifier), position=position ) list_of_flights.append(flight) return list_of_flights
def test_should_serialize_even_an_array(self): # Given flights = [ Flight(status='ongoing', duration=267, start_time=datetime(2020, 4, 5, 15, 25, 16), identifier=Identifier('0937'), position=Position(latitude=15.4156, longitude=54.2567)) ] # When json = FlightSerializer.to_json(flights) # Then assert json == [{ 'status': 'ongoing', 'duration': 267, 'start_time': '2020-04-05T15:25:16', 'identifier': '0937', 'position': { 'latitude': 15.4156, 'longitude': 54.2567 } }]