Skip to content

Commit

Permalink
Implement new tests and update existing tests
Browse files Browse the repository at this point in the history
Implement two new tests to ensure that filter_with_single_community
works as inteded with packages that belong to single and multiple
communities.

Implement changes to existing tests as the filter_with_single_community
changes the aggregated results in these tests.

Refs. TS-2265
  • Loading branch information
Roffenlund committed Jan 16, 2025
1 parent cf1e9ed commit 5b60927
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,25 @@ def test_community_aggregated_fields__update_for_community__calculates_packages(
assert community1.aggregated.package_count == 5
assert community2.aggregated.package_count == 0

# The following listings are shared between the two communities and will not affect
# the package count of community2 and for community1.
for _ in range(10):
pl = PackageListingFactory(community_=community1)
PackageListingFactory(community_=community2, package=pl.package)

CommunityAggregatedFields.update_for_community(community1)
CommunityAggregatedFields.update_for_community(community2)

assert community1.aggregated.package_count == 5
assert community2.aggregated.package_count == 0

for _ in range(10):
PackageListingFactory(community_=community1)
PackageListingFactory(community_=community2)

CommunityAggregatedFields.update_for_community(community1)
CommunityAggregatedFields.update_for_community(community2)

assert community1.aggregated.package_count == 15
assert community2.aggregated.package_count == 10

Expand All @@ -100,7 +112,7 @@ def test_community_aggregated_fields__update_for_community__calculates_downloads

PackageListingFactory(
community_=community2,
package_version_kwargs={"downloads": 0},
package_version_kwargs={"downloads": 1},
)
PackageListingFactory(
community_=community3,
Expand All @@ -112,22 +124,46 @@ def test_community_aggregated_fields__update_for_community__calculates_downloads
CommunityAggregatedFields.update_for_community(community3)

assert community1.aggregated.download_count == 0
assert community2.aggregated.download_count == 0
assert community2.aggregated.download_count == 1
assert community3.aggregated.download_count == 1

# This listing will not affect the download count of community2 since it is shared
# with community3.
listing = PackageListingFactory(
community_=community2,
package_version_kwargs={"downloads": 2},
)

# This listing will not affect the download count of community3 since it is shared
# with community2.
PackageListingFactory(community_=community3, package=listing.package)

CommunityAggregatedFields.update_for_community(community1)
CommunityAggregatedFields.update_for_community(community2)
CommunityAggregatedFields.update_for_community(community3)

assert community1.aggregated.download_count == 0
assert community2.aggregated.download_count == 2
assert community3.aggregated.download_count == 3
assert community2.aggregated.download_count == 1
assert community3.aggregated.download_count == 1

for _ in range(10):
PackageListingFactory(
community_=community1, package_version_kwargs={"downloads": 1}
)
PackageListingFactory(
community_=community2, package_version_kwargs={"downloads": 2}
)
PackageListingFactory(
community_=community3, package_version_kwargs={"downloads": 3}
)

CommunityAggregatedFields.update_for_community(community1)
CommunityAggregatedFields.update_for_community(community2)
CommunityAggregatedFields.update_for_community(community3)

assert community1.aggregated.download_count == 10
assert community2.aggregated.download_count == 21
assert community3.aggregated.download_count == 31


@pytest.mark.django_db
Expand Down
47 changes: 47 additions & 0 deletions django/thunderstore/community/tests/test_package_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
CommunityFactory,
CommunitySiteFactory,
PackageListingFactory,
PackageVersionFactory,
)
from thunderstore.community.models import (
Community,
CommunityAggregatedFields,
CommunityMemberRole,
CommunityMembership,
PackageCategory,
Expand Down Expand Up @@ -369,3 +371,48 @@ def test_package_listing_queryset_filter_by_community_approval_rule(
assert count == 0
else:
assert count == 1


@pytest.mark.django_db
def test_package_listing_filter_with_single_community_packages() -> None:
communities = [
CommunityFactory(aggregated_fields=CommunityAggregatedFields.objects.create())
for _ in range(3)
]

[PackageListingFactory(community_=community) for community in communities]

for community in communities:
CommunityAggregatedFields.update_for_community(community)
count = community.package_listings.filter_with_single_community().count()
assert count == 1
assert community.aggregated.package_count == count


@pytest.mark.django_db
def test_package_listing_filter_with_multiple_community_packages() -> None:
communities = [
CommunityFactory(aggregated_fields=CommunityAggregatedFields.objects.create())
for _ in range(3)
]

# Setup a shared package listing for another community.
shared_package = PackageVersionFactory().package
extra_community = CommunityFactory(
aggregated_fields=CommunityAggregatedFields.objects.create()
)
extra_listing = PackageListingFactory(
community_=extra_community, package_=shared_package
)

# Add two package listings for each community, one with the shared package and one
# with a unique package.
for community in communities:
PackageListingFactory(community_=community, package_=extra_listing.package)
PackageListingFactory(community_=community)
CommunityAggregatedFields.update_for_community(community)

for community in communities:
count = community.package_listings.filter_with_single_community().count()
assert count == 1
assert community.aggregated.package_count == count
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,29 @@ def test_counts_when_no_packages_exists(api_client: APIClient) -> None:
@pytest.mark.django_db
def test_counts_when_packages_have_been_downloaded(api_client: APIClient) -> None:
site = CommunitySite.objects.get()

ver1 = PackageVersionFactory(downloads=0)
ver2 = PackageVersionFactory(downloads=3)
ver3 = PackageVersionFactory(downloads=5)

ver3.package.is_deprecated = True # This should still count.

PackageListing.objects.create(community=site.community, package=ver1.package)
PackageListing.objects.create(community=site.community, package=ver2.package)
PackageListing.objects.create(community=site.community, package=ver3.package)

CommunityAggregatedFields.create_missing()
site.community.refresh_from_db()
CommunityAggregatedFields.update_for_community(site.community)

data = __query_api(api_client)

assert len(data["communities"]) == 1

assert data["communities"][0]["download_count"] == 8
assert data["communities"][0]["package_count"] == 3

# Total counts should be the same given the same packages for both communities.
assert data["download_count"] == 8
assert data["package_count"] == 3

Expand Down

0 comments on commit 5b60927

Please sign in to comment.