Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Feb 4, 2024
1 parent c0c0cba commit ba4b189
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
10 changes: 9 additions & 1 deletion modelcluster/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ def get_model_field(model, name):
subject_model=subject_model,
)
)
if hasattr(field, "related_model"):
if getattr(field, "related_model", None):
traversals.append(TraversedRelationship(subject_model, field))
subject_model = field.related_model
else:
raise FieldDoesNotExist(
"Failed attempting to traverse from {from_field} to '{to_field}'."
.format(
from_field=subject_model._meta.app_label + '.' + field.name,
to_field=field_name,
)
)
try:
field = subject_model._meta.get_field(field_name)
except FieldDoesNotExist:
Expand Down
42 changes: 42 additions & 0 deletions tests/migrations/0012_add_record_label.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
9 changes: 9 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)

Expand Down
28 changes: 26 additions & 2 deletions tests/tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit ba4b189

Please sign in to comment.