-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest_api_result.py
60 lines (49 loc) · 1.96 KB
/
test_api_result.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import pytest
from django.test.client import Client
from .api_config import API_ENDPOINTS
@pytest.mark.django_db
@pytest.mark.parametrize(
"endpoint,wp_endpoint,expected_result,expected_code,expected_queries", API_ENDPOINTS
)
def test_api_result(
load_test_data,
django_assert_num_queries,
endpoint,
wp_endpoint,
expected_result,
expected_code,
expected_queries,
):
"""
This test class checks all endpoints defined in :attr:`~tests.api.api_config.API_ENDPOINTS`.
It verifies that the content delivered by the endpoint is equivalent with the data
provided in the corresponding json file.
:param load_test_data: The fixture providing the test data (see :meth:`~tests.conftest.load_test_data`)
:type load_test_data: tuple
:param django_assert_num_queries: The fixture providing the query assertion
:type django_assert_num_queries: functools.partialmethod
:param endpoint: The url of the new Django pattern
:type endpoint: str
:param wp_endpoint: The legacy url of the wordpress endpoint pattern
:type wp_endpoint: str
:param expected_result: The path to the json file that contains the expected result
:type expected_result: str
:param expected_code: The expected HTTP status code
:type expected_code: int
:param expected_queries: The expected number of SQL queries
:type expected_queries: int
"""
client = Client()
with django_assert_num_queries(expected_queries):
response = client.get(endpoint, format="json")
print(response.headers)
assert response.status_code == expected_code
with django_assert_num_queries(expected_queries):
response_wp = client.get(wp_endpoint, format="json")
print(response_wp.headers)
assert response_wp.status_code == expected_code
with open(expected_result, encoding="utf-8") as f:
result = json.load(f)
assert result == response.json()
assert result == response_wp.json()