Enhanced CSV Scenario Testing for Bulk Import Forms #11410

Closed
opened 2025-12-29 21:44:54 +01:00 by adam · 0 comments
Owner

Originally created by @jnovinger on GitHub (Jul 23, 2025).

Originally assigned to: @jnovinger on GitHub.

NetBox version

v4.3.4

Feature type

Change to existing functionality

Proposed functionality

Extend BulkImportObjectViewTestCase to support multiple CSV scenarios within the existing test_bulk_import_objects_with_permission method.

Problem: The existing BulkImportObjectViewTestCase provides excellent coverage for permissions and basic import functionality, but testing multiple CSV data variations (edge cases, backwards compatibility) requires duplicating test methods or complex workarounds.

Solution: Allow csv_data to accept a dictionary of named scenarios in addition to the current tuple format:

class CableTestCase(ViewTestCases.BulkImportObjectViewTestCase):
    # Option 1: Current tuple format (backwards compatible)
    csv_data = (
        "side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name",
        "Device 1,dcim.interface,eth0,Device 2,dcim.interface,eth0",
    )
    
    # Option 2: New dictionary format for multiple scenarios
    csv_data = {
        'default': (
            "side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name",
            "Device 1,dcim.interface,eth0,Device 2,dcim.interface,eth0",
        ),
        'with_sites': (
            "side_a_site,side_a_device,side_a_type,side_a_name,side_b_site,side_b_device,side_b_type,side_b_name",
            "Site 1,Device 1,dcim.interface,eth0,Site 2,Device 2,dcim.interface,eth0",
        )
    }

Note: The csv_data attribute must use either the tuple format OR the dictionary format, not both.

Implementation:

  • Modify BulkImportObjectViewTestCase.test_bulk_import_objects_with_permission's helper to detect dictionary format
  • Run each scenario as a unittest subTest() for clear test reporting
  • Maintain 100% backwards compatibility with existing tuple format
  • All scenarios expect successful imports
  • Include pointers in BulkImportObjectViewTestCase as to how the new mechanism works

Example test output:

test_bulk_import_objects_with_permission (scenario=default) ... ok
test_bulk_import_objects_with_permission (scenario=with_sites) ... ok

Future Enhancement Possibilities (Explicitly Not Included):

Validation failure testing with expected error scenarios, custom metadata for test scenarios, and integration with other test methods were considered but deemed out of scope to keep the initial enhancement focused and simple.

Use case

This addresses the specific problem encountered during PR #19840 review where multiple CSV variations needed testing but felt out of scope for the core feature PR.

Benefits for NetBox Maintainers:

  • Multiple CSV variations: Test with/without new optional fields in a single test case
  • Edge case scenarios: Add scenarios for backwards compatibility without duplicating test methods
  • Clear reporting: Each scenario shows as separate test result with descriptive names
  • Minimal scope: Small extension to existing infrastructure rather than new testing framework

Benefits for Contributors:

  • Backwards compatible: Existing test cases work unchanged
  • Opt-in: Only test cases that need multiple scenarios use dictionary format
  • Named scenarios: Dictionary keys provide context for failing tests
  • Consistent patterns: Uses existing BulkImportObjectViewTestCase infrastructure

Database changes

None required

External dependencies

None required

Originally created by @jnovinger on GitHub (Jul 23, 2025). Originally assigned to: @jnovinger on GitHub. ### NetBox version v4.3.4 ### Feature type Change to existing functionality ### Proposed functionality Extend `BulkImportObjectViewTestCase` to support multiple CSV scenarios within the existing `test_bulk_import_objects_with_permission` method. **Problem**: The existing `BulkImportObjectViewTestCase` provides excellent coverage for permissions and basic import functionality, but testing multiple CSV data variations (edge cases, backwards compatibility) requires duplicating test methods or complex workarounds. **Solution**: Allow `csv_data` to accept a dictionary of named scenarios in addition to the current tuple format: ```python class CableTestCase(ViewTestCases.BulkImportObjectViewTestCase): # Option 1: Current tuple format (backwards compatible) csv_data = ( "side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name", "Device 1,dcim.interface,eth0,Device 2,dcim.interface,eth0", ) # Option 2: New dictionary format for multiple scenarios csv_data = { 'default': ( "side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name", "Device 1,dcim.interface,eth0,Device 2,dcim.interface,eth0", ), 'with_sites': ( "side_a_site,side_a_device,side_a_type,side_a_name,side_b_site,side_b_device,side_b_type,side_b_name", "Site 1,Device 1,dcim.interface,eth0,Site 2,Device 2,dcim.interface,eth0", ) } ``` **Note:** The `csv_data` attribute must use either the tuple format OR the dictionary format, not both. **Implementation:** - Modify `BulkImportObjectViewTestCase.test_bulk_import_objects_with_permission`'s helper to detect dictionary format - Run each scenario as a `unittest` `subTest()` for clear test reporting - Maintain 100% backwards compatibility with existing tuple format - All scenarios expect successful imports - Include pointers in `BulkImportObjectViewTestCase` as to how the new mechanism works **Example test output**: ``` test_bulk_import_objects_with_permission (scenario=default) ... ok test_bulk_import_objects_with_permission (scenario=with_sites) ... ok ``` **Future Enhancement Possibilities (Explicitly Not Included):** Validation failure testing with expected error scenarios, custom metadata for test scenarios, and integration with other test methods were considered but deemed out of scope to keep the initial enhancement focused and simple. ### Use case This addresses the specific problem encountered during PR #19840 review where multiple CSV variations needed testing but felt out of scope for the core feature PR. **Benefits for NetBox Maintainers**: - **Multiple CSV variations**: Test with/without new optional fields in a single test case - **Edge case scenarios**: Add scenarios for backwards compatibility without duplicating test methods - **Clear reporting**: Each scenario shows as separate test result with descriptive names - **Minimal scope**: Small extension to existing infrastructure rather than new testing framework **Benefits for Contributors**: - **Backwards compatible**: Existing test cases work unchanged - **Opt-in**: Only test cases that need multiple scenarios use dictionary format - **Named scenarios**: Dictionary keys provide context for failing tests - **Consistent patterns**: Uses existing `BulkImportObjectViewTestCase` infrastructure ### Database changes None required ### External dependencies None required
adam closed this issue 2025-12-29 21:44:54 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11410