Skip to content

[Code Quality] Add _api_post_form counter increment test to test_api_tracking.py #566

@github-actions

Description

@github-actions

Description

tests/test_api_tracking.py verifies that _api_get, _api_post, and _api_delete each increment the control_d_api_calls counter in _api_stats. However, _api_post_form — a fourth HTTP wrapper used by push_rules for batch rule creation — is not covered:

# test_api_tracking.py — existing tests
test_api_get_increments_countertest_api_post_increments_countertest_api_delete_increments_countertest_api_post_form_increments_countermissing

_api_post_form is the function used when push_rules submits rule batches to the Control D API. It was optimized in PR #544 (merged 2026-03-04, noted in Daily QAReport – March 5 #562) and carries the same counter-increment responsibility as the other three wrappers:

def _api_post_form(client: httpx.Client, url: str, data: dict) -> httpx.Response:
    with _api_stats_lock:
        _api_stats["control_d_api_calls"] += 1   # <-- needs test coverage
    return _retry_request(...)

Without a test, a future refactor that accidentally drops the counter increment from _api_post_form would go undetected by the test suite.

Suggested Changes

Add one test to tests/test_api_tracking.py (following the same pattern as the existing three tests):

`@patch`("main.httpx.Client")
def test_api_post_form_increments_counter(self, mock_client_class):
    """Test that _api_post_form increments the API call counter."""
    import main

    initial_count = main._api_stats["control_d_api_calls"]

    mock_client = MagicMock()
    mock_response = MagicMock()
    mock_response.raise_for_status = MagicMock()
    mock_client.post.return_value = mock_response

    main._api_post_form(mock_client, "(test.url/redacted) {"key": "value"})

    self.assertEqual(main._api_stats["control_d_api_calls"], initial_count + 1)

Optionally, add a second test verifying that _api_post_form passes the Content-Type: application/x-www-form-urlencoded header, since that is its distinguishing behavior vs _api_post.

Files Affected

  • tests/test_api_tracking.py — add 1–2 test methods to TestAPITracking

Success Criteria

  • test_api_post_form_increments_counter added and passing
  • ✅ All existing tests continue to pass: uv run pytest tests/ -v
  • ✅ No changes to api_client.py or main.py

Source

Gap identified while reviewing tests/test_api_tracking.py coverage in context of Daily QAReport – March 5 #562 noting PR #544 (push_rules deduplication) and the four exported HTTP wrappers in api_client.py.

Priority

Low — Adds missing parity test. Prevents a silent regression if the counter increment is accidentally removed from _api_post_form during future refactors.

🔍 Task mining by Discussion Task Miner - Code Quality Improvement Agent

To install this agentic workflow, run

gh aw add github/gh-aw/.github/workflows/discussion-task-miner.md@94662b1dee8ce96c876ba9f33b3ab8be32de82a4
  • expires on Mar 6, 2026, 9:14 AM UTC

Metadata

Metadata

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions