Beispiel #1
0
 def test_process_no_lap_list(self):
     session = Session(session_uid="vettel2021")
     session.lap_number_current = 1
     packet = get_packet_mock()
     packet.carStatusData = [MagicMock(visualTyreCompound=7)]
     session = CarStatusPacket().process(packet, session)
     self.assertEqual(session.lap_list, {})
Beispiel #2
0
 def test_lap_new_without_old(self):
     session = Session(session_uid="vettel2021")
     session.lap_number_current = 1
     packet = get_packet_mock()
     packet.lapData = [
         MagicMock(currentLapNum=2,
                   currentLapTime=80,
                   sector1TimeInMS=10002,
                   sector2TimeInMS=20002,
                   pitStatus=2,
                   carPosition=4,
                   lastLapTime=60.006)
     ]
     session = LapPacket().process(packet, session)
     self.assertEqual(
         session.lap_list, {
             2: {
                 'sector_1_time_ms': 10002,
                 'sector_2_time_ms': 20002,
                 'sector_3_time_ms': 49996,
                 'lap_number': 2,
                 'car_race_position': 4,
                 'pit_status': 2,
                 'is_valid': True
             }
         })
     self.assertEqual(session.lap_number_current, 2)
Beispiel #3
0
 def test_process(self):
     session = Session(session_uid="vettel2021")
     session.process_lap_in_f1laps = MagicMock()
     packet = get_packet_mock()
     packet.classificationData = [
         MagicMock(position=2, resultStatus=3, points=21)
     ]
     session = FinalClassificationPacket().process(packet, session)
     self.assertEqual(session.finish_position, 2)
     self.assertEqual(session.result_status, 3)
     self.assertEqual(session.points, 21)
     session.process_lap_in_f1laps.assert_called_with()
Beispiel #4
0
 def setUp(self):
     self.session = Session(session_uid="vettel2021")
     self.session.team_id = 1
     self.session.track_id = 5
     self.session.lap_number_current = 2
     self.session.lap_list = {
         1: {
             "sector_1_time_ms": 10001,
             "sector_2_time_ms": 20001,
             "sector_3_time_ms": 30001,
             "lap_number": 1,
             "car_race_position": 1,
             "pit_status": 0
         }
     }
Beispiel #5
0
 def test_lap_none(self):
     session = Session(session_uid="vettel2021")
     packet = get_packet_mock()
     packet.lapData = [
         MagicMock(currentLapNum=1,
                   currentLapTime=1,
                   sector1TimeInMS=1000,
                   sector2TimeInMS=0,
                   pitStatus=0,
                   carPosition=1)
     ]
     session = LapPacket().process(packet, session)
     self.assertEqual(
         session.lap_list, {
             1: {
                 'sector_1_time_ms': 1000,
                 'sector_2_time_ms': 0,
                 'sector_3_time_ms': 0,
                 'lap_number': 1,
                 'car_race_position': 1,
                 'pit_status': 0,
                 'is_valid': True
             }
         })
     self.assertEqual(session.lap_number_current, 1)
Beispiel #6
0
 def test_process_no_lap_list_with_current_lap(self):
     session = Session(session_uid="vettel2021")
     session.lap_number_current = 1
     session.lap_list = {
         0: {
             "sector_1_time_ms": 0,
             "sector_2_time_ms": 0,
             "sector_3_time_ms": 30002,
             "lap_number": 1,
             "car_race_position": 1,
             "pit_status": 1,
             'is_valid': True
         }
     }
     packet = get_packet_mock()
     packet.carStatusData = [MagicMock(visualTyreCompound=7)]
     session = CarStatusPacket().process(packet, session)
     self.assertEqual(session.lap_list.get(session.lap_number_current),
                      None)
Beispiel #7
0
 def test_get_lap_telemetry_data(self):
     session = Session(session_uid="vettel2021")
     self.assertEqual(session.get_lap_telemetry_data(1), None)
     session.telemetry = MagicMock()
     session.telemetry.get_telemetry_api_dict.return_value = {
         "test": "dict"
     }
     self.assertEqual(session.get_lap_telemetry_data(1), '{"test": "dict"}')
     session.telemetry_enabled = False
     self.assertEqual(session.get_lap_telemetry_data(1), None)
Beispiel #8
0
 def test_lap_current(self):
     session = Session(session_uid="vettel2021")
     session.lap_number_current = 2
     session.lap_list = {
         2: {
             "sector_1_time_ms": 10002,
             "sector_2_time_ms": 20002,
             "sector_3_time_ms": 30002,
             "lap_number": 2,
             "car_race_position": 1,
             "pit_status": 1
         }
     }
     packet = get_packet_mock()
     packet.lapData = [
         MagicMock(currentLapNum=2,
                   currentLapTime=80,
                   sector1TimeInMS=10002,
                   sector2TimeInMS=20002,
                   pitStatus=2,
                   carPosition=4,
                   currentLapInvalid=0)
     ]
     session = LapPacket().process(packet, session)
     self.assertEqual(
         session.lap_list, {
             2: {
                 'sector_1_time_ms': 10002,
                 'sector_2_time_ms': 20002,
                 'sector_3_time_ms': 49996,
                 'lap_number': 2,
                 'car_race_position': 4,
                 'pit_status': 2,
                 'is_valid': True
             }
         })
     self.assertEqual(session.lap_number_current, 2)
     # invalid lap
     packet.lapData = [
         MagicMock(currentLapNum=2,
                   currentLapTime=80,
                   sector1TimeInMS=10002,
                   sector2TimeInMS=20002,
                   pitStatus=2,
                   carPosition=4,
                   currentLapInvalid=1)
     ]
     session = LapPacket().process(packet, session)
     self.assertEqual(
         session.lap_list, {
             2: {
                 'sector_1_time_ms': 10002,
                 'sector_2_time_ms': 20002,
                 'sector_3_time_ms': 49996,
                 'lap_number': 2,
                 'car_race_position': 4,
                 'pit_status': 2,
                 'is_valid': False
             }
         })
Beispiel #9
0
 def test_session_new(self):
     existing_session = Session(session_uid="vettel2021")
     packet = get_packet_mock()
     packet.header.sessionUID = "george2021"
     packet.sessionType = 10
     packet.trackId = 5
     packet.weather = 1
     new_session = SessionPacket().process(packet, existing_session)
     self.assertEqual(new_session.session_udp_uid, "george2021")
     self.assertEqual(new_session.track_id, 5)
     self.assertEqual(new_session.session_type, 10)
     self.assertEqual(new_session.weather_ids, [1])
     self.assertEqual(new_session.is_online_game, False)
Beispiel #10
0
 def test_lap_new_with_old_without_sector_2(self):
     session = Session(session_uid="vettel2021")
     session.lap_number_current = 1
     session.lap_list = {
         1: {
             "sector_1_time_ms": 0,
             "sector_2_time_ms": 0,
             "sector_3_time_ms": 30002,
             "lap_number": 1,
             "car_race_position": 1,
             "pit_status": 1,
             'is_valid': True
         }
     }
     session.process_lap_in_f1laps = MagicMock()
     packet = get_packet_mock()
     packet.lapData = [
         MagicMock(currentLapNum=2,
                   currentLapTime=80,
                   sector1TimeInMS=10002,
                   sector2TimeInMS=20002,
                   pitStatus=2,
                   carPosition=4,
                   lastLapTime=30.002)
     ]
     session = LapPacket().process(packet, session)
     self.assertEqual(
         session.lap_list, {
             1: {
                 "sector_1_time_ms": 0,
                 "sector_2_time_ms": 0,
                 "sector_3_time_ms": 30002,
                 "lap_number": 1,
                 "car_race_position": 1,
                 "pit_status": 1,
                 'is_valid': True
             },
             2: {
                 'sector_1_time_ms': 10002,
                 'sector_2_time_ms': 20002,
                 'sector_3_time_ms': 49996,
                 'lap_number': 2,
                 'car_race_position': 4,
                 'pit_status': 2,
                 'is_valid': True
             }
         })
     self.assertEqual(session.lap_number_current, 2)
     session.process_lap_in_f1laps.assert_not_called()
Beispiel #11
0
 def test_session_weather_changes(self):
     # add first value
     session = Session(session_uid="vettel2021")
     packet = get_packet_mock()
     packet.sessionType = 10
     packet.trackId = 5
     packet.weather = 1
     session = SessionPacket().process(packet, session)
     self.assertEqual(session.weather_ids, [1])
     # add a new value
     packet = get_packet_mock()
     packet.sessionType = 10
     packet.trackId = 5
     packet.weather = 4
     session = SessionPacket().process(packet, session)
     self.assertEqual(session.weather_ids, [1, 4])
     # add the same value again
     packet = get_packet_mock()
     packet.sessionType = 10
     packet.trackId = 5
     packet.weather = 4
     session = SessionPacket().process(packet, session)
     self.assertEqual(session.weather_ids, [1, 4])
Beispiel #12
0
 def test_empty_session_object(self):
     session = Session(session_uid="vettel2021")
     self.assertEqual(session.session_udp_uid, "vettel2021")
Beispiel #13
0
 def test_get_f1laps_lap_times_list(self):
     session = Session(session_uid="vettel2021")
     self.assertEqual(session.get_f1laps_lap_times_list(), [])
     session.lap_list = {
         1: {
             "sector_1_time_ms": 10001,
             "sector_2_time_ms": 20001,
             "sector_3_time_ms": 30001,
             "lap_number": 1,
             "car_race_position": 1,
             "pit_status": 0
         },
         2: {
             "sector_1_time_ms": 10002,
             "sector_2_time_ms": 20002,
             "sector_3_time_ms": 30002,
             "lap_number": 2,
             "car_race_position": 1,
             "pit_status": 1,
             "tyre_compound_visual": 8
         },
     }
     self.assertEqual(session.get_f1laps_lap_times_list(),
                      [{
                          'car_race_position': 1,
                          'lap_number': 1,
                          'pit_status': 0,
                          'sector_1_time_ms': 10001,
                          'sector_2_time_ms': 20001,
                          'sector_3_time_ms': 30001,
                          "tyre_compound_visual": None,
                          'telemetry_data_string': None,
                      }, {
                          'car_race_position': 1,
                          'lap_number': 2,
                          'pit_status': 1,
                          'sector_1_time_ms': 10002,
                          'sector_2_time_ms': 20002,
                          'sector_3_time_ms': 30002,
                          "tyre_compound_visual": 8,
                          'telemetry_data_string': None,
                      }])
     # incomplete lap should not be returned
     session.lap_list = {
         1: {
             "sector_1_time_ms": 10001,
             "sector_2_time_ms": 20001,
             "sector_3_time_ms": 30001,
             "lap_number": 1,
             "car_race_position": 1,
             "pit_status": 0
         },
         2: {
             "sector_1_time_ms": 10002,
             "sector_2_time_ms": 20002,
             "sector_3_time_ms": 30002,
             "lap_number": 2,
             "car_race_position": 1,
             "pit_status": 1,
             "tyre_compound_visual": 16
         },
         3: {
             "sector_1_time_ms": 10002,
             "sector_2_time_ms": None,
             "sector_3_time_ms": 30002,
             "lap_number": 3,
             "car_race_position": 1,
             "pit_status": 1
         },
     }
     self.assertEqual(session.get_f1laps_lap_times_list(),
                      [{
                          'car_race_position': 1,
                          'lap_number': 1,
                          'pit_status': 0,
                          'sector_1_time_ms': 10001,
                          'sector_2_time_ms': 20001,
                          'sector_3_time_ms': 30001,
                          "tyre_compound_visual": None,
                          'telemetry_data_string': None,
                      }, {
                          'car_race_position': 1,
                          'lap_number': 2,
                          'pit_status': 1,
                          'sector_1_time_ms': 10002,
                          'sector_2_time_ms': 20002,
                          'sector_3_time_ms': 30002,
                          "tyre_compound_visual": 16,
                          'telemetry_data_string': None,
                      }])
Beispiel #14
0
 def test_map_weather_ids_to_f1laps_token(self):
     session = Session(session_uid="vettel2021")
     self.assertEqual(session.map_weather_ids_to_f1laps_token(), "dry")
     session.weather_ids = [1]
     self.assertEqual(session.map_weather_ids_to_f1laps_token(), "dry")
     session.weather_ids = [0, 1]
     self.assertEqual(session.map_weather_ids_to_f1laps_token(), "dry")
     session.weather_ids = [3]
     self.assertEqual(session.map_weather_ids_to_f1laps_token(), "wet")
     session.weather_ids = [3, 5]
     self.assertEqual(session.map_weather_ids_to_f1laps_token(), "wet")
     session.weather_ids = [0, 1, 4, 5]
     self.assertEqual(session.map_weather_ids_to_f1laps_token(), "mixed")
Beispiel #15
0
 def test_session_existing(self):
     existing_session = Session(session_uid="vettel2021")
     packet = get_packet_mock()
     new_session = SessionPacket().process(packet, existing_session)
     self.assertEqual(new_session, existing_session)
Beispiel #16
0
class SessionAPITests(TestCase):
    def setUp(self):
        self.session = Session(session_uid="vettel2021")
        self.session.team_id = 1
        self.session.track_id = 5
        self.session.lap_number_current = 2
        self.session.lap_list = {
            1: {
                "sector_1_time_ms": 10001,
                "sector_2_time_ms": 20001,
                "sector_3_time_ms": 30001,
                "lap_number": 1,
                "car_race_position": 1,
                "pit_status": 0
            }
        }

    @patch('receiver.f12020.session.F1LapsAPI.lap_create')
    def test_single_lap_success(self, mock_lap_create_api):
        # set session_type to time_trial to test single lap
        self.session.session_type = 12
        mock_lap_create_api.return_value = MagicMock(status_code=201)
        self.assertEqual(self.session.process_lap_in_f1laps(1), True)

    @patch('receiver.f12020.session.F1LapsAPI.lap_create')
    def test_single_lap_fail(self, mock_lap_create_api):
        # set session_type to time_trial to test single lap
        self.session.session_type = 12
        mock_lap_create_api.return_value = MagicMock(
            status_code=404, content=json.dumps({"error": "it didnt work"}))
        self.assertEqual(self.session.process_lap_in_f1laps(1), False)

    @patch('receiver.f12020.session.F1LapsAPI.session_create')
    def test_session_create_success(self, mock_session_create_api):
        # set session_type to race to test session
        self.session.session_type = 10
        # make sure we have no f1laps id in the session yet
        self.session.f1_laps_session_id = None
        mock_session_create_api.return_value = MagicMock(
            status_code=201, content=json.dumps({"id": "astonmartin4tw"}))
        self.assertEqual(self.session.process_lap_in_f1laps(1), True)
        self.assertEqual(self.session.f1_laps_session_id, "astonmartin4tw")

    @patch('receiver.f12020.session.F1LapsAPI.session_update')
    def test_session_update_success(self, mock_session_update_api):
        # set session_type to race to test session
        self.session.session_type = 10
        # make sure we have a f1laps id in the session
        self.session.f1_laps_session_id = "astonmartin4tw"
        mock_session_update_api.return_value = MagicMock(status_code=200)
        self.assertEqual(self.session.process_lap_in_f1laps(1), True)

    @patch('receiver.f12020.session.F1LapsAPI.session_update')
    def test_session_update_error(self, mock_session_update_api):
        # set session_type to race to test session
        self.session.session_type = 10
        # make sure we have a f1laps id in the session
        self.session.f1_laps_session_id = "astonmartin4tw"
        mock_session_update_api.return_value = MagicMock(
            status_code=404, content=json.dumps({"error": "it didnt work"}))
        self.assertEqual(self.session.process_lap_in_f1laps(1), False)

    @patch('receiver.f12020.session.F1LapsAPI.session_update')
    @patch('receiver.f12020.session.F1LapsAPI.session_list')
    @patch('receiver.f12020.session.F1LapsAPI.session_create')
    def test_session_create_error_list_success_update(self,
                                                      mock_session_create_api,
                                                      mock_session_list_api,
                                                      mock_session_update_api):
        # set session_type to race to test session
        self.session.session_type = 10
        # make sure we don't have a f1laps id in the session yet
        self.session.f1_laps_session_id = None
        mock_session_create_api.return_value = MagicMock(
            status_code=400, content=json.dumps({"error": "already exists"}))
        mock_session_list_api.return_value = MagicMock(
            status_code=200,
            content=json.dumps({"results": [{
                "id": "astonmartin4tw"
            }]}))
        mock_session_update_api.return_value = MagicMock(status_code=200)
        self.assertEqual(self.session.process_lap_in_f1laps(1), True)
        self.assertEqual(self.session.f1_laps_session_id, "astonmartin4tw")

    @patch('receiver.f12020.session.F1LapsAPI.session_list')
    @patch('receiver.f12020.session.F1LapsAPI.session_create')
    def test_session_create_error_list_error(self, mock_session_create_api,
                                             mock_session_list_api):
        # set session_type to race to test session
        self.session.session_type = 10
        # make sure we don't have a f1laps id in the session yet
        self.session.f1_laps_session_id = None
        mock_session_create_api.return_value = MagicMock(
            status_code=400, content=json.dumps({"error": "already exists"}))
        mock_session_list_api.return_value = MagicMock(status_code=200,
                                                       content=json.dumps(
                                                           {"results": []}))
        self.assertEqual(self.session.process_lap_in_f1laps(1), False)

    @patch('receiver.f12020.session.F1LapsAPI.session_create')
    def test_session_create_error_401(self, mock_session_create_api):
        # set session_type to race to test session
        self.session.session_type = 10
        # make sure we don't have a f1laps id in the session yet
        self.session.f1_laps_session_id = None
        mock_session_create_api.return_value = MagicMock(
            status_code=401, content=json.dumps({"error": "invalid token"}))
        self.assertEqual(self.session.process_lap_in_f1laps(1), False)
Beispiel #17
0
 def test_get_track_name(self):
     session = Session(session_uid="vettel2021")
     self.assertEqual(session.get_track_name(), None)
     session.track_id = 11
     self.assertEqual(session.get_track_name(), "Monza")
Beispiel #18
0
 def test_map_udp_session_id_to_f1laps_token(self):
     session = Session(session_uid="vettel2021")
     self.assertEqual(session.map_udp_session_id_to_f1laps_token(), None)
     self.assertEqual(session.session_type_supported_by_f1laps_as_session(),
                      False)
     session.session_type = 10
     self.assertEqual(session.map_udp_session_id_to_f1laps_token(), "race")
     self.assertEqual(session.session_type_supported_by_f1laps_as_session(),
                      True)
     session.session_type = 12
     self.assertEqual(session.map_udp_session_id_to_f1laps_token(),
                      "time_trial")
     self.assertEqual(session.session_type_supported_by_f1laps_as_session(),
                      False)
     session.session_type = 7
     self.assertEqual(session.map_udp_session_id_to_f1laps_token(),
                      "qualifying_3")