summaryrefslogtreecommitdiff
path: root/tests/test_stubs.py
blob: 583101395dad19df5de1b3c24798c349ec1b7334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import difflib
from pathlib import Path
import subprocess
import sys

import alembic
from alembic.testing import combinations
from alembic.testing import eq_
from alembic.testing import TestBase

_home = Path(__file__).parent.parent


def run_command(file):
    res = subprocess.run(
        [
            sys.executable,
            str((_home / "tools" / "write_pyi.py").relative_to(_home)),
            "--stdout",
            "--name",
            file,
        ],
        stdout=subprocess.PIPE,
        cwd=_home,
        encoding="utf-8",
    )
    return res


class TestStubFiles(TestBase):
    __requires__ = ("stubs_test",)

    def test_op_pyi(self):
        res = run_command("op")
        generated = res.stdout
        file_path = Path(alembic.__file__).parent / "op.pyi"
        expected = file_path.read_text()
        eq_(generated, expected, compare(generated, expected))

    def test_context_pyi(self):
        res = run_command("context")
        generated = res.stdout
        file_path = Path(alembic.__file__).parent / "context.pyi"
        expected = file_path.read_text()
        eq_(generated, expected, compare(generated, expected))

    @combinations("batch_op", "op_cls")
    def test_operation_base_file(self, name):
        res = run_command(name)
        generated = res.stdout
        file_path = Path(alembic.__file__).parent / "operations/base.py"
        expected = file_path.read_text()
        eq_(generated, expected, compare(generated, expected))


def compare(actual: str, expected: str):
    diff = difflib.unified_diff(
        actual.splitlines(),
        expected.splitlines(),
        fromfile="generated",
        tofile="expected",
    )
    return "\n".join(diff)