datapilot-cli icon indicating copy to clipboard operation
datapilot-cli copied to clipboard

test: add comprehensive tests for catalog extra fields handling

Open jontsai opened this issue 1 year ago • 1 comments

Summary

Add test suite to verify that the Metadata class correctly handles extra fields from dbt, preventing job failures when dbt adds new fields.

Motivation

As discussed in the previous PR, dbt frequently adds new fields to their schema (like invocation_started_at). With extra="allow" already merged to main, we now need tests to:

  1. Document the expected behavior
  2. Prevent regressions if someone changes it back to extra="forbid"
  3. Validate that extra fields work correctly

Changes

Tests Added (tests/test_vendor/test_catalog_v1.py)

  • ✅ Extra fields are accepted without validation errors
  • ✅ Extra fields are stored in __pydantic_extra__
  • ✅ model_dump() includes extra fields
  • ✅ Works with no extra fields (backwards compatible)
  • ✅ Works with only extra fields
  • ✅ Specific case of invocation_started_at field

Developer Experience Improvements

  • Makefile added with common targets (make test, make test-vendor, make test-cov, etc.)
  • CONTRIBUTING.rst updated with comprehensive test running instructions
  • Fixed import conflicts by renaming tests/vendor to tests/test_vendor

Test Results

All 6 tests pass ✅

Red/Green verification:

  • With extra="forbid": 5 tests fail with clear error messages ❌
  • With extra="allow": All tests pass ✅

Coverage

  • catalog_v1.py coverage: 100%
  • Overall project coverage: 88.74% (unchanged)

CI Integration

Tests will automatically run in GitHub Actions across:

  • Python 3.10, 3.11, 3.12, PyPy 3.9
  • Pydantic 2.8, 2.10

Checklist

  • [x] Tests added and passing
  • [x] Red/Green verification performed
  • [x] Documentation added (CONTRIBUTING.rst)
  • [x] Developer tooling improved (Makefile)
  • [x] No breaking changes
  • [x] Minimal, focused tests (no bloat)

jontsai avatar Nov 06 '25 23:11 jontsai

Good question! tests/conftest.py is indeed needed.

Why it's required:

In tox.ini, the usedevelop setting varies by test mode:

  • cover: true - Package installed in editable mode (imports work via package installation)
  • nocov: false - Package NOT installed in editable mode

When usedevelop=false (nocov mode), the package isn't installed, so Python can't find the vendor module when tests try to import:

from vendor.dbt_artifacts_parser.parsers.catalog.catalog_v1 import Metadata

The conftest.py solves this by adding src/ to sys.path at import time, before pytest collects tests. This allows imports to work in nocov mode.

Verification: If we remove conftest.py, the nocov tests would fail with:

ModuleNotFoundError: No module named 'vendor'

This is why we created it when we first encountered import issues with the test suite.

jontsai avatar Nov 07 '25 02:11 jontsai