Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement composite member lookup #32

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions postgres_composite_types/composite_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ def __new__(cls, name, bases, attrs):
new_cls = super().__new__(cls, name, bases, attrs)
new_cls._meta = meta_obj

for _, field in new_cls._meta.fields:
new_cls.Field.register_lookup(
type(
f"{name}{field.attname}Lookup",
(models.Transform,),
{
"lookup_name": field.attname,
"arity": 1,
"template": f'(%(expressions)s)."{field.column}"',
},
)
)

meta_obj.model = new_cls

return new_cls
Expand Down
3 changes: 1 addition & 2 deletions postgres_composite_types/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ def sql_field_definition(field_name, field, schema_editor):

def sql_create_type(type_name, fields, schema_editor):
fields_list = ", ".join(
sql_field_definition(field_name, field, schema_editor)
for field_name, field in fields
sql_field_definition(field.column, field, schema_editor) for _, field in fields
)
quoted_name = schema_editor.quote_name(type_name)
return f"CREATE TYPE {quoted_name} AS ({fields_list})"
Expand Down
2 changes: 2 additions & 0 deletions tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DescriptorType,
OptionalBits,
Point,
RenamedMemberType,
SimpleType,
)

Expand All @@ -28,4 +29,5 @@ class Migration(migrations.Migration):
Box.Operation(),
DateRange.Operation(),
DescriptorType.Operation(),
RenamedMemberType.Operation(),
]
7 changes: 6 additions & 1 deletion tests/migrations/0002_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down Expand Up @@ -115,4 +114,10 @@ class Migration(migrations.Migration):
("test_field", tests.models.SimpleTypeField()),
],
),
migrations.CreateModel(
name="RenamedMemberModel",
fields=[
("field", tests.models.RenamedMemberTypeField()),
],
),
]
16 changes: 16 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ class DescriptorModel(models.Model):
"""Has a composite type with a field implementing a custom descriptor"""

field = DescriptorType.Field()


class RenamedMemberType(CompositeType):
"""Has a field with a different name in ORM vs db"""

class Meta:
db_type = "test_renamed_member"

orm_name = models.CharField(db_column="db_name", max_length=32)
other = models.BooleanField(default=False)


class RenamedMemberModel(models.Model):
"""Has a composite type with a member attr name that differs from the column name"""

field = RenamedMemberType.Field()
47 changes: 46 additions & 1 deletion tests/test_more.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"""
Tests for composite fields in combination with other interesting fields.
"""
import datetime

from django.db.migrations.writer import MigrationWriter
from django.test import TestCase

from .models import Box, Card, DescriptorModel, DescriptorType, Hand, Item, Point
from .models import (
Box,
Card,
DescriptorModel,
DescriptorType,
Hand,
Item,
Point,
RenamedMemberModel,
RenamedMemberType,
SimpleModel,
SimpleType,
)


class TestArrayFields(TestCase):
Expand Down Expand Up @@ -115,3 +128,35 @@ def test_set(self):
model = DescriptorModel(field=DescriptorType(value=0))
model.field.value = 14
self.assertEqual(model.field.value, 42)


class TestMemberLookup(TestCase):
"""
Test filtering queryset on composite type members.
"""

def test_value(self):
"""Test finding a record by composite member value."""
t = SimpleType(a=1, b="β ☃", c=datetime.datetime(1985, 10, 26, 9, 0))
m = SimpleModel(test_field=t)
m.save()

self.assertEqual(SimpleModel.objects.get(test_field__a=1), m)

def test_comparison(self):
"""Test finding a record by comparison on a composite member value."""
t = SimpleType(a=1, b="β ☃", c=datetime.datetime(1985, 10, 26, 9, 0))
m = SimpleModel(test_field=t)
m.save()
t.a = 3
SimpleModel.objects.create(test_field=t)

self.assertEqual(SimpleModel.objects.get(test_field__a__lt=2), m)

def test_renamed(self):
"""Test finding a record by composite member value with a different ORM name."""
t = RenamedMemberType(orm_name="foo")
m = RenamedMemberModel(field=t)
m.save()

self.assertEqual(RenamedMemberModel.objects.get(field__orm_name="foo"), m)