From bc3e01a40497d65db578cecfe88c8a32fa897cc1 Mon Sep 17 00:00:00 2001 From: Andy Babic Date: Sun, 4 Feb 2024 10:31:17 +0000 Subject: [PATCH] Update tests --- tests/migrations/0012_add_record_label.py | 42 +++++++++++++++++++++++ tests/models.py | 9 +++++ tests/tests/test_cluster.py | 28 +++++++++++++-- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 tests/migrations/0012_add_record_label.py diff --git a/tests/migrations/0012_add_record_label.py b/tests/migrations/0012_add_record_label.py new file mode 100644 index 0000000..b37d2e0 --- /dev/null +++ b/tests/migrations/0012_add_record_label.py @@ -0,0 +1,42 @@ +# Generated by Django 4.2.9 on 2024-02-04 06:59 + +from django.db import migrations, models +import django.db.models.deletion +import modelcluster.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ("taggit", "0005_auto_20220424_2025"), + ("tests", "0011_add_room_features"), + ] + + operations = [ + migrations.CreateModel( + name="RecordLabel", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=200)), + ("range", models.SmallIntegerField(blank=True, default=5)), + ], + ), + migrations.AddField( + model_name="album", + name="label", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="tests.recordlabel", + ), + ), + ] diff --git a/tests/models.py b/tests/models.py index 76820bb..c973047 100644 --- a/tests/models.py +++ b/tests/models.py @@ -36,6 +36,7 @@ class Album(ClusterableModel): name = models.CharField(max_length=255) release_date = models.DateField(null=True, blank=True) sort_order = models.IntegerField(null=True, blank=True, editable=False) + label = models.ForeignKey("RecordLabel", blank=True, null=True, on_delete=models.SET_NULL) sort_order_field = 'sort_order' @@ -60,6 +61,14 @@ class Meta: ordering = ['sort_order'] +class RecordLabel(models.Model): + name = models.CharField(max_length=200) + range = models.SmallIntegerField(default=5, blank=True) + + def __str__(self): + return self.name + + class TaggedPlace(TaggedItemBase): content_object = ParentalKey('Place', related_name='tagged_items', on_delete=models.CASCADE) diff --git a/tests/tests/test_cluster.py b/tests/tests/test_cluster.py index 7abd3ad..536e7c8 100644 --- a/tests/tests/test_cluster.py +++ b/tests/tests/test_cluster.py @@ -11,8 +11,9 @@ from modelcluster.queryset import FakeQuerySet from modelcluster.utils import ManyToManyTraversalError -from tests.models import Band, BandMember, Chef, Feature, Place, Restaurant, SeafoodRestaurant, \ - Review, Album, Article, Author, Category, Person, Room, House, Log, Dish, MenuItem, Wine +from tests.models import Band, BandMember, Chef, Feature, Place, Restaurant, \ + Review, Album, Song, RecordLabel, Article, Author, Category, Person, \ + Room, House, Log, Dish, MenuItem, Wine class ClusterTest(TestCase): @@ -142,6 +143,29 @@ def test_can_create_cluster(self): # queries on beatles.members should now revert to SQL self.assertTrue(beatles.members.extra(where=["tests_bandmember.name='John Lennon'"]).exists()) + def test_filter_expression_token_clash_handling(self): + """ + This tests ensures that the field name 'range' should not be mistaken + for the 'range' from FILTER_EXPRESSION_TOKENS when used in filter() + or exclude(). + + Plus, extract_field_value() should not crash when encountering albums + without a 'label' value specified (they should be classed as automatic + test failures and excluded from the result). + """ + label = RecordLabel.objects.create(name="Parlophone", range=7) + beatles = Band( + name="The Beatles", + albums=[ + Album(name='Please Please Me', label=label, sort_order=1), + Album(name='With The Beatles', sort_order=2), + Album(name='A Hard Day\'s Night', sort_order=3), + ], + ) + + self.assertEqual(beatles.albums.filter(label__range=7).count(), 1) + self.assertEqual(beatles.albums.exclude(label__range=7).count(), 2) + def test_values_list(self): beatles = Band( name="The Beatles",