def assert_dict_equal(d1, d2):
# check keys
nt.assert_is_instance(d1, dict)
nt.assert_is_instance(d2, dict)
nt.assert_set_equal(set(d1.keys()), set(d2.keys()))
for k in d1:
v1 = d1[k]
v2 = d2[k]
asserter = nt.assert_equal
if isinstance(v1, pd.DataFrame):
asserter = tm.assert_frame_equal
if isinstance(v1, pd.Series):
asserter = tm.assert_series_equal
try:
asserter(v1, v2)
except AssertionError:
raise AssertionError('{k} is not equal'.format(k=k))
python类assert_is_instance()的实例源码
def test_class_inheritance():
@interactive
class C(object):
a=5
@interactive
class D(C):
b=10
bufs = serialize_object(dict(D=D))
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned['D'], CannedClass)
d, r = deserialize_object(bufs)
D2 = d['D']
nt.assert_equal(D2.a, D.a)
nt.assert_equal(D2.b, D.b)
def test_display_handle():
ip = get_ipython()
handle = display.DisplayHandle()
nt.assert_is_instance(handle.display_id, str)
handle = display.DisplayHandle('my-id')
nt.assert_equal(handle.display_id, 'my-id')
with mock.patch.object(ip.display_pub, 'publish') as pub:
handle.display('x')
handle.update('y')
args, kwargs = pub.call_args_list[0]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('x')
},
'metadata': {},
'transient': {
'display_id': handle.display_id,
}
})
args, kwargs = pub.call_args_list[1]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('y')
},
'metadata': {},
'transient': {
'display_id': handle.display_id,
},
'update': True,
})
def test_bad_module_all():
"""Test module with invalid __all__
https://github.com/ipython/ipython/issues/9678
"""
testsdir = os.path.dirname(__file__)
sys.path.insert(0, testsdir)
try:
results = module_completion('from bad_all import ')
nt.assert_in('puppies', results)
for r in results:
nt.assert_is_instance(r, str)
finally:
sys.path.remove(testsdir)
def test_clipboard_get():
# Smoketest for clipboard access - we can't easily guarantee that the
# clipboard is accessible and has something on it, but this tries to
# exercise the relevant code anyway.
try:
a = get_ipython().hooks.clipboard_get()
except ClipboardEmpty:
# Nothing in clipboard to get
pass
except TryNext:
# No clipboard access API available
pass
else:
nt.assert_is_instance(a, str)
def test_LSString():
lss = text.LSString("abc\ndef")
nt.assert_equal(lss.l, ['abc', 'def'])
nt.assert_equal(lss.s, 'abc def')
lss = text.LSString(os.getcwd())
nt.assert_is_instance(lss.p[0], Path)
def test_Dataset():
for i in ['thumos14', 'activitynet']:
ds = Dataset(i)
nt.assert_is_instance(ds.wrapped_dataset, DatasetBase)
# Assert main methods
nt.assert_true(hasattr(ds.wrapped_dataset, 'segments_info'))
nt.assert_true(hasattr(ds.wrapped_dataset, 'video_info'))
def test_default_pickle_model_persistor():
ds = TestAlgorithm({'model.pickle': ''})
nt.assert_is_instance(ds.persistor, PickleModelPersistor)
def test_with_custom_model_persistor():
ds = TestAlgorithmWithCustomPersistor()
nt.assert_is_instance(ds.persistor, ModelPersistor)
def test_numpy_in_seq():
import numpy
from numpy.testing.utils import assert_array_equal
for shape in SHAPES:
for dtype in DTYPES:
A = new_array(shape, dtype=dtype)
bufs = serialize_object((A,1,2,b'hello'))
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned[0], CannedArray)
tup, r = deserialize_object(bufs)
B = tup[0]
nt.assert_equal(r, [])
nt.assert_equal(A.shape, B.shape)
nt.assert_equal(A.dtype, B.dtype)
assert_array_equal(A,B)
def test_numpy_in_dict():
import numpy
from numpy.testing.utils import assert_array_equal
for shape in SHAPES:
for dtype in DTYPES:
A = new_array(shape, dtype=dtype)
bufs = serialize_object(dict(a=A,b=1,c=range(20)))
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned['a'], CannedArray)
d, r = deserialize_object(bufs)
B = d['a']
nt.assert_equal(r, [])
nt.assert_equal(A.shape, B.shape)
nt.assert_equal(A.dtype, B.dtype)
assert_array_equal(A,B)
def test_class():
@interactive
class C(object):
a=5
bufs = serialize_object(dict(C=C))
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned['C'], CannedClass)
d, r = deserialize_object(bufs)
C2 = d['C']
nt.assert_equal(C2.a, C.a)
def test_class_oldstyle():
@interactive
class C:
a=5
bufs = serialize_object(dict(C=C))
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned['C'], CannedClass)
d, r = deserialize_object(bufs)
C2 = d['C']
nt.assert_equal(C2.a, C.a)
def test_tuple():
tup = (lambda x:x, 1)
bufs = serialize_object(tup)
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned, tuple)
t2, r = deserialize_object(bufs)
nt.assert_equal(t2[0](t2[1]), tup[0](tup[1]))
def test_list():
lis = [lambda x:x, 1]
bufs = serialize_object(lis)
canned = pickle.loads(bufs[0])
nt.assert_is_instance(canned, list)
l2, r = deserialize_object(bufs)
nt.assert_equal(l2[0](l2[1]), lis[0](lis[1]))
def _data_changed(self, name, old, new):
for k,v in iteritems(new):
assert mime_pat.match(k)
nt.assert_is_instance(v, string_types)
# shell replies
def test_bad_module_all():
"""Test module with invalid __all__
https://github.com/ipython/ipython/issues/9678
"""
testsdir = os.path.dirname(__file__)
sys.path.insert(0, testsdir)
try:
results = module_completion('from bad_all import ')
nt.assert_in('puppies', results)
for r in results:
nt.assert_is_instance(r, py3compat.string_types)
finally:
sys.path.remove(testsdir)
def test_clipboard_get():
# Smoketest for clipboard access - we can't easily guarantee that the
# clipboard is accessible and has something on it, but this tries to
# exercise the relevant code anyway.
try:
a = get_ipython().hooks.clipboard_get()
except ClipboardEmpty:
# Nothing in clipboard to get
pass
except TryNext:
# No clipboard access API available
pass
else:
nt.assert_is_instance(a, unicode_type)
def test_LSString():
lss = text.LSString("abc\ndef")
nt.assert_equal(lss.l, ['abc', 'def'])
nt.assert_equal(lss.s, 'abc def')
lss = text.LSString(os.getcwd())
nt.assert_is_instance(lss.p[0], Path)
def test_validate_input_step(self):
"""Should return validation generator if step flag is supplied."""
validation_generator = self.dtm1.validate_input('00001111', step=True)
nose.assert_is_instance(validation_generator, types.GeneratorType)
configs = []
for current_state, tape in validation_generator:
configs.append((current_state, tape.copy()))
nose.assert_equal(configs[0][0], 'q0')
nose.assert_equal(str(configs[0][1]), 'TMTape(\'00001111\')')
nose.assert_equal(configs[-1][0], 'q4')
nose.assert_equal(str(configs[-1][1]), 'TMTape(\'xxxxyyyy.\')')
def test_validate_input_step(self):
"""Should return validation generator if step flag is supplied."""
validation_generator = self.nfa.validate_input('aba', step=True)
nose.assert_is_instance(validation_generator, types.GeneratorType)
nose.assert_equal(list(validation_generator), [
{'q0'}, {'q1', 'q2'}, {'q0'}, {'q1', 'q2'}
])
def test_validate_input_step(self):
"""Should return validation generator if step flag is supplied."""
validation_generator = self.dfa.validate_input('0111', step=True)
nose.assert_is_instance(validation_generator, types.GeneratorType)
nose.assert_equal(list(validation_generator), [
'q0', 'q0', 'q1', 'q2', 'q1'
])
def test_bad_module_all():
"""Test module with invalid __all__
https://github.com/ipython/ipython/issues/9678
"""
testsdir = os.path.dirname(__file__)
sys.path.insert(0, testsdir)
try:
results = module_completion('from bad_all import ')
nt.assert_in('puppies', results)
for r in results:
nt.assert_is_instance(r, py3compat.string_types)
finally:
sys.path.remove(testsdir)
def test_clipboard_get():
# Smoketest for clipboard access - we can't easily guarantee that the
# clipboard is accessible and has something on it, but this tries to
# exercise the relevant code anyway.
try:
a = get_ipython().hooks.clipboard_get()
except ClipboardEmpty:
# Nothing in clipboard to get
pass
except TryNext:
# No clipboard access API available
pass
else:
nt.assert_is_instance(a, unicode_type)
def test_LSString():
lss = text.LSString("abc\ndef")
nt.assert_equal(lss.l, ['abc', 'def'])
nt.assert_equal(lss.s, 'abc def')
lss = text.LSString(os.getcwd())
nt.assert_is_instance(lss.p[0], Path)
def test_display_handle():
ip = get_ipython()
handle = display.DisplayHandle()
nt.assert_is_instance(handle.display_id, str)
handle = display.DisplayHandle('my-id')
nt.assert_equal(handle.display_id, 'my-id')
with mock.patch.object(ip.display_pub, 'publish') as pub:
handle.display('x')
handle.update('y')
args, kwargs = pub.call_args_list[0]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('x')
},
'metadata': {},
'transient': {
'display_id': handle.display_id,
}
})
args, kwargs = pub.call_args_list[1]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('y')
},
'metadata': {},
'transient': {
'display_id': handle.display_id,
},
'update': True,
})
def test_bad_module_all():
"""Test module with invalid __all__
https://github.com/ipython/ipython/issues/9678
"""
testsdir = os.path.dirname(__file__)
sys.path.insert(0, testsdir)
try:
results = module_completion('from bad_all import ')
nt.assert_in('puppies', results)
for r in results:
nt.assert_is_instance(r, str)
finally:
sys.path.remove(testsdir)
def test_clipboard_get():
# Smoketest for clipboard access - we can't easily guarantee that the
# clipboard is accessible and has something on it, but this tries to
# exercise the relevant code anyway.
try:
a = get_ipython().hooks.clipboard_get()
except ClipboardEmpty:
# Nothing in clipboard to get
pass
except TryNext:
# No clipboard access API available
pass
else:
nt.assert_is_instance(a, str)
def test_LSString():
lss = text.LSString("abc\ndef")
nt.assert_equal(lss.l, ['abc', 'def'])
nt.assert_equal(lss.s, 'abc def')
lss = text.LSString(os.getcwd())
nt.assert_is_instance(lss.p[0], Path)
def test_display_id():
ip = get_ipython()
with mock.patch.object(ip.display_pub, 'publish') as pub:
handle = display.display('x')
nt.assert_is(handle, None)
handle = display.display('y', display_id='secret')
nt.assert_is_instance(handle, display.DisplayHandle)
handle2 = display.display('z', display_id=True)
nt.assert_is_instance(handle2, display.DisplayHandle)
nt.assert_not_equal(handle.display_id, handle2.display_id)
nt.assert_equal(pub.call_count, 3)
args, kwargs = pub.call_args_list[0]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('x')
},
'metadata': {},
})
args, kwargs = pub.call_args_list[1]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('y')
},
'metadata': {},
'transient': {
'display_id': handle.display_id,
},
})
args, kwargs = pub.call_args_list[2]
nt.assert_equal(args, ())
nt.assert_equal(kwargs, {
'data': {
'text/plain': repr('z')
},
'metadata': {},
'transient': {
'display_id': handle2.display_id,
},
})