From 10b4e0c0bbce657c1346e2ed1142e1a68df17acf Mon Sep 17 00:00:00 2001 From: Dan Jacob Date: Wed, 20 Dec 2023 10:34:00 +0200 Subject: [PATCH] Resources: add default_min_price/default_max_price check to free filter queries If the default min or max price is not NULL and > 0 then we should include this resource in the filtering logic. Refs: TTVA-186 --- resources/models/resource.py | 4 +- resources/tests/test_resource.py | 72 ++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/resources/models/resource.py b/resources/models/resource.py index c2834e84e..906efc2fa 100644 --- a/resources/models/resource.py +++ b/resources/models/resource.py @@ -224,7 +224,9 @@ def free_of_charge(self, is_free): queryset = self.annotate( has_pricing=models.Exists( self.filter( - Q( + Q(default_min_price__gt=0) + | Q(default_max_price__gt=0) + | Q( products__pricedproduct__price_list__usergroup_prices__price__gt=0, ) | Q( diff --git a/resources/tests/test_resource.py b/resources/tests/test_resource.py index 97831af59..4d7c4d508 100644 --- a/resources/tests/test_resource.py +++ b/resources/tests/test_resource.py @@ -69,6 +69,78 @@ def test_free_of_charge_free_to_use_true_no_pricing_info(space_resource): assert Resource.objects.free_of_charge(False).count() == 0 +@pytest.mark.django_db +def test_free_of_charge_free_to_use_false_has_default_min_price(space_resource): + space_resource.free_to_use = False + space_resource.default_min_price = Decimal("10.00") + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 0 + assert Resource.objects.free_of_charge(False).count() == 1 + + +@pytest.mark.django_db +def test_free_of_charge_free_to_use_false_has_default_max_price(space_resource): + space_resource.free_to_use = False + space_resource.default_min_price = Decimal("0") + space_resource.default_max_price = Decimal("10.00") + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 0 + assert Resource.objects.free_of_charge(False).count() == 1 + + +@pytest.mark.django_db +def test_free_of_charge_free_to_use_false_has_default_min_max_price(space_resource): + space_resource.free_to_use = False + space_resource.default_min_price = Decimal("10.00") + space_resource.default_max_price = Decimal("20.00") + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 0 + assert Resource.objects.free_of_charge(False).count() == 1 + + +@pytest.mark.django_db +def test_free_of_charge_free_to_use_false_has_default_min_price_zero(space_resource): + space_resource.free_to_use = False + space_resource.default_min_price = Decimal("0") + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 1 + assert Resource.objects.free_of_charge(False).count() == 0 + + +@pytest.mark.django_db +def test_free_of_charge_free_to_use_false_has_default_min_price_null(space_resource): + space_resource.free_to_use = False + space_resource.default_min_price = None + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 1 + assert Resource.objects.free_of_charge(False).count() == 0 + + +@pytest.mark.django_db +def test_free_of_charge_free_to_use_true_has_default_min_price_gt_zero(space_resource): + space_resource.free_to_use = True + space_resource.default_min_price = Decimal("10.00") + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 1 + assert Resource.objects.free_of_charge(False).count() == 0 + + +@pytest.mark.django_db +def test_free_of_charge_free_to_use_true_has_default_max_price_gt_zero(space_resource): + space_resource.free_to_use = True + space_resource.default_max_price = Decimal("10.00") + space_resource.save() + + assert Resource.objects.free_of_charge(True).count() == 1 + assert Resource.objects.free_of_charge(False).count() == 0 + + @pytest.mark.django_db def test_free_of_charge_free_to_use_false_no_pricing_info(space_resource): """Even if free_to_use is False, is still free of charge as there is