Coverage for tests/test_generation/test_prisma_cleanup.py: 100%
44 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import sys
2import subprocess
4import pytest
5from prisma_cleanup import cleanup
7from prisma.generator import BASE_PACKAGE_DIR
8from prisma.generator.utils import copy_tree
10from .utils import assert_module_is_clean, assert_module_not_clean
11from ..utils import Testdir
14def test_main(testdir: Testdir) -> None:
15 """Main entrypoint"""
16 path = testdir.path / 'prisma'
17 assert not path.exists()
18 copy_tree(BASE_PACKAGE_DIR, path)
20 assert_module_not_clean(path)
21 subprocess.run(
22 [sys.executable, '-m', 'prisma_cleanup'],
23 check=True,
24 stdout=subprocess.PIPE,
25 )
26 assert_module_is_clean(path)
29def test_main_works_with_erroneous_client(testdir: Testdir) -> None:
30 """The main entrypoint works regardless of any errors in the prisma/client.py file"""
31 path = testdir.path / 'prisma'
32 assert not path.exists()
33 copy_tree(BASE_PACKAGE_DIR, path)
34 (path / 'client.py').write_text('raise RuntimeError("Hello, there!")')
36 # ensure importing would fail
37 with pytest.raises(subprocess.CalledProcessError) as exc:
38 subprocess.run(
39 ['python', '-c', 'import prisma'],
40 check=True,
41 stderr=subprocess.PIPE,
42 )
44 assert 'Hello, there!' in exc.value.stderr.decode('utf-8')
46 # ensure cleanup works
47 assert_module_not_clean(path)
48 subprocess.run(
49 [sys.executable, '-m', 'prisma_cleanup'],
50 check=True,
51 stdout=subprocess.PIPE,
52 )
53 assert_module_is_clean(path)
56def test_custom_package(testdir: Testdir) -> None:
57 """Cleaning up custom package location"""
58 path = testdir.path / 'app' / 'prisma'
59 assert not path.exists()
60 copy_tree(BASE_PACKAGE_DIR, path)
62 assert_module_not_clean(path)
63 subprocess.run(
64 [sys.executable, '-m', 'prisma_cleanup', 'app.prisma'],
65 check=True,
66 stdout=subprocess.PIPE,
67 )
68 assert_module_is_clean(path)
71def test_cleanup_non_prisma_package(testdir: Testdir) -> None:
72 """Cleaning up a non Prisma package does not work"""
73 path = testdir.path / 'prisma_custom' / '__init__.py'
74 path.parent.mkdir()
75 path.touch()
77 with pytest.raises(RuntimeError) as exc:
78 cleanup('prisma_custom')
80 assert exc.value.args[0] == 'The given package does not appear to be a Prisma Client Python package.'
83def test_cleanup_package_not_found(testdir: Testdir) -> None:
84 """Cleaning up a non-existent package does not work"""
85 with pytest.raises(RuntimeError) as exc:
86 cleanup('prisma_custom')
88 assert exc.value.args[0] == 'Could not resolve package: prisma_custom'