- Fixed field extraction logic in test_field_schema_validation.py to properly parse methods with docstrings - Previous regex was too greedy and matched across multiple method definitions - Now uses proper parenthesis and docstring matching to isolate method bodies - Correctly handles both 'fields = [...]' and 'fields = common_fields + [...]' patterns - Updated db_schema.sql to include missing columns: - campaign_insights: added frequency, cpp, cost_per_action_type columns - adset_insights: added account_currency column - campaign_insights_by_country: added frequency, cpp, cost_per_action_type columns - All field schema validation tests now pass - Test dynamically extracts fields from scheduled_grabber.py source code - Compares against actual database schema from db_schema.sql - Properly filters metadata-only fields (campaign_id, campaign_name, etc.) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
3.1 KiB
Tests
This directory contains tests for the meta_api_grabber project.
Running Tests
Install test dependencies:
uv sync --extra test
Run all tests:
uv run pytest
Run specific test file:
uv run pytest tests/test_field_schema_validation.py -v
Run with verbose output:
uv run pytest tests/test_field_schema_validation.py -v -s
Test Files
test_field_schema_validation.py (Integration Test)
This is a critical integration test that validates all fields requested by the grab_* methods in scheduled_grabber.py exist in the actual database schema.
What it does:
- Parses
db_schema.sqlto extract actual table columns - Checks fields requested by each grab method:
grab_account_insights()→account_insightstablegrab_campaign_insights()→campaign_insightstablegrab_adset_insights()→adset_insightstablegrab_campaign_insights_by_country()→campaign_insights_by_countrytable
- Verifies all requested fields exist in the corresponding database table
Why this test is important: When new fields are added to the Meta API field lists, this test quickly alerts you if the corresponding database columns need to be added. Since fields are only added (never removed), the test helps catch schema mismatches early.
Test methods:
test_account_insights_fields()- Validates account-level insight fieldstest_campaign_insights_fields()- Validates campaign-level insight fieldstest_adset_insights_fields()- Validates ad set-level insight fieldstest_campaign_insights_by_country_fields()- Validates country breakdown fieldstest_all_tables_exist()- Ensures all required insight tables existtest_schema_documentation()- Prints out the parsed schema for reference
Output example:
Table: account_insights
Columns (17): account_id, actions, clicks, cost_per_action_type, cpc, cpm, cpp, ctr, ...
Table: campaign_insights
Columns (15): account_id, actions, campaign_id, clicks, cpc, cpm, ctr, ...
Writing Tests
Use markers to categorize tests:
@pytest.mark.unit
def test_something():
pass
@pytest.mark.integration
async def test_database_connection():
pass
Run only unit tests:
uv run pytest -m unit
Run everything except integration tests:
uv run pytest -m "not integration"
Schema Validation Workflow
When you add new fields to a grab method:
-
Add fields to
scheduled_grabber.py:fields = [ ... AdsInsights.Field.new_field, # New field added ] -
Run tests to see what's missing:
uv run pytest tests/test_field_schema_validation.py -v -s -
Test output will show:
adset_insights table missing columns: {'new_field'} Available: [account_id, actions, adset_id, ...] -
Update
db_schema.sqlwith the new column:ALTER TABLE adset_insights ADD COLUMN IF NOT EXISTS new_field TYPE; -
Run tests again to verify:
uv run pytest tests/test_field_schema_validation.py -v