def _select_origin(context: Optional[Context] = None) -> datetime: if context is None: return now() origin = context.get(CONTEXT_KEY_STARTS) if not origin: return now() if isinstance(origin, datetime): return origin if isinstance(origin, str): return isoparse(origin) raise ValueError(f"Invalid datetime value: {origin}")
def test_now_pdt(mocker): localtime = NonCallableMock(spec=struct_time, tm_zone="PDT", tm_gmtoff=-28800) mocker.patch(f"{PKG}.localtime", return_value=localtime) current = now() assert current.tzname() == "PDT" assert current.utcoffset().total_seconds() == -28800
def execute( self, request: Request, session: Optional[requests.Session] = None, context: Optional[Context] = None, ) -> Tuple[ExecutionReport, Optional[Response]]: """ Executes a request. Args: request: A request. session: A session object to execute. context: Execution context. Returns: A tuple of execution report and response. When there is no response, the response will be ``None``. """ if session is None: with requests.Session() as new_session: return self.execute(request, session=new_session) context = context if context is not None else Context() starts = now() report = ExecutionReport(starts=starts) try: with closed_context(context, starts=starts) as context: prepped = self._prepare_request(request, context) proxies = session.rebuild_proxies(prepped, proxies=None) except Exception as error: message = to_message(error) report = replace(report, status=Status.FAILURE, message=message) return report, None report = replace( report, request=PreparedRequest( method=prepped.method or "", url=prepped.url or "", headers=prepped.headers, body=prepped.body, ), ) try: res = session.send(prepped, proxies=proxies, timeout=self._timeout) except Exception as error: message = to_message(error) report = replace(report, status=Status.UNSTABLE, message=message) return report, None report = replace(report, status=Status.SUCCESS) response = ResponseWrapper(id=_generate_id(), res=res) return report, response
def submit(self, scenario: Scenario) -> ScenarioTask: starts = now() context = Context( **{ CONTEXT_KEY_STARTS: starts, CONTEXT_KEY_BASE_URL: self._case_runner.base_url, }) context_analyzer = MappingAnalyzer(context) conditions = Verification.collect( condition.verify(context_analyzer, context) for condition in scenario.conditions) if not conditions.status.is_succeeded: status = Status.SKIPPED if conditions.status is Status.FAILURE: status = Status.FAILURE result = ScenarioResult(label=scenario.label, status=status, conditions=conditions) return StaticScenarioTask(result) if scenario.ordered: cases: CasesTask = OrderedCasesTask( self._executor, self._case_runner, scenario.cases, context=context, ) else: cases = UnorderedCasesTask(self._executor, self._case_runner, scenario.cases) subscenarios = [ self.submit(subscenario) for subscenario in scenario.subscenarios ] return RunningScenarioTask( label=scenario.label, conditions=conditions, cases=cases, subscenarios=subscenarios, )
class CaseRunner: def __init__(self, unit_runner: UnitRunner, listener: Optional[CaseListener] = None): self._unit_runner = unit_runner self._listener = listener or CaseListener() @property def base_url(self) -> str: return self._unit_runner.base_url def run( self, case: Case, session: Optional[requests.Session] = None, context: Optional[Context] = None, ) -> CaseResult: if not case.enabled: return CaseResult(label=case.label) context = context if context is not None else Context() with closed_context(context, starts=now(), base_url=self.base_url) as context: context_analyzer = MappingAnalyzer(context) conditions = Verification.collect( condition.verify(context_analyzer, context) for condition in case.conditions) if not conditions.status.is_succeeded: return CaseResult(case.label, conditions) execution, response, verification = self._unit_runner.run( request=case.request, requirements=case.response, session=session, context=context, ) self._listener.on_execution(execution, response) return CaseResult(case.label, conditions, execution, verification)