def test_marker_object(): assert_eq("text", str(qcore.MarkerObject("text"))) with AssertRaises(TypeError): qcore.MarkerObject(b"bytes") # MarkerObjects should be unique assert_ne(qcore.MarkerObject("name"), qcore.MarkerObject("name"))
def test_slots(self): obj1 = ObjectWithSlots(1) obj2 = ObjectWithSlots(2) obj3 = ObjectWithSlots(1) assert_eq(obj1, obj3) assert_eq(obj1, obj1) assert_ne(obj1, obj2) self._check_repr_and_str('ObjectWithSlots(oid=1)', obj1)
def test_time_offset(): time_before_offset = qcore.utime() with TimeOffset(qcore.HOUR): time_during_offset = qcore.utime() time_after_offset = qcore.utime() assert_eq(time_before_offset, time_after_offset, tolerance=qcore.MINUTE) assert_ne(time_before_offset, time_during_offset, tolerance=qcore.MINUTE) assert_le(time_after_offset, time_during_offset)
def test_excluded_attributes(self): obj1 = ObjectWithExcludedAttributes(1, 1) obj2 = ObjectWithExcludedAttributes(2, 1) obj3 = ObjectWithExcludedAttributes(1, 2) assert_eq(obj1, obj2) assert_ne(obj1, obj3) assert_ne(obj2, obj3) self._check_repr_and_str('ObjectWithExcludedAttributes(attr2=1)', obj1) self._check_repr_and_str('ObjectWithExcludedAttributes(attr2=1)', obj2) self._check_repr_and_str('ObjectWithExcludedAttributes(attr2=2)', obj3)
def test_get_function_call_repr(): def dummy_function(): pass function_repr_kv = qcore.inspection.get_function_call_repr( dummy_function, ("x", ), {"k": "v"}) function_repr_kr = qcore.inspection.get_function_call_repr( dummy_function, ("x", ), {"k": "r"}) assert_eq("test_inspection.dummy_function('x',k='v')", function_repr_kv) assert_ne(function_repr_kv, function_repr_kr)
def test_clear(self): lazy_constant = ThreadLocalLazyConstant(qcore.utime) lazy_time = lazy_constant.get_value() with qcore.TimeOffset(qcore.HOUR): assert_eq(lazy_time, lazy_constant.get_value(), tolerance=qcore.MINUTE) lazy_constant.clear() assert_ne(lazy_time, lazy_constant.get_value(), tolerance=qcore.MINUTE)
def test_get_function_call_repr(): def dummy_function(): pass function_repr_kv = qcore.inspection.get_function_call_repr( dummy_function, ('x', ), {'k': 'v'}) function_repr_kr = qcore.inspection.get_function_call_repr( dummy_function, ('x', ), {'k': 'r'}) assert_eq('test_inspection.dummy_function(\'x\',k=\'v\')', function_repr_kv) assert_ne(function_repr_kv, function_repr_kr)
def test_marker_object(): assert_eq("text", six.text_type(qcore.MarkerObject("text"))) assert_is_instance(six.text_type(qcore.MarkerObject("text")), six.text_type) # bytes should work in py2 but not py3 if six.PY2: assert_eq(u"bytes", six.text_type(qcore.MarkerObject(b"bytes"))) assert_eq(b"bytes", six.binary_type(qcore.MarkerObject(b"bytes"))) else: with AssertRaises(TypeError): qcore.MarkerObject(b"bytes") # MarkerObjects should be unique assert_ne(qcore.MarkerObject("name"), qcore.MarkerObject("name"))
def test_marker_object(): assert_eq('text', six.text_type(qcore.MarkerObject('text'))) assert_is_instance(six.text_type(qcore.MarkerObject('text')), six.text_type) # bytes should work in py2 but not py3 if six.PY2: assert_eq(u'bytes', six.text_type(qcore.MarkerObject(b'bytes'))) assert_eq(b'bytes', six.binary_type(qcore.MarkerObject(b'bytes'))) else: with AssertRaises(TypeError): qcore.MarkerObject(b'bytes') # MarkerObjects should be unique assert_ne(qcore.MarkerObject('name'), qcore.MarkerObject('name'))
def test_compare_objects(self): assert_eq(SimpleObjectWithDictComparison(), SimpleObjectWithDictComparison()) assert_eq(SimpleObjectWithDictComparison(foo='bar'), SimpleObjectWithDictComparison(foo='bar')) assert_eq( SimpleObjectWithDictComparison(x=SimpleObjectWithDictComparison( y='z')), SimpleObjectWithDictComparison(x=SimpleObjectWithDictComparison( y='z'))) # not equal assert_ne(SimpleObjectWithDictComparison(x=42), SimpleObjectWithDictComparison(x=43)) assert_ne(SimpleObjectWithDictComparison(), SimpleObjectWithDictComparison(foo='bar'))
def test_compare_objects(self): assert_eq(SimpleObjectWithDictComparison(), SimpleObjectWithDictComparison()) assert_eq( SimpleObjectWithDictComparison(foo="bar"), SimpleObjectWithDictComparison(foo="bar"), ) assert_eq( SimpleObjectWithDictComparison(x=SimpleObjectWithDictComparison( y="z")), SimpleObjectWithDictComparison(x=SimpleObjectWithDictComparison( y="z")), ) # not equal assert_ne(SimpleObjectWithDictComparison(x=42), SimpleObjectWithDictComparison(x=43)) assert_ne(SimpleObjectWithDictComparison(), SimpleObjectWithDictComparison(foo="bar"))
def test_copy_public_attrs(): def f(): pass f.hi = 1 f.hello = 2 f._hi = 3 f._hello = 4 def g(): pass g.hello = 5 g._hi = 6 g._hello = 7 with AssertRaises(AttributeError): assert g.hi assert_eq(5, g.hello) assert_eq(6, g._hi) assert_eq(7, g._hello) qcore.copy_public_attrs(f, g) assert_eq(1, g.hi) assert_eq(2, g.hello) assert_eq(6, g._hi) assert_eq(7, g._hello) assert_ne(g.__code__, f.__code__) assert_ne(g.__name__, f.__name__) class A(object): pass a1 = A() a1._hey = 0 a1.hi = 1 a1.hello = 2 a1._hi = 3 a1._hello = 4 a2 = A() a2.hello = 5 a2._hi = 6 a2._hello = 7 assert_eq(0, a1._hey) assert_eq(1, a1.hi) assert_eq(2, a1.hello) assert_eq(3, a1._hi) assert_eq(4, a1._hello) with AssertRaises(AttributeError): assert a2.hi assert_eq(5, a2.hello) assert_eq(6, a2._hi) assert_eq(7, a2._hello) qcore.copy_public_attrs(a1, a2) assert_eq(0, a1._hey) assert_eq(1, a1.hi) assert_eq(2, a1.hello) assert_eq(3, a1._hi) assert_eq(4, a1._hello) with AssertRaises(AttributeError): assert a2._hey assert_eq(1, a2.hi) assert_eq(2, a2.hello) assert_eq(6, a2._hi) assert_eq(7, a2._hello)
def test_assert_ne(): assert_ne(1, 2) assert_ne('abc', 'abcd') assert_ne(None, 1) assert_ne(1.0004, 1.0005, tolerance=0.00001) assert_ne(1, 1.01, tolerance=0.001)
def test_cached_hash_wrapper(): class TestClass(object): pass w1a = qcore.CachedHashWrapper(TestClass()) w1b = qcore.CachedHashWrapper(w1a()) w2a = qcore.CachedHashWrapper(TestClass()) print("w1a", w1a) print("w1b", w1b) print("w2a", w2a) assert_is(w1a.value(), w1a()) assert_is(w1a(), w1b()) assert_is_not(w1a(), w2a()) assert_eq(w1a, w1b) assert_ne(w1a, w2a) assert_ne(w1b, w2a) assert_eq(w1a, w1b()) assert_ne(w1a, w2a()) assert_ne(w1b, w2a()) assert_eq(hash(w1a), hash(w1b)) assert_ne(hash(w1a), hash(w2a)) assert_ne(hash(w1b), hash(w2a))
def _assert_equality_both_directions(left, right, not_equal): assert_eq(left, right) assert_eq(right, left) assert_ne(not_equal, right) assert_ne(right, not_equal)
def test_assert_ne_with_failures(): with AssertRaises(AssertionError): assert_ne(1, 1) with AssertRaises(AssertionError): assert_ne('abc', 'abc') with AssertRaises(AssertionError): assert_ne(None, None) with AssertRaises(AssertionError): assert_ne(1.0004, 1.0005, tolerance=0.001) with AssertRaises(AssertionError): assert_ne(1, 1.0, tolerance=0.001) # type errors with AssertRaises(AssertionError): assert_ne('s', 1, tolerance=0.1) with AssertRaises(AssertionError): assert_ne(None, 1, tolerance=0.1) with AssertRaises(AssertionError): assert_ne(1, 1, tolerance='s')
def test_binder_equality(self): assert_eq(CachedMethods.f, CachedMethods.f) instance = CachedMethods() assert_eq(instance.f, instance.f) assert_ne(instance.f, CachedMethods.f) assert_eq(1, len({instance.f, instance.f}))
def test_assert_ne_with_failures(): with AssertRaises(AssertionError): assert_ne(1, 1) with AssertRaises(AssertionError): assert_ne("abc", "abc") with AssertRaises(AssertionError): assert_ne(None, None) with AssertRaises(AssertionError): assert_ne(1.0004, 1.0005, tolerance=0.001) with AssertRaises(AssertionError): assert_ne(1, 1.0, tolerance=0.001) # type errors with AssertRaises(AssertionError): assert_ne("s", 1, tolerance=0.1) with AssertRaises(AssertionError): assert_ne(None, 1, tolerance=0.1) with AssertRaises(AssertionError): assert_ne(1, 1, tolerance="s")
def test_assert_ne(): assert_ne(1, 2) assert_ne("abc", "abcd") assert_ne(None, 1) assert_ne(1.0004, 1.0005, tolerance=0.00001) assert_ne(1, 1.01, tolerance=0.001)