2016-05-20 05:14:48 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from pytest import raises
|
|
|
|
|
|
|
|
from boltons.debugutils import wrap_trace
|
|
|
|
|
|
|
|
|
|
|
|
def test_trace_dict():
|
|
|
|
target = {}
|
|
|
|
wrapped = wrap_trace(target)
|
|
|
|
|
|
|
|
assert target is not wrapped
|
|
|
|
assert isinstance(wrapped, dict)
|
|
|
|
|
|
|
|
wrapped['a'] = 'A'
|
|
|
|
assert target['a'] == 'A'
|
|
|
|
assert len(wrapped) == len(target)
|
|
|
|
|
|
|
|
wrapped.pop('a')
|
|
|
|
assert 'a' not in target
|
|
|
|
|
|
|
|
with raises(AttributeError):
|
|
|
|
wrapped.nonexistent_attr = 'nope'
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2016-05-20 08:46:28 +00:00
|
|
|
def test_trace_bytes():
|
2024-02-03 17:30:39 +00:00
|
|
|
target = b'Hello'
|
2016-05-20 05:14:48 +00:00
|
|
|
|
|
|
|
wrapped = wrap_trace(target)
|
|
|
|
|
|
|
|
assert target is not wrapped
|
2016-05-20 08:46:28 +00:00
|
|
|
assert isinstance(wrapped, bytes)
|
2016-05-20 05:14:48 +00:00
|
|
|
|
|
|
|
assert len(wrapped) == len(target)
|
2024-02-03 17:30:39 +00:00
|
|
|
assert wrapped.decode('utf-8') == 'Hello'
|
2016-05-20 05:14:48 +00:00
|
|
|
assert wrapped.lower() == target.lower()
|
|
|
|
|
|
|
|
|
|
|
|
def test_trace_exc():
|
|
|
|
class TestException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
target = TestException('exceptions can be a good thing')
|
|
|
|
wrapped = wrap_trace(target)
|
|
|
|
|
|
|
|
try:
|
|
|
|
raise wrapped
|
|
|
|
except TestException as te:
|
|
|
|
assert te.args == target.args
|
|
|
|
|
|
|
|
|
|
|
|
def test_trace_which():
|
2024-02-03 17:30:39 +00:00
|
|
|
class Config:
|
2016-05-20 05:14:48 +00:00
|
|
|
def __init__(self, value):
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
config = Config('first')
|
|
|
|
wrapped = wrap_trace(config, which='__setattr__')
|
|
|
|
|
|
|
|
wrapped.value = 'second'
|
|
|
|
assert config.value == 'second'
|
|
|
|
|
|
|
|
|
|
|
|
def test_trace_namedtuple():
|
|
|
|
TargetType = namedtuple('TargetType', 'x y z')
|
|
|
|
target = TargetType(1, 2, 3)
|
|
|
|
|
|
|
|
wrapped = wrap_trace(target)
|
|
|
|
|
|
|
|
assert wrapped == (1, 2, 3)
|
|
|
|
|
|
|
|
|
|
|
|
def test_trace_oldstyle():
|
|
|
|
class Oldie:
|
|
|
|
test = object()
|
|
|
|
|
|
|
|
def get_test(self):
|
|
|
|
return self.test
|
|
|
|
|
|
|
|
oldie = Oldie()
|
|
|
|
|
|
|
|
wrapped = wrap_trace(oldie)
|
|
|
|
assert wrapped.get_test() is oldie.test
|
|
|
|
|
|
|
|
return
|