Exemple #1
0
def test_execute_error():
    ds = ErrorDialogService(timezone=timezone("Asia/Tokyo"))
    performance = PerformanceInfo()
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST", channel_user_id="test_user", text="hello")
    response = ds.execute(request, context, None, performance)
    assert response.messages[0].text == "?"
Exemple #2
0
def test_execute_default():
    ds = MyDialog()
    performance = PerformanceInfo()
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST", channel_user_id="test_user", text="hello")
    response = ds.execute(request, context, None, performance)
    assert response.messages == []
    assert request.entities == {}
    assert context.data == {}
Exemple #3
0
def test_init():
    msg1 = Message(text="message 1")
    msg2 = Message(text="message 2")
    headers = {"key1": "value1", "key2": 1}
    performance = PerformanceInfo()
    response = Response(messages=[msg1, msg2],
                        headers=headers,
                        performance=performance)
    assert response.messages == [msg1, msg2]
    assert response.headers == headers
    assert isinstance(response.performance, PerformanceInfo)
Exemple #4
0
def test_from_dict():
    # setup response dict
    msg1 = Message(text="message 1")
    msg2 = Message(text="message 2")
    headers = {"key1": "value1", "key2": 1}
    performance = PerformanceInfo()
    response_dict = Response(messages=[msg1, msg2],
                             headers=headers,
                             performance=performance).to_dict()
    # restore
    response = Response.from_dict(response_dict)
    assert response.messages[0].text == msg1.text
    assert response.messages[1].text == msg2.text
    assert response.headers == headers
    assert isinstance(response.performance, PerformanceInfo)
Exemple #5
0
def test_execute():
    ds = PizzaDialogService(timezone=timezone("Asia/Tokyo"))
    performance = PerformanceInfo()

    # first contact
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST", channel_user_id="test_user", text="Give me pizza")
    response = ds.execute(request, context, None, performance)
    assert response.messages[0].text == "Which pizza?"
    assert request.entities == {"pizza_name": ""}
    assert context.data == {
        "pizza_name": "",
        "pizza_count": 0
    }
    # say pizza name
    context.is_new = False
    context.topic.is_new = False
    request = Message(channel="TEST", channel_user_id="test_user", text="seafood pizza")
    response = ds.execute(request, context, None, performance)
    assert response.messages[0].text == "Your order is Seafood Pizza?"

    # confirmation
    request = Message(channel="TEST", channel_user_id="test_user", text="yes")
    response = ds.execute(request, context, None, performance)
    assert response.messages[0].text == "Thank you!"
    assert response.messages[1].text == "We will deliver Seafood Pizza in 30min."

    # raise error
    request = Message(channel="TEST", channel_user_id="test_user", text="error")
    response = ds.execute(request, context, None, performance)
    assert response.messages[0].text == "?"
    assert context.error["exception"] == "division by zero"

    # no response messages
    request = Message(channel="TEST", channel_user_id="test_user", text="no message")
    response = ds.execute(request, context, None, performance)
    assert response.messages == []
Exemple #6
0
def test_execute():
    dr = MyDialogRouter(timezone=timezone("Asia/Tokyo"),
                        default_dialog_service=EchoDialogService)
    performance = PerformanceInfo()

    # default
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="Hello")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, dr.default_dialog_service)

    # pizza
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="give me pizza")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)

    # continue pizza
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="seafood")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)

    # soba lower priority (continume pizza)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="lower")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)

    # soba higher priority (update to soba)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="give me soba")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, SobaDialogService)

    # pizza highest (update pizza)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="highest p")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)
    assert context.topic.priority == Priority.Highest - 1
    # soba with high priority (continue pizza)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="give me soba")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)
    assert context.topic.priority == Priority.Highest - 1
    # soba with highest priority (update soba)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="highest s")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, SobaDialogService)

    # adhoc
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="pizza")
    # start pizza
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="adhoc")
    # adhoc
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, AdhocDialogService)
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="seafood")
    # continue pizza
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, PizzaDialogService)

    # no intent
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST", channel_user_id="test_user", text="_")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, dr.default_dialog_service)

    # unknown
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="unknown")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, dr.default_dialog_service)

    # dialog for intent not registered
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="not_registered")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, DialogService)

    # error
    context = Context("TEST", "test_user")
    context.topic.is_new = True
    request = Message(channel="TEST",
                      channel_user_id="test_user",
                      text="error")
    ds = dr.execute(request, context, None, performance)
    assert isinstance(ds, ErrorDialogService)
Exemple #7
0
def test_init():
    performance = PerformanceInfo()
    assert isinstance(performance.start_time, float)
    assert performance.ticks == []
    assert performance.milliseconds == 0
Exemple #8
0
def test_append():
    performance = PerformanceInfo()
    sleep(2)
    performance.append("operation")
    assert performance.ticks[0][1] > 1
    assert performance.milliseconds > 0