Skip to content

test: improve JSON parsing module test coverage#1

Draft
tpsaint wants to merge 1 commit intomasterfrom
test/improve-json-parsing-coverage
Draft

test: improve JSON parsing module test coverage#1
tpsaint wants to merge 1 commit intomasterfrom
test/improve-json-parsing-coverage

Conversation

@tpsaint
Copy link
Owner

@tpsaint tpsaint commented Jul 20, 2025

This PR significantly improves test coverage for the JSON parsing module in the AutoGPT Forge framework. The forge/json/parsing.py module had only 42% test coverage, with two important functions (extract_dict_from_json and extract_list_from_json) completely untested. This creates a risk for regressions and makes it difficult to maintain code quality.

Changes 🏗️

  • Added comprehensive tests for extract_dict_from_json function (10 new test cases)

    • JSON extraction from code blocks (both json and JSON markers)
    • JSON extraction from text with embedded JSON objects
    • Plain JSON dictionary parsing
    • Nested dictionary structures
    • JSON with fixable syntax issues (extra commas, etc.)
    • Error validation for non-dict values (lists, strings, numbers)
    • Error handling for completely invalid JSON
  • Added comprehensive tests for extract_list_from_json function (10 new test cases)

    • JSON extraction from code blocks (both json and JSON markers)
    • JSON extraction from text with embedded JSON arrays
    • Plain JSON list parsing
    • Nested list structures
    • Mixed-type lists (numbers, strings, booleans, objects)
    • JSON with fixable syntax issues (trailing commas, etc.)
    • Error validation for non-list values (dicts, strings, numbers)
    • Error handling for completely invalid JSON
  • Enhanced json_loads error handling tests (3 new test cases)

    • Invalid JSON string handling
    • Empty string behavior (returns None)
    • Whitespace-only string behavior (returns None)
  • Fixed conftest.py to remove problematic VCR plugin reference that was preventing tests from running

Test Coverage Results:

  • forge/json/parsing.py: 42% → 100% coverage (all 43 lines now covered)
  • Overall project coverage: 9% → 11% improvement
  • All 52 new tests pass successfully

Checklist 📋

For code changes:

  • I have clearly listed my changes in the PR description
  • I have made a test plan
  • I have tested my changes according to the test plan:
    • Run all new JSON parsing tests to ensure they pass (pytest forge/json/test_parsing.py -v)
    • Verify 100% coverage for the JSON parsing module (pytest --cov=forge.json.parsing --cov-report=term-missing forge/json/test_parsing.py)
    • Test actual functionality with sample JSON strings to ensure the functions work correctly
    • Run existing tests to ensure no regressions (pytest forge/utils/test_file_operations.py forge/utils/test_url_validator.py)
    • Verify overall project coverage improvement

For configuration changes:

  • .env.example is updated or already compatible with my changes (no env changes needed)
  • docker-compose.yml is updated or already compatible with my changes (no docker changes needed)
  • I have included a list of my configuration changes in the PR description (no configuration changes)

@tpsaint can click here to continue refining the PR

- Add comprehensive tests for extract_dict_from_json function
- Add comprehensive tests for extract_list_from_json function
- Add additional error handling tests for json_loads function
- Achieve 100% test coverage for forge/json/parsing.py module
- Fix conftest.py to remove problematic VCR plugin reference

The new tests cover:
- JSON extraction from code blocks (both lowercase and uppercase)
- JSON extraction from text with embedded JSON
- Plain JSON parsing for dicts and lists
- Nested data structures
- JSON with fixable syntax issues
- Comprehensive error cases for type validation
- Edge cases like empty strings and whitespace
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.

2 participants