nordabiz/tests/migration/test_migrations.py
Maciej Pienczyn a57187e05f
Some checks are pending
NordaBiz Tests / Unit & Integration Tests (push) Waiting to run
NordaBiz Tests / E2E Tests (Playwright) (push) Blocked by required conditions
NordaBiz Tests / Smoke Tests (Production) (push) Blocked by required conditions
NordaBiz Tests / Send Failure Notification (push) Blocked by required conditions
test: Add comprehensive testing infrastructure
- pytest framework with fixtures for auth (auth_client, admin_client)
- Unit tests for SearchService
- Integration tests for auth flow
- Security tests (OWASP Top 10: SQL injection, XSS, CSRF)
- Smoke tests for production health and backup monitoring
- E2E tests with Playwright (basic structure)
- DR tests for backup/restore procedures
- GitHub Actions CI/CD workflow (.github/workflows/test.yml)
- Coverage configuration (.coveragerc) with 80% minimum
- DR documentation and restore script

Staging environment: VM 248, staging.nordabiznes.pl

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 07:52:34 +01:00

109 lines
3.5 KiB
Python

"""
Database migration tests
=========================
Verify that SQL migrations preserve data integrity.
"""
import os
import subprocess
from pathlib import Path
import pytest
pytestmark = pytest.mark.migration
# Project paths
PROJECT_ROOT = Path(__file__).parent.parent.parent
MIGRATIONS_DIR = PROJECT_ROOT / 'database' / 'migrations'
class TestMigrationFiles:
"""Tests for migration file structure."""
def test_migrations_directory_exists(self):
"""Migrations directory should exist."""
assert MIGRATIONS_DIR.exists(), f"Migrations directory not found: {MIGRATIONS_DIR}"
def test_migration_files_have_sequence(self):
"""Migration files should have sequence numbers."""
if not MIGRATIONS_DIR.exists():
pytest.skip("Migrations directory not found")
migration_files = list(MIGRATIONS_DIR.glob('*.sql'))
for f in migration_files:
# Files should start with number like 001_ or 20240101_
name = f.stem
assert name[0].isdigit(), f"Migration {f.name} should start with sequence number"
def test_migration_files_are_valid_sql(self):
"""Migration files should be valid SQL."""
if not MIGRATIONS_DIR.exists():
pytest.skip("Migrations directory not found")
migration_files = list(MIGRATIONS_DIR.glob('*.sql'))
for f in migration_files:
content = f.read_text()
# Should not be empty
assert content.strip(), f"Migration {f.name} is empty"
# Should not have obvious syntax errors
assert content.count('(') == content.count(')'), f"Unbalanced parentheses in {f.name}"
class TestMigrationRunner:
"""Tests for migration runner script."""
def test_migration_runner_exists(self):
"""Migration runner script should exist."""
runner = PROJECT_ROOT / 'scripts' / 'run_migration.py'
assert runner.exists(), f"Migration runner not found: {runner}"
def test_migration_runner_has_dry_run(self):
"""Migration runner should have dry-run option."""
runner = PROJECT_ROOT / 'scripts' / 'run_migration.py'
if not runner.exists():
pytest.skip("Migration runner not found")
content = runner.read_text()
assert 'dry' in content.lower() or 'preview' in content.lower(), \
"Migration runner should have dry-run option"
class TestMigrationIntegrity:
"""Integration tests for migration integrity."""
@pytest.mark.slow
def test_migration_preserves_company_count(self, app, db):
"""Migration should not change company count."""
# This test would need a staging database
with app.app_context():
# Get current count
from database import Company
count_before = db.session.query(Company).count()
# Here you would run migration
# run_migration("test_migration.sql")
# Verify count unchanged
count_after = db.session.query(Company).count()
assert count_before == count_after, "Migration changed company count"
@pytest.mark.slow
def test_migration_preserves_user_count(self, app, db):
"""Migration should not change user count."""
with app.app_context():
from database import User
count_before = db.session.query(User).count()
# Run migration here
count_after = db.session.query(User).count()
assert count_before == count_after