From b8d43a629ba23760f2364e21d1f4b01d5b45acd4 Mon Sep 17 00:00:00 2001 From: Sli Date: Mon, 23 Dec 2024 00:00:40 +0100 Subject: [PATCH] Increase selling label size and add more counter click tests --- .../migrations/0029_alter_selling_label.py | 17 ++++ counter/models.py | 3 +- counter/tests/test_counter.py | 88 +++++++++++++++++-- 3 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 counter/migrations/0029_alter_selling_label.py diff --git a/counter/migrations/0029_alter_selling_label.py b/counter/migrations/0029_alter_selling_label.py new file mode 100644 index 00000000..f239efd4 --- /dev/null +++ b/counter/migrations/0029_alter_selling_label.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.17 on 2024-12-22 22:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("counter", "0028_alter_producttype_comment_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="selling", + name="label", + field=models.CharField(max_length=128, verbose_name="label"), + ), + ] diff --git a/counter/models.py b/counter/models.py index b1d1b691..489b0794 100644 --- a/counter/models.py +++ b/counter/models.py @@ -791,7 +791,8 @@ class SellingQuerySet(models.QuerySet): class Selling(models.Model): """Handle the sellings.""" - label = models.CharField(_("label"), max_length=64) + # We make sure that sellings have a way begger label than any product name is allowed to + label = models.CharField(_("label"), max_length=128) product = models.ForeignKey( Product, related_name="sellings", diff --git a/counter/tests/test_counter.py b/counter/tests/test_counter.py index 4e8da3fb..1649196d 100644 --- a/counter/tests/test_counter.py +++ b/counter/tests/test_counter.py @@ -31,6 +31,7 @@ from model_bakery import baker from club.models import Club, Membership from core.baker_recipes import board_user, old_subscriber_user, subscriber_user from core.models import User +from counter.baker_recipes import product_recipe from counter.models import ( Counter, Customer, @@ -216,26 +217,33 @@ class TestCounterClick(FullClickSetup, TestCase): @classmethod def setUpTestData(cls): super().setUpTestData() - cls.beer = baker.make( - Product, limit_age=18, selling_price="1.5", special_selling_price="1" + + cls.underage_customer = subscriber_user.make() + + cls.set_age(cls.customer, 20) + cls.set_age(cls.barmen, 20) + cls.set_age(cls.club_admin, 20) + cls.set_age(cls.underage_customer, 17) + + cls.beer = product_recipe.make( + limit_age=18, selling_price="1.5", special_selling_price="1" ) - cls.beer_tape = baker.make( - Product, + cls.beer_tap = product_recipe.make( limit_age=18, tray=True, selling_price="1.5", special_selling_price="1", ) - cls.snack = baker.make( - Product, limit_age=0, selling_price="1.5", special_selling_price="1" + cls.snack = product_recipe.make( + limit_age=0, selling_price="1.5", special_selling_price="1" ) - cls.stamps = baker.make( - Product, limit_age=0, selling_price="1.5", special_selling_price="1" + cls.stamps = product_recipe.make( + limit_age=0, selling_price="1.5", special_selling_price="1" ) cls.counter.products.add(cls.beer) - cls.counter.products.add(cls.beer_tape) + cls.counter.products.add(cls.beer_tap) cls.counter.products.add(cls.snack) cls.counter.save() @@ -252,6 +260,11 @@ class TestCounterClick(FullClickSetup, TestCase): {"username": used_barman.username, "password": "plop"}, ) + @classmethod + def set_age(cls, user: User, age: int): + user.date_of_birth = localdate().replace(year=localdate().year - age) + user.save() + def submit_basket( self, user: User, @@ -306,6 +319,63 @@ class TestCounterClick(FullClickSetup, TestCase): assert self.updated_amount(self.club_admin) == Decimal("8.5") + def test_click_bar_success(self): + self.refill_user(self.customer, 10) + self.login_in_bar(self.barmen) + + assert ( + self.submit_basket( + self.customer, + [ + BasketItem(self.beer.id, 2), + BasketItem(self.snack.id, 1), + ], + ).status_code + == 302 + ) + + assert self.updated_amount(self.customer) == Decimal("5.5") + + # Test barmen special price + + self.refill_user(self.barmen, 10) + + assert ( + self.submit_basket(self.barmen, [BasketItem(self.beer.id, 1)]) + ).status_code == 302 + + assert self.updated_amount(self.barmen) == Decimal("9") + + def test_click_tray_price(self): + self.refill_user(self.customer, 20) + self.login_in_bar(self.barmen) + + # Not applying tray price + assert ( + self.submit_basket( + self.customer, + [ + BasketItem(self.beer_tap.id, 2), + ], + ).status_code + == 302 + ) + + assert self.updated_amount(self.customer) == Decimal("17") + + # Applying tray price + assert ( + self.submit_basket( + self.customer, + [ + BasketItem(self.beer_tap.id, 7), + ], + ).status_code + == 302 + ) + + assert self.updated_amount(self.customer) == Decimal("8") + def test_annotate_has_barman_queryset(self): """Test if the custom queryset method `annotate_has_barman` works as intended.""" counters = Counter.objects.annotate_has_barman(self.barmen)