Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Jan 29, 2024
1 parent a62088e commit 68e306f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

## 0.8.0 (unreleased)

- `Tree.save()` accepts a `compress` argument that will enable ZIP compression.
- `Tree.save()` accepts a `compression` argument that will enable compression.
`Tree.load()` can detect if the input file has a compression header and will
decompress automatically.
- New traversal methods `LEVEL_ORDER`, `LEVEL_ORDER_RTL`, `ZIGZAG`, `ZIGZAG_RTL`.
decompress transparently.

## 0.7.1 (2023-11-08)

Expand Down
8 changes: 4 additions & 4 deletions docs/sphinx/ug_serialize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ extension::
tree_2 = Tree.load(path, auto_uncompress=True) # default is True
assert tree.compare(tree_2) == 0

If `compression` is true, the file is compressed using zipfile.ZIP_DEFLATED,
which is used by zip/gzip. |br|
Other values are: zipfile.ZIP_STORED, ZIP_BZIP2, and ZIP_LZMA.
Pass False to disable compression and store as plain json.
`compression` defines an optional compression method.
Possible values are: zipfile.ZIP_STORED, .ZIP_DEFLATED, .ZIP_BZIP2, and .ZIP_LZMA. |br|
`True` is uses the default compression zipfile.ZIP_BZIP2. |br|
The default `False` disables compression and stores as plain json. |br|
Though mileage may vary, ZIP_DEFLATED is usually the fastest compression method,
while ZIP_LZMA is the most effective but slower. ZIP_BZIP2 is somewhere in the
middle. |br|
Expand Down
2 changes: 1 addition & 1 deletion nutree/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def open_as_compressed_output_stream(
yield fp
else:
if compression is True:
compression = zipfile.ZIP_DEFLATED
compression = zipfile.ZIP_BZIP2
compression = int(compression)
name = f"{path.name}.json"
with zipfile.ZipFile(path, mode="w", compression=compression) as zf:
Expand Down
2 changes: 1 addition & 1 deletion nutree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __exit__(self, type, value, traceback):
return

def __eq__(self, other) -> bool:
raise NotImplementedError("Use `is` or `tree.compare()` instead of `==`.")
raise NotImplementedError("Use `is` instead of `==`.")

def __getitem__(self, data: object) -> Node:
"""Implement ``tree[data]`` syntax to lookup a node.
Expand Down
19 changes: 14 additions & 5 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import pprint
import tempfile
import zipfile
from typing import Tuple

from nutree import Node, Tree
Expand Down Expand Up @@ -78,14 +79,22 @@ def test_serialize_plain_str(self):

def test_serialize_compressed(self):
tree = fixture.create_tree()
tree.add_child("äöüß: \u00e4\u00f6\u00fc\u00df")
tree.add_child("emoji: 😀")

with fixture.WritableTempFile("r+t") as temp_file:
# Serialize
tree.save(temp_file.name, meta={"foo": "bar"}, compression=True)
# Deserialize
meta_2 = {}
tree_2 = Tree.load(temp_file.name, file_meta=meta_2)
tree.save(temp_file.name, compression=zipfile.ZIP_DEFLATED)
tree_2 = Tree.load(temp_file.name)
assert fixture.trees_equal(tree, tree_2)

with fixture.WritableTempFile("r+t") as temp_file:
tree.save(temp_file.name, compression=zipfile.ZIP_BZIP2)
tree_2 = Tree.load(temp_file.name)
assert fixture.trees_equal(tree, tree_2)

with fixture.WritableTempFile("r+t") as temp_file:
tree.save(temp_file.name, compression=zipfile.ZIP_LZMA)
tree_2 = Tree.load(temp_file.name)
assert fixture.trees_equal(tree, tree_2)

def _test_serialize_objects(self, *, mode: str):
Expand Down

0 comments on commit 68e306f

Please sign in to comment.