-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.py
41 lines (37 loc) · 2.6 KB
/
e2e_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pytest
import requests
# End to end tests for the QA system
# This fixture can be used if I need to perform setup tasks, not needed directly here but useful in larger contexts.
@pytest.fixture
def query_url(): # this is a fixture that returns the URL of the query endpoint.
return "http://localhost:8000/query"
# Using the fixture to provide the URL, making it easy to modify in one place if needed.
def test_valid_question(query_url):
# Sending the POST request to the specified URL with JSON body.
response = requests.post(query_url, json={"text": "How many days of vacation can I have per year?"})
# Asserting that the HTTP response status code is 200.
assert response.status_code == 200, "Expected status code 200, but got {}".format(response.status_code)
# Parsing JSON response once to avoid multiple calls to response.json()
response_data = response.json()
# Asserting the structure and data types of the response
assert "response" in response_data, "Expected 'response' key in the JSON response"
assert isinstance(response_data["response"], str), "Expected 'response' to be a string"
assert '5' in response_data["response"] or 'five' in response_data["response"], "Expected '5' or 'five' in the response"
assert "sources" in response_data, "Expected 'sources' key in the JSON response"
assert isinstance(response_data["sources"], list), "Expected 'sources' to be a list"
assert len(response_data["sources"]) > 0, "Expected at least one source in the response"
filtered_sources = list(filter(lambda x: x["file"] == 'GPT - leave policy.pdf' and x["page"] == "1", response_data["sources"]))
assert len(filtered_sources) != 0, "Expected GPT - leave policy.pdf in sources"
# now what I test here is invalid as wrong input query
def test_invalid_question(query_url):
# Sending the POST request to the specified URL with JSON body.
response = requests.post(query_url, json={"apple": "This is not a valid question."})
# Asserting that the HTTP response status code is 400.
assert response.status_code == 422, "Expected status code 422, but got {}".format(response.status_code)
# now I test invalid as wrong input query
def test_empty_question(query_url):
# Sending the POST request to the specified URL with JSON body.
response = requests.post(query_url, json={"text": ""})
# Asserting that the HTTP response status code is 400.
assert response.status_code == 400, "Expected status code 400, but got {}".format(response.status_code)
# The tests can be run using a pytest command in the terminal. Make sure the server is running before running the tests.