diff --git a/games/__tests__/test_admin.py b/games/__tests__/test_admin.py index af69463..97b4b29 100644 --- a/games/__tests__/test_admin.py +++ b/games/__tests__/test_admin.py @@ -83,26 +83,29 @@ def test_total_price_paid_display_zero(self): def test_latest_added_date_display(self): """Test latest_added_date shows most recent platform addition date""" - - game = GameFactory(name="Test Game") + # Prevent factory from creating automatic platform relationship + game = GameFactory(name="Test Game", platforms=False) older_date = date.today() - timedelta(days=10) newer_date = date.today() - timedelta(days=5) # Add game to platform on different dates - GameOnPlatform.objects.create( + gop1 = GameOnPlatform.objects.create( game=game, platform=self.platform, vendor=self.vendor1, - added=older_date, deleted=False, ) - GameOnPlatform.objects.create( + # Set the date after creation to bypass auto_now_add + GameOnPlatform.objects.filter(pk=gop1.pk).update(added=older_date) + + gop2 = GameOnPlatform.objects.create( game=game, platform=self.platform, vendor=self.vendor2, - added=newer_date, deleted=False, ) + # Set the date after creation to bypass auto_now_add + GameOnPlatform.objects.filter(pk=gop2.pk).update(added=newer_date) # Annotate as admin would game = ( @@ -116,9 +119,20 @@ def test_latest_added_date_display(self): def test_latest_added_date_display_never(self): """Test latest_added_date shows 'Never' for games without dates""" - game = GameFactory(name="Undated Game") + # Prevent factory from creating automatic platform relationship + game = GameFactory(name="Undated Game", platforms=False) - # Game with no platform relationships + # Create GameOnPlatform with added=None + gop = GameOnPlatform.objects.create( + game=game, + platform=self.platform, + vendor=self.vendor1, + deleted=False, + ) + # Set added to None after creation (bypasses auto_now_add) + GameOnPlatform.objects.filter(pk=gop.pk).update(added=None) + + # Annotate as admin would - AFTER updating the added field game = ( Game.objects.all_with_orphaned() .annotate(latest_added=models.Max("platforms_meta_data__added")) diff --git a/games/migrations/0006_alter_gameonplatform_added_and_more.py b/games/migrations/0006_alter_gameonplatform_added_and_more.py new file mode 100644 index 0000000..5b54c49 --- /dev/null +++ b/games/migrations/0006_alter_gameonplatform_added_and_more.py @@ -0,0 +1,29 @@ +# Generated by Django 5.2.5 on 2026-01-24 12:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("games", "0005_rename_source_to_vendor"), + ] + + operations = [ + migrations.AlterField( + model_name="gameonplatform", + name="added", + field=models.DateField(auto_now_add=True, null=True), + ), + migrations.AlterField( + model_name="gameonplatform", + name="price", + field=models.DecimalField( + blank=True, + decimal_places=2, + default=0.0, + max_digits=6, + null=True, + verbose_name="Purchase price", + ), + ), + ] diff --git a/games/models.py b/games/models.py index c63546b..51c2a48 100644 --- a/games/models.py +++ b/games/models.py @@ -115,7 +115,7 @@ class GameOnPlatform(models.Model): platform = models.ForeignKey( Platform, on_delete=models.CASCADE, related_name="games_meta_data" ) - added = models.DateField(null=True, blank=True) + added = models.DateField(null=True, blank=True, auto_now_add=True) identifier = models.CharField( verbose_name="ID in the platform for generating URLs", max_length=255, @@ -128,6 +128,7 @@ class GameOnPlatform(models.Model): max_digits=6, blank=True, null=True, + default=0.0, ) vendor = models.ForeignKey(Vendor, on_delete=models.SET_NULL, null=True, blank=True) deleted = models.BooleanField(default=False, db_index=True)