|
1 | 1 | import pytest |
2 | 2 | from django.urls import reverse |
3 | 3 |
|
| 4 | + |
| 5 | + |
4 | 6 | pytestmark = pytest.mark.django_db |
5 | 7 |
|
6 | 8 |
|
@@ -36,7 +38,10 @@ def test_missing_field_not_included(author_bio_factory, author_factory, client): |
36 | 38 | # First author does not have a bio |
37 | 39 | author = author_factory(bio=None) |
38 | 40 | response = client.get(reverse("author-detail", args=[author.pk]) + "?include=bio") |
39 | | - assert "included" not in response.json() |
| 41 | + ### |
| 42 | + content = response.json() |
| 43 | + assert "included" in content |
| 44 | + assert content["included"] == [] |
40 | 45 | # Second author does |
41 | 46 | author = author_factory() |
42 | 47 | response = client.get(reverse("author-detail", args=[author.pk]) + "?include=bio") |
@@ -204,3 +209,24 @@ def test_meta_object_added_to_included_resources(single_entry, client): |
204 | 209 | ) |
205 | 210 | assert response.json()["included"][0].get("meta") |
206 | 211 | assert response.json()["included"][1].get("meta") |
| 212 | + |
| 213 | + |
| 214 | +def test_included_empty_array_when_requested(client, author_factory): |
| 215 | + author = author_factory(bio=None) # explicitly ensure no related bio |
| 216 | + url = reverse("author-detail", args=[author.pk]) + "?include=bio" |
| 217 | + response = client.get(url) |
| 218 | + assert response.status_code == 200 |
| 219 | + |
| 220 | + content = response.json() |
| 221 | + assert "included" in content |
| 222 | + assert content["included"] == [] |
| 223 | + |
| 224 | + |
| 225 | +def test_included_absent_when_not_requested(client, author_factory): |
| 226 | + # Create an author (bio can be None or default, doesn't matter here) |
| 227 | + author = author_factory(bio=None) |
| 228 | + url = reverse("author-detail", args=[author.pk]) |
| 229 | + response = client.get(url) |
| 230 | + assert response.status_code == 200 |
| 231 | + content = response.json() |
| 232 | + assert "included" not in content |
0 commit comments