Skip to content

Commit 4262a90

Browse files
committed
Add a test for multi querysets
1 parent 6b1c0cf commit 4262a90

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

ansible_base/resource_registry/fields.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ def get_queryset(self, **hints):
1515

1616
def get_prefetch_querysets(self, instances, querysets=None):
1717
if querysets and len(querysets) != 1:
18-
raise ValueError(
19-
"querysets argument of get_prefetch_querysets() should have a length "
20-
"of 1."
21-
)
18+
raise ValueError("querysets argument of get_prefetch_querysets() should have a length " "of 1.")
2219
queryset = querysets[0] if querysets else self.get_queryset()
2320
queryset._add_hints(instance=instances[0])
2421

test_app/tests/resource_registry/models/test_resource_field.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.contrib.contenttypes.models import ContentType
33

44
from ansible_base.resource_registry.models import Resource
5-
from test_app.models import Organization
5+
from test_app.models import Inventory, Organization
66

77

88
@pytest.mark.django_db
@@ -100,3 +100,48 @@ def test_resource_field_filtering(organization):
100100

101101
org = Organization.objects.get(resource__name=organization.name)
102102
assert org.resource.pk == resource.pk
103+
104+
105+
@pytest.mark.django_db
106+
def test_resource_field_prefetch_related_across_foreign_key(organization, organization_1, organization_2, django_assert_num_queries):
107+
"""
108+
Generated by Claude Code (claude-sonnet-4-5@20250929)
109+
Test that prefetch_related works across a ForeignKey to a model with a resource field.
110+
This tests prefetching organization__resource on Inventory objects.
111+
"""
112+
# Create inventory objects linked to organizations
113+
Inventory.objects.create(name="Inventory 1", organization=organization)
114+
Inventory.objects.create(name="Inventory 2", organization=organization_1)
115+
Inventory.objects.create(name="Inventory 3", organization=organization_2)
116+
117+
org_ctype = ContentType.objects.get_for_model(Organization)
118+
119+
# Prefetch organization__resource should result in 3 queries:
120+
# 1. Fetch all Inventory objects
121+
# 2. Fetch all related Organization objects
122+
# 3. Fetch all related Resource objects
123+
with django_assert_num_queries(3) as captured:
124+
inventory_qs = list(Inventory.objects.prefetch_related("organization__resource").all())
125+
126+
# Verify the queries were as expected
127+
assert "test_app_inventory" in captured[0]["sql"]
128+
assert "test_app_organization" in captured[1]["sql"]
129+
assert "dab_resource_registry_resource" in captured[2]["sql"]
130+
131+
assert len(inventory_qs) == 3
132+
133+
# Collect resource pks for later verification
134+
resource_pks = {}
135+
with django_assert_num_queries(0):
136+
for inv in inventory_qs:
137+
assert inv.organization is not None
138+
assert inv.organization.resource is not None
139+
# Verify the resource data is correct
140+
assert inv.organization.name == inv.organization.resource.name
141+
assert str(inv.organization.pk) == inv.organization.resource.object_id
142+
resource_pks[inv.organization.pk] = inv.organization.resource.pk
143+
144+
# Verify the resources match what's in the database
145+
for org_pk, resource_pk in resource_pks.items():
146+
expected_resource = Resource.objects.get(object_id=org_pk, content_type=org_ctype)
147+
assert resource_pk == expected_resource.pk

0 commit comments

Comments
 (0)