提交 347d7e6f 编写于 作者: HansBug's avatar HansBug 😆

test(hansbug): move the unittest here && may be not runnable

上级 35cd89f6
from .config import *
from .entry import *
from .tree import *
from .utils import *
from .test_meta import TestConfigMeta
from .cli import *
from .script import *
from .test_export import TestEntryCliExport
from .test_graph import TestEntryCliGraph
from .test_version import TestEntryCliVersion
from .common import *
from .func import *
from .general import *
from .tree import *
from .test_tree import TestTreeCommonTree
from .test_view import TestTreeCommonView
import pickle
import pytest
from treevalue.tree.common import raw, unraw
from treevalue.tree.common.base import _RawWrapper
@pytest.mark.unittest
class TestTreeBase:
def test_raw(self):
assert raw(1) == 1
assert raw('sdklfgj') == 'sdklfgj'
h = {'a': 1, 'b': 2}
r = raw(h)
assert isinstance(r, _RawWrapper)
assert r.value is h
def test_unraw(self):
assert unraw(1) == 1
assert unraw('sdklfgj') == 'sdklfgj'
h = {'a': 1, 'b': 2}
r = raw(h)
u = unraw(r)
assert u is h
def test_pickle(self):
h = {'a': 1, 'b': 2}
r = raw(h)
bt = pickle.dumps(r)
nt = pickle.loads(bt)
assert isinstance(nt, _RawWrapper)
assert nt.value == h
import pickle
import pytest
from treevalue.tree.common import create_storage, raw, TreeStorage
# noinspection PyArgumentList,DuplicatedCode
@pytest.mark.unittest
class TestTreeStorage:
def test_init(self):
_ = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
def test_get(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
assert t.get('a') == 1
assert t.get('b') == 2
assert t.get('c') == {'x': 3, 'y': 4}
assert isinstance(t.get('d'), TreeStorage)
assert t.get('d').get('x') == 3
assert t.get('d').get('y') == 4
with pytest.raises(KeyError):
_ = t.get('fff')
def test_set(self):
t = create_storage({})
t.set('a', 1)
t.set('b', 2)
t.set('c', {'x': 3, 'y': 4})
t.set('d', create_storage({'x': 3, 'y': 4}))
t.set('_0a', None)
assert t.get('a') == 1
assert t.get('b') == 2
assert t.get('c') == {'x': 3, 'y': 4}
assert isinstance(t.get('d'), TreeStorage)
assert t.get('_0a') is None
with pytest.raises(KeyError):
t.set('', 233)
with pytest.raises(KeyError):
t.set('a' * 1000, 233)
with pytest.raises(KeyError):
t.set('0' + 'a' * 10, 233)
def test_del_(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
t.del_('c')
t.del_('b')
assert t.get('a') == 1
with pytest.raises(KeyError):
_ = t.get('c')
with pytest.raises(KeyError):
_ = t.get('b')
assert isinstance(t.get('d'), TreeStorage)
with pytest.raises(KeyError):
t.del_('fff')
def test_contains(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
assert t.contains('a')
assert t.contains('b')
assert t.contains('c')
assert t.contains('d')
assert not t.contains('f')
assert not t.contains('kdfsj')
def test_size(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
assert t.size() == 4
assert t.get('d').size() == 2
t.set('f', None)
assert t.size() == 5
t.del_('a')
t.del_('c')
t.del_('d')
assert t.size() == 2
def test_empty(self):
t = create_storage({'a': 1, 'b': 2, 'c': raw({'x': 3, 'y': 4}), 'd': {'x': 3, 'y': 4}})
assert not t.empty()
assert not t.get('d').empty()
t.del_('a')
t.del_('c')
t.get('d').del_('x')
assert not t.empty()
assert not t.get('d').empty()
t.get('d').del_('y')
assert t.get('d').empty()
t.del_('b')
t.del_('d')
assert t.empty()
def test_dump(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
_dumped = t.dump()
assert _dumped['a'] == 1
assert _dumped['b'] == 2
assert _dumped['c'].value is h1
assert _dumped['d']['x'] == 3
assert _dumped['d']['y'] == 4
def test_deepdump(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
_dumped = t.deepdump()
assert _dumped['a'] == 1
assert _dumped['b'] == 2
assert _dumped['c'].value == h1
assert _dumped['c'] is not h1
assert _dumped['d']['x'] == 3
assert _dumped['d']['y'] == 4
def test_deepdumpx(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
_dumped = t.deepdumpx(lambda x: -x if isinstance(x, int) else {'holy': 'shit'})
assert _dumped['a'] == -1
assert _dumped['b'] == -2
assert _dumped['c'].value == {'holy': 'shit'}
assert _dumped['d']['x'] == -3
assert _dumped['d']['y'] == -4
def test_copy(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
t1 = t.copy()
assert t1.get('a') == 1
assert t1.get('b') == 2
assert t1.get('c') is h1
assert t1.get('d').get('x') == 3
assert t1.get('d').get('y') == 4
def test_deepcopy(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
t1 = t.deepcopy()
assert t1.get('a') == 1
assert t1.get('b') == 2
assert t1.get('c') == h1
assert t1.get('c') is not h1
assert t1.get('d').get('x') == 3
assert t1.get('d').get('y') == 4
def test_deepcopyx(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
t1 = t.deepcopyx(lambda x: -x if isinstance(x, int) else {'holy': 'shit'})
assert t1.get('a') == -1
assert t1.get('b') == -2
assert t1.get('c') == {'holy': 'shit'}
assert t1.get('d').get('x') == -3
assert t1.get('d').get('y') == -4
def test_pickle(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
t1 = pickle.loads(pickle.dumps(t))
assert t1.get('a') == 1
assert t1.get('b') == 2
assert t1.get('c') == h1
assert t1.get('c') is not h1
assert t1.get('d').get('x') == 3
assert t1.get('d').get('y') == 4
def test_detach(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2})
dt = t.detach()
assert dt['a'] == 1
assert dt['b'] == 2
assert dt['c'] == h1
assert isinstance(dt['d'], TreeStorage)
assert dt['d'].get('x') == 3
assert dt['d'].get('y') == 4
def test_copy_from(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2, 'f': h2})
h3 = {'x': 33, 'y': 44}
h4 = {'x': 33, 'y': 44}
t1 = create_storage({'a': 11, 'e': 2333, 'c': raw(h3), 'd': h4})
did = id(t1.get('d'))
t1.copy_from(t)
assert t1 is not t
assert t1.get('a') == 1
assert t1.get('b') == 2
assert t1.get('c') is h1
assert t1.get('d').get('x') == 3
assert t1.get('d').get('y') == 4
assert id(t1.get('d')) == did
assert not t1.contains('e')
assert t1.get('f').get('x') == 3
assert t1.get('f').get('y') == 4
assert t1.get('f') is not t.get('f')
def test_repr(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2, 'f': h2})
assert repr(('a', 'b', 'c', 'd', 'f')) in repr(t)
assert repr(('x', 'y')) in repr(t.get('d'))
assert repr(('x', 'y')) in repr(t.get('f'))
def test_eq(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2, 'f': h2})
t1 = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2, 'f': h2})
t2 = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2, 'f': {'x': 3, 'y': 3, 'z': 4}})
assert t == t1
assert t != t2
def test_keys(self):
h1 = {'x': 3, 'y': 4}
h2 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'c': raw(h1), 'd': h2, 'f': h2})
assert set(t.keys()) == {'a', 'b', 'c', 'd', 'f'}
assert set(t.get('f').keys()) == {'x', 'y'}
def test_values(self):
h1 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'd': h1})
assert set(t.get('d').values()) == {3, 4}
assert len(t.values()) == 3
assert 1 in t.values()
assert 2 in t.values()
def test_items(self):
h1 = {'x': 3, 'y': 4}
t = create_storage({'a': 1, 'b': 2, 'd': raw(h1)})
for k, v in t.items():
if k == 'a':
assert v == 1
elif k == 'b':
assert v == 2
elif k == 'd':
assert v == h1
else:
pytest.fail('Should not reach here.')
import pickle
import re
import pytest
from treevalue.tree.common import Tree, raw
# noinspection DuplicatedCode
@pytest.mark.unittest
class TestTreeCommonTree:
def test_tree_init_and_eq(self):
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
assert t == Tree({'x': {'c': 2, 'b': 1}, 'a': 1})
assert t == t
assert t != 233
t1 = Tree(t)
assert t == t1
assert t is not t1
assert t == t.clone()
assert hash(t) == hash(t.clone())
assert t is not t.clone()
with pytest.raises(TypeError):
Tree(233)
t1 = Tree({'a': 1, 'x': {'b': 1, 'c': None}})
assert t != t1
assert hash(t) != hash(t1)
assert t1 != Tree({'a': 1, 'x': {'b': 1, 'f': None}})
h = {t: 1, t1: 2}
assert h[t] == 1
assert h[t.clone()] == 1
assert h[Tree(t)] == 1
assert h[t1] == 2
assert h[Tree(t1.json())] == 2
def test_copy_from(self):
t1 = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
t2 = Tree({'a': 11, 'b': 24, 'x': {'b': 12, 'e': {'dfkj': 892374}}})
original_id = id(t1)
original_id_x = id(t1['x'])
t1.copy_from(t2)
assert t1 == t2
assert t1 is not t2
assert id(t1) == original_id
assert id(t1['x']) == original_id_x
assert t1['x']['e'] == t2['x']['e']
assert t1['x']['e'] is not t2['x']['e']
def test_tree_items(self):
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
assert t['a'] == 1
assert t['x'] == Tree({'b': 1, 'c': 2})
with pytest.raises(KeyError):
_ = t['233']
t1 = Tree(t)
t1['x'] = {'b1': 1, 'b2': 2, 'b4': 5, 't': {'v': 33}}
assert t == Tree({'x': {'c': 2, 'b': 1}, 'a': 1})
assert t1 == Tree({'x': {'b1': 1, 'b2': 2, 'b4': 5, 't': {'v': 33}}, 'a': 1})
del t1['x']['b2']
assert t == Tree({'x': {'c': 2, 'b': 1}, 'a': 1})
assert t1 == Tree({'x': {'b1': 1, 'b4': 5, 't': {'v': 33}}, 'a': 1})
def test_tree_str(self):
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
assert "'a' --> 1" in str(t)
assert "'b' --> 1" in str(t)
assert "'c' --> 2" in str(t)
def test_tree_repr(self):
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
t1 = Tree(t)
t1['x'] = {'b1': 1, 'b2': 2, 'b4': 5, 't': {'v': 33}}
del t1['x']['b2']
assert re.fullmatch(r"<Tree 0x[0-9a-f]+ keys: \['a', 'x']>", repr(t))
assert re.fullmatch(r"<Tree 0x[0-9a-f]+ keys: \['a', 'x']>", repr(t1))
assert re.fullmatch(r"<Tree 0x[0-9a-f]+ keys: \['b1', 'b4', 't']>", repr(t1['x']))
def test_tree_len(self):
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
t1 = Tree(t)
t1['x'] = {'b1': 1, 'b2': 2, 'b4': 5, 't': {'v': 33}}
del t1['x']['b2']
assert len(t) == 2
assert len(t1) == 2
assert len(t1['x']) == 3
assert t
assert t1
assert not Tree({})
def test_tree_iters(self):
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
assert 'a' in t.keys()
assert 'x' in t.keys()
assert 't' not in t.keys()
assert 'b' in t['x'].keys()
assert 'xx' not in t['x'].keys()
assert 1 in t.values()
assert Tree({'b': 1, 'c': 2}) in t.values()
assert Tree({'b': 1, 'c': None}) not in t.values()
assert {key: value for key, value in t.items()} == \
{'a': 1, 'x': Tree({'b': 1, 'c': 2})}
def test_raw(self):
t = Tree({
'a': raw({'a': 1, 'b': 2}),
'b': raw({'a': 3, 'b': 4}),
'x': {
'c': raw({'a': 5, 'b': 6}),
'd': raw({'a': 7, 'b': 8}),
}
})
assert t['a'] == {'a': 1, 'b': 2}
assert t['b'] == {'a': 3, 'b': 4}
assert t['x']['c'] == {'a': 5, 'b': 6}
assert t['x']['d'] == {'a': 7, 'b': 8}
t1 = t.clone()
assert t1['a'] == {'a': 1, 'b': 2}
assert t1['b'] == {'a': 3, 'b': 4}
assert t1['x']['c'] == {'a': 5, 'b': 6}
assert t1['x']['d'] == {'a': 7, 'b': 8}
t['a'] = raw({'a': 9, 'b': 10})
assert t['a'] == {'a': 9, 'b': 10}
assert t['b'] == {'a': 3, 'b': 4}
assert t['x']['c'] == {'a': 5, 'b': 6}
assert t['x']['d'] == {'a': 7, 'b': 8}
def test_invalid_key(self):
with pytest.raises(KeyError):
_ = Tree({'a': 123, '\uffff': 321})
t = Tree({'a': 1, 'x': {'b': 1, 'c': 2}})
with pytest.raises(KeyError):
t['\uffff'] = 233
with pytest.raises(KeyError):
t['a' * 0x1ffff] = 233
with pytest.raises(KeyError):
t[''] = 233
with pytest.raises(KeyError):
t['0'] = 233
def test_deep_clone(self):
t = Tree({
'a': raw({'a': 1, 'b': 2}),
'b': raw({'a': 3, 'b': 4}),
'x': {
'c': raw({'a': 5, 'b': 6}),
'd': raw({'a': 7, 'b': 8}),
}
})
t1 = t.clone()
t2 = t.clone(copy_value=True)
assert t1 == t
assert t1['a'] is t['a']
assert t1['b'] is t['b']
assert t1['x']['c'] is t['x']['c']
assert t1['x']['d'] is t['x']['d']
assert t2 == t
assert t2['a'] is not t['a']
assert t2['b'] is not t['b']
assert t2['x']['c'] is not t['x']['c']
assert t2['x']['d'] is not t['x']['d']
t3 = t.clone(copy_value=pickle)
assert t3 == t
assert t3['a'] is not t['a']
assert t3['b'] is not t['b']
assert t3['x']['c'] is not t['x']['c']
assert t3['x']['d'] is not t['x']['d']
t4 = t.clone(copy_value=lambda x: pickle.loads(pickle.dumps(x)))
assert t4 == t
assert t4['a'] is not t['a']
assert t4['b'] is not t['b']
assert t4['x']['c'] is not t['x']['c']
assert t4['x']['d'] is not t['x']['d']
t5 = t.clone(copy_value=(pickle.dumps, pickle.loads))
assert t5 == t
assert t5['a'] is not t['a']
assert t5['b'] is not t['b']
assert t5['x']['c'] is not t['x']['c']
assert t5['x']['d'] is not t['x']['d']
import pickle
import re
import pytest
from treevalue.tree.common import Tree, raw
# noinspection DuplicatedCode
@pytest.mark.unittest
class TestTreeCommonView:
def test_simple_view(self):
t = Tree({'a': 1, 'x': {'b': 233, 'c': 'sdklfgjl', 'f': {'t': 2, 'p': 3}}})
tv1 = t.view(['x'])
assert tv1 == Tree({'b': 233, 'c': 'sdklfgjl', 'f': {'t': 2, 'p': 3}})
with pytest.raises(TypeError):
tv2 = t.view(['a'])
tv2.json()
t['x']['b'] = 234
assert tv1 == Tree({'b': 234, 'c': 'sdklfgjl', 'f': {'t': 2, 'p': 3}})
assert tv1['b'] == 234
tv1['b'] = 235
assert t == Tree({'a': 1, 'x': {'b': 235, 'c': 'sdklfgjl', 'f': {'t': 2, 'p': 3}}})
del tv1['c']
assert t == Tree({'a': 1, 'x': {'b': 235, 'f': {'t': 2, 'p': 3}}})
tv3 = tv1.clone()
assert isinstance(tv3, Tree)
tv1['d'] = 345
assert tv1 == Tree({'b': 235, 'd': 345, 'f': {'t': 2, 'p': 3}})
assert tv3 == Tree({'b': 235, 'f': {'t': 2, 'p': 3}})
assert t == Tree({'a': 1, 'x': {'b': 235, 'd': 345, 'f': {'t': 2, 'p': 3}}})
assert re.fullmatch(r"<TreeView 0x[0-9a-f]+ keys: \['b', 'd', 'f']>", repr(tv1))
assert re.fullmatch(r"<Tree 0x[0-9a-f]+ keys: \['b', 'f']>", repr(tv3))
t['x'] = {'a': 1, 'b': 2, 'c': 3, 'd': {'t': 'p', 'q': -1}}
assert tv1 == Tree({'a': 1, 'b': 2, 'c': 3, 'd': {'t': 'p', 'q': -1}})
tv4 = tv1.view(['d'])
assert tv4 == Tree({'t': 'p', 'q': -1})
t['x'] = {'a': 3, 'b': 2, 'c': 1, 'd': {'t': -1, 'q': -2}}
assert tv4 == Tree({'t': -1, 'q': -2})
assert set(tv4.values()) == {-1, -2}
assert set(tv1.values()) == {
3, 2, 1, Tree({'t': -1, 'q': -2})
}
def test_copy_from(self):
t = Tree({'a': 1, 'x': {'b': 233, 'c': 'sdklfgjl', 'f': {'t': 2, 'p': 3}}})
tv1 = t.view(['x'])
t2 = Tree({'bf': 233, 'c': 'sdklfgjl', 'f': {'t': 2, 'p': 3, 'tp': 3}})
original_id_x = id(t['x'])
original_id_x_f = id(t['x']['f'])
tv1.copy_from(t2)
assert tv1 == t2
assert id(tv1) == original_id_x
assert id(tv1['f']) == original_id_x_f
def test_deep_clone(self):
tx = Tree({
'a': {
'a': raw({'a': 1, 'b': 2}),
'b': raw({'a': 3, 'b': 4}),
'x': {
'c': raw({'a': 5, 'b': 6}),
'd': raw({'a': 7, 'b': 8}),
}
}
})
t = tx.view(['a'])
t1 = t.clone()
t2 = t.clone(copy_value=True)
assert t1 == t
assert t1['a'] is t['a']
assert t1['b'] is t['b']
assert t1['x']['c'] is t['x']['c']
assert t1['x']['d'] is t['x']['d']
assert t2 == t
assert t2['a'] is not t['a']
assert t2['b'] is not t['b']
assert t2['x']['c'] is not t['x']['c']
assert t2['x']['d'] is not t['x']['d']
t3 = t.clone(copy_value=pickle)
assert t3 == t
assert t3['a'] is not t['a']
assert t3['b'] is not t['b']
assert t3['x']['c'] is not t['x']['c']
assert t3['x']['d'] is not t['x']['d']
t4 = t.clone(copy_value=lambda x: pickle.loads(pickle.dumps(x)))
assert t4 == t
assert t4['a'] is not t['a']
assert t4['b'] is not t['b']
assert t4['x']['c'] is not t['x']['c']
assert t4['x']['d'] is not t['x']['d']
t5 = t.clone(copy_value=(pickle.dumps, pickle.loads))
assert t5 == t
assert t5['a'] is not t['a']
assert t5['b'] is not t['b']
assert t5['x']['c'] is not t['x']['c']
assert t5['x']['d'] is not t['x']['d']
from .test_func import TestTreeFuncFunc
from .test_inner import TestTreeFuncInner
from .test_left import TestTreeFuncLeft
from .test_outer import TestTreeFuncOuter
from .test_strict import TestTreeFuncStrict
from .test_fast import TestTreeGeneralFast
from .test_general import TestTreeGeneralGeneral
from .test_tensor import test_for_torch_support
from .test_graph import TestTreeTreeGraph
from .test_io import TestTreeTreeIo
from .test_tree import TestTreeTreeTree
from .test_utils import TestTreeTreeUtils
from .test_clazz import TestUtilsClazz
from .test_color import TestUtilsColor
from .test_enum import TestUtilsEnum
from .test_exception import TestUtilsException
from .test_final import TestUtilsFinal
from .test_func import TestUtilsFunc
from .test_imports import TestUtilsImports
from .test_random import TestUtilsRandom
from .test_singleton import TestUtilsSingleton
from .test_tree import TestUtilsTree
......@@ -130,8 +130,8 @@ cdef class TreeStorage:
cdef dict other_map = other.detach()
cdef list other_keys = sorted(other.detach().keys())
cdef str key
if self_keys == other_keys:
cdef str key
for key in self_keys:
if not (self.map[key] == other_map[key]):
return False
......@@ -146,7 +146,7 @@ cdef class TreeStorage:
return self.map.values()
def items(self):
return self.map.values()
return self.map.items()
def create_storage(dict value):
cdef dict _map = {}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册