Coverage for tests/test_generation/test_models.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1from pathlib import Path 

2 

3import pytest 

4from pydantic import ValidationError 

5 

6from prisma._compat import ( 

7 PYDANTIC_V2, 

8 model_json, 

9 model_parse, 

10 model_parse_json, 

11) 

12from prisma.generator.models import Config, Module 

13 

14 

15def test_module_serialization() -> None: 

16 """Python module serialization to json""" 

17 path = Path(__file__).parent.parent.joinpath('scripts/partial_type_generator.py') 

18 module = model_parse(Module, {'spec': str(path)}) 

19 assert model_parse_json(Module, model_json(module)).spec.name == module.spec.name 

20 

21 

22def test_recursive_type_depth() -> None: 

23 """Recursive type depth option disallows values that are less than 2 and do not equal -1.""" 

24 for value in [-2, -3, 0, 1]: 

25 with pytest.raises(ValidationError) as exc: 

26 Config(recursive_type_depth=value) 

27 

28 assert exc.match('Value must equal -1 or be greater than 1.') 

29 

30 with pytest.raises(ValidationError) as exc: 

31 Config( 

32 recursive_type_depth='a' # pyright: ignore[reportArgumentType] 

33 ) 

34 

35 if PYDANTIC_V2: 

36 assert exc.match('Input should be a valid integer, unable to parse string as an integer') 

37 else: 

38 assert exc.match('value is not a valid integer') 

39 

40 for value in [-1, 2, 3, 10, 99]: 

41 config = Config(recursive_type_depth=value) 

42 assert config.recursive_type_depth == value 

43 

44 

45def test_default_recursive_type_depth( 

46 capsys: pytest.CaptureFixture[str], 

47) -> None: 

48 """Warn when recursive type depth is not set: 

49 

50 https://github.com/RobertCraigie/prisma-client-py/issues/252 

51 

52 Ensure that we provide advice on what value to use and that it defaults to 5. 

53 

54 Also validate that when a type depth is provided, no warning is shown. 

55 """ 

56 c = Config() 

57 captured = capsys.readouterr() 

58 assert 'it is highly recommended to use Pyright' in captured.out.replace('\n', ' ') 

59 assert c.recursive_type_depth == 5 

60 

61 c = Config(recursive_type_depth=5) 

62 captured = capsys.readouterr() 

63 assert 'it is highly recommended to use Pyright' not in captured.out.replace('\n', ' ') 

64 assert c.recursive_type_depth == 5 

65 

66 c = Config(recursive_type_depth=2) 

67 captured = capsys.readouterr() 

68 assert 'it is highly recommended to use Pyright' not in captured.out.replace('\n', ' ') 

69 assert c.recursive_type_depth == 2