Best Python code snippet using lemoncheesecake
runner.py
Source:runner.py
...107 for arg_name in test.get_arguments():108 if arg_name in test.parameters:109 args[arg_name] = test.parameters[arg_name]110 else:111 args[arg_name] = scheduled_fixtures.get_fixture_result(arg_name)112 return args113 def run(self, context):114 ###115 # Checker whether the test must be executed or not116 ###117 if self._is_test_disabled(context):118 self._handle_disabled_test(context)119 return120 ###121 # Begin test122 ###123 context.session.start_test(self.test)124 ###125 # Setup test (setup and fixtures)...
fixture.py
Source:fixture.py
...24 global _scheduled_fixtures25 assert _scheduled_fixtures, "Fixture cache has not yet been initialized"26 if not _scheduled_fixtures.has_fixture(name):27 raise LookupError("Fixture '%s' either does not exist or doesn't have a pre_run scope" % name)28 return _scheduled_fixtures.get_fixture_result(name)29class _FixtureInfo:30 def __init__(self, names, scope, per_thread):31 self.names = names32 self.scope = scope33 self.per_thread = per_thread34def fixture(names=None, scope="test", per_thread=False):35 """36 Decorator, declare a function as a fixture.37 :param names: an optional list of names that can be used to access the fixture value,38 if no names are provided the decorated function name will be used39 :param scope: the fixture scope, available scopes are: ``test``, ``suite``, ``session``, ``pre_run``;40 default is ``test``41 :param per_thread: whether or not the fixture must be executed on a per-thread basis42 Please note that when ``per_thread`` is set to ``True``:43 - the scope can only be ``session`` or ``suite``44 - the fixture can only be used in tests or by fixtures with the ``test`` scope45 """46 def wrapper(func):47 if scope not in _SCOPE_LEVELS.keys():48 raise ValueError("Invalid fixture scope '%s' in fixture function '%s'" % (scope, func.__name__))49 if per_thread and scope not in ("session", "suite"):50 raise AssertionError("The fixture can only be per_thread=True if scope is 'session' or 'suite'")51 setattr(func, "_lccfixtureinfo", _FixtureInfo(names or [func.__name__], scope, per_thread))52 return func53 return wrapper54class _BaseFixtureResult(object):55 def get(self):56 raise NotImplementedError()57 def teardown(self):58 pass59class _FixtureResult(_BaseFixtureResult):60 def __init__(self, value):61 self.value = value62 def get(self):63 return self.value64class _GeneratorFixtureResult(_BaseFixtureResult):65 def __init__(self, name, generator):66 self.name = name67 self.generator = generator68 self.value = next(generator)69 def get(self):70 return self.value71 def teardown(self):72 try:73 next(self.generator)74 except StopIteration:75 pass76 else:77 raise AssertionError(78 "Fixture '%s' yields more than once: only one yield is supported." % self.name79 )80class _PerThreadFixtureResult(_BaseFixtureResult, ThreadedFactory):81 def __init__(self, name, func, params):82 ThreadedFactory.__init__(self)83 self.name = name84 self.func = func85 self.params = params86 def get(self):87 return self.get_object().get()88 def teardown(self):89 self.teardown_factory()90 def setup_object(self):91 return _build_fixture_result_from_func(self.name, self.func, self.params)92 def teardown_object(self, result):93 result.teardown()94def _build_fixture_result_from_func(name, func, params, per_thread=False):95 if per_thread:96 return _PerThreadFixtureResult(name, func, params)97 else:98 value = func(**params)99 if inspect.isgenerator(value):100 return _GeneratorFixtureResult(name, value)101 else:102 return _FixtureResult(value)103class _BaseFixture(object):104 def __init__(self, name, scope, params, per_thread):105 self.name = name106 self.scope = scope107 self.params = params108 self.per_thread = per_thread109 @property110 def scope_level(self):111 return _SCOPE_LEVELS[self.scope]112 def execute(self, params):113 # type: (dict) -> _BaseFixtureResult114 raise NotImplementedError()115class Fixture(_BaseFixture):116 def __init__(self, name, func, scope, params, per_thread):117 _BaseFixture.__init__(self, name, scope, params, per_thread)118 self.func = func119 def execute(self, params):120 for param_name in params.keys():121 assert param_name in self.params122 return _build_fixture_result_from_func(self.name, self.func, params, self.per_thread)123class BuiltinFixture(_BaseFixture):124 def __init__(self, name, value):125 _BaseFixture.__init__(self, name, scope="pre_run", params={}, per_thread=False)126 self.result = _FixtureResult(value)127 def execute(self, _):128 return self.result129class ScheduledFixtures(object):130 def __init__(self, scope, fixtures=(), parent_scheduled_fixtures=None):131 self.scope = scope132 self._fixtures = OrderedDict()133 self._parent_scheduled_fixtures = parent_scheduled_fixtures134 self._results = {}135 for fixture in fixtures:136 self.add_fixture(fixture)137 def add_fixture(self, fixture):138 assert fixture.scope == self.scope139 self._fixtures[fixture.name] = fixture140 def get_fixture_names(self):141 return self._fixtures.keys()142 def has_fixture(self, name):143 return name in self._fixtures144 def is_empty(self):145 return len(self._fixtures) == 0146 def _get_fixture_params(self, name):147 return {148 param_name: name if param_name == "fixture_name" else self.get_fixture_result(param_name)149 for param_name in self._fixtures[name].params150 }151 def _setup_fixture(self, name):152 assert name not in self._results, "Cannot setup fixture '%s', it has already been executed" % name153 self._results[name] = self._fixtures[name].execute(self._get_fixture_params(name))154 def _teardown_fixture(self, name):155 assert name in self._results, "Cannot teardown fixture '%s', it has not been previously executed" % name156 self._results[name].teardown()157 del self._results[name]158 def get_setup_teardown_pairs(self):159 return list(map(160 lambda name: (lambda: self._setup_fixture(name), lambda: self._teardown_fixture(name)),161 self._fixtures162 ))163 def get_fixture_result(self, name):164 if name in self._fixtures:165 assert name in self._results, "Cannot get fixture '%s' result, it has not been previously executed" % name166 return self._results[name].get()167 elif self._parent_scheduled_fixtures:168 return self._parent_scheduled_fixtures.get_fixture_result(name)169 else:170 raise LookupError("Cannot find fixture named '%s' in scheduled fixtures" % name)171 def get_fixture_results(self, names):172 return {name: self.get_fixture_result(name) for name in names}173class FixtureRegistry:174 def __init__(self):175 self._fixtures = {}176 def add_fixture(self, fixture):177 if fixture.name in self._fixtures and isinstance(self._fixtures[fixture.name], BuiltinFixture):178 raise FixtureConstraintViolation("'%s' is a builtin fixture name" % fixture.name)179 self._fixtures[fixture.name] = fixture180 def add_fixtures(self, fixtures):181 for fixture in fixtures:182 self.add_fixture(fixture)183 def get_fixture(self, name):184 return self._fixtures[name]185 def get_fixture_dependencies(self, name, ref_fixtures=()):186 fixture_params = [p for p in self._fixtures[name].params if p != "fixture_name"]...
update_db.py
Source:update_db.py
...252 return Fixture.objects.get(home_team=home_team,253 away_team=away_team,254 competition=competition255 )256def get_fixture_result(goals_home_team, goals_away_team):257 """258 Returns fixture result based on home/away goals259 :param goals_home_team: int260 :param goals_away_team: int261 :return: if home win: 1, else if draw: 0, else if away win: 2262 """263 if goals_home_team > goals_away_team:264 return 1265 elif goals_home_team == goals_away_team:266 return 0267 elif goals_home_team < goals_away_team:268 return 2269def cash_user(bet):270 """271 Transfers money to AppUser account if user had won his bet272 :param bet: Bet object273 """274 app_user = bet.bet_user275 # if bet result == 1(winning bet) transfer money to user account276 # this condition is checked just in case...277 if bet.bet_result == 1:278 cash_win = bet.bet_amount * Decimal(bet.bet_course)279 app_user.cash += cash_win280 #app_user.cash = round(app_user.cash, 2)281 app_user.save()282def check_bets(fixture):283 """284 Checks all bets related to given fixture285 :param fixture: Fixture object286 """287 bets = Bet.objects.filter(fixture=fixture)288 for bet in bets:289 # if bet(possibilities: 1, 0, 2) is290 # the same as fixture result(possibilities: 1, 0, 2),291 # set bet result to 1(winning bet) and run function that transfer cash292 if bet.bet == fixture.fixture_result:293 bet.bet_result = 1294 bet.save()295 cash_user(bet)296 else:297 bet.bet_result = 0298 bet.save()299def update_fixture(fixture, goals_away_team, goals_home_team):300 """301 Updates given fixture with away/home goals and runs function that302 checks all bets related to fixture303 :param fixture: Fixture object304 :param goals_away_team: int305 :param goals_home_team: int306 :return: None307 """308 # if fixture status == SCHEDULED or PLAYING update fixture, save it and run309 # function that checks related bets310 if fixture.status == 1 or fixture.status == 3:311 'and not fixture.goals_away_team and not fixture.goals_home_team)'312 fixture.goals_home_team = goals_home_team313 fixture.goals_away_team = goals_away_team314 fixture.status = 2 # fixture PLAYED315 # Get fixture bet result 1, 2 or 0316 fixture.fixture_result = get_fixture_result(goals_home_team,317 goals_away_team318 )319 fixture.save()320 check_bets(fixture)321def update_fixtures(api_id, matchday=""):322 """323 Updates fixtures with data from api server. 324 Fixtures are updated according to matchday.325 :param api_id: int - id of competition in api server326 :param matchday: int - number of matchday327 :return: None328 """329 # gets data from api serer330 data = get_fixtures(api_id, matchday)...
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!!