pytest-pickle-cache 0.1.0__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.
File without changes
@@ -0,0 +1,38 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import binascii
5
+ import pickle
6
+ from typing import TYPE_CHECKING
7
+
8
+ import pytest
9
+
10
+ if TYPE_CHECKING:
11
+ from collections.abc import Callable
12
+ from typing import Any
13
+
14
+
15
+ @pytest.fixture(scope="session")
16
+ def use_cache(request: pytest.FixtureRequest) -> Callable[[str, Callable], Any]:
17
+ """A pytest fixture to use cache.
18
+
19
+ This fixture provides a caching mechanism for pytest, allowing you to
20
+ store and retrieve objects using a specified key. The objects are serialized
21
+ and deserialized using pickle and base64 encoding.
22
+ """
23
+
24
+ def use_cache(key: str, create: Callable[[], object]) -> Any:
25
+ try:
26
+ if value := request.config.cache.get(key, None):
27
+ return pickle.loads(base64.b64decode(value))
28
+ except (pickle.UnpicklingError, binascii.Error):
29
+ request.config.cache.set(key, None)
30
+
31
+ obj = create()
32
+ value = base64.b64encode(pickle.dumps(obj)).decode("utf-8")
33
+
34
+ request.config.cache.set(key, value)
35
+
36
+ return obj
37
+
38
+ return use_cache
@@ -0,0 +1,154 @@
1
+ Metadata-Version: 2.4
2
+ Name: pytest-pickle-cache
3
+ Version: 0.1.0
4
+ Summary: A pytest plugin for caching test results using pickle.
5
+ Project-URL: Documentation, https://daizutabi.github.io/pytest-pickle-cache/
6
+ Project-URL: Source, https://github.com/daizutabi/pytest-pickle-cache
7
+ Project-URL: Issues, https://github.com/daizutabi/pytest-pickle-cache/issues
8
+ Author-email: daizutabi <daizutabi@gmail.com>
9
+ License: MIT License
10
+
11
+ Copyright (c) 2020-present daizutabi <daizutabi@gmail.com>
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
+ License-File: LICENSE
19
+ Classifier: Framework :: Pytest
20
+ Classifier: Intended Audience :: Developers
21
+ Classifier: License :: OSI Approved :: MIT License
22
+ Classifier: Programming Language :: Python
23
+ Classifier: Programming Language :: Python :: 3.10
24
+ Classifier: Programming Language :: Python :: 3.11
25
+ Classifier: Programming Language :: Python :: 3.12
26
+ Classifier: Programming Language :: Python :: 3.13
27
+ Classifier: Topic :: Software Development :: Testing
28
+ Requires-Python: >=3.10
29
+ Requires-Dist: pytest>=7
30
+ Description-Content-Type: text/markdown
31
+
32
+ # pytest-pickle-cache
33
+
34
+ [![PyPI Version][pypi-v-image]][pypi-v-link]
35
+ [![Python Version][python-v-image]][python-v-link]
36
+ [![Build Status][GHAction-image]][GHAction-link]
37
+ [![Coverage Status][codecov-image]][codecov-link]
38
+
39
+ ## Overview
40
+
41
+ `pytest-pickle-cache` is a pytest plugin for caching test results using pickle.
42
+ By utilizing this plugin, you can reduce test execution time and perform tests
43
+ more efficiently.
44
+
45
+ ## Installation
46
+
47
+ You can install `pytest-pickle-cache` using the following command:
48
+
49
+ ```bash
50
+ pip install pytest-pickle-cache
51
+ ```
52
+
53
+ ## Fixture
54
+
55
+ The `use_cache` fixture is a pytest fixture that provides a caching mechanism
56
+ for pytest, allowing you to store and retrieve objects using a specified key.
57
+ The objects are serialized and deserialized using pickle and base64 encoding.
58
+
59
+ ```python
60
+ def use_cache(key: str, func: Callable[[], Any]) -> Any:
61
+ """Retrieve a cached result or execute the function if not cached.
62
+
63
+ Args:
64
+ key (str): The key to identify the cached result.
65
+ func (Callable[[], Any]): The function to execute if the result is
66
+ not cached. The result of the function is serialized and stored
67
+ in the cache for future use.
68
+
69
+ Returns:
70
+ Any: The cached result or the result of the executed function.
71
+ """
72
+ ```
73
+
74
+ ## Example
75
+
76
+ Here is a specific example of how to use `pytest-pickle-cache` to cache test results.
77
+
78
+ ```python
79
+ import datetime
80
+
81
+ import pytest
82
+ from pandas import DataFrame
83
+
84
+
85
+ def create() -> DataFrame:
86
+ """Create a DataFrame with the current time."""
87
+ now = datetime.datetime.now()
88
+ return DataFrame({"now": [now]})
89
+
90
+
91
+ def test_create(use_cache):
92
+ """Create a DataFrame using cache and compare the results."""
93
+ # Retrieve DataFrame using cache
94
+ df_cached = use_cache("key", create)
95
+
96
+ # Create a new DataFrame
97
+ df_created = create()
98
+
99
+ # Assert that the cached DataFrame and the newly created DataFrame are different.
100
+ assert not df_created.equals(df_cached)
101
+
102
+
103
+ def test_create_with_cache(use_cache):
104
+ """Use cache to retrieve the same DataFrame and ensure the results are the same."""
105
+ # Cache the DataFrame on the first call
106
+ df_cached_first = use_cache("key", create)
107
+
108
+ # Call the same function again to retrieve from cache
109
+ df_cached_second = use_cache("key", create)
110
+
111
+ # Assert that the cached DataFrame is the same on the second call.
112
+ assert df_cached_first.equals(df_cached_second)
113
+ ```
114
+
115
+
116
+ You can also use `use_cache` fixture as a fixture in your test file.
117
+
118
+ ```python
119
+ @pytest.fixture
120
+ def df(use_cache):
121
+ return use_cache("key", create)
122
+ ```
123
+
124
+ You can also use `use_cache` fixture with a parametrized fixture.
125
+
126
+ ```python
127
+ def create(param: int) -> DataFrame:
128
+ """Create a DataFrame with the current time."""
129
+ now = datetime.datetime.now()
130
+ return DataFrame({"now": [now], "param": [param]})
131
+
132
+
133
+ @pytest.fixture(params=[1, 2, 3])
134
+ def df(use_cache, request):
135
+ return use_cache(f"key_{request.param}", lambda: create(request.param))
136
+ ```
137
+
138
+ ## Benefits of this Example
139
+
140
+ - **Efficiency in Testing**: By using `pytest-pickle-cache`, you can avoid running
141
+ the same test multiple times, reducing the overall test execution time.
142
+
143
+ - **Consistency of Results**: Using cache ensures that you get the same result
144
+ for the same input, maintaining consistency in your tests.
145
+
146
+ <!-- Badges -->
147
+ [pypi-v-image]: https://img.shields.io/pypi/v/pytest-pickle-cache.svg
148
+ [pypi-v-link]: https://pypi.org/project/pytest-pickle-cache/
149
+ [python-v-image]: https://img.shields.io/pypi/pyversions/pytest-pickle-cache.svg
150
+ [python-v-link]: https://pypi.org/project/pytest-pickle-cache
151
+ [GHAction-image]: https://github.com/daizutabi/pytest-pickle-cache/actions/workflows/ci.yml/badge.svg?branch=main&event=push
152
+ [GHAction-link]: https://github.com/daizutabi/pytest-pickle-cache/actions?query=event%3Apush+branch%3Amain
153
+ [codecov-image]: https://codecov.io/github/daizutabi/pytest-pickle-cache/coverage.svg?branch=main
154
+ [codecov-link]: https://codecov.io/github/daizutabi/pytest-pickle-cache?branch=main
@@ -0,0 +1,7 @@
1
+ pytest_pickle_cache/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ pytest_pickle_cache/plugin.py,sha256=oPVlVc5tRS2C567G2UWC11CI9VN9w1wuXHuM26F49EY,1075
3
+ pytest_pickle_cache-0.1.0.dist-info/METADATA,sha256=xryu8HRwtD88RU_FLNauU4NMOlQrN2QoF64OTiO3qQ4,6111
4
+ pytest_pickle_cache-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
5
+ pytest_pickle_cache-0.1.0.dist-info/entry_points.txt,sha256=OEfVnY9Bw249Hf7T6TAPEw69R_FbjipFlDpo26eohrc,53
6
+ pytest_pickle_cache-0.1.0.dist-info/licenses/LICENSE,sha256=dbO9NY-nQXzIS5St3j3rPUd7gm5r6pczFYaNy5GVNdI,1105
7
+ pytest_pickle_cache-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [pytest11]
2
+ pickle_cache = pytest_pickle_cache.plugin
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-present daizutabi <daizutabi@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.