Skip to content

Commit

Permalink
add copy and json lazy_nodes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Feb 12, 2024
1 parent 5d7e55d commit fb81020
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 9 additions & 3 deletions asdf/_lazy_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class AsdfListNode(AsdfNode, collections.UserList, list):
def __init__(self, data=None, af_ref=None):
if data is None:
data = []
super().__init__(data, af_ref)
AsdfNode.__init__(self, data, af_ref)
collections.UserList.__init__(self, data)
list.__init__(self, data)

def __eq__(self, other):
Expand Down Expand Up @@ -136,7 +137,8 @@ class AsdfDictNode(AsdfNode, collections.UserDict, dict):
def __init__(self, data=None, af_ref=None):
if data is None:
data = {}
super().__init__(data, af_ref)
AsdfNode.__init__(self, data, af_ref)
collections.UserDict.__init__(self, data)
dict.__init__(self, data)

def __eq__(self, other):
Expand Down Expand Up @@ -192,4 +194,8 @@ def __getitem__(self, key):


class AsdfOrderedDictNode(AsdfDictNode, collections.OrderedDict):
pass
def __init__(self, data=None, af_ref=None):
if data is None:
data = collections.OrderedDict()
AsdfDictNode.__init__(self, data, af_ref)
collections.OrderedDict.__init__(self, data)
30 changes: 30 additions & 0 deletions asdf/_tests/test_lazy_nodes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import collections
import copy
import json
import weakref

import numpy as np
Expand Down Expand Up @@ -94,6 +96,34 @@ def test_ordered_dict():
assert isinstance(node["a"]["b"][2], _lazy_nodes.AsdfOrderedDictNode)


@pytest.mark.parametrize(
"node",
[
_lazy_nodes.AsdfDictNode({"a": 1, "b": 2}),
_lazy_nodes.AsdfListNode([1, 2, 3]),
_lazy_nodes.AsdfOrderedDictNode({"a": 1, "b": 2}),
],
)
@pytest.mark.parametrize("copy_operation", [copy.copy, copy.deepcopy])
def test_copy(node, copy_operation):
copied_node = copy_operation(node)
assert isinstance(copied_node, type(node))
assert copied_node == node


@pytest.mark.parametrize(
"node",
[
_lazy_nodes.AsdfDictNode({"a": 1, "b": 2}),
_lazy_nodes.AsdfListNode([1, 2, 3]),
_lazy_nodes.AsdfOrderedDictNode({"a": 1, "b": 2}),
],
)
def test_json_serialization(node):
rt_node = json.loads(json.dumps(node))
assert rt_node == node


def test_cache_clear_on_close(tmp_path):
fn = tmp_path / "test.asdf"

Expand Down

0 comments on commit fb81020

Please sign in to comment.