pytest-pickle-cache 0.1.0__tar.gz

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.
@@ -0,0 +1,18 @@
1
+ {
2
+ "image": "mcr.microsoft.com/vscode/devcontainers/base:ubuntu24.04",
3
+ "features": {
4
+ "ghcr.io/devcontainers-contrib/features/starship:1": {}
5
+ },
6
+ "customizations": {
7
+ "vscode": {
8
+ "extensions": [
9
+ "charliermarsh.ruff",
10
+ "fill-labs.dependi",
11
+ "ms-python.python",
12
+ "ms-python.vscode-pylance",
13
+ "tamasfe.even-better-toml"
14
+ ]
15
+ }
16
+ },
17
+ "postCreateCommand": ".devcontainer/postCreate.sh"
18
+ }
@@ -0,0 +1,10 @@
1
+ #!/bin/bash
2
+
3
+ echo 'eval "$(starship init bash)"' >> ~/.bashrc
4
+ mkdir -p ~/.config
5
+ cp .devcontainer/starship.toml ~/.config
6
+
7
+ curl -LsSf https://astral.sh/uv/install.sh | sh
8
+ source $HOME/.cargo/env
9
+ uv tool install ruff@latest
10
+ echo 'eval "$(uv generate-shell-completion bash)"' >> ~/.bashrc
@@ -0,0 +1,29 @@
1
+ "$schema" = 'https://starship.rs/config-schema.json'
2
+
3
+ add_newline = true
4
+
5
+ [username]
6
+ disabled = true
7
+
8
+ [hostname]
9
+ disabled = true
10
+
11
+ [package]
12
+ format = '[$symbol$version]($style) '
13
+ symbol = "󰏗 "
14
+ style = 'bold blue'
15
+
16
+ [git_branch]
17
+ format = '[$symbol$branch(:$remote_branch)]($style)'
18
+ symbol = ' '
19
+ style = 'bold green'
20
+
21
+ [git_status]
22
+ style = 'red'
23
+ modified = '*'
24
+ format = '([$modified]($style)) '
25
+
26
+ [python]
27
+ format = '[${symbol}${pyenv_prefix}(${version} )(\($virtualenv\) )]($style)'
28
+ symbol = ' '
29
+ style = 'yellow'
@@ -0,0 +1,6 @@
1
+ .coverage
2
+ .venv/
3
+ __pycache__/
4
+ dist/
5
+ lcov.info
6
+ uv.lock
@@ -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.
@@ -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,123 @@
1
+ # pytest-pickle-cache
2
+
3
+ [![PyPI Version][pypi-v-image]][pypi-v-link]
4
+ [![Python Version][python-v-image]][python-v-link]
5
+ [![Build Status][GHAction-image]][GHAction-link]
6
+ [![Coverage Status][codecov-image]][codecov-link]
7
+
8
+ ## Overview
9
+
10
+ `pytest-pickle-cache` is a pytest plugin for caching test results using pickle.
11
+ By utilizing this plugin, you can reduce test execution time and perform tests
12
+ more efficiently.
13
+
14
+ ## Installation
15
+
16
+ You can install `pytest-pickle-cache` using the following command:
17
+
18
+ ```bash
19
+ pip install pytest-pickle-cache
20
+ ```
21
+
22
+ ## Fixture
23
+
24
+ The `use_cache` fixture is a pytest fixture that provides a caching mechanism
25
+ for pytest, allowing you to store and retrieve objects using a specified key.
26
+ The objects are serialized and deserialized using pickle and base64 encoding.
27
+
28
+ ```python
29
+ def use_cache(key: str, func: Callable[[], Any]) -> Any:
30
+ """Retrieve a cached result or execute the function if not cached.
31
+
32
+ Args:
33
+ key (str): The key to identify the cached result.
34
+ func (Callable[[], Any]): The function to execute if the result is
35
+ not cached. The result of the function is serialized and stored
36
+ in the cache for future use.
37
+
38
+ Returns:
39
+ Any: The cached result or the result of the executed function.
40
+ """
41
+ ```
42
+
43
+ ## Example
44
+
45
+ Here is a specific example of how to use `pytest-pickle-cache` to cache test results.
46
+
47
+ ```python
48
+ import datetime
49
+
50
+ import pytest
51
+ from pandas import DataFrame
52
+
53
+
54
+ def create() -> DataFrame:
55
+ """Create a DataFrame with the current time."""
56
+ now = datetime.datetime.now()
57
+ return DataFrame({"now": [now]})
58
+
59
+
60
+ def test_create(use_cache):
61
+ """Create a DataFrame using cache and compare the results."""
62
+ # Retrieve DataFrame using cache
63
+ df_cached = use_cache("key", create)
64
+
65
+ # Create a new DataFrame
66
+ df_created = create()
67
+
68
+ # Assert that the cached DataFrame and the newly created DataFrame are different.
69
+ assert not df_created.equals(df_cached)
70
+
71
+
72
+ def test_create_with_cache(use_cache):
73
+ """Use cache to retrieve the same DataFrame and ensure the results are the same."""
74
+ # Cache the DataFrame on the first call
75
+ df_cached_first = use_cache("key", create)
76
+
77
+ # Call the same function again to retrieve from cache
78
+ df_cached_second = use_cache("key", create)
79
+
80
+ # Assert that the cached DataFrame is the same on the second call.
81
+ assert df_cached_first.equals(df_cached_second)
82
+ ```
83
+
84
+
85
+ You can also use `use_cache` fixture as a fixture in your test file.
86
+
87
+ ```python
88
+ @pytest.fixture
89
+ def df(use_cache):
90
+ return use_cache("key", create)
91
+ ```
92
+
93
+ You can also use `use_cache` fixture with a parametrized fixture.
94
+
95
+ ```python
96
+ def create(param: int) -> DataFrame:
97
+ """Create a DataFrame with the current time."""
98
+ now = datetime.datetime.now()
99
+ return DataFrame({"now": [now], "param": [param]})
100
+
101
+
102
+ @pytest.fixture(params=[1, 2, 3])
103
+ def df(use_cache, request):
104
+ return use_cache(f"key_{request.param}", lambda: create(request.param))
105
+ ```
106
+
107
+ ## Benefits of this Example
108
+
109
+ - **Efficiency in Testing**: By using `pytest-pickle-cache`, you can avoid running
110
+ the same test multiple times, reducing the overall test execution time.
111
+
112
+ - **Consistency of Results**: Using cache ensures that you get the same result
113
+ for the same input, maintaining consistency in your tests.
114
+
115
+ <!-- Badges -->
116
+ [pypi-v-image]: https://img.shields.io/pypi/v/pytest-pickle-cache.svg
117
+ [pypi-v-link]: https://pypi.org/project/pytest-pickle-cache/
118
+ [python-v-image]: https://img.shields.io/pypi/pyversions/pytest-pickle-cache.svg
119
+ [python-v-link]: https://pypi.org/project/pytest-pickle-cache
120
+ [GHAction-image]: https://github.com/daizutabi/pytest-pickle-cache/actions/workflows/ci.yml/badge.svg?branch=main&event=push
121
+ [GHAction-link]: https://github.com/daizutabi/pytest-pickle-cache/actions?query=event%3Apush+branch%3Amain
122
+ [codecov-image]: https://codecov.io/github/daizutabi/pytest-pickle-cache/coverage.svg?branch=main
123
+ [codecov-link]: https://codecov.io/github/daizutabi/pytest-pickle-cache?branch=main
@@ -0,0 +1,64 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "pytest-pickle-cache"
7
+ version = "0.1.0"
8
+ authors = [{ name = "daizutabi", email = "daizutabi@gmail.com" }]
9
+ description = "A pytest plugin for caching test results using pickle."
10
+ readme = "README.md"
11
+ license = { file = "LICENSE" }
12
+ classifiers = [
13
+ "Framework :: Pytest",
14
+ "Intended Audience :: Developers",
15
+ "License :: OSI Approved :: MIT License",
16
+ "Topic :: Software Development :: Testing",
17
+ "Programming Language :: Python",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ ]
23
+ requires-python = ">=3.10"
24
+ dependencies = ["pytest>=7"]
25
+
26
+ [project.urls]
27
+ Documentation = "https://daizutabi.github.io/pytest-pickle-cache/"
28
+ Source = "https://github.com/daizutabi/pytest-pickle-cache"
29
+ Issues = "https://github.com/daizutabi/pytest-pickle-cache/issues"
30
+
31
+ [project.entry-points.pytest11]
32
+ pickle_cache = "pytest_pickle_cache.plugin"
33
+
34
+ [tool.uv]
35
+ dev-dependencies = ["pandas>=2", "pytest-cov>=6.0"]
36
+
37
+ [tool.hatch.build.targets.sdist]
38
+ exclude = ["/.github", "/docs"]
39
+
40
+ [tool.hatch.build.targets.wheel]
41
+ packages = ["src/pytest_pickle_cache"]
42
+
43
+ [tool.pytest.ini_options]
44
+ addopts = [
45
+ "--doctest-modules",
46
+ "--cov=pytest_pickle_cache",
47
+ "--cov-report=lcov:lcov.info",
48
+ ]
49
+ doctest_optionflags = ["NORMALIZE_WHITESPACE", "IGNORE_EXCEPTION_DETAIL"]
50
+ filterwarnings = []
51
+
52
+ [tool.coverage.run]
53
+ disable_warnings = ["module-not-measured"]
54
+
55
+ [tool.coverage.report]
56
+ exclude_lines = ["no cov", "raise NotImplementedError", "if TYPE_CHECKING:"]
57
+ skip_covered = true
58
+
59
+ [tool.ruff]
60
+ line-length = 88
61
+ target-version = "py311"
62
+
63
+ [tool.ruff.lint]
64
+ unfixable = ["F401"]
@@ -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
File without changes
@@ -0,0 +1,30 @@
1
+ """Test the pytest_pickle_cache plugin."""
2
+
3
+ import datetime
4
+ import time
5
+
6
+ import pytest
7
+ from pandas import DataFrame
8
+
9
+
10
+ def create() -> DataFrame:
11
+ """Create a DataFrame with the current time."""
12
+ now = datetime.datetime.now()
13
+ return DataFrame({"now": [now]})
14
+
15
+
16
+ @pytest.fixture
17
+ def df(use_cache):
18
+ return use_cache("use_cache", create)
19
+
20
+
21
+ def test_create(use_cache):
22
+ df_cached = use_cache("use_cache", create)
23
+ time.sleep(1)
24
+ df_created = create()
25
+ assert not df_created.equals(df_cached)
26
+
27
+
28
+ def test_create_df(df: DataFrame, use_cache):
29
+ df_cached = use_cache("use_cache", create)
30
+ assert df.equals(df_cached)