mockable 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mockable/__init__.py
ADDED
mockable/decorator.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from functools import wraps
|
|
2
|
+
import json
|
|
3
|
+
from typing import Optional, Type
|
|
4
|
+
from .scenario_mock import MockConfig, TestCaseType
|
|
5
|
+
|
|
6
|
+
__all__ = ["mockable", "set_mock_config"]
|
|
7
|
+
|
|
8
|
+
_current_config = None
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def set_mock_config(config: Optional[MockConfig]):
|
|
12
|
+
global _current_config
|
|
13
|
+
_current_config = config
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def mockable(func):
|
|
17
|
+
@wraps(func)
|
|
18
|
+
def wrapper(*args, **kwargs):
|
|
19
|
+
# Определяем, нужно ли использовать мок
|
|
20
|
+
|
|
21
|
+
if not _current_config:
|
|
22
|
+
return func(*args, **kwargs)
|
|
23
|
+
|
|
24
|
+
# Мок-логика
|
|
25
|
+
method_name = func.__name__
|
|
26
|
+
method_cfg = _current_config.methods.get(method_name)
|
|
27
|
+
|
|
28
|
+
if not method_cfg:
|
|
29
|
+
return func(*args, **kwargs)
|
|
30
|
+
|
|
31
|
+
case = _current_config.active_cases.get(method_name, TestCaseType.POSITIVE)
|
|
32
|
+
fixture = method_cfg.get_fixture(case)
|
|
33
|
+
|
|
34
|
+
if fixture.file_path:
|
|
35
|
+
with open(fixture.file_path, "r", encoding="UTF-8") as f:
|
|
36
|
+
return json.load(f)
|
|
37
|
+
|
|
38
|
+
if fixture.exception:
|
|
39
|
+
raise fixture.exception
|
|
40
|
+
|
|
41
|
+
if fixture.side_effect:
|
|
42
|
+
return fixture.side_effect(*args, **kwargs)
|
|
43
|
+
|
|
44
|
+
return fixture.result
|
|
45
|
+
|
|
46
|
+
return wrapper
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def mockable_class(cls: Type) -> Type:
|
|
50
|
+
for attr_name, attr_value in cls.__dict__.items():
|
|
51
|
+
if callable(attr_value) and not isinstance(
|
|
52
|
+
attr_value, (property, staticmethod, classmethod)
|
|
53
|
+
):
|
|
54
|
+
setattr(cls, attr_name, mockable(attr_value))
|
|
55
|
+
elif isinstance(attr_value, staticmethod):
|
|
56
|
+
original_func = attr_value.__func__
|
|
57
|
+
setattr(cls, attr_name, staticmethod(mockable(original_func)))
|
|
58
|
+
elif isinstance(attr_value, classmethod):
|
|
59
|
+
original_func = attr_value.__func__
|
|
60
|
+
setattr(cls, attr_name, classmethod(mockable(original_func)))
|
|
61
|
+
|
|
62
|
+
return cls
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Any, Callable, Optional
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"MockConfig",
|
|
7
|
+
"MethodMockConfig",
|
|
8
|
+
"MockFixture",
|
|
9
|
+
"TestCaseType",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TestCaseType(Enum):
|
|
14
|
+
POSITIVE = "positive"
|
|
15
|
+
NEGATIVE = "negative"
|
|
16
|
+
ERROR = "error"
|
|
17
|
+
SPECIAL = "special"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class MockFixture:
|
|
22
|
+
# Обычный хардкод результат
|
|
23
|
+
result: Any = None
|
|
24
|
+
# Когда в моке использует какая-то функциональность
|
|
25
|
+
side_effect: Optional[Callable[..., Any]] = None
|
|
26
|
+
# Для ERROR кейса, вызовет raise Exception
|
|
27
|
+
exception: Optional[Exception] = None
|
|
28
|
+
# Путь к json файлу с готовым моком
|
|
29
|
+
file_path: Optional[str] = None
|
|
30
|
+
# Описание мок данных
|
|
31
|
+
description: str = ""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class MethodMockConfig:
|
|
36
|
+
positive: MockFixture
|
|
37
|
+
|
|
38
|
+
negative: Optional[MockFixture] = None
|
|
39
|
+
|
|
40
|
+
special: Optional[MockFixture] = None
|
|
41
|
+
|
|
42
|
+
error: Optional[MockFixture] = None
|
|
43
|
+
|
|
44
|
+
def get_fixture(self, case: TestCaseType) -> MockFixture:
|
|
45
|
+
return {
|
|
46
|
+
TestCaseType.POSITIVE: self.positive,
|
|
47
|
+
TestCaseType.NEGATIVE: self.negative,
|
|
48
|
+
TestCaseType.SPECIAL: self.special,
|
|
49
|
+
TestCaseType.ERROR: self.error,
|
|
50
|
+
}.get(case) or self.positive
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class MockConfig:
|
|
55
|
+
active_cases: dict[str, TestCaseType] = field(default_factory=dict)
|
|
56
|
+
methods: dict[str, MethodMockConfig] = field(default_factory=dict)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mockable
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: mockable for ses
|
|
5
|
+
Author-email: Arimaqqe <beebo120103@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# mockable
|
|
16
|
+
|
|
17
|
+
Моя первая библиотека.
|
|
18
|
+
|
|
19
|
+
## Установка
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install mypackage
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
mockable/__init__.py,sha256=vu0vkUrcLrsjLMuq_j43AAXfUxKrcvPiNrMxg4b3Ps4,150
|
|
2
|
+
mockable/decorator.py,sha256=GTA8ePv2obnsz0KFyXdACdxWXKYpLj5uh9_8p-yoURU,1906
|
|
3
|
+
mockable/scenario_mock.py,sha256=5RTBpkB5v1M7GQkXX_kXAeaxMHVeWCagwJjyQCFUWvk,1590
|
|
4
|
+
mockable-0.0.1.dist-info/licenses/LICENSE,sha256=HBg3-RxyPz1UMsE2jnuhOv0spbXxIksqRlGn3tZa45g,1086
|
|
5
|
+
mockable-0.0.1.dist-info/METADATA,sha256=sBxfQWK9m8ixdxyLNejVU5rt2VUVzyc0o9fIyvDioPc,522
|
|
6
|
+
mockable-0.0.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
mockable-0.0.1.dist-info/top_level.txt,sha256=SQKkcNMPw8Nse2QPNdbY-Y46mvt0-TiU77L1mqPpeXA,9
|
|
8
|
+
mockable-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arimaqqe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mockable
|