Skip to content

Commit

Permalink
Cyberstorm API: include deactived dependencies for package listing
Browse files Browse the repository at this point in the history
Even if a dependency is deactived, it's still a dependency of the
package, which might not work without it. To avoid misleading
information on the website, include deactived dependencies too.

PackageVersion is considered deactived if the parent Package is
deactived, even if the specific version would be active.

Annotating the "effective active status" to the QuerySet was
considered, but since the related package is prefetched for other
purposes anyway, it seemed like an unnecessary step.

Refs TS-2013
  • Loading branch information
anttimaki committed Jan 3, 2024
1 parent e961162 commit ceccff5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 33 additions & 1 deletion django/thunderstore/api/cyberstorm/tests/test_package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import pytest
from rest_framework.test import APIClient

from thunderstore.api.cyberstorm.views.package_listing import get_custom_package_listing
from thunderstore.api.cyberstorm.views.package_listing import (
DependencySerializer,
get_custom_package_listing,
)
from thunderstore.community.factories import (
CommunityFactory,
PackageCategoryFactory,
Expand Down Expand Up @@ -280,5 +283,34 @@ def test_package_listing_view__serializes_url_correctly(api_client: APIClient) -
assert actual["website_url"] is None


@pytest.mark.django_db
@pytest.mark.parametrize(
("package_is_active", "version_is_active"),
(
(False, False),
(True, False),
(False, True),
(True, True),
),
)
def test_dependency_serializer__reads_is_active_from_correct_field(
package_is_active: bool,
version_is_active: bool,
) -> None:
dependant = PackageVersionFactory()
dependency = PackageVersionFactory(is_active=version_is_active)
dependency.package.is_active = package_is_active
dependency.package.save()
dependant.dependencies.set([dependency])

# community_identifier is normally added using annotations, but
# it's irrelavant for this test case.
dependency.community_identifier = "greendale"

actual = DependencySerializer(dependency).data

assert actual["is_active"] == (package_is_active and version_is_active)


def _date_to_z(value: datetime) -> str:
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
4 changes: 2 additions & 2 deletions django/thunderstore/api/cyberstorm/views/package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DependencySerializer(serializers.Serializer):
community_identifier = serializers.CharField()
description = serializers.CharField()
icon_url = serializers.CharField(source="icon.url")
is_active = serializers.BooleanField(source="is_effectively_active")
name = serializers.CharField()
namespace = serializers.CharField(source="package.namespace.name")
version_number = serializers.CharField()
Expand Down Expand Up @@ -165,8 +166,7 @@ def get_custom_package_listing(
)

dependencies = (
listing.package.latest.dependencies.active()
.listed_in(community_id)
listing.package.latest.dependencies.listed_in(community_id)
.annotate(
community_identifier=Value(community_id, CharField()),
)
Expand Down

0 comments on commit ceccff5

Please sign in to comment.