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

1import sys 

2import subprocess 

3 

4import pytest 

5from prisma_cleanup import cleanup 

6 

7from prisma.generator import BASE_PACKAGE_DIR 

8from prisma.generator.utils import copy_tree 

9 

10from .utils import assert_module_is_clean, assert_module_not_clean 

11from ..utils import Testdir 

12 

13 

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) 

19 

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) 

27 

28 

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!")') 

35 

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 ) 

43 

44 assert 'Hello, there!' in exc.value.stderr.decode('utf-8') 

45 

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) 

54 

55 

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) 

61 

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) 

69 

70 

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() 

76 

77 with pytest.raises(RuntimeError) as exc: 

78 cleanup('prisma_custom') 

79 

80 assert exc.value.args[0] == 'The given package does not appear to be a Prisma Client Python package.' 

81 

82 

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') 

87 

88 assert exc.value.args[0] == 'Could not resolve package: prisma_custom'