Skip to content

Closes #952: Split the polymorphic field tests by layer - #953

Open
bctiemann wants to merge 1 commit into
featurefrom
952-polymorphic-tests-by-layer
Open

bctiemann wants to merge 1 commit into
featurefrom
952-polymorphic-tests-by-layer

Conversation

@bctiemann

Copy link
Copy Markdown
Contributor

Closes: #952

Splits tests/test_polymorphic_fields.py (about 2,300 lines) by layer, as proposed in the NPL-1248 test audit. Each part goes into the package that already holds that layer's tests.

Module Contents Tests
tests/api/test_polymorphic_fields.py PolymorphicFieldAPITest: defining polymorphic fields, and reading and writing their values over REST 26
tests/test_polymorphic_views.py PolymorphicFieldUITest: the edit, bulk edit, detail and delete pages and their forms 19
tests/models/test_polymorphic_fields.py PolymorphicThroughModelRegistrationTest, ReferencedObjectDeletionTest, PolymorphicCycleDetectionTest, PolymorphicReverseDescriptorTest, PolymorphicReverseDescriptorRecursionTestCase, GetModelCacheRegistryConsistencyTestCase 43

The UI tests stay at the top level next to test_views.py, since there's no views package.

The test classes move unchanged. The only other changes:

  • _grant_perm, which the API and UI tests both use, moves unchanged to tests/base.py, so there's one copy.
  • Each module imports only what it uses and has its own docstring. The "API tests" and "UI / form tests" section banners are gone, since each of those modules holds one class.

The cache and registry classes are among those the audit plans to gather into test_model_cache.py. They sit with the other model-level tests until then.

Issue numbers in section comments and class names are left for the naming pass, the audit's last item.

Testing

🤖 Generated with Claude Code

Move the REST tests to tests/api, the UI tests to
test_polymorphic_views, and the model-level tests to tests/models, and
move the shared _grant_perm helper to tests/base. Tests move unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bctiemann

Copy link
Copy Markdown
Contributor Author

@claude /review and report only on actionable issues.

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Claude finished @bctiemann's task in 2m 39s —— View job


Review: Closes #952 — Split polymorphic field tests by layer

  • Read changed files
  • Analyze diff against base branch
  • Report actionable issues

The split is clean and the test logic moves verbatim. Four actionable issues below.


1. Unused self.field_perm_ot in API test setUp

tests/api/test_polymorphic_fields.py:79:

self.field_perm_ot = ObjectType.objects.get_for_model(CustomObjectTypeField)

This attribute is assigned but never referenced in any test method. It triggers an unnecessary DB query on every test setup. Fix this →


2. Redundant inline re-imports of APP_LABEL in the model tests

tests/models/test_polymorphic_fields.py imports APP_LABEL at module level (line 13) but then re-imports it inside two test methods:

  • line 49: from netbox_custom_objects.constants import APP_LABEL
  • line 176: from netbox_custom_objects.constants import APP_LABEL

These are dead statements — the name is already in scope. Fix this →


3. ContentType imported inline 9 times instead of at module level

tests/api/test_polymorphic_fields.py imports from django.contrib.contenttypes.models import ContentType inside 9 separate test methods (lines 267, 283, 330, 355, 413, 474, 527, 546, 571). The same pattern appears 6 times in tests/test_polymorphic_views.py (lines 219, 234, 283, 344, 427, 451).

ContentType and CustomValidator (from extras.validators import CustomValidator, appearing 3 times inline in the views file) should be promoted to module-level imports. Fix this →


4. setUp-level imports that should be at module level

  • tests/api/test_polymorphic_fields.py:32–33:
    from django.test import Client as DjangoClient
    from utilities.testing import create_test_user
  • tests/models/test_polymorphic_fields.py:27:
    from utilities.testing import create_test_user

These are module-level dependencies that happen to be deferred into setUp. Both Client and create_test_user are used unconditionally in every test run of their respective classes, so deferring them buys nothing. Fix this →


No correctness issues found. All four issues are style/cleanup; none affect test behaviour.

@bctiemann

Copy link
Copy Markdown
Contributor Author

Thanks. All four are accurate, but each one is already in test_polymorphic_fields.py on feature. This PR moves the classes unchanged, and a script confirms every class body is identical to the original, so I'm leaving them out of this split.

They'll be handled in the naming and cleanup pass at the end of the NPL-1248 test audit:

  • remove the unused field_perm_ot and the repeated APP_LABEL imports;
  • move imports out of test methods and setUp to module level across all three modules. That covers ContentType, CustomValidator and create_test_user, including the one in test_polymorphic_views.py setUp, plus the other imports done inside test methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant