def test_context_local_context(self, err_mock): self.assertIsNone(request.context) err_mock.assert_called_with( "Accessing request local object outside of the request-response cycle." ) req = create_request("TELEKOM_Demo_Intent", session={ "key-1": "value-1", "key-2": "value-2" }) with RequestContextVar(request=req): self.assertEqual(request.context.intent, "TELEKOM_Demo_Intent") self.assertEqual( { "id": "123", "attributes": { "key-1": "value-1", "key-2": "value-2" }, "new": True, }, request.session.dict(), ) with RequestContextVar(request=req.with_translation(Translations())): self.assertEqual("HELLO", request.context._("HELLO")) self.assertEqual("HELLO", request.context._n("HELLO", "HOLA", 1)) self.assertEqual(["HELLO"], request.context._a("HELLO"))
async def invoke(handler: AnyFunc, request: Request) -> Response: """ Invoke intent handler: awaits the call if handler is a coroutine runs in executor if handler is a simple `def` @param handler: @param request: @return: """ with RequestContextVar(request=request): logger.debug( "Calling intent %s with handler: %s", repr(request.context.intent), repr(handler), ) if inspect.iscoroutinefunction(handler): response = await handler(request) else: loop = asyncio.get_running_loop() response = await loop.run_in_executor( ContextVarExecutor(), handler, request, ) if isinstance(response, str): response = Response(text=response) logger.debug("Intent call result: %s", repr(response)) return response
def one(_): with RequestContextVar(request=_): del request.session["key-1"] del request.session["key-2"] assert request.session.dict() == { "id": "123", "attributes": {}, "new": True, }
def run(): now = self.now.astimezone( datetime.timezone(datetime.timedelta(hours=1))) with RequestContextVar(request=req): self.assertEqual( request.context.today().timestamp(), now.replace(hour=0, minute=0, tzinfo=None).timestamp(), ) self.assertEqual(request.context.now().timestamp(), self.now.timestamp())
def test_context_local_now(self): req = create_request("TELEKOM_Demo_Intent", timezone=["Europe/Athens"]) next_day = datetime.datetime(year=2100, month=12, day=20, hour=0, minute=0) with RequestContextVar(request=req): self.assertEqual(request.context.today().timestamp(), next_day.timestamp()) self.assertEqual(request.context.now().timestamp(), self.now.timestamp())
def two(_): with RequestContextVar(request=_): request.session["key-1"] = "value-12" request.session["key-2"] = "value-22" assert request.session.dict() == { "id": "123", "attributes": { "key-1": "value-12", "key-2": "value-22" }, "new": True, }
def init(_): with RequestContextVar(request=_): with pytest.raises(TypeError): request.context.locale = "de" # noqa: attempt to modify "read-only" context (to raise TypeError) assert request.session.dict() == { "id": "123", "attributes": { "key-1": "value-1", "key-2": "value-2" }, "new": True, }
def test_make_lazy_translation(self): from skill_sdk.intents import RequestContextVar tr = Translations("de.mo") request = util.create_request("Test__Intent") tr._catalog["KEY"] = "VALUE" tr._catalog[("KEY", 1)] = "VALUES" tr._catalog["KEY_PLURAL"] = "VALUES" self.assertEqual(_("KEY"), "KEY") self.assertEqual(_n("KEY", "KEY_PLURAL", 1), "KEY") self.assertEqual(_n("KEY", "KEY_PLURAL", 2), "KEY_PLURAL") self.assertEqual(_a("KEY"), ["KEY"]) with RequestContextVar(request=request.with_translation(tr)): self.assertEqual(_("KEY"), "VALUE") self.assertEqual(_n("KEY", "KEY_PLURAL", 2), "VALUES") self.assertEqual(_a("KEY"), ["KEY"])