json2xml icon indicating copy to clipboard operation
json2xml copied to clipboard

feat: Increase test coverage and strengthen dicttoxml typing

Open vinitkumar opened this issue 7 months ago • 2 comments

Summary

  • Add targeted tests for edge branches in dicttoxml (CDATA end sequence, namespaced/numeric keys, list header and @flat handling, Decimal typing, ids attribute behavior)
  • Refactor get_unique_id to accept an optional ids parameter for controllable uniqueness and simplify type checks in get_xml_type

Test plan

  • Run: python -m pip install -r requirements-dev.txt
  • Run: pytest -q --cov=json2xml --cov-report=term-missing
  • Verify no regressions and improved coverage, especially around dict2xml_str, list2xml_str, and CDATA handling

Summary by Sourcery

Allow external ID tracking in get_unique_id, refine XML type detection, and add targeted tests to cover CDATA handling, XML naming, list flattening, Decimal typing, and auto-generated IDs.

Enhancements:

  • Extend get_unique_id to accept an optional list of existing IDs for duplicate prevention
  • Simplify get_xml_type to only distinguish Python str, int, float, and number types

Tests:

  • Add tests for CDATA end-sequence splitting and wrapping
  • Add tests for valid XML name conversion of numeric and namespaced keys with @flat handling
  • Add tests for dict2xml_str and list2xml_str behaviors including list headers and item_flat
  • Add tests for Decimal values to be treated as numbers and proper type attributes
  • Add tests for automatic id attribute assignment in convert_dict
  • Update test for get_unique_id to verify duplicate resolution with a provided ids list

vinitkumar avatar Aug 08 '25 11:08 vinitkumar

Reviewer's Guide

This PR refactors the unique ID generation and XML type detection in dicttoxml for stronger typing and controlled uniqueness, and adds a suite of targeted tests to cover edge cases like CDATA splitting, namespaced/numeric keys, list wrapping behavior, Decimal handling, and ID attribute injection.

Class diagram for test coverage improvements in test_additional_coverage.py

classDiagram
    class test_additional_coverage
    test_additional_coverage : +test_cdata_end_sequence()
    test_additional_coverage : +test_namespaced_numeric_keys()
    test_additional_coverage : +test_list_header_flat_handling()
    test_additional_coverage : +test_decimal_typing()
    test_additional_coverage : +test_ids_attribute_behavior()
    test_additional_coverage --> dicttoxml : tests

File-Level Changes

Change Details Files
Refactor get_unique_id to accept optional ids list and improve uniqueness logic
  • Added ids parameter to get_unique_id signature
  • Initialized ids list only when not provided
  • Retained generation loop to avoid duplicates against passed-in IDs
json2xml/dicttoxml.py
Simplify get_xml_type by removing legacy type-name checks
  • Restricted str detection to type name 'str'
  • Restricted int detection to type name 'int'
  • Left other types to fall through to 'float' or 'number'
json2xml/dicttoxml.py
Update duplicate-ID test to leverage new ids parameter and monkeypatching
  • Refactored test to inject existing IDs list
  • Used Mock and monkeypatch for make_id calls
  • Asserted correct call count and returned unique ID
tests/test_dict2xml.py
Add targeted edge-case tests to increase coverage
  • Test wrap_cdata splitting around ']]>'
  • Validate XML name conversion for numeric and namespaced keys
  • Cover list/dict wrapping flags and list_headers behavior
  • Verify Decimal typing as 'number' and convert_kv output
  • Assert ID attributes in convert_dict output
tests/test_additional_coverage.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Aug 08 '25 11:08 sourcery-ai[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 100.00%. Comparing base (43c4e18) to head (8f19e8a). :warning: Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master      #246      +/-   ##
===========================================
+ Coverage   99.30%   100.00%   +0.69%     
===========================================
  Files           3         3              
  Lines         288       289       +1     
===========================================
+ Hits          286       289       +3     
+ Misses          2         0       -2     
Flag Coverage Δ
unittests 100.00% <100.00%> (+0.69%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 08 '25 11:08 codecov[bot]