Skip to content

Commit

Permalink
refactor(Prices): transform receipt_quantity field to Decimal (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Jan 1, 2025
1 parent c62ccae commit c807dee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
27 changes: 27 additions & 0 deletions open_prices/prices/migrations/0005_alter_price_receipt_quantity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.1.4 on 2025-01-01 22:11

from decimal import Decimal

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("prices", "0004_price_type"),
]

operations = [
migrations.AlterField(
model_name="price",
name="receipt_quantity",
field=models.DecimalField(
blank=True,
decimal_places=2,
max_digits=10,
null=True,
validators=[django.core.validators.MinValueValidator(Decimal("0"))],
verbose_name="Receipt's price quantity (user input)",
),
),
]
6 changes: 4 additions & 2 deletions open_prices/prices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ class Price(models.Model):
related_name="prices",
)

receipt_quantity = models.PositiveIntegerField(
receipt_quantity = models.DecimalField(
verbose_name="Receipt's price quantity (user input)",
validators=[MinValueValidator(1)],
max_digits=10,
decimal_places=2,
validators=[MinValueValidator(decimal.Decimal(0))],
blank=True,
null=True,
)
Expand Down
8 changes: 4 additions & 4 deletions open_prices/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_price_origin_validation(self):
self.assertEqual(price.origins_tags, expected_origin_tags)

def test_price_price_validation(self):
for PRICE_OK in [5, 0]:
for PRICE_OK in [0, 5, Decimal("1.5")]:
PriceFactory(price=PRICE_OK)
for PRICE_NOT_OK in [-5, "test", None, "None"]: # True
self.assertRaises(ValidationError, PriceFactory, price=PRICE_NOT_OK)
Expand Down Expand Up @@ -256,7 +256,7 @@ def test_price_discount_validation(self):
price_is_discounted=True,
price_without_discount=PRICE_WITHOUT_DISCOUNT_OK,
)
for PRICE_WITHOUT_DISCOUNT_NOT_OK in [-5, "test"]:
for PRICE_WITHOUT_DISCOUNT_NOT_OK in [-5, "test", "None"]:
self.assertRaises(
ValidationError,
PriceFactory,
Expand Down Expand Up @@ -445,7 +445,7 @@ def test_price_proof_validation(self):
owner=user_proof_receipt.owner,
)
# receipt_quantity
for RECEIPT_QUANTITY_NOT_OK in [-5, 0]:
for RECEIPT_QUANTITY_NOT_OK in [-5, "test"]:
with self.subTest(RECEIPT_QUANTITY_NOT_OK=RECEIPT_QUANTITY_NOT_OK):
self.assertRaises(
ValidationError,
Expand All @@ -458,7 +458,7 @@ def test_price_proof_validation(self):
owner=user_proof_receipt.owner,
receipt_quantity=RECEIPT_QUANTITY_NOT_OK,
)
for RECEIPT_QUANTITY_OK in [None, 1, 2]:
for RECEIPT_QUANTITY_OK in [None, 0, 1, 2, Decimal("1.5")]:
with self.subTest(RECEIPT_QUANTITY_OK=RECEIPT_QUANTITY_OK):
PriceFactory(
proof_id=user_proof_receipt.id,
Expand Down

0 comments on commit c807dee

Please sign in to comment.