Best Python code snippet using molotov_python
test_back.py
Source:test_back.py
1from unittest import mock, skipUnless2import requests3from django.test import TestCase4from autostew_back import settings5from autostew_back.ds_api.mocked_api import FakeApi6from autostew_web_enums.models import DamageDefinition, TireWearDefinition, FuelUsageDefinition, PenaltyDefinition, \7 AllowedViewsDefinition, WeatherDefinition, GameModeDefinition, PrivacyDefinition, MemberLoadState8from autostew_web_session.models.member import Member9from autostew_web_session.models.models import VehicleClass, Track, Vehicle, SetupQueueEntry, SetupRotationEntry10from autostew_web_session.models.participant import Participant11from autostew_web_session.models.server import Server, NoSessionSetupTemplateAvailableException12from autostew_web_session.models.session import SessionSetup, Session13class TestBack(TestCase):14 @classmethod15 def make_test_setup(cls) -> SessionSetup:16 cls.enum_defaults = defaults = {'ingame_id': 0}17 vehicle_class = VehicleClass.objects.get_or_create(name="GT3", defaults=cls.enum_defaults)[0]18 return SessionSetup(19 name='test',20 is_template=True,21 server_controls_setup=True,22 server_controls_track=True,23 server_controls_vehicle_class=True,24 server_controls_vehicle=True,25 grid_size=24,26 max_players=24,27 opponent_difficulty=100,28 force_identical_vehicles=False,29 allow_custom_vehicle_setup=True,30 force_realistic_driving_aids=True,31 abs_allowed=False,32 sc_allowed=False,33 tcs_allowed=False,34 force_manual=False,35 rolling_starts=False,36 force_same_vehicle_class=True,37 fill_session_with_ai=False,38 mechanical_failures=True,39 auto_start_engine=True,40 timed_race=False,41 ghost_griefers=True,42 enforced_pitstop=True,43 practice1_length=60,44 practice2_length=0,45 qualify_length=15,46 warmup_length=5,47 race1_length=25,48 race2_length=0,49 privacy=PrivacyDefinition.objects.get_or_create(name="Public", ingame_id=2)[0],50 damage=DamageDefinition.objects.get_or_create(name="FULL", defaults=cls.enum_defaults)[0],51 tire_wear=TireWearDefinition.objects.get_or_create(name="X2", defaults=cls.enum_defaults)[0],52 fuel_usage=FuelUsageDefinition.objects.get_or_create(name="STANDARD", defaults=cls.enum_defaults)[0],53 penalties=PenaltyDefinition.objects.get_or_create(name="FULL", defaults=cls.enum_defaults)[0],54 allowed_views=AllowedViewsDefinition.objects.get_or_create(name="Any", defaults=cls.enum_defaults)[0],55 track=Track.objects.get_or_create(name="Hockenheim GP", defaults={'ingame_id': 1, 'grid_size': 24})[0],56 vehicle_class=vehicle_class,57 vehicle=Vehicle.objects.get_or_create(58 name="McLaren 12C GT3",59 vehicle_class=vehicle_class,60 defaults=cls.enum_defaults61 )[0],62 date_year=2016,63 date_month=1,64 date_day=1,65 date_hour=0,66 date_minute=0,67 date_progression=1,68 weather_progression=1,69 weather_slots=1,70 weather_1=WeatherDefinition.objects.get_or_create(name="Clear", defaults=cls.enum_defaults)[0],71 weather_2=WeatherDefinition.objects.get(name="Clear"),72 weather_3=WeatherDefinition.objects.get(name="Clear"),73 weather_4=WeatherDefinition.objects.get(name="Clear"),74 game_mode=GameModeDefinition.objects.get_or_create(name="MP_Race", defaults=cls.enum_defaults)[0],75 )76 def setUp(self):77 self.server = Server.objects.create(78 name="Test",79 api_url="http://localhost:9000",80 setup_rotation_index=0,81 running=False,82 )83 self.api = FakeApi()84 with mock.patch.object(requests, 'get', self.api.fake_request):85 self.server.back_init_env(settings)86 def test_back_start(self):87 with mock.patch.object(requests, 'get', self.api.fake_request):88 self.server.back_start(settings, False)89 def test_back_server_data(self):90 with mock.patch.object(requests, 'get', self.api.fake_request):91 self.server.back_start(settings, False)92 self.assertEqual(self.server.lobby_id, 0)93 self.assertEqual(self.server.joinable_internal, False)94 self.assertEqual(self.server.max_member_count, 0)95 self.assertEqual(self.server.running, True)96 @skipUnless(False, 'it fails!')97 def test_back_start_running_server_without_setup(self):98 self.api = FakeApi('autostew_back/tests/test_assets/session_in_lobby_one_player.json')99 with mock.patch.object(requests, 'get', self.api.fake_request):100 self.assertRaises(NoSessionSetupTemplateAvailableException, self.server.back_start, settings, False)101 def test_back_start_running_server_in_lobby_with_setup(self):102 self.api = FakeApi('autostew_back/tests/test_assets/session_in_lobby_one_player.json')103 setup = self.make_test_setup()104 setup.save()105 SetupQueueEntry.objects.create(order=0, setup=setup, server=self.server)106 with mock.patch.object(requests, 'get', self.api.fake_request):107 self.server.back_start(settings)108 def test_back_start_running_server_in_quali_with_setup(self):109 self.api = FakeApi('autostew_back/tests/test_assets/session_in_quali_two_players_14ai.json')110 setup = self.make_test_setup()111 setup.save()112 SetupQueueEntry.objects.create(order=0, setup=setup, server=self.server)113 with mock.patch.object(requests, 'get', self.api.fake_request):114 self.server.back_start(settings)115 @classmethod116 def make_test_setup(cls) -> SessionSetup:117 cls.enum_defaults = defaults = {'ingame_id': 0}118 vehicle_class = VehicleClass.objects.get_or_create(name="GT3", defaults=cls.enum_defaults)[0]119 return SessionSetup(120 name='test',121 is_template=True,122 server_controls_setup=True,123 server_controls_track=True,124 server_controls_vehicle_class=True,125 server_controls_vehicle=True,126 grid_size=24,127 max_players=24,128 opponent_difficulty=100,129 force_identical_vehicles=False,130 allow_custom_vehicle_setup=True,131 force_realistic_driving_aids=True,132 abs_allowed=False,133 sc_allowed=False,134 tcs_allowed=False,135 force_manual=False,136 rolling_starts=False,137 force_same_vehicle_class=True,138 fill_session_with_ai=False,139 mechanical_failures=True,140 auto_start_engine=True,141 timed_race=False,142 ghost_griefers=True,143 enforced_pitstop=True,144 practice1_length=60,145 practice2_length=0,146 qualify_length=15,147 warmup_length=5,148 race1_length=25,149 race2_length=0,150 privacy=PrivacyDefinition.get_or_create_default(2),151 damage=DamageDefinition.objects.get_or_create(name="FULL", defaults=cls.enum_defaults)[0],152 tire_wear=TireWearDefinition.objects.get_or_create(name="X2", defaults=cls.enum_defaults)[0],153 fuel_usage=FuelUsageDefinition.objects.get_or_create(name="STANDARD", defaults=cls.enum_defaults)[0],154 penalties=PenaltyDefinition.objects.get_or_create(name="FULL", defaults=cls.enum_defaults)[0],155 allowed_views=AllowedViewsDefinition.objects.get_or_create(name="Any", defaults=cls.enum_defaults)[0],156 track=Track.objects.get_or_create(name="Hockenheim GP", defaults={'ingame_id': 1, 'grid_size': 24})[0],157 vehicle_class=vehicle_class,158 vehicle=Vehicle.objects.get_or_create(159 name="McLaren 12C GT3",160 vehicle_class=vehicle_class,161 defaults=cls.enum_defaults162 )[0],163 date_year=2016,164 date_month=1,165 date_day=1,166 date_hour=0,167 date_minute=0,168 date_progression=1,169 weather_progression=1,170 weather_slots=1,171 weather_1=WeatherDefinition.objects.get_or_create(name="Clear", defaults=cls.enum_defaults)[0],172 weather_2=WeatherDefinition.objects.get(name="Clear"),173 weather_3=WeatherDefinition.objects.get(name="Clear"),174 weather_4=WeatherDefinition.objects.get(name="Clear"),175 game_mode=GameModeDefinition.objects.get_or_create(name="MP_Race", defaults=cls.enum_defaults)[0],176 )177 @skipUnless(False, 'it fails!')178 def test_create_session_in_lobby(self):179 """180 Tests writing a basic session into the database181 """182 api = FakeApi('autostew_back/tests/test_assets/session_in_lobby_one_player.json')183 test_setup = self.make_test_setup()184 test_setup.save(True)185 SetupRotationEntry.objects.create(186 order=0,187 server=self.server,188 setup=test_setup189 )190 with mock.patch.object(requests, 'get', api.fake_request):191 self.server.back_start(settings, True)192 self.assertEqual(Server.objects.count(), 1)193 self.assertEqual(SessionSetup.objects.count(), 3)194 self.assertEqual(Session.objects.filter(parent=None).count(), 1)195 self.assertEqual(Session.objects.count(), 2)196 self.assertEqual(Member.objects.filter(parent=None).count(), 1)197 self.assertEqual(Member.objects.count(), 2)198 self.assertEqual(Participant.objects.count(), 0)199 server_in_db = Server.objects.all()[0]200 self.assertTrue(server_in_db.running)201 session_setup = SessionSetup.objects.filter(is_template=False)[0]202 self.assertTrue(session_setup.server_controls_setup)203 self.assertTrue(session_setup.server_controls_track)204 self.assertFalse(session_setup.server_controls_vehicle)205 self.assertTrue(session_setup.server_controls_vehicle_class)206 self.assertEqual(session_setup.grid_size, 22)207 self.assertEqual(session_setup.max_players, 8)208 self.assertEqual(session_setup.opponent_difficulty, 100)209 self.assertEqual(session_setup.force_identical_vehicles, False)210 self.assertEqual(session_setup.allow_custom_vehicle_setup, True)211 self.assertEqual(session_setup.force_realistic_driving_aids, True)212 self.assertEqual(session_setup.force_manual, False)213 self.assertEqual(session_setup.rolling_starts, False)214 self.assertEqual(session_setup.force_same_vehicle_class, True)215 self.assertEqual(session_setup.fill_session_with_ai, True)216 self.assertEqual(session_setup.mechanical_failures, False)217 self.assertEqual(session_setup.auto_start_engine, True)218 self.assertEqual(session_setup.timed_race, False)219 self.assertEqual(session_setup.ghost_griefers, True)220 self.assertEqual(session_setup.enforced_pitstop, True)221 self.assertEqual(session_setup.practice1_length, 0)222 self.assertEqual(session_setup.practice2_length, 0)223 self.assertEqual(session_setup.qualify_length, 10)224 self.assertEqual(session_setup.warmup_length, 0)225 self.assertEqual(session_setup.race1_length, 5)226 self.assertEqual(session_setup.race2_length, 0)227 self.assertEqual(session_setup.privacy.ingame_id, 0)228 self.assertEqual(session_setup.damage.name, "FULL")229 self.assertEqual(session_setup.tire_wear.name, "X2")230 self.assertEqual(session_setup.fuel_usage.name, "STANDARD")231 self.assertEqual(session_setup.penalties.name, "FULL")232 self.assertEqual(session_setup.allowed_views.name, "Any")233 self.assertEqual(session_setup.track.name, "Zolder")234 self.assertEqual(session_setup.vehicle_class.name, "GT3")235 self.assertEqual(session_setup.date_progression, 5)236 self.assertEqual(session_setup.weather_slots, 1)237 self.assertEqual(session_setup.weather_1.name, "Clear")238 session = Session.objects.all()[0]239 session_snapshot = Session.objects.exclude(parent=None)[0]240 self.assertEqual(session.server, server_in_db)241 self.assertEqual(session.setup_actual, session_setup)242 self.assertEqual(session.running, True)243 self.assertEqual(session.finished, False)244 self.assertEqual(session.max_member_count, 22)245 self.assertEqual(session_snapshot.parent, session)246 self.assertEqual(session_snapshot.session_state.name, "Lobby")247 self.assertEqual(session_snapshot.session_stage.name, "Practice1")248 self.assertEqual(session_snapshot.session_phase.name, "Invalid")249 self.assertEqual(session_snapshot.session_time_elapsed, 0)250 self.assertEqual(session_snapshot.session_time_duration, 0)251 self.assertEqual(session_snapshot.num_participants_valid, 0)252 self.assertEqual(session_snapshot.num_participants_disq, 0)253 self.assertEqual(session_snapshot.num_participants_retired, 0)254 self.assertEqual(session_snapshot.num_participants_dnf, 0)255 self.assertEqual(session_snapshot.num_participants_finished, 0)256 self.assertEqual(session_snapshot.current_year, 2012)257 self.assertEqual(session_snapshot.current_month, 9)258 self.assertEqual(session_snapshot.current_day, 22)259 self.assertEqual(session_snapshot.current_hour, 6)260 self.assertEqual(session_snapshot.current_minute, 56)261 self.assertEqual(session_snapshot.rain_density_visual, 0)262 self.assertEqual(session_snapshot.wetness_path, 725)263 self.assertEqual(session_snapshot.wetness_off_path, 725)264 self.assertEqual(session_snapshot.wetness_avg, 725)265 self.assertEqual(session_snapshot.wetness_predicted_max, 0)266 self.assertEqual(session_snapshot.wetness_max_level, 725)267 self.assertEqual(session_snapshot.temperature_ambient, 21128)268 self.assertEqual(session_snapshot.temperature_track, 29692)269 self.assertEqual(session_snapshot.air_pressure, 101325)270 member = Member.objects.all()[0]271 self.assertEqual(member.session, session)272 self.assertEqual(member.still_connected, True)273 self.assertEqual(member.vehicle.name, "McLaren 12C GT3")274 self.assertEqual(member.livery.name, "McLaren #59")275 self.assertEqual(member.refid, 26752)276 self.assertEqual(member.steam_id, '76561197969382874')277 self.assertEqual(member.name, 'blak')278 # flags are not set, so they are not tested279 m_snap = Member.objects.exclude(parent=None)[0]280 self.assertEqual(m_snap.parent, member)281 self.assertEqual(m_snap.session, session_snapshot)282 self.assertEqual(m_snap.still_connected, True)283 self.assertEqual(m_snap.ingame_state.name, "Connected")284 self.assertEqual(m_snap.ping, 10)285 self.assertEqual(m_snap.ingame_index, 0)286 self.assertEqual(m_snap.is_host, True)287 @skipUnless(False, 'it fails!')288 def test_create_session_in_race(self):289 """290 Tests writing an in-qualifying session into the db291 """292 api = FakeApi('autostew_back/tests/test_assets/session_in_quali_two_players_14ai.json')293 test_setup = self.make_test_setup()294 test_setup.save(True)295 SetupRotationEntry.objects.create(296 order=0,297 server=self.server,298 setup=test_setup299 )300 with mock.patch.object(requests, 'get', api.fake_request):301 self.server.back_start(settings)302 self.assertEqual(Server.objects.count(), 1)303 self.assertEqual(SessionSetup.objects.count(), 3)304 self.assertEqual(Session.objects.count(), 2)305 self.assertEqual(Member.objects.count(), 4)306 self.assertEqual(Participant.objects.count(), 32)307 server_in_db = Server.objects.all()[0]308 self.assertTrue(server_in_db.running)309 session_setup = SessionSetup.objects.filter(is_template=False)[0]310 self.assertTrue(session_setup.server_controls_setup)311 self.assertTrue(session_setup.server_controls_track)312 self.assertFalse(session_setup.server_controls_vehicle)313 self.assertTrue(session_setup.server_controls_vehicle_class)314 self.assertEqual(session_setup.grid_size, 22)315 self.assertEqual(session_setup.max_players, 8)316 self.assertEqual(session_setup.opponent_difficulty, 100)317 self.assertEqual(session_setup.force_identical_vehicles, False)318 self.assertEqual(session_setup.allow_custom_vehicle_setup, True)319 self.assertEqual(session_setup.force_realistic_driving_aids, True)320 self.assertEqual(session_setup.force_manual, False)321 self.assertEqual(session_setup.rolling_starts, False)322 self.assertEqual(session_setup.force_same_vehicle_class, True)323 self.assertEqual(session_setup.fill_session_with_ai, True)324 self.assertEqual(session_setup.mechanical_failures, False)325 self.assertEqual(session_setup.auto_start_engine, True)326 self.assertEqual(session_setup.timed_race, False)327 self.assertEqual(session_setup.ghost_griefers, True)328 self.assertEqual(session_setup.enforced_pitstop, True)329 self.assertEqual(session_setup.practice1_length, 0)330 self.assertEqual(session_setup.practice2_length, 0)331 self.assertEqual(session_setup.qualify_length, 10)332 self.assertEqual(session_setup.warmup_length, 0)333 self.assertEqual(session_setup.race1_length, 5)334 self.assertEqual(session_setup.race2_length, 0)335 self.assertEqual(session_setup.privacy.ingame_id, 0)336 self.assertEqual(session_setup.damage.name, "FULL")337 self.assertEqual(session_setup.tire_wear.name, "X2")338 self.assertEqual(session_setup.fuel_usage.name, "STANDARD")339 self.assertEqual(session_setup.penalties.name, "FULL")340 self.assertEqual(session_setup.allowed_views.name, "Any")341 self.assertEqual(session_setup.track.name, "Zolder")342 self.assertEqual(session_setup.vehicle_class.name, "GT3")343 self.assertEqual(session_setup.date_progression, 5)344 self.assertEqual(session_setup.weather_slots, 1)345 self.assertEqual(session_setup.weather_1.name, "Clear")346 session = Session.objects.all()[0]347 session_snapshot = Session.objects.exclude(parent=None)[0]348 self.assertEqual(session.server, server_in_db)349 self.assertEqual(session.setup_actual, session_setup)350 self.assertEqual(session.running, True)351 self.assertEqual(session.finished, False)352 self.assertEqual(session.max_member_count, 22)353 self.assertEqual(session_snapshot.parent, session)354 self.assertEqual(session_snapshot.session_state.name, "Race")355 self.assertEqual(session_snapshot.session_stage.name, "Qualifying")356 self.assertEqual(session_snapshot.session_phase.name, "Green")357 self.assertEqual(session_snapshot.session_time_elapsed, 185)358 self.assertEqual(session_snapshot.session_time_duration, 600)359 self.assertEqual(session_snapshot.num_participants_valid, 16)360 self.assertEqual(session_snapshot.num_participants_disq, 0)361 self.assertEqual(session_snapshot.num_participants_retired, 0)362 self.assertEqual(session_snapshot.num_participants_dnf, 0)363 self.assertEqual(session_snapshot.num_participants_finished, 0)364 self.assertEqual(session_snapshot.current_year, 2015)365 self.assertEqual(session_snapshot.current_month, 7)366 self.assertEqual(session_snapshot.current_day, 6)367 self.assertEqual(session_snapshot.current_hour, 13)368 self.assertEqual(session_snapshot.current_minute, 14)369 self.assertEqual(session_snapshot.rain_density_visual, 0)370 self.assertEqual(session_snapshot.wetness_path, 0)371 self.assertEqual(session_snapshot.wetness_off_path, 0)372 self.assertEqual(session_snapshot.wetness_avg, 0)373 self.assertEqual(session_snapshot.wetness_predicted_max, 0)374 self.assertEqual(session_snapshot.wetness_max_level, 0)375 self.assertEqual(session_snapshot.temperature_ambient, 27890)376 self.assertEqual(session_snapshot.temperature_track, 39835)377 self.assertEqual(session_snapshot.air_pressure, 100760)378 member = Member.objects.all()[0]379 self.assertEqual(member.session, session)380 self.assertEqual(member.still_connected, True)381 self.assertEqual(member.vehicle.name, "McLaren 12C GT3")382 self.assertEqual(member.livery.name, "McLaren #59")383 self.assertEqual(member.refid, 26752)384 self.assertEqual(member.steam_id, '76561197969382874')385 self.assertEqual(member.name, 'blak')386 self.assertEqual(member.setup_used, True)387 self.assertEqual(member.controller_gamepad, False)388 self.assertEqual(member.controller_wheel, True)389 self.assertEqual(member.aid_steering, False)390 self.assertEqual(member.aid_braking, False)391 self.assertEqual(member.aid_abs, True)392 self.assertEqual(member.aid_traction, True)393 self.assertEqual(member.aid_stability, True)394 self.assertEqual(member.aid_no_damage, False)395 self.assertEqual(member.aid_auto_gears, False)396 self.assertEqual(member.aid_auto_clutch, True)397 self.assertEqual(member.model_normal, False)398 self.assertEqual(member.model_experienced, True)399 self.assertEqual(member.model_pro, True)400 self.assertEqual(member.model_elite, False)401 self.assertEqual(member.aid_driving_line, False)402 self.assertEqual(member.valid, True)403 m_snap = Member.objects.exclude(parent=None)[0]404 self.assertEqual(m_snap.parent, member)405 self.assertEqual(m_snap.session, session_snapshot)406 self.assertEqual(m_snap.still_connected, True)407 self.assertEqual(m_snap.ingame_load_state.name, MemberLoadState.admin_started_race)408 self.assertEqual(m_snap.ping, 14)409 self.assertEqual(m_snap.ingame_index, 0)410 self.assertEqual(m_snap.ingame_state.name, "Connected")...
forms.py
Source:forms.py
1import logging2from django.forms import ModelForm3from autostew_web_session.models.session import SessionSetup4class SessionSetupForm(ModelForm):5 def save(self, commit=True):6 session_setup = super(SessionSetupForm, self).save(commit=False)7 session_setup.is_template = True8 session_setup.server_controls_setup = True9 session_setup.server_controls_track = True10 session_setup.server_controls_vehicle_class = True11 session_setup.server_controls_vehicle = True12 if commit:13 session_setup.save()14 return session_setup15 class Meta:16 exclude = ['is_template', 'server_controls_setup', 'server_controls_track', 'server_controls_vehicle', 'server_controls_vehicle_class']...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!