Skip to content

Commit

Permalink
feat(proofs): new type SHOP_IMPORT to explicitly link shop-imported p…
Browse files Browse the repository at this point in the history
…rices (#485)
  • Loading branch information
raphodn authored Oct 2, 2024
1 parent 1088f65 commit 82da2b9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
7 changes: 6 additions & 1 deletion open_prices/proofs/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
TYPE_PRICE_TAG = "PRICE_TAG"
TYPE_RECEIPT = "RECEIPT"
TYPE_GDPR_REQUEST = "GDPR_REQUEST"
TYPE_SHOP_IMPORT = "SHOP_IMPORT"

TYPE_LIST = [TYPE_PRICE_TAG, TYPE_RECEIPT, TYPE_GDPR_REQUEST]
TYPE_LIST = [TYPE_PRICE_TAG, TYPE_RECEIPT, TYPE_GDPR_REQUEST, TYPE_SHOP_IMPORT]
TYPE_CHOICES = [(key, key) for key in TYPE_LIST]

# 1 proof = 1 shop + 1 date
TYPE_SINGLE_SHOP_LIST = [TYPE_PRICE_TAG, TYPE_RECEIPT, TYPE_SHOP_IMPORT]
TYPE_MULTIPLE_SHOP_LIST = [TYPE_GDPR_REQUEST]
25 changes: 25 additions & 0 deletions open_prices/proofs/migrations/0003_alter_proof_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.1 on 2024-10-01 21:49

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("proofs", "0002_alter_proof_currency"),
]

operations = [
migrations.AlterField(
model_name="proof",
name="type",
field=models.CharField(
choices=[
("PRICE_TAG", "PRICE_TAG"),
("RECEIPT", "RECEIPT"),
("GDPR_REQUEST", "GDPR_REQUEST"),
("SHOP_IMPORT", "SHOP_IMPORT"),
],
max_length=20,
),
),
]
7 changes: 7 additions & 0 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class ProofQuerySet(models.QuerySet):
def has_type_single_shop(self):
return self.filter(type__in=proof_constants.TYPE_SINGLE_SHOP_LIST)

def has_prices(self):
return self.filter(price_count__gt=0)

Expand Down Expand Up @@ -127,6 +130,10 @@ def save(self, *args, **kwargs):
self.set_location()
super().save(*args, **kwargs)

@property
def is_type_single_shop(self):
return self.type in proof_constants.TYPE_SINGLE_SHOP_LIST

def update_price_count(self):
self.price_count = self.prices.count()
self.save(update_fields=["price_count"])
Expand Down
17 changes: 14 additions & 3 deletions open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from open_prices.locations import constants as location_constants
from open_prices.locations.factories import LocationFactory
from open_prices.prices.factories import PriceFactory
from open_prices.proofs import constants as proof_constants
from open_prices.proofs.factories import ProofFactory
from open_prices.proofs.models import Proof

Expand Down Expand Up @@ -63,11 +64,16 @@ def test_proof_location_validation(self):
class ProofQuerySetTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.proof_without_price = ProofFactory()
cls.proof_with_price = ProofFactory()
cls.proof_without_price = ProofFactory(type=proof_constants.TYPE_PRICE_TAG)
cls.proof_with_price = ProofFactory(type=proof_constants.TYPE_GDPR_REQUEST)
PriceFactory(proof_id=cls.proof_with_price.id, price=1.0)

def test_has_type_single_shop(self):
self.assertEqual(Proof.objects.count(), 2)
self.assertEqual(Proof.objects.has_type_single_shop().count(), 1)

def test_has_prices(self):
self.assertEqual(Proof.objects.count(), 2)
self.assertEqual(Proof.objects.has_prices().count(), 1)

def test_with_stats(self):
Expand All @@ -84,7 +90,9 @@ class ProofPropertyTest(TestCase):
def setUpTestData(cls):
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.proof = ProofFactory(
location_osm_id=cls.location.osm_id, location_osm_type=cls.location.osm_type
type=proof_constants.TYPE_PRICE_TAG,
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
)
PriceFactory(
proof_id=cls.proof.id,
Expand All @@ -99,6 +107,9 @@ def setUpTestData(cls):
price=2.0,
)

def test_is_type_single_shop(self):
self.assertTrue(self.proof.is_type_single_shop)

def test_update_price_count(self):
self.proof.refresh_from_db()
self.assertEqual(self.proof.price_count, 2)
Expand Down

0 comments on commit 82da2b9

Please sign in to comment.