|
| 1 | +# Copyright 2022, Google LLC |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +"""Test webhook to enable an agent response.""" |
| 15 | + |
| 16 | +import flask |
| 17 | +import pytest |
| 18 | + |
| 19 | +from webhook_configure_session_parameters_enable_agent_response import ( |
| 20 | + enable_agent_response, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(name="app", scope="module") |
| 25 | +def fixture_app(): |
| 26 | + """Flask fixture to pass a flask.Request to the test function.""" |
| 27 | + return flask.Flask(__name__) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "tag,value,expected_value", |
| 32 | + [ |
| 33 | + ("increase number", 100, 200), |
| 34 | + ("decrease number", 100, 50), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_enable_agent_response(tag, value, expected_value, app): |
| 38 | + """Test for webhook to enable an agent response.""" |
| 39 | + |
| 40 | + request = { |
| 41 | + "fulfillmentInfo": {"tag": tag}, |
| 42 | + "sessionInfo": {"parameters": {"number": value}}, |
| 43 | + } |
| 44 | + |
| 45 | + if tag == "increase number": |
| 46 | + expected_text = ( |
| 47 | + f"The new increased value of the number parameter is {expected_value}" |
| 48 | + ) |
| 49 | + else: |
| 50 | + expected_text = ( |
| 51 | + f"The new decreased value of the number parameter is {expected_value}" |
| 52 | + ) |
| 53 | + |
| 54 | + with app.test_request_context(json=request): |
| 55 | + res = enable_agent_response(flask.request) |
| 56 | + assert ( |
| 57 | + res["fulfillment_response"]["messages"][0]["text"]["text"][0] |
| 58 | + == expected_text |
| 59 | + ) |
| 60 | + assert res["sessionInfo"]["parameters"]["number"] == expected_value |
0 commit comments