Skip to content

Commit

Permalink
Migrated included tests to pytest style
Browse files Browse the repository at this point in the history
  • Loading branch information
sliverc committed Mar 20, 2024
1 parent 9da7b9f commit ccc1fd6
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 83 deletions.
65 changes: 0 additions & 65 deletions example/tests/integration/test_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,6 @@
pytestmark = pytest.mark.django_db


def test_included_data_on_list(multiple_entries, client):
response = client.get(
reverse("entry-list"), data={"include": "comments", "page[size]": 5}
)
included = response.json().get("included")

assert len(response.json()["data"]) == len(
multiple_entries
), "Incorrect entry count"
assert [x.get("type") for x in included] == [
"comments",
"comments",
], "List included types are incorrect"

comment_count = len(
[resource for resource in included if resource["type"] == "comments"]
)
expected_comment_count = sum(entry.comments.count() for entry in multiple_entries)
assert comment_count == expected_comment_count, "List comment count is incorrect"


def test_included_data_on_list_with_one_to_one_relations(multiple_entries, client):
response = client.get(
reverse("entry-list"), data={"include": "authors.bio.metadata", "page[size]": 5}
)
included = response.json().get("included")

assert len(response.json()["data"]) == len(
multiple_entries
), "Incorrect entry count"
expected_include_types = [
"authorBioMetadata",
"authorBioMetadata",
"authorBios",
"authorBios",
"authors",
"authors",
]
include_types = [x.get("type") for x in included]
assert include_types == expected_include_types, "List included types are incorrect"


def test_default_included_data_on_detail(single_entry, client):
return test_included_data_on_detail(
single_entry=single_entry, client=client, query=""
)


def test_included_data_on_detail(single_entry, client, query="?include=comments"):
response = client.get(
reverse("entry-detail", kwargs={"pk": single_entry.pk}) + query
)
included = response.json().get("included")

assert [x.get("type") for x in included] == [
"comments"
], "Detail included types are incorrect"

comment_count = len(
[resource for resource in included if resource["type"] == "comments"]
)
expected_comment_count = single_entry.comments.count()
assert comment_count == expected_comment_count, "Detail comment count is incorrect"


def test_dynamic_related_data_is_included(single_entry, entry_factory, client):
entry_factory()
response = client.get(
Expand Down
35 changes: 35 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

from tests.models import (
BasicModel,
ForeignKeySource,
ForeignKeyTarget,
ManyToManySource,
ManyToManyTarget,
NestedRelatedSource,
)


Expand Down Expand Up @@ -39,6 +41,11 @@ def foreign_key_target(db):
return ForeignKeyTarget.objects.create(name="Target")


@pytest.fixture
def foreign_key_source(db, foreign_key_target):
return ForeignKeySource.objects.create(name="Source", target=foreign_key_target)


@pytest.fixture
def many_to_many_source(db, many_to_many_targets):
source = ManyToManySource.objects.create(name="Source")
Expand All @@ -54,6 +61,34 @@ def many_to_many_targets(db):
]


@pytest.fixture
def many_to_many_sources(db, many_to_many_targets):
source1 = ManyToManySource.objects.create(name="Source1")
source2 = ManyToManySource.objects.create(name="Source2")

source1.targets.add(*many_to_many_targets)
source2.targets.add(*many_to_many_targets)

return [source1, source2]


@pytest.fixture
def nested_related_source(
db,
foreign_key_source,
foreign_key_target,
many_to_many_targets,
many_to_many_sources,
):
source = NestedRelatedSource.objects.create(
fk_source=foreign_key_source, fk_target=foreign_key_target
)
source.m2m_targets.add(*many_to_many_targets)
source.m2m_sources.add(*many_to_many_sources)

return source


@pytest.fixture
def client():
return APIClient()
6 changes: 3 additions & 3 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class ForeignKeySource(DJAModel):


class NestedRelatedSource(DJAModel):
m2m_source = models.ManyToManyField(ManyToManySource, related_name="nested_source")
m2m_sources = models.ManyToManyField(ManyToManySource, related_name="nested_source")
fk_source = models.ForeignKey(
ForeignKeySource, related_name="nested_source", on_delete=models.CASCADE
)
m2m_target = models.ManyToManyField(ManyToManySource, related_name="nested_target")
m2m_targets = models.ManyToManyField(ManyToManyTarget, related_name="nested_target")
fk_target = models.ForeignKey(
ForeignKeySource, related_name="nested_target", on_delete=models.CASCADE
ForeignKeyTarget, related_name="nested_target", on_delete=models.CASCADE
)
36 changes: 27 additions & 9 deletions tests/serializers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from rest_framework_json_api import serializers
from rest_framework_json_api.relations import ResourceRelatedField
from tests.models import (
BasicModel,
ForeignKeySource,
ForeignKeyTarget,
ManyToManySource,
ManyToManyTarget,
NestedRelatedSource,
)


Expand All @@ -15,33 +15,51 @@ class Meta:
model = BasicModel


class ForeignKeyTargetSerializer(serializers.ModelSerializer):
class Meta:
fields = ("name",)
model = ForeignKeyTarget


class ForeignKeySourceSerializer(serializers.ModelSerializer):
target = ResourceRelatedField(queryset=ForeignKeyTarget.objects)
included_serializers = {"target": ForeignKeyTargetSerializer}

class Meta:
model = ForeignKeySource
fields = ("target",)


class ManyToManyTargetSerializer(serializers.ModelSerializer):
class Meta:
fields = ("name",)
model = ManyToManyTarget


class ManyToManySourceSerializer(serializers.ModelSerializer):
targets = ResourceRelatedField(many=True, queryset=ManyToManyTarget.objects)
included_serializers = {"targets": "tests.serializers.ManyToManyTargetSerializer"}

class Meta:
model = ManyToManySource
fields = ("targets",)


class ManyToManyTargetSerializer(serializers.ModelSerializer):
class ManyToManySourceReadOnlySerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyTarget
model = ManyToManySource
fields = ("targets",)


class ManyToManySourceReadOnlySerializer(serializers.ModelSerializer):
targets = ResourceRelatedField(many=True, read_only=True)
class NestedRelatedSourceSerializer(serializers.ModelSerializer):
included_serializers = {
"m2m_sources": ManyToManySourceSerializer,
"fk_source": ForeignKeySourceSerializer,
"m2m_targets": ManyToManyTargetSerializer,
"fk_target": ForeignKeyTargetSerializer,
}

class Meta:
model = ManyToManySource
fields = ("targets",)
model = NestedRelatedSource
fields = ("m2m_sources", "fk_source", "m2m_targets", "fk_target")


class CallableDefaultSerializer(serializers.Serializer):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class Meta:
{"many": True, "queryset": ManyToManyTarget.objects.all()},
),
(
"m2m_target.sources.",
"m2m_target.sources",
"ManyToManySource",
{"many": True, "queryset": ManyToManySource.objects.all()},
),
Expand Down
Loading

0 comments on commit ccc1fd6

Please sign in to comment.