Example #1
0
def test_duckling_api_success() -> None:
    """
    Initialize DucklingParser and `.get_entities`.

    This test-case bypasses duckling API calls via httpretty. The expected response
    was originally generated using:

    ```shell

    export month_q="I need four of those on 27th next month."
    export time_q="Can I have it at 5 am""&dims="[date"]"
    export text="$month_q $time_q"
    curl -XPOST http://0.0.0.0:8000/parse --data 'locale=en_US&text=$text'
    ```
    """
    body = "I need four of those on 27th next month. Can I have it at 5 am"

    expected_response = [
        config.mock_number_entity,
        config.mock_date_entity,
        config.mock_time_entity,
    ]

    request_callback = request_builder(expected_response)
    httpretty.register_uri(httpretty.POST,
                           "http://0.0.0.0:8000/parse",
                           body=request_callback)

    parser = DucklingParser(locale="en_IN")

    response = parser.get_entities(body)
    assert response == expected_response
Example #2
0
def test_duckling_api_failure() -> None:
    """
    Simulate Duckling returning 500.
    """
    body = "27th next month"
    expected_response = [config.mock_date_entity]

    request_callback = request_builder(expected_response, response_code=500)

    httpretty.register_uri(httpretty.POST,
                           "http://0.0.0.0:8000/parse",
                           body=request_callback)

    parser = DucklingParser(dimensions=["time"], locale="en_IN")

    with pytest.raises(ValueError):
        parser.get_entities(body)
Example #3
0
def test_duckling_wrong_tz() -> None:
    """
    In case the timezone is incorrect or exceptions need to be handled.
    """
    body = "i need it at 5 am"

    request_callback = request_builder({})
    httpretty.register_uri(httpretty.POST,
                           "http://0.0.0.0:8000/parse",
                           body=request_callback)

    parser = DucklingParser(locale="en_IN", timezone="Earth/Someplace")

    with pytest.raises(pytz.UnknownTimeZoneError):
        _ = parser.get_entities(body)
Example #4
0
def test_duckling_with_tz() -> None:
    """
    Using DucklingParser with timezone.
    """
    body = "i need it at 5 am"
    expected_response = [config.mock_time_entity]

    request_callback = request_builder(expected_response)
    httpretty.register_uri(httpretty.POST,
                           "http://0.0.0.0:8000/parse",
                           body=request_callback)

    parser = DucklingParser(locale="en_IN", timezone="Asia/Kolkata")

    response = parser.get_entities(body)
    assert response == expected_response