Skip to content

Commit

Permalink
Add is_unavailable to DependencySerializer
Browse files Browse the repository at this point in the history
Add the is_unavailable status to the DependencySerializer. Use
SerializerMethodField and implement a function to fetch the
is_unavailable value from the PackageVersion model.

Implement tests for the endpoint using the serializer. The tests
should verify that the field is present and correctly reflects the
result of the is_unavailable function.

Refs. TS-2276
  • Loading branch information
Roffenlund committed Jan 14, 2025
1 parent 4d807f5 commit 0b9ea35
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
39 changes: 39 additions & 0 deletions django/thunderstore/api/cyberstorm/tests/test_package_listing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
from typing import Optional
from unittest.mock import Mock, patch

import pytest
from rest_framework.test import APIClient
Expand All @@ -9,11 +10,13 @@
get_custom_package_listing,
)
from thunderstore.community.factories import (
Community,
CommunityFactory,
PackageCategoryFactory,
PackageListingFactory,
)
from thunderstore.repository.factories import (
NamespaceFactory,
PackageRatingFactory,
PackageVersionFactory,
TeamMemberFactory,
Expand Down Expand Up @@ -334,3 +337,39 @@ def test_dependency_serializer__when_dependency_is_not_active__censors_icon_and_

def _date_to_z(value: datetime) -> str:
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")


@pytest.mark.django_db
@pytest.mark.parametrize("return_val", [True, False])
@patch("thunderstore.repository.models.package_version.PackageVersion.is_unavailable")
def test_package_listing_is_unavailable(
is_unavailable_func: Mock,
return_val: bool,
api_client: APIClient,
community: Community,
) -> None:
is_unavailable_func.return_value = return_val

package = "Mod"
target_ns = NamespaceFactory()

target_dependency = PackageListingFactory(
community_=community,
package_kwargs={"name": package, "namespace": target_ns},
)

target_package = PackageListingFactory(community_=community)
target_package.package.latest.dependencies.set(
[target_dependency.package.latest.id],
)

community_id = target_package.community.identifier
namespace = target_package.package.namespace.name
package_name = target_package.package.name

url = f"/api/cyberstorm/listing/{community_id}/{namespace}/{package_name}/"
response = api_client.get(url)
response_dependencies = response.json()["dependencies"][0]

assert "is_unavailable" in response_dependencies
assert response_dependencies["is_unavailable"] == return_val
9 changes: 9 additions & 0 deletions django/thunderstore/api/cyberstorm/views/package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from thunderstore.community.models.package_listing import PackageListing
from thunderstore.repository.models.package import get_package_dependants
from thunderstore.repository.models.package_version import PackageVersion
from thunderstore.community.models.community import Community


class DependencySerializer(serializers.Serializer):
Expand All @@ -44,6 +45,7 @@ class DependencySerializer(serializers.Serializer):
name = serializers.CharField()
namespace = serializers.CharField(source="package.namespace.name")
version_number = serializers.CharField()
is_unavailable = serializers.SerializerMethodField()

def get_description(self, obj: PackageVersion) -> str:
return (
Expand All @@ -55,6 +57,13 @@ def get_description(self, obj: PackageVersion) -> str:
def get_icon_url(self, obj: PackageVersion) -> Optional[str]:
return obj.icon.url if obj.is_effectively_active else None

def get_is_unavailable(self, obj: PackageVersion) -> bool:
community = Community.objects.filter(
identifier=obj.community_identifier
).first()

return obj.is_unavailable(community)


class TeamSerializer(serializers.Serializer):
"""
Expand Down

0 comments on commit 0b9ea35

Please sign in to comment.