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 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 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_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_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 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 } }]