writing-tests-pg-data
Instructions for writing tests for PG Data services, likely involving database fixtures and integration tests.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- writing-tests-pg-data
- description
- Instructions for writing tests for PG Data services, likely involving database fixtures and integration tests.
03 SKILL.md 正文
Writing PG Data Tests
If this plugin is not initialized or an Agent IX command fails, read the dev-tools setup guide for its prerequisites and local diagnosis.
This skill provides guidelines for writing tests for services using SQLModel and postgres, aligned with the Agent-IX Golden Path.
Source Spec: Derived from pg-data-service (SQLModel, Alembic).
Core Rules
- Framework: Use
pytest-sqlmodel. This plugin provides all necessary fixtures and database management. - Real Database: Tests must ALWAYS run against a real Postgres database managed by the
postgresfixture. Do not use SQLite/in-memory databases. - Isolation: The
db_sessionfixture provided bypytest-sqlmodelhandles transaction isolation (rollbacks) automatically. - Factories: Use helper functions (factories) to create test data rather than raw SQL inserts.
Fixtures
Standard fixtures used in conftest.py (provided by pytest-sqlmodel):
db_session: ASessionobject isolated by a transaction. Use this for DB interactions in tests.postgres: The underlyingtestcontainersor service-managed postgres instance URL.client: ATestClientwith the session dependency overridden to usedb_session.
Testing Models
Test that models can be created and retrieved.
class TestHeroModel:
"""Tests for the Hero model."""
def test_create_hero(self, session: Session):
"""
Description:
Test that a Hero can be created and persisted.
Assumptions:
- Database is available
- Session is active
Criteria:
- Hero is saved with a generated ID
- Fields match input values
Trace: FR-001-AC-1
"""
hero = Hero(name="Deadpond", secret_name="Dive Wilson")
session.add(hero)
session.commit()
session.refresh(hero)
assert hero.id is not None
assert hero.age is None
Testing API Endpoints
Test that endpoints interact correctly with the DB.
class TestHeroAPI:
"""Tests for the Hero API endpoints."""
def test_create_hero_api(self, client: TestClient):
"""
Description:
Test the create hero POST endpoint.
Assumptions:
- API is running
- Database connection is valid
Criteria:
- Returns 200 OK
- Response contains created hero with ID
"""
response = client.post(
"/heroes/",
json={"name": "Spider-Boy", "secret_name": "Pedro Parqueador"}
)
data = response.json()
assert response.status_code == 200
assert data["name"] == "Spider-Boy"
assert data["id"] is not None
04 同仓库更多 Skills
assurance-onboarding
Capture a generic handoff in the fixture host.
writing-tests-typescript
Instructions for writing TypeScript tests (Jest), including typed mocks and testing patterns.
writing-tests-react
Index to React component testing guides.
writing-tests-python
Instructions for writing Python tests (pytest), including mocking, fixtures, and golden path standards.
security-deepsec
Run a generic DeepSec-backed security review and remediation burndown for one or more target repositories. Use when asked to audit a repo, app area, service family, frontend/backend/client/SDK surface, infrastructure, or security-sensitive workflow with DeepSec; when asked to create local audit artifacts, GitHub security issues, checksite coverage, finding reports, spec/AC/test traceability, or a repeatable security burndown process.
rust-style
Agent IX Rust conventions for every Rust coding and review task. Load this baseline even when the repository has additional Rust guidance; read the linked API, implementation, safety, boundary, testing, and Tokio references for the work at hand.