forked from pydantic/pydantic-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
114 lines (84 loc) · 3.29 KB
/
conftest.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import annotations as _annotations
import functools
import importlib.util
import json
import os
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Type
import hypothesis
import pytest
from typing_extensions import Literal
from pydantic_core import SchemaValidator
__all__ = 'Err', 'PyAndJson', 'plain_repr', 'infinite_generator'
hypothesis.settings.register_profile('fast', max_examples=2)
hypothesis.settings.register_profile('slow', max_examples=1_000)
hypothesis.settings.load_profile(os.getenv('HYPOTHESIS_PROFILE', 'fast'))
def plain_repr(obj):
r = repr(obj)
r = re.sub(r',\s*([)}])', r'\1', r)
r = re.sub(r'\s+', '', r)
return r
@dataclass
class Err:
message: str
errors: Any | None = None
def __repr__(self):
if self.errors:
return f'Err({self.message!r}, errors={self.errors!r})'
else:
return f'Err({self.message!r})'
class PyAndJsonValidator:
def __init__(self, schema, validator_type: Literal['json', 'python'] | None = None):
self.validator = SchemaValidator(schema)
self.validator_type = validator_type
def validate_python(self, py_input, strict: bool | None = None, context: Any = None):
return self.validator.validate_python(py_input, strict, context)
def validate_test(self, py_input, strict: bool | None = None, context: Any = None):
if self.validator_type == 'json':
return self.validator.validate_json(json.dumps(py_input), strict, context)
else:
assert self.validator_type == 'python', self.validator_type
return self.validator.validate_python(py_input, strict, context)
def isinstance_test(self, py_input, strict: bool | None = None, context: Any = None):
if self.validator_type == 'json':
return self.validator.isinstance_json(json.dumps(py_input), strict, context)
else:
assert self.validator_type == 'python', self.validator_type
return self.validator.isinstance_python(py_input, strict, context)
PyAndJson = Type[PyAndJsonValidator]
@pytest.fixture(params=['python', 'json'])
def py_and_json(request) -> PyAndJson:
class ChosenPyAndJsonValidator(PyAndJsonValidator):
__init__ = functools.partialmethod(PyAndJsonValidator.__init__, validator_type=request.param)
return ChosenPyAndJsonValidator
@pytest.fixture
def tmp_work_path(tmp_path: Path):
"""
Create a temporary working directory.
"""
previous_cwd = Path.cwd()
os.chdir(tmp_path)
yield tmp_path
os.chdir(previous_cwd)
@pytest.fixture
def import_execute(request, tmp_work_path: Path):
def _import_execute(source: str, *, custom_module_name: 'str | None' = None):
module_name = custom_module_name or request.node.name
module_path = tmp_work_path / f'{module_name}.py'
module_path.write_text(source)
spec = importlib.util.spec_from_file_location('__main__', str(module_path))
module = importlib.util.module_from_spec(spec)
try:
spec.loader.exec_module(module)
except KeyboardInterrupt:
print('KeyboardInterrupt')
else:
return module
return _import_execute
def infinite_generator():
i = 0
while True:
yield i
i += 1