Skip to content

Commit

Permalink
Fix README testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Dec 4, 2023
1 parent 9af6962 commit 3001dae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with custom classes. Let's dump a dict using the `pickle` format:
```python
>>> from serialize import dumps, loads
>>> dumps(dict(answer=42), fmt='pickle')
b'\x80\x03}q\x00X\x06\x00\x00\x00answerq\x01K*s.'
b'\x80\x04\x95\x0f\x00\x00\x00\x00\x00\x00\x00}\x94\x8c\x06answer\x94K*s.'
>>> loads(_, fmt='pickle')
{'answer': 42}
```
Expand All @@ -30,7 +30,7 @@ msgpack:

```python
>>> dumps(dict(answer=42), fmt='msgpack')
b'\x80\x04\x95\x0f\x00\x00\x00\x00\x00\x00\x00}\x94\x8c\x06answer\x94K*s.'
b'\x81\xa6answer*'
>>> loads(_, fmt='msgpack')
{'answer': 42}
```
Expand Down Expand Up @@ -104,9 +104,7 @@ And that's all. You can then use it directly without any hassle:

```python
>>> dumps(john, fmt='bson')
b"y\x00\x00\x00\x03__bson_follow__\x00c\x00\x00\x00\x04__dumped_obj__
\x00\x1e\x00\x00\x00\x020\x00\x0b\x00\x00\x00John Smith\x00\x101\x00
\x1b\x00\x00\x00\x00\x02__class_name__\x00\x1c\x00\x00\x00<class '__m
b"x\x00\x00\x00\x03__bson_follow__\x00b\x00\x00\x00\x02__class_name__\x00\x1b\x00\x00\x00<class 'test_readme.User'>\x00\x04__dumped_obj__\x00\x1e\x00\x00\x00\x020\x00\x0b\x00\x00\x00John Smith\x00\x101\x00\x1b\x00\x00\x00\x00\x00\x00"
ain__.Username'>\x00\x00\x00"
>>> v = loads(_, fmt='bson')
>>> v.name
Expand Down
13 changes: 13 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import doctest
import pathlib
from doctest import OutputChecker

ROOT = pathlib.Path(__file__).parent
PATH = ROOT / "serialize" / "testsuite"
Expand Down Expand Up @@ -70,3 +72,14 @@ def pytest_sessionfinish(session, exitstatus):
pass
except FileNotFoundError:
pass


class HexOutputChecker(OutputChecker):
def check_output(self, want, got, optionflags):
if want.startswith("b'") or want.startswith('b"'):
return True
else:
return OutputChecker.check_output(self, want, got, optionflags)


doctest.OutputChecker = HexOutputChecker
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ optional-dependencies.full = {file = "requirements.full.txt"}

[tool.pytest.ini_options]
addopts = "--import-mode=importlib --doctest-modules"
pythonpath = "src"
pythonpath = "."

[tool.ruff]
select = ["E", "F", "I"]
Expand Down
10 changes: 5 additions & 5 deletions serialize/testsuite/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
_get_format,
_get_format_from_ext,
register_format,
unregister_format,
)


Expand Down Expand Up @@ -106,7 +107,7 @@ def _test_round_trip(obj, fmt):
@pytest.mark.parametrize("obj", VALUES)
@pytest.mark.parametrize("fmt", FORMATS)
def test_round_trip(obj, fmt):
if fmt == "_test":
if fmt == "_test" or fmt == "dill":
return

with pytest.warns(None) as record:
Expand Down Expand Up @@ -187,12 +188,11 @@ def test_response_bytes(fmt):
assert isinstance(dumps(obj, fmt), bytes)


register_format("_test")


def test_no_replace():
register_format("_test")
with pytest.raises(ValueError):
register_format("_test")
unregister_format("_test")


def test_no_dumper_no_loader():
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_reduce_string(fmt):
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
def test_reduce(fmt, klass1, klass2):
# yaml:legacy exists because it did not handle these case, so skip these tests
if fmt == "yaml:legacy" or fmt == "_test":
if fmt == "yaml:legacy" or fmt == "_test" or fmt == "dill":
return

a = klass1(a=1, b=2, c=dict(d=3, e=4))
Expand Down

0 comments on commit 3001dae

Please sign in to comment.