|
1 | 1 | import pytest
|
2 | 2 | from django.contrib.contenttypes.models import ContentType
|
3 | 3 |
|
4 |
| -from ansible_base.resource_registry.models import Resource |
| 4 | +from ansible_base.resource_registry.models import Resource, ResourceType |
5 | 5 | from test_app.models import Inventory, Organization
|
6 | 6 |
|
7 | 7 |
|
@@ -145,3 +145,46 @@ def test_resource_field_prefetch_related_across_foreign_key(organization, organi
|
145 | 145 | for org_pk, resource_pk in resource_pks.items():
|
146 | 146 | expected_resource = Resource.objects.get(object_id=org_pk, content_type=org_ctype)
|
147 | 147 | assert resource_pk == expected_resource.pk
|
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.django_db |
| 151 | +def test_resource_field_prefetch_resource_type_from_organization(organization, organization_1, organization_2, django_assert_num_queries): |
| 152 | + """ |
| 153 | + Generated by Claude Code (claude-sonnet-4-5@20250929) |
| 154 | + Test that prefetch_related works from Organization through resource to resource_type. |
| 155 | + This tests prefetching resource__content_type__resource_type on Organization objects. |
| 156 | + """ |
| 157 | + org_ctype = ContentType.objects.get_for_model(Organization) |
| 158 | + |
| 159 | + # Prefetch resource__content_type__resource_type should result in 4 queries: |
| 160 | + # 1. Fetch all Organization objects |
| 161 | + # 2. Fetch all related Resource objects |
| 162 | + # 3. Fetch all related ContentType objects |
| 163 | + # 4. Fetch all related ResourceType objects |
| 164 | + with django_assert_num_queries(4) as captured: |
| 165 | + org_qs = list(Organization.objects.prefetch_related("resource__content_type__resource_type").all()) |
| 166 | + |
| 167 | + # Verify the queries were as expected |
| 168 | + assert "test_app_organization" in captured[0]["sql"] |
| 169 | + assert "dab_resource_registry_resource" in captured[1]["sql"] |
| 170 | + assert "django_content_type" in captured[2]["sql"] |
| 171 | + assert "dab_resource_registry_resourcetype" in captured[3]["sql"] |
| 172 | + |
| 173 | + assert len(org_qs) > 2 |
| 174 | + |
| 175 | + # Collect resource type data for later verification |
| 176 | + resource_type_data = {} |
| 177 | + with django_assert_num_queries(0): |
| 178 | + for org in org_qs: |
| 179 | + assert org.resource is not None |
| 180 | + assert org.resource.content_type is not None |
| 181 | + assert org.resource.content_type.resource_type is not None |
| 182 | + # Verify the resource type is correct |
| 183 | + resource_type = org.resource.content_type.resource_type |
| 184 | + assert resource_type.content_type == org_ctype |
| 185 | + resource_type_data[org.pk] = resource_type.pk |
| 186 | + |
| 187 | + # Verify the resource types match what's in the database |
| 188 | + expected_resource_type = ResourceType.objects.get(content_type=org_ctype) |
| 189 | + for org_pk, resource_type_pk in resource_type_data.items(): |
| 190 | + assert resource_type_pk == expected_resource_type.pk |
0 commit comments