Skip to content

Commit 33f4c68

Browse files
authored
Merge pull request #2 from constructive-io/devin/1769017225-add-ci-workflow
ci: Add GitHub Actions workflow and example test
2 parents ac74c2b + 570db4d commit 33f4c68

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

.github/workflows/test.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: pysql-test CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
pull_request:
9+
branches:
10+
- main
11+
- develop
12+
workflow_dispatch:
13+
14+
concurrency:
15+
group: ${{ github.workflow }}-${{ github.ref }}-run-tests
16+
cancel-in-progress: true
17+
18+
jobs:
19+
test:
20+
runs-on: ubuntu-latest
21+
22+
env:
23+
PGHOST: localhost
24+
PGPORT: 5432
25+
PGUSER: postgres
26+
PGPASSWORD: password
27+
28+
services:
29+
pg_db:
30+
image: ghcr.io/constructive-io/docker/postgres-plus:17
31+
env:
32+
POSTGRES_USER: postgres
33+
POSTGRES_PASSWORD: password
34+
options: >-
35+
--health-cmd pg_isready
36+
--health-interval 10s
37+
--health-timeout 5s
38+
--health-retries 5
39+
ports:
40+
- 5432:5432
41+
42+
steps:
43+
- name: Checkout
44+
uses: actions/checkout@v4
45+
46+
- name: Set up Python
47+
uses: actions/setup-python@v5
48+
with:
49+
python-version: "3.12"
50+
51+
- name: Install Poetry
52+
uses: snok/install-poetry@v1
53+
with:
54+
version: latest
55+
virtualenvs-create: true
56+
virtualenvs-in-project: true
57+
58+
- name: Install dependencies
59+
run: poetry install
60+
61+
- name: Run linting
62+
run: poetry run ruff check .
63+
64+
- name: Run type checking
65+
run: poetry run mypy src --ignore-missing-imports
66+
67+
- name: Run tests
68+
run: poetry run pytest -v

tests/test_example.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Simple example test demonstrating pysql-test usage.
3+
4+
This is a minimal example showing how to use the testing framework
5+
for PostgreSQL integration tests with automatic database isolation.
6+
"""
7+
8+
import pytest
9+
10+
from pysql_test import get_connections, seed
11+
12+
13+
@pytest.fixture
14+
def db():
15+
"""
16+
Create an isolated test database with sample schema.
17+
18+
Each test gets a fresh database that is automatically
19+
cleaned up after the test completes.
20+
"""
21+
conn = get_connections(
22+
seed_adapters=[
23+
seed.fn(lambda ctx: ctx["pg"].query("""
24+
CREATE TABLE users (
25+
id SERIAL PRIMARY KEY,
26+
name TEXT NOT NULL,
27+
email TEXT UNIQUE
28+
)
29+
"""))
30+
]
31+
)
32+
db = conn.db
33+
db.before_each()
34+
yield db
35+
db.after_each()
36+
conn.teardown()
37+
38+
39+
def test_insert_and_query_user(db):
40+
"""Test inserting and querying a user."""
41+
# Insert a user
42+
db.execute(
43+
"INSERT INTO users (name, email) VALUES (%s, %s)",
44+
("Alice", "alice@example.com"),
45+
)
46+
47+
# Query the user
48+
user = db.one("SELECT * FROM users WHERE name = %s", ("Alice",))
49+
50+
assert user["name"] == "Alice"
51+
assert user["email"] == "alice@example.com"
52+
53+
54+
def test_transaction_isolation(db):
55+
"""Test that changes are rolled back between tests."""
56+
# This insert will be rolled back after the test
57+
db.execute(
58+
"INSERT INTO users (name, email) VALUES (%s, %s)",
59+
("Bob", "bob@example.com"),
60+
)
61+
62+
# Verify the user exists within this test
63+
count = db.one("SELECT COUNT(*) as count FROM users")
64+
assert count["count"] == 1
65+
66+
67+
def test_empty_table_after_rollback(db):
68+
"""Verify previous test's data was rolled back."""
69+
# Table should be empty because previous test's insert was rolled back
70+
count = db.one("SELECT COUNT(*) as count FROM users")
71+
assert count["count"] == 0

0 commit comments

Comments
 (0)