|
| 1 | +import pytest |
| 2 | +from django.db import connection |
| 3 | +from django.test.utils import CaptureQueriesContext |
| 4 | + |
| 5 | +from ansible_base.lib.abstract_models.common import get_url_for_object |
| 6 | +from test_app.models import Organization, Team, User |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.django_db |
| 10 | +class TestRelatedFieldsQueryOptimization: |
| 11 | + """Verify that related_fields() builds correct URLs using raw FK IDs |
| 12 | + without loading related objects from the database.""" |
| 13 | + |
| 14 | + def test_related_fields_urls_match_original_method(self, system_user): |
| 15 | + """The optimized related_fields() must produce the same URLs as |
| 16 | + loading the full FK object and calling get_url_for_object().""" |
| 17 | + org = Organization.objects.create(name="test-org") |
| 18 | + team = Team.objects.create(name="test-team", organization=org) |
| 19 | + |
| 20 | + related = team.related_fields(None) |
| 21 | + |
| 22 | + # The organization FK should produce a valid URL |
| 23 | + assert "organization" in related |
| 24 | + expected_url = get_url_for_object(org) |
| 25 | + assert related["organization"] == expected_url |
| 26 | + |
| 27 | + def test_related_fields_skips_null_fks(self, system_user): |
| 28 | + """Null FK fields should not appear in related_fields output.""" |
| 29 | + org = Organization.objects.create(name="test-org") |
| 30 | + # Force a known-null FK to make the assertion non-vacuous |
| 31 | + Organization.objects.filter(pk=org.pk).update(modified_by=None) |
| 32 | + org.refresh_from_db() |
| 33 | + assert org.modified_by_id is None |
| 34 | + |
| 35 | + related = org.related_fields(None) |
| 36 | + |
| 37 | + assert "modified_by" not in related |
| 38 | + |
| 39 | + def test_related_fields_no_lazy_load_queries(self, system_user): |
| 40 | + """related_fields() should build URLs from raw FK IDs without loading |
| 41 | + related objects, even when the object was fetched without select_related.""" |
| 42 | + org = Organization.objects.create(name="test-org") |
| 43 | + team = Team.objects.create(name="test-team", organization=org) |
| 44 | + |
| 45 | + # Re-fetch without select_related so this exercises the raw FK-id path. |
| 46 | + # With select_related, the old implementation would also be query-free. |
| 47 | + team = Team.objects.get(pk=team.pk) |
| 48 | + |
| 49 | + with CaptureQueriesContext(connection) as ctx: |
| 50 | + team.related_fields(None) |
| 51 | + |
| 52 | + # With the optimization, related_fields uses raw FK IDs for URL |
| 53 | + # construction and should not trigger any FK lazy-load queries. |
| 54 | + assert len(ctx.captured_queries) == 0, f"related_fields() triggered {len(ctx.captured_queries)} queries: " f"{[q['sql'] for q in ctx.captured_queries]}" |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.django_db |
| 58 | +class TestGetSummaryFieldsOptimization: |
| 59 | + """Verify that get_summary_fields() skips null FKs without loading them.""" |
| 60 | + |
| 61 | + def test_summary_fields_null_fk_no_query(self, system_user): |
| 62 | + """get_summary_fields() should not trigger any query for a null FK |
| 63 | + even without select_related.""" |
| 64 | + org = Organization.objects.create(name="test-org") |
| 65 | + # Force null FKs to exercise the null-FK short-circuit path |
| 66 | + Organization.objects.filter(pk=org.pk).update(modified_by=None, created_by=None) |
| 67 | + org = Organization.objects.get(pk=org.pk) |
| 68 | + assert org.modified_by_id is None |
| 69 | + assert org.created_by_id is None |
| 70 | + |
| 71 | + with CaptureQueriesContext(connection) as ctx: |
| 72 | + summary = org.get_summary_fields() |
| 73 | + |
| 74 | + assert isinstance(summary, dict) |
| 75 | + assert "modified_by" not in summary |
| 76 | + assert "created_by" not in summary |
| 77 | + assert len(ctx.captured_queries) == 0, ( |
| 78 | + f"get_summary_fields() triggered {len(ctx.captured_queries)} queries for null FKs: " f"{[q['sql'] for q in ctx.captured_queries]}" |
| 79 | + ) |
| 80 | + |
| 81 | + def test_summary_fields_no_lazy_load_with_select_related(self, system_user): |
| 82 | + """get_summary_fields() should not trigger per-FK lazy-load queries |
| 83 | + when the object was fetched with select_related.""" |
| 84 | + user = User.objects.create(username="test-user") |
| 85 | + |
| 86 | + # Pre-load all FK relations so get_summary_fields() doesn't need to query |
| 87 | + user = User.objects.select_related("modified_by", "created_by", "resource").get(pk=user.pk) |
| 88 | + |
| 89 | + with CaptureQueriesContext(connection) as ctx: |
| 90 | + summary = user.get_summary_fields() |
| 91 | + |
| 92 | + assert isinstance(summary, dict) |
| 93 | + assert len(ctx.captured_queries) == 0, ( |
| 94 | + f"get_summary_fields() triggered {len(ctx.captured_queries)} queries: " f"{[q['sql'] for q in ctx.captured_queries]}" |
| 95 | + ) |
| 96 | + |
| 97 | + def test_summary_fields_content_unchanged(self, system_user): |
| 98 | + """get_summary_fields() should return the same content as before |
| 99 | + the optimization.""" |
| 100 | + org = Organization.objects.create(name="test-org") |
| 101 | + team = Team.objects.create(name="test-team", organization=org) |
| 102 | + |
| 103 | + summary = team.get_summary_fields() |
| 104 | + |
| 105 | + # Organization FK should be in summary with expected structure |
| 106 | + assert "organization" in summary |
| 107 | + assert "id" in summary["organization"] |
| 108 | + assert "name" in summary["organization"] |
| 109 | + assert summary["organization"]["name"] == "test-org" |
0 commit comments