|
2 | 2 | from django.contrib.contenttypes.models import ContentType
|
3 | 3 |
|
4 | 4 | from ansible_base.resource_registry.models import Resource
|
5 |
| -from test_app.models import Organization |
| 5 | +from test_app.models import Inventory, Organization |
6 | 6 |
|
7 | 7 |
|
8 | 8 | @pytest.mark.django_db
|
@@ -100,3 +100,48 @@ def test_resource_field_filtering(organization):
|
100 | 100 |
|
101 | 101 | org = Organization.objects.get(resource__name=organization.name)
|
102 | 102 | 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