# profiling/test_harness_self_check.py from __future__ import annotations import json from pathlib import Path import pytest from profiling.harness import append_profiling_history from profiling.harness import require_postgres from profiling.harness import run_profile from profiling.seed import seed_permission_dataset @pytest.mark.profiling @pytest.mark.django_db def test_seed_medium_scale_produces_expected_counts() -> None: require_postgres() data = seed_permission_dataset(scale="medium") assert len(data.documents) == 20_000 assert len(data.tags) == 100 assert len(data.correspondents) == 300 assert len(data.document_types) == 50 assert len(data.storage_paths) == 20 from guardian.models import GroupObjectPermission from guardian.models import UserObjectPermission user_perm_count = UserObjectPermission.objects.count() group_perm_count = GroupObjectPermission.objects.count() # within 5% of the ratio-derived target -- exact counts depend on random # sampling without replacement, see seed.py assert 2_242 <= user_perm_count <= 2_478 assert 43_111 <= group_perm_count <= 47_649 @pytest.mark.profiling @pytest.mark.django_db def test_run_profile_reports_query_count_and_timing() -> None: require_postgres() def trivial() -> list[int]: from documents.models import Document return list(Document.objects.values_list("id", flat=True)[:1]) result = run_profile(trivial, repeat=3) assert result.best_seconds >= 0 assert len(result.all_seconds) == 3 assert result.query_count >= 1 def test_append_profiling_history_writes_a_valid_json_line() -> None: history_path = Path(__file__).parent / "results" / "history.jsonl" before_lines = ( history_path.read_text().splitlines() if history_path.exists() else [] ) append_profiling_history( stage="self-check", scenario="test_append_profiling_history_writes_a_valid_json_line", best_seconds=0.001, query_count=0, scale="medium", code_ref="test-run", ) after_lines = history_path.read_text().splitlines() assert len(after_lines) == len(before_lines) + 1 entry = json.loads(after_lines[-1]) assert entry["stage"] == "self-check" assert entry["code_ref"] == "test-run" assert entry["scale"] == "medium" assert "timestamp" in entry