def test_fallback_level_reset_on_non_fallback_intent(self, agent): session_ctx_before = Context('_session_context', parameters={'fallback_level': 2}) resp = agent.test_request('Test', contexts=[session_ctx_before]) session_ctx = resp.context('_session_context') fallback_level = session_ctx.parameters['fallback_level'] assert fallback_level == 0
def test_fallback_level_increased_on_repeated_fallback_intent(self, agent): session_ctx_before = Context('_session_context', parameters={'fallback_level': 2}) resp = agent.test_request('Test', contexts=[session_ctx_before], is_fallback=True) session_ctx = resp.context('_session_context') fallback_level = session_ctx.parameters['fallback_level'] assert fallback_level == 3
def test_keep_around_lifespan_is_reset_before_handler(self, agent): agent.register_context('foo', keep_around=True) incoming_ctx = Context('foo', lifespan_count=1) @agent.handle('CtxTest') def handler(conv): assert conv.contexts.foo.lifespan_count == CTX_KEEP_AROUND_LIFESPAN return conv agent.test_request('CtxTest', contexts=[incoming_ctx])
def test_register_class_with_custom_serializer(self, agent): serializer = lambda foo: asdict(foo) deserializer = lambda params: Foo(**params) @agent.context('foo', serializer=serializer, deserializer=deserializer) @dataclass class Foo: bar: int @agent.handle('CtxTest') def handler(conv): assert isinstance(conv.contexts.foo.parameters, Foo) assert conv.contexts.foo.parameters.bar == 1000 conv.contexts.foo.parameters.bar = 42 return conv incoming_ctx = Context('foo', parameters={'bar': 1000}) resp = agent.test_request('CtxTest', contexts=[incoming_ctx]) assert resp.has_context('foo') assert resp.context('foo').parameters == {'bar': 42}
def test_register_JSONType_context_class(self, agent): class FooSchema(JSONTypeSchema): bar = Int() @agent.context('foo') @dataclass class Foo(JSONType, schema=FooSchema): bar: int @agent.handle('CtxTest') def handler(conv): assert isinstance(conv.contexts.foo.parameters, Foo) assert conv.contexts.foo.parameters.bar == 1000 conv.contexts.foo.parameters.bar = 42 return conv incoming_ctx = Context('foo', parameters={'bar': 1000}) resp = agent.test_request('CtxTest', contexts=[incoming_ctx]) assert resp.has_context('foo') assert resp.context('foo').parameters == {'bar': 42}
def test_context(self, agent): ctx = Context('foo') resp = agent.test_request('Test', contexts=[ctx]) assert resp.context('foo').to_json() == ctx.to_json()
def test_has_context(self, agent): resp = agent.test_request('Test', contexts=[Context('foo')]) assert resp.has_context('foo')
def test_register_ctx_keep_around_resets_lifespan(self, agent): agent.register_context('foo', keep_around=True) resp = agent.test_request('Test', contexts=[Context('foo', 1)]) assert resp.has_context('foo') assert resp.context('foo').lifespan_count == CTX_KEEP_AROUND_LIFESPAN