Skip to content

Commit fac8253

Browse files
feat: update pagination test
1 parent 3915a41 commit fac8253

6 files changed

Lines changed: 53 additions & 202 deletions

File tree

api-batch-test/src/database.py

Lines changed: 0 additions & 198 deletions
This file was deleted.

api-batch-test/tests/schemas/page_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"previous": {"type": ["string", "null"]},
88
"results": {"type": "array"},
99
},
10-
"additionalProperties": True,
10+
"additionalProperties": False,
1111
}
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
PEOPLE_ITEM_SCHEMA = {
22
"type": "object",
3-
"required": ["name", "url"],
3+
"required": ["name", "height", "mass", "hair_color", "skin_color", "eye_color", "birth_year", "gender", "homeworld", "films", "species", "vehicles", "starships", "created", "edited", "url"],
44
"properties": {
55
"name": {"type": "string", "minLength": 1},
6+
"height": {"type": "string"},
7+
"mass": {"type": "string"},
8+
"hair_color": {"type": "string"},
9+
"skin_color": {"type": "string"},
10+
"eye_color": {"type": "string"},
11+
"birth_year": {"type": "string"},
12+
"gender": {"type": "string"},
13+
"homeworld": {"type": "string", "pattern": "^https?://"},
14+
"films": {
15+
"type": "array",
16+
"items": {"type": "string", "pattern": "^https?://"}
17+
},
18+
"species": {
19+
"type": "array",
20+
"items": {"type": "string", "pattern": "^https?://"}
21+
},
22+
"vehicles": {
23+
"type": "array",
24+
"items": {"type": "string", "pattern": "^https?://"}
25+
},
26+
"starships": {
27+
"type": "array",
28+
"items": {"type": "string", "pattern": "^https?://"}
29+
},
30+
"created": {"type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}T"},
31+
"edited": {"type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}T"},
632
"url": {"type": "string", "pattern": "^https?://"},
733
},
8-
"additionalProperties": True,
34+
"additionalProperties": False,
935
}

api-batch-test/tests/test_people_contract.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ def test_people_page_schema(http):
1212
data = r.json()
1313
validate(instance=data, schema=PAGE_SCHEMA)
1414
assert isinstance(data["results"], list)
15+
assert data["count"] == 82
1516
for item in data["results"]:
1617
validate(instance=item, schema=PEOPLE_ITEM_SCHEMA)
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
from tests.helpers.pagination import iter_pages
22
from src.api_endpoints import Endpoints
33

4+
from jsonschema import validate
5+
from tests.schemas.page_schema import PAGE_SCHEMA
6+
from tests.schemas.people_schema import PEOPLE_ITEM_SCHEMA
7+
48
import pytest
59

10+
11+
@pytest.mark.contract
612
@pytest.mark.pagination
713
def test_people_pagination_integrity(http):
814
total_count = None
@@ -11,22 +17,38 @@ def test_people_pagination_integrity(http):
1117
page_index = 0
1218

1319
for page in iter_pages(http, Endpoints.PEOPLE):
20+
# Validate the current page schema
21+
validate(instance=page, schema=PAGE_SCHEMA)
1422
if total_count is None:
1523
total_count = page["count"]
24+
# The first page should not have a previous page reference
1625
assert page["previous"] is None
1726
results = page.get("results", [])
27+
# The results must be a list
1828
assert isinstance(results, list)
1929
for item in results:
30+
# Validate the item/person schema
31+
validate(instance=item, schema=PEOPLE_ITEM_SCHEMA)
2032
uid = item.get("url")
33+
# Each item must have a valid URL starting with http/https
2134
assert uid and uid.startswith("http"), "Item sem URL válida"
35+
# Itens URLs must not repeat across pages
2236
assert uid not in seen_urls, f"Duplicado: {uid}"
2337
seen_urls.add(uid)
2438
total_items += len(results)
2539
page_index += 1
2640
if page.get("next"):
2741
r = http.get(page["next"])
42+
# Following the 'next' link must return 200 OK
2843
assert r.status_code == 200
2944

45+
# The field 'count' must be a non-negative integer
3046
assert isinstance(total_count, int) and total_count >= 0
47+
# The total count must be exactly 82 people
48+
assert total_count == 82
49+
# The sum of collected items must equal the 'count' value
3150
assert total_items == total_count, f"soma({total_items}) != count({total_count})"
32-
assert page_index >= 1
51+
# Must have seen at least one item if count > 0
52+
assert page_index >= 1
53+
# On the last page of pagination, 'next' must be null
54+
assert page.get("next") is None

setup.cfg

Whitespace-only changes.

0 commit comments

Comments
 (0)