Coverage for tests/conftest.py: 96%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-08 00:02 +0000

1import shutil 1fabcde

2import subprocess 1fabcde

3import sys 1fabcde

4from pathlib import Path 1fabcde

5from typing import Any, Callable, Dict, List, Union 1fabcde

6 

7import pytest 1fabcde

8from pydantic import BaseModel 1fabcde

9from sqlmodel import SQLModel 1fabcde

10from sqlmodel._compat import IS_PYDANTIC_V2 1fabcde

11from sqlmodel.main import default_registry 1fabcde

12 

13top_level_path = Path(__file__).resolve().parent.parent 1fabcde

14docs_src_path = top_level_path / "docs_src" 1fabcde

15 

16 

17@pytest.fixture() 1fabcde

18def clear_sqlmodel(): 1abcde

19 # Clear the tables in the metadata for the default base model 

20 SQLModel.metadata.clear() 1fabcde

21 # Clear the Models associated with the registry, to avoid warnings 

22 default_registry.dispose() 1fabcde

23 yield 1fabcde

24 SQLModel.metadata.clear() 1fabcde

25 default_registry.dispose() 1fabcde

26 

27 

28@pytest.fixture() 1fabcde

29def cov_tmp_path(tmp_path: Path): 1fabcde

30 yield tmp_path 1fabcde

31 for coverage_path in tmp_path.glob(".coverage*"): 1fabcde

32 coverage_destiny_path = top_level_path / coverage_path.name 

33 shutil.copy(coverage_path, coverage_destiny_path) 

34 

35 

36def coverage_run(*, module: str, cwd: Union[str, Path]) -> subprocess.CompletedProcess: 1fabcde

37 result = subprocess.run( 1fabcde

38 [ 

39 "coverage", 

40 "run", 

41 "--parallel-mode", 

42 "--source=docs_src,tests,sqlmodel", 

43 "-m", 

44 module, 

45 ], 

46 cwd=str(cwd), 

47 capture_output=True, 

48 encoding="utf-8", 

49 ) 

50 return result 1fabcde

51 

52 

53def get_testing_print_function( 1abcde

54 calls: List[List[Union[str, Dict[str, Any]]]], 

55) -> Callable[..., Any]: 

56 def new_print(*args): 1fabcde

57 data = [] 1fabcde

58 for arg in args: 1fabcde

59 if isinstance(arg, BaseModel): 1fabcde

60 data.append(arg.model_dump()) 1fabcde

61 elif isinstance(arg, list): 1fabcde

62 new_list = [] 1fabcde

63 for item in arg: 1fabcde

64 if isinstance(item, BaseModel): 1fabcde

65 new_list.append(item.model_dump()) 1fabcde

66 data.append(new_list) 1fabcde

67 else: 

68 data.append(arg) 1fabcde

69 calls.append(data) 1fabcde

70 

71 return new_print 1fabcde

72 

73 

74needs_pydanticv2 = pytest.mark.skipif(not IS_PYDANTIC_V2, reason="requires Pydantic v2") 1fabcde

75needs_pydanticv1 = pytest.mark.skipif(IS_PYDANTIC_V2, reason="requires Pydantic v1") 1fabcde

76 

77needs_py39 = pytest.mark.skipif(sys.version_info < (3, 9), reason="requires python3.9+") 1fabcde

78needs_py310 = pytest.mark.skipif( 1fabcde

79 sys.version_info < (3, 10), reason="requires python3.10+" 

80)