def test_immutability_of_sample_request_and_params(): """Test the immutability of the sample_request and params objects""" with pytest.raises(FrozenInstanceError): params = FrozenParams() params.allowed_intents = [] with pytest.raises(TypeError): params = FrozenParams() params.dynamic_resource["a"] = "b" with pytest.raises(FrozenInstanceError): request = Request() request.params = Params() with pytest.raises(TypeError): request = Request() request.frame["a"] = "b"
def test_custom_action_merge(): "Test `merge=True` for custom actions" action_config = {"url": "http://localhost:8080/v2/action"} action = CustomAction(name="action_call_people", config=action_config) with patch("requests.post") as mock_object: mock_object.return_value = Mock() mock_object.return_value.status_code = 200 mock_object.return_value.json.return_value = { "directives": ["directive3", "directive4"], "frame": {"k2": "v2"}, "slots": {"s2": "v2"}, "params": { "allowed_intents": ["intent3", "intent4"], "dynamic_resource": {"r2": "v2"}, "language": "some-language", "locale": "some-locale", "time_zone": "some-time-zone", "target_dialogue_state": "some-state", "timestamp": "some-timestamp", }, } request = Request( text="sing a song", domain="some domain", intent="some intent" ) responder = DialogueResponder() responder.directives = ["directive1", "directive2"] responder.frame = {"k1": "v1"} responder.slots = {"s1": "v1"} responder.params.allowed_intents = ("intent1", "intent2") responder.params.dynamic_resource = {"r1": "v1"} assert action.invoke(request, responder) assert responder.directives == [ "directive1", "directive2", "directive3", "directive4", ] assert responder.frame == {"k1": "v1", "k2": "v2"} assert responder.slots == {"s1": "v1", "s2": "v2"} assert responder.params.allowed_intents == ( "intent1", "intent2", "intent3", "intent4", ) assert responder.params.dynamic_resource == {"r1": "v1", "r2": "v2"} assert responder.params.target_dialogue_state == "some-state" assert responder.params.language == "some-language" assert responder.params.locale == "some-locale" assert responder.params.time_zone == "some-time-zone" assert responder.params.timestamp == "some-timestamp"
def sample_request(): dict_list = [{"key": "value"}, {"key": "value"}, {"key": "value"}] return Request( domain="some_domain", intent="some_intent", entities=(dict_list), text="some_text", history=(dict_list), nbest_transcripts_text=(dict_list), nbest_transcripts_entities=(dict_list), nbest_aligned_entities=(dict_list), )
def test_auto_fill_invoke(kwik_e_mart_app): """Tests slot-filling invoke functionality""" app = kwik_e_mart_app request = Request( text="elm street", domain="store_info", intent="get_store_number", entities=[{ "type": "store_name", "value": [{ "cname": "23 Elm Street" }], "role": None }], ) responder = DialogueResponder() # custom eval func def test_custom_eval(r): # entity already passed in, this is to check custom eval flow. del r return True # mock handler for invoke handler_sub = MagicMock() handler_sub.__name__ = "handler_sub" form = { "entities": [ FormEntity( entity="store_name", value="23 Elm Street", default_eval=False, custom_eval=test_custom_eval, ) ], } @app.handle(domain="store_info", intent="get_store_number") def handler_main(request, responder): AutoEntityFilling(handler_sub, form, app).invoke(request, responder) handler_main(request, responder) # check whether the sub handler was invoked. handler_sub.assert_called_once_with(request, responder) # check whether new rule has been added for sub handler. assert any([ rule.dialogue_state == handler_sub.__name__ for rule in list(app.app_manager.dialogue_manager.rules) ])
def sample_request(): dict_list = [{"key": "value"}, {"key": "value"}, {"key": "value"}] dict_list_of_lists = [[{"key": "value"}, {"key": "value"}, {"key": "value"}], [{"key": "value"}, {"key": "value"}, {"key": "value"}]] lists = ['key_1', 'key_2', 'key_3'] return Request( domain="some_domain", intent="some_intent", entities=dict_list, text="some_text", history=dict_list, nbest_transcripts_text=lists, nbest_transcripts_entities=dict_list_of_lists, nbest_aligned_entities=dict_list_of_lists, )
def test_custom_action(): """Test CustomAction.invoke to ensure that the parameters of the JSON body are correct""" action_config = {"url": "http://localhost:8080/v2/action"} action = CustomAction(name="action_call_people", config=action_config) with patch("requests.post") as mock_object: mock_object.return_value = Mock() mock_object.return_value.status_code = 200 mock_object.return_value.json.return_value = {} request = Request( text="sing a song", domain="some domain", intent="some intent" ) responder = DialogueResponder() assert action.invoke(request, responder) assert mock_object.call_args[1]["url"] == action_config["url"] assert "request" in mock_object.call_args[1]["json"] assert "responder" in mock_object.call_args[1]["json"] assert mock_object.call_args[1]["json"]["action"] == "action_call_people"
async def test_custom_action_async(): """Test CustomAction.invoke_async to ensure that the parameters of the JSON body are correct""" action_config = {"url": "http://localhost:8080/v2/action"} action = CustomAction(name="action_call_people", config=action_config) with patch("mindmeld.components.CustomAction.post_async") as mock_object: async def mock_coroutine(): return 200, {} mock_object.return_value = mock_coroutine() request = Request(text="sing a song", domain="some domain", intent="some intent") responder = DialogueResponder() assert await action.invoke_async(request, responder) call_args = mock_object.call_args_list[0][0][0] assert "request" in call_args assert "responder" in call_args assert call_args["action"] == "action_call_people"
def test_auto_fill_custom_validation_resolution(kwik_e_mart_app): """Tests slot-filling's custom validation with custom resolution""" app = kwik_e_mart_app request = Request( text="what is the sum of 5 and 15?", domain="some domain", intent="some intent", ) responder = DialogueResponder() # custom eval func @app.register_func() def test_custom_eval(r): return 5 + 15 form = { "entities": [ FormEntity( entity="sys_number", default_eval=False, custom_eval="test_custom_eval", ) ], } def handler_sub(request, responder): entity = next((e for e in request.entities if e["type"] == "sys_number"), None) # Check custom resolution validity assert entity assert entity["value"][0]["value"] == 20 @app.handle(domain="some domain", intent="some intent") def handler(request, responder): AutoEntityFilling(handler_sub, form, app).invoke(request, responder) handler(request, responder)
def create_request(domain, intent, entities=None): """Creates a request object for use by the dialogue manager""" entities = entities or () return Request(domain=domain, intent=intent, entities=entities, text="")
def request(): return Request(domain="some_domain", intent="some_intent", entities=(), text="some_text")
def request(): return Request(domain='some_domain', intent='some_intent', entities=(), text='some_text')