architect/_archive/2025-11-13-before-restructure/platform-projects/projects/cifra/14_TESTING_AND_QUALITY.md

CIFRA — Тестирование и качество

Версия: 2.0.0
Дата: 2025-11-10


Testing Pyramid

      E2E (5%)
        ↑
   Integration (15%)
        ↑
     Unit (80%)

Unit Tests

import pytest
from cifra.models import Contact

def test_contact_creation():
    contact = Contact(
        name="John Doe",
        email="john@example.com"
    )
    assert contact.name == "John Doe"
    assert contact.email == "john@example.com"

@pytest.mark.parametrize("email,valid", [
    ("john@example.com", True),
    ("invalid", False),
])
def test_email_validation(email, valid):
    try:
        contact = Contact(name="John", email=email)
        assert valid
    except ValidationError:
        assert not valid

Integration Tests

import pytest
from cifra.testing import TestClient, TestDatabase

@pytest.fixture
async def db():
    async with TestDatabase() as db:
        yield db

async def test_create_contact_api(client, db):
    response = await client.post("/api/contacts", json={
        "name": "John Doe",
        "email": "john@example.com"
    })
    assert response.status_code == 201

    # Verify in database
    contact = await Contact.get(response.json()['id'])
    assert contact.name == "John Doe"

E2E Tests (Playwright)

from playwright.async_api import async_playwright

async def test_login_flow():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()

        await page.goto("http://localhost:8000/login")
        await page.fill('[name="email"]', "user@example.com")
        await page.fill('[name="password"]', "password")
        await page.click('button[type="submit"]')

        await page.wait_for_url("**/dashboard")
        assert "/dashboard" in page.url

Performance Guide

Target metrics

API Response Time (p95): < 200ms
Database Query (p95): < 50ms
Page Load (p50): < 1s
Throughput: > 1000 RPS

Database optimization

# ❌ BAD (N+1)
contacts = await Contact.list()
for contact in contacts:
    print(contact.company.name)  # +1 query each!

# ✅ GOOD (2 queries)
from sqlalchemy.orm import selectinload
contacts = await Contact.list(
    options=[selectinload(Contact.company)]
)

Caching

@cached(ttl=600)
async def get_contact(contact_id):
    return await Contact.get(contact_id)

Monitoring

monitoring:
  prometheus:
    enabled: true
    port: 9090

  grafana:
    enabled: true
    port: 3000

Следующий документ: REFERENCES.md →