|
| 1 | +"""Tests for SchemaForge schema consistency checker (check.py).""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import os |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
| 10 | + |
| 11 | +from schemaforge.check import check_directory, detect_format |
| 12 | + |
| 13 | +# ── detect_format tests ── |
| 14 | + |
| 15 | +def test_detect_format_sql(): |
| 16 | + assert detect_format("schema.sql") == "sql" |
| 17 | + |
| 18 | + |
| 19 | +def test_detect_format_prisma(): |
| 20 | + assert detect_format("schema.prisma") == "prisma" |
| 21 | + |
| 22 | + |
| 23 | +def test_detect_format_drizzle(): |
| 24 | + assert detect_format("schema.ts") == "drizzle" |
| 25 | + assert detect_format("schema.tsx") == "drizzle" |
| 26 | + |
| 27 | + |
| 28 | +def test_detect_format_django(): |
| 29 | + assert detect_format("models.py") == "django" |
| 30 | + |
| 31 | + |
| 32 | +def test_detect_format_json_schema(): |
| 33 | + assert detect_format("schema.json") == "json_schema" |
| 34 | + |
| 35 | + |
| 36 | +def test_detect_format_graphql(): |
| 37 | + assert detect_format("schema.graphql") == "graphql" |
| 38 | + assert detect_format("schema.gql") == "graphql" |
| 39 | + |
| 40 | + |
| 41 | +def test_detect_format_unknown(): |
| 42 | + assert detect_format("readme.md") is None |
| 43 | + |
| 44 | + |
| 45 | +def test_detect_format_alembic(): |
| 46 | + """Alembic .py files are detected as Django by extension.""" |
| 47 | + assert detect_format("migration.py") == "django" |
| 48 | + |
| 49 | + |
| 50 | +# ── check_directory tests ── |
| 51 | + |
| 52 | +SAMPLE_SQL = """CREATE TABLE users ( |
| 53 | + id INTEGER PRIMARY KEY, |
| 54 | + name VARCHAR(100) NOT NULL |
| 55 | +); |
| 56 | +""" |
| 57 | + |
| 58 | +SAMPLE_PRISMA = """generator client { |
| 59 | + provider = "prisma-client-js" |
| 60 | +} |
| 61 | +
|
| 62 | +datasource db { |
| 63 | + provider = "postgresql" |
| 64 | + url = env("DATABASE_URL") |
| 65 | +} |
| 66 | +
|
| 67 | +model users { |
| 68 | + id Int @id @default(autoincrement()) |
| 69 | + name String @db.VarChar(100) |
| 70 | +} |
| 71 | +""" |
| 72 | + |
| 73 | + |
| 74 | +def test_check_directory_empty(): |
| 75 | + """Empty directory should report need at least 2 files.""" |
| 76 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 77 | + result = check_directory(tmpdir) |
| 78 | + assert "Need at least 2 schema files" in result |
| 79 | + |
| 80 | + |
| 81 | +def test_check_directory_single_file(): |
| 82 | + """Single file directory should report need at least 2 files.""" |
| 83 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 84 | + Path(tmpdir, "schema.sql").write_text(SAMPLE_SQL) |
| 85 | + result = check_directory(tmpdir) |
| 86 | + assert "Need at least 2 schema files" in result |
| 87 | + |
| 88 | + |
| 89 | +def test_check_directory_consistent_files(): |
| 90 | + """Files that produce equivalent schemas should pass.""" |
| 91 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 92 | + Path(tmpdir, "schema.sql").write_text(SAMPLE_SQL) |
| 93 | + Path(tmpdir, "schema.prisma").write_text(SAMPLE_PRISMA) |
| 94 | + |
| 95 | + result = check_directory(tmpdir) |
| 96 | + # The check might find minor differences (nullable, naming), but |
| 97 | + # the output should mention both files and attempt comparison |
| 98 | + assert "Files found: 2" in result |
| 99 | + assert "schema.sql" in result or "schema" in result |
| 100 | + |
| 101 | + |
| 102 | +def test_check_directory_not_a_directory(): |
| 103 | + """Non-directory path should raise NotADirectoryError.""" |
| 104 | + import pytest |
| 105 | + with tempfile.NamedTemporaryFile() as f: |
| 106 | + with pytest.raises(NotADirectoryError): |
| 107 | + check_directory(f.name) |
| 108 | + |
| 109 | + |
| 110 | +def test_check_directory_ignores_non_schema_files(): |
| 111 | + """Non-schema files should be ignored by the check.""" |
| 112 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 113 | + Path(tmpdir, "schema.sql").write_text(SAMPLE_SQL) |
| 114 | + Path(tmpdir, "readme.md").write_text("# Not a schema") |
| 115 | + Path(tmpdir, "data.csv").write_text("a,b,c") |
| 116 | + |
| 117 | + result = check_directory(tmpdir) |
| 118 | + assert "Need at least 2" in result |
| 119 | + assert "found 1" in result |
| 120 | + |
| 121 | + |
| 122 | +def test_check_directory_canonical_format(): |
| 123 | + """--canonical option should change the comparison target format.""" |
| 124 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 125 | + Path(tmpdir, "schema.sql").write_text(SAMPLE_SQL) |
| 126 | + Path(tmpdir, "schema.prisma").write_text(SAMPLE_PRISMA) |
| 127 | + |
| 128 | + result = check_directory(tmpdir, canonical="prisma") |
| 129 | + assert "compared via prisma" in result |
0 commit comments