diff options
Diffstat (limited to 'test/base/test_utils.py')
| -rw-r--r-- | test/base/test_utils.py | 1028 |
1 files changed, 537 insertions, 491 deletions
diff --git a/test/base/test_utils.py b/test/base/test_utils.py index bf65d4fc9..88b865b1d 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -13,13 +13,12 @@ import inspect class _KeyedTupleTest(object): - def _fixture(self, values, labels): raise NotImplementedError() def test_empty(self): keyed_tuple = self._fixture([], []) - eq_(str(keyed_tuple), '()') + eq_(str(keyed_tuple), "()") eq_(len(keyed_tuple), 0) eq_(list(keyed_tuple.keys()), []) @@ -28,7 +27,7 @@ class _KeyedTupleTest(object): def test_values_but_no_labels(self): keyed_tuple = self._fixture([1, 2], []) - eq_(str(keyed_tuple), '(1, 2)') + eq_(str(keyed_tuple), "(1, 2)") eq_(len(keyed_tuple), 2) eq_(list(keyed_tuple.keys()), []) @@ -39,37 +38,39 @@ class _KeyedTupleTest(object): eq_(keyed_tuple[1], 2) def test_basic_creation(self): - keyed_tuple = self._fixture([1, 2], ['a', 'b']) - eq_(str(keyed_tuple), '(1, 2)') - eq_(list(keyed_tuple.keys()), ['a', 'b']) - eq_(keyed_tuple._fields, ('a', 'b')) - eq_(keyed_tuple._asdict(), {'a': 1, 'b': 2}) + keyed_tuple = self._fixture([1, 2], ["a", "b"]) + eq_(str(keyed_tuple), "(1, 2)") + eq_(list(keyed_tuple.keys()), ["a", "b"]) + eq_(keyed_tuple._fields, ("a", "b")) + eq_(keyed_tuple._asdict(), {"a": 1, "b": 2}) def test_basic_index_access(self): - keyed_tuple = self._fixture([1, 2], ['a', 'b']) + keyed_tuple = self._fixture([1, 2], ["a", "b"]) eq_(keyed_tuple[0], 1) eq_(keyed_tuple[1], 2) def should_raise(): keyed_tuple[2] + assert_raises(IndexError, should_raise) def test_basic_attribute_access(self): - keyed_tuple = self._fixture([1, 2], ['a', 'b']) + keyed_tuple = self._fixture([1, 2], ["a", "b"]) eq_(keyed_tuple.a, 1) eq_(keyed_tuple.b, 2) def should_raise(): keyed_tuple.c + assert_raises(AttributeError, should_raise) def test_none_label(self): - keyed_tuple = self._fixture([1, 2, 3], ['a', None, 'b']) - eq_(str(keyed_tuple), '(1, 2, 3)') + keyed_tuple = self._fixture([1, 2, 3], ["a", None, "b"]) + eq_(str(keyed_tuple), "(1, 2, 3)") - eq_(list(keyed_tuple.keys()), ['a', 'b']) - eq_(keyed_tuple._fields, ('a', 'b')) - eq_(keyed_tuple._asdict(), {'a': 1, 'b': 3}) + eq_(list(keyed_tuple.keys()), ["a", "b"]) + eq_(keyed_tuple._fields, ("a", "b")) + eq_(keyed_tuple._asdict(), {"a": 1, "b": 3}) # attribute access: can't get at value 2 eq_(keyed_tuple.a, 1) @@ -81,12 +82,12 @@ class _KeyedTupleTest(object): eq_(keyed_tuple[2], 3) def test_duplicate_labels(self): - keyed_tuple = self._fixture([1, 2, 3], ['a', 'b', 'b']) - eq_(str(keyed_tuple), '(1, 2, 3)') + keyed_tuple = self._fixture([1, 2, 3], ["a", "b", "b"]) + eq_(str(keyed_tuple), "(1, 2, 3)") - eq_(list(keyed_tuple.keys()), ['a', 'b', 'b']) - eq_(keyed_tuple._fields, ('a', 'b', 'b')) - eq_(keyed_tuple._asdict(), {'a': 1, 'b': 3}) + eq_(list(keyed_tuple.keys()), ["a", "b", "b"]) + eq_(keyed_tuple._fields, ("a", "b", "b")) + eq_(keyed_tuple._asdict(), {"a": 1, "b": 3}) # attribute access: can't get at value 2 eq_(keyed_tuple.a, 1) @@ -98,8 +99,8 @@ class _KeyedTupleTest(object): eq_(keyed_tuple[2], 3) def test_immutable(self): - keyed_tuple = self._fixture([1, 2], ['a', 'b']) - eq_(str(keyed_tuple), '(1, 2)') + keyed_tuple = self._fixture([1, 2], ["a", "b"]) + eq_(str(keyed_tuple), "(1, 2)") eq_(keyed_tuple.a, 1) @@ -107,20 +108,21 @@ class _KeyedTupleTest(object): def should_raise(): keyed_tuple[0] = 100 + assert_raises(TypeError, should_raise) def test_serialize(self): - keyed_tuple = self._fixture([1, 2, 3], ['a', None, 'b']) + keyed_tuple = self._fixture([1, 2, 3], ["a", None, "b"]) for loads, dumps in picklers(): kt = loads(dumps(keyed_tuple)) - eq_(str(kt), '(1, 2, 3)') + eq_(str(kt), "(1, 2, 3)") - eq_(list(kt.keys()), ['a', 'b']) - eq_(kt._fields, ('a', 'b')) - eq_(kt._asdict(), {'a': 1, 'b': 3}) + eq_(list(kt.keys()), ["a", "b"]) + eq_(kt._fields, ("a", "b")) + eq_(kt._asdict(), {"a": 1, "b": 3}) class KeyedTupleTest(_KeyedTupleTest, fixtures.TestBase): @@ -130,7 +132,7 @@ class KeyedTupleTest(_KeyedTupleTest, fixtures.TestBase): class LWKeyedTupleTest(_KeyedTupleTest, fixtures.TestBase): def _fixture(self, values, labels): - return util.lightweight_named_tuple('n', labels)(values) + return util.lightweight_named_tuple("n", labels)(values) class WeakSequenceTest(fixtures.TestBase): @@ -138,6 +140,7 @@ class WeakSequenceTest(fixtures.TestBase): def test_cleanout_elements(self): class Foo(object): pass + f1, f2, f3 = Foo(), Foo(), Foo() w = WeakSequence([f1, f2, f3]) eq_(len(w), 3) @@ -151,6 +154,7 @@ class WeakSequenceTest(fixtures.TestBase): def test_cleanout_appended(self): class Foo(object): pass + f1, f2, f3 = Foo(), Foo(), Foo() w = WeakSequence() w.append(f1) @@ -165,63 +169,63 @@ class WeakSequenceTest(fixtures.TestBase): class OrderedDictTest(fixtures.TestBase): - def test_odict(self): o = util.OrderedDict() - o['a'] = 1 - o['b'] = 2 - o['snack'] = 'attack' - o['c'] = 3 + o["a"] = 1 + o["b"] = 2 + o["snack"] = "attack" + o["c"] = 3 - eq_(list(o.keys()), ['a', 'b', 'snack', 'c']) - eq_(list(o.values()), [1, 2, 'attack', 3]) + eq_(list(o.keys()), ["a", "b", "snack", "c"]) + eq_(list(o.values()), [1, 2, "attack", 3]) - o.pop('snack') - eq_(list(o.keys()), ['a', 'b', 'c']) + o.pop("snack") + eq_(list(o.keys()), ["a", "b", "c"]) eq_(list(o.values()), [1, 2, 3]) try: - o.pop('eep') + o.pop("eep") assert False except KeyError: pass - eq_(o.pop('eep', 'woot'), 'woot') + eq_(o.pop("eep", "woot"), "woot") try: - o.pop('whiff', 'bang', 'pow') + o.pop("whiff", "bang", "pow") assert False except TypeError: pass - eq_(list(o.keys()), ['a', 'b', 'c']) + eq_(list(o.keys()), ["a", "b", "c"]) eq_(list(o.values()), [1, 2, 3]) o2 = util.OrderedDict(d=4) - o2['e'] = 5 + o2["e"] = 5 - eq_(list(o2.keys()), ['d', 'e']) + eq_(list(o2.keys()), ["d", "e"]) eq_(list(o2.values()), [4, 5]) o.update(o2) - eq_(list(o.keys()), ['a', 'b', 'c', 'd', 'e']) + eq_(list(o.keys()), ["a", "b", "c", "d", "e"]) eq_(list(o.values()), [1, 2, 3, 4, 5]) - o.setdefault('c', 'zzz') - o.setdefault('f', 6) - eq_(list(o.keys()), ['a', 'b', 'c', 'd', 'e', 'f']) + o.setdefault("c", "zzz") + o.setdefault("f", 6) + eq_(list(o.keys()), ["a", "b", "c", "d", "e", "f"]) eq_(list(o.values()), [1, 2, 3, 4, 5, 6]) def test_odict_constructor(self): - o = util.OrderedDict([('name', 'jbe'), - ('fullname', 'jonathan'), ('password', '')]) - eq_(list(o.keys()), ['name', 'fullname', 'password']) + o = util.OrderedDict( + [("name", "jbe"), ("fullname", "jonathan"), ("password", "")] + ) + eq_(list(o.keys()), ["name", "fullname", "password"]) def test_odict_copy(self): o = util.OrderedDict() o["zzz"] = 1 o["aaa"] = 2 - eq_(list(o.keys()), ['zzz', 'aaa']) + eq_(list(o.keys()), ["zzz", "aaa"]) o2 = o.copy() eq_(list(o2.keys()), list(o.keys())) @@ -231,7 +235,6 @@ class OrderedDictTest(fixtures.TestBase): class OrderedSetTest(fixtures.TestBase): - def test_mutators_against_iter(self): # testing a set modified against an iterator o = util.OrderedSet([3, 2, 4, 5]) @@ -242,7 +245,6 @@ class OrderedSetTest(fixtures.TestBase): class FrozenDictTest(fixtures.TestBase): - def test_serialize(self): d = util.immutabledict({1: 2, 3: 4}) for loads, dumps in picklers(): @@ -250,7 +252,6 @@ class FrozenDictTest(fixtures.TestBase): class MemoizedAttrTest(fixtures.TestBase): - def test_memoized_property(self): val = [20] @@ -263,11 +264,11 @@ class MemoizedAttrTest(fixtures.TestBase): ne_(Foo.bar, None) f1 = Foo() - assert 'bar' not in f1.__dict__ + assert "bar" not in f1.__dict__ eq_(f1.bar, 20) eq_(f1.bar, 20) eq_(val[0], 21) - eq_(f1.__dict__['bar'], 20) + eq_(f1.__dict__["bar"], 20) def test_memoized_instancemethod(self): val = [20] @@ -282,7 +283,7 @@ class MemoizedAttrTest(fixtures.TestBase): assert inspect.ismethod(Foo().bar) ne_(Foo.bar, None) f1 = Foo() - assert 'bar' not in f1.__dict__ + assert "bar" not in f1.__dict__ eq_(f1.bar(), 20) eq_(f1.bar(), 20) eq_(val[0], 21) @@ -291,7 +292,7 @@ class MemoizedAttrTest(fixtures.TestBase): canary = mock.Mock() class Foob(util.MemoizedSlots): - __slots__ = ('foo_bar', 'gogo') + __slots__ = ("foo_bar", "gogo") def _memoized_method_gogo(self): canary.method() @@ -350,7 +351,6 @@ class WrapCallableTest(fixtures.TestBase): def test_wrapping_update_wrapper_cls_noclsdocstring(self): class MyFancyDefault(object): - def __call__(self): """run the fancy default""" return 10 @@ -374,7 +374,6 @@ class WrapCallableTest(fixtures.TestBase): def test_wrapping_update_wrapper_cls_noclsdocstring_nomethdocstring(self): class MyFancyDefault(object): - def __call__(self): return 10 @@ -388,10 +387,12 @@ class WrapCallableTest(fixtures.TestBase): return x import functools + my_functools_default = functools.partial(my_default, 5) c = util.wrap_callable( - lambda: my_functools_default(), my_functools_default) + lambda: my_functools_default(), my_functools_default + ) eq_(c.__name__, "partial") eq_(c.__doc__, my_functools_default.__call__.__doc__) eq_(c(), 5) @@ -399,59 +400,42 @@ class WrapCallableTest(fixtures.TestBase): class ToListTest(fixtures.TestBase): def test_from_string(self): - eq_( - util.to_list("xyz"), - ["xyz"] - ) + eq_(util.to_list("xyz"), ["xyz"]) def test_from_set(self): spec = util.to_list(set([1, 2, 3])) assert isinstance(spec, list) - eq_( - sorted(spec), - [1, 2, 3] - ) + eq_(sorted(spec), [1, 2, 3]) def test_from_dict(self): spec = util.to_list({1: "a", 2: "b", 3: "c"}) assert isinstance(spec, list) - eq_( - sorted(spec), - [1, 2, 3] - ) + eq_(sorted(spec), [1, 2, 3]) def test_from_tuple(self): - eq_( - util.to_list((1, 2, 3)), - [1, 2, 3] - ) + eq_(util.to_list((1, 2, 3)), [1, 2, 3]) def test_from_bytes(self): - eq_( - util.to_list(compat.b('abc')), - [compat.b('abc')] - ) + eq_(util.to_list(compat.b("abc")), [compat.b("abc")]) eq_( - util.to_list([ - compat.b('abc'), compat.b('def')]), - [compat.b('abc'), compat.b('def')] + util.to_list([compat.b("abc"), compat.b("def")]), + [compat.b("abc"), compat.b("def")], ) class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): - def test_in(self): cc = sql.ColumnCollection() - cc.add(sql.column('col1')) - cc.add(sql.column('col2')) - cc.add(sql.column('col3')) - assert 'col1' in cc - assert 'col2' in cc + cc.add(sql.column("col1")) + cc.add(sql.column("col2")) + cc.add(sql.column("col3")) + assert "col1" in cc + assert "col2" in cc try: - cc['col1'] in cc + cc["col1"] in cc assert False except exc.ArgumentError as e: eq_(str(e), "__contains__ requires a string argument") @@ -460,9 +444,9 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): cc1 = sql.ColumnCollection() cc2 = sql.ColumnCollection() cc3 = sql.ColumnCollection() - c1 = sql.column('col1') - c2 = c1.label('col2') - c3 = sql.column('col3') + c1 = sql.column("col1") + c2 = c1.label("col2") + c3 = sql.column("col3") cc1.add(c1) cc2.add(c2) cc3.add(c3) @@ -473,10 +457,12 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_dupes_add(self): cc = sql.ColumnCollection() - c1, c2a, c3, c2b = (column('c1'), - column('c2'), - column('c3'), - column('c2')) + c1, c2a, c3, c2b = ( + column("c1"), + column("c2"), + column("c3"), + column("c2"), + ) cc.add(c1) cc.add(c2a) @@ -500,9 +486,7 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_identical_dupe_add(self): cc = sql.ColumnCollection() - c1, c2, c3 = (column('c1'), - column('c2'), - column('c3')) + c1, c2, c3 = (column("c1"), column("c2"), column("c3")) cc.add(c1) cc.add(c2) @@ -512,8 +496,7 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): eq_(cc._all_columns, [c1, c2, c3]) self.assert_compile( - cc == [c1, c2, c3], - "c1 = c1 AND c2 = c2 AND c3 = c3" + cc == [c1, c2, c3], "c1 = c1 AND c2 = c2 AND c3 = c3" ) # for iter, c2a is replaced by c2b, ordering @@ -528,17 +511,18 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): eq_(list(ci), [c1, c2, c3]) self.assert_compile( - ci == [c1, c2, c3], - "c1 = c1 AND c2 = c2 AND c3 = c3" + ci == [c1, c2, c3], "c1 = c1 AND c2 = c2 AND c3 = c3" ) def test_replace(self): cc = sql.ColumnCollection() - c1, c2a, c3, c2b = (column('c1'), - column('c2'), - column('c3'), - column('c2')) + c1, c2a, c3, c2b = ( + column("c1"), + column("c2"), + column("c3"), + column("c2"), + ) cc.add(c1) cc.add(c2a) @@ -559,11 +543,13 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_replace_key_matches(self): cc = sql.ColumnCollection() - c1, c2a, c3, c2b = (column('c1'), - column('c2'), - column('c3'), - column('X')) - c2b.key = 'c2' + c1, c2a, c3, c2b = ( + column("c1"), + column("c2"), + column("c3"), + column("X"), + ) + c2b.key = "c2" cc.add(c1) cc.add(c2a) @@ -584,11 +570,13 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_replace_name_matches(self): cc = sql.ColumnCollection() - c1, c2a, c3, c2b = (column('c1'), - column('c2'), - column('c3'), - column('c2')) - c2b.key = 'X' + c1, c2a, c3, c2b = ( + column("c1"), + column("c2"), + column("c3"), + column("c2"), + ) + c2b.key = "X" cc.add(c1) cc.add(c2a) @@ -609,8 +597,8 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_replace_no_match(self): cc = sql.ColumnCollection() - c1, c2, c3, c4 = column('c1'), column('c2'), column('c3'), column('c4') - c4.key = 'X' + c1, c2, c3, c4 = column("c1"), column("c2"), column("c3"), column("c4") + c4.key = "X" cc.add(c1) cc.add(c2) @@ -631,10 +619,12 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_dupes_extend(self): cc = sql.ColumnCollection() - c1, c2a, c3, c2b = (column('c1'), - column('c2'), - column('c3'), - column('c2')) + c1, c2a, c3, c2b = ( + column("c1"), + column("c2"), + column("c3"), + column("c2"), + ) cc.add(c1) cc.add(c2a) @@ -658,10 +648,12 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_dupes_update(self): cc = sql.ColumnCollection() - c1, c2a, c3, c2b = (column('c1'), - column('c2'), - column('c3'), - column('c2')) + c1, c2a, c3, c2b = ( + column("c1"), + column("c2"), + column("c3"), + column("c2"), + ) cc.add(c1) cc.add(c2a) @@ -681,11 +673,13 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_extend_existing(self): cc = sql.ColumnCollection() - c1, c2, c3, c4, c5 = (column('c1'), - column('c2'), - column('c3'), - column('c4'), - column('c5')) + c1, c2, c3, c4, c5 = ( + column("c1"), + column("c2"), + column("c3"), + column("c4"), + column("c5"), + ) cc.extend([c1, c2]) eq_(cc._all_columns, [c1, c2]) @@ -699,24 +693,25 @@ class ColumnCollectionTest(testing.AssertsCompiledSQL, fixtures.TestBase): def test_update_existing(self): cc = sql.ColumnCollection() - c1, c2, c3, c4, c5 = (column('c1'), - column('c2'), - column('c3'), - column('c4'), - column('c5')) + c1, c2, c3, c4, c5 = ( + column("c1"), + column("c2"), + column("c3"), + column("c4"), + column("c5"), + ) - cc.update([('c1', c1), ('c2', c2)]) + cc.update([("c1", c1), ("c2", c2)]) eq_(cc._all_columns, [c1, c2]) - cc.update([('c3', c3)]) + cc.update([("c3", c3)]) eq_(cc._all_columns, [c1, c2, c3]) - cc.update([('c4', c4), ('c2', c2), ('c5', c5)]) + cc.update([("c4", c4), ("c2", c2), ("c5", c5)]) eq_(cc._all_columns, [c1, c2, c3, c4, c5]) class LRUTest(fixtures.TestBase): - def test_lru(self): class item(object): def __init__(self, id): @@ -725,7 +720,7 @@ class LRUTest(fixtures.TestBase): def __str__(self): return "item id %d" % self.id - lru = util.LRUCache(10, threshold=.2) + lru = util.LRUCache(10, threshold=0.2) for id in range(1, 20): lru[id] = item(id) @@ -764,10 +759,17 @@ class ImmutableSubclass(str): class FlattenIteratorTest(fixtures.TestBase): - def test_flatten(self): - assert list(util.flatten_iterator([[1, 2, 3], [4, 5, 6], 7, - 8])) == [1, 2, 3, 4, 5, 6, 7, 8] + assert list(util.flatten_iterator([[1, 2, 3], [4, 5, 6], 7, 8])) == [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + ] def test_str_with_iter(self): """ensure that a str object with an __iter__ method (like in @@ -777,15 +779,14 @@ class FlattenIteratorTest(fixtures.TestBase): class IterString(str): def __iter__(self): - return iter(self + '') + return iter(self + "") - iter_list = [IterString('asdf'), [IterString('x'), IterString('y')]] + iter_list = [IterString("asdf"), [IterString("x"), IterString("y")]] - assert list(util.flatten_iterator(iter_list)) == ['asdf', 'x', 'y'] + assert list(util.flatten_iterator(iter_list)) == ["asdf", "x", "y"] class HashOverride(object): - def __init__(self, value=None): self.value = value @@ -794,9 +795,9 @@ class HashOverride(object): class EqOverride(object): - def __init__(self, value=None): self.value = value + __hash__ = object.__hash__ def __eq__(self, other): @@ -813,7 +814,6 @@ class EqOverride(object): class HashEqOverride(object): - def __init__(self, value=None): self.value = value @@ -834,7 +834,6 @@ class HashEqOverride(object): class IdentitySetTest(fixtures.TestBase): - def assert_eq(self, identityset, expected_iterable): expected = sorted([id(o) for o in expected_iterable]) found = sorted([id(o) for o in identityset]) @@ -876,10 +875,7 @@ class IdentitySetTest(fixtures.TestBase): o1, o2, o3 = object(), object(), object() ids1 = IdentitySet([o1]) ids2 = IdentitySet([o1, o2, o3]) - eq_( - ids2 - ids1, - IdentitySet([o2, o3]) - ) + eq_(ids2 - ids1, IdentitySet([o2, o3])) ids2 -= ids1 eq_(ids2, IdentitySet([o2, o3])) @@ -925,6 +921,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 <= not_an_identity_set + self._assert_unorderable_types(should_raise) def test_dunder_lt(self): @@ -946,6 +943,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 < not_an_identity_set + self._assert_unorderable_types(should_raise) def test_dunder_ge(self): @@ -967,6 +965,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 >= not_an_identity_set + self._assert_unorderable_types(should_raise) def test_dunder_gt(self): @@ -988,6 +987,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 > not_an_identity_set + self._assert_unorderable_types(should_raise) def test_issubset(self): @@ -1076,6 +1076,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 | not_an_identity_set + assert_raises(TypeError, should_raise) def test_update(self): @@ -1102,6 +1103,7 @@ class IdentitySetTest(fixtures.TestBase): unique = util.IdentitySet([1]) not_an_identity_set = object() unique |= not_an_identity_set + assert_raises(TypeError, should_raise) def test_difference(self): @@ -1158,6 +1160,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() unique1 - not_an_identity_set + assert_raises(TypeError, should_raise) def test_difference_update(self): @@ -1210,6 +1213,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 & not_an_identity_set + assert_raises(TypeError, should_raise) def test_intersection_update(self): @@ -1244,7 +1248,8 @@ class IdentitySetTest(fixtures.TestBase): # not an IdentitySet not_an_identity_set = object() assert_raises( - TypeError, unique1.symmetric_difference, not_an_identity_set) + TypeError, unique1.symmetric_difference, not_an_identity_set + ) def test_dunder_xor(self): _, _, twin1, twin2, _, _ = self._create_sets() @@ -1273,6 +1278,7 @@ class IdentitySetTest(fixtures.TestBase): def should_raise(): not_an_identity_set = object() return unique1 ^ not_an_identity_set + assert_raises(TypeError, should_raise) def test_symmetric_difference_update(self): @@ -1291,13 +1297,14 @@ class IdentitySetTest(fixtures.TestBase): def _assert_unorderable_types(self, callable_): if util.py36: assert_raises_message( - TypeError, 'not supported between instances of', callable_) + TypeError, "not supported between instances of", callable_ + ) elif util.py3k: - assert_raises_message( - TypeError, 'unorderable types', callable_) + assert_raises_message(TypeError, "unorderable types", callable_) else: assert_raises_message( - TypeError, 'cannot compare sets using cmp()', callable_) + TypeError, "cannot compare sets using cmp()", callable_ + ) def test_basic_sanity(self): IdentitySet = util.IdentitySet @@ -1314,7 +1321,7 @@ class IdentitySetTest(fixtures.TestBase): # explicit __eq__ and __ne__ tests assert ids != None # noqa - assert not(ids == None) # noqa + assert not (ids == None) # noqa ne_(ids, IdentitySet([o1, o2, o3])) ids.clear() @@ -1349,9 +1356,9 @@ class IdentitySetTest(fixtures.TestBase): ids.symmetric_difference_update(isuper) ids ^= isuper - ids.update('foobar') + ids.update("foobar") try: - ids |= 'foobar' + ids |= "foobar" assert False except TypeError: assert True @@ -1368,7 +1375,6 @@ class IdentitySetTest(fixtures.TestBase): class OrderedIdentitySetTest(fixtures.TestBase): - def assert_eq(self, identityset, expected_iterable): expected = [id(o) for o in expected_iterable] found = [id(o) for o in identityset] @@ -1384,8 +1390,15 @@ class OrderedIdentitySetTest(fixtures.TestBase): elem = object eq_ = self.assert_eq - a, b, c, d, e, f, g = \ - elem(), elem(), elem(), elem(), elem(), elem(), elem() + a, b, c, d, e, f, g = ( + elem(), + elem(), + elem(), + elem(), + elem(), + elem(), + elem(), + ) s1 = util.OrderedIdentitySet([a, b, c]) s2 = util.OrderedIdentitySet([d, e, f]) @@ -1396,16 +1409,14 @@ class OrderedIdentitySetTest(fixtures.TestBase): class DictlikeIteritemsTest(fixtures.TestBase): - baseline = set([('a', 1), ('b', 2), ('c', 3)]) + baseline = set([("a", 1), ("b", 2), ("c", 3)]) def _ok(self, instance): iterator = util.dictlike_iteritems(instance) eq_(set(iterator), self.baseline) def _notok(self, instance): - assert_raises(TypeError, - util.dictlike_iteritems, - instance) + assert_raises(TypeError, util.dictlike_iteritems, instance) def test_dict(self): d = dict(a=1, b=2, c=3) @@ -1414,12 +1425,15 @@ class DictlikeIteritemsTest(fixtures.TestBase): def test_subdict(self): class subdict(dict): pass + d = subdict(a=1, b=2, c=3) self._ok(d) if util.py2k: + def test_UserDict(self): import UserDict + d = UserDict.UserDict(a=1, b=2, c=3) self._ok(d) @@ -1427,52 +1441,59 @@ class DictlikeIteritemsTest(fixtures.TestBase): self._notok(object()) if util.py2k: + def test_duck_1(self): class duck1(object): def iteritems(duck): return iter(self.baseline) + self._ok(duck1()) def test_duck_2(self): class duck2(object): def items(duck): return list(self.baseline) + self._ok(duck2()) if util.py2k: + def test_duck_3(self): class duck3(object): def iterkeys(duck): - return iter(['a', 'b', 'c']) + return iter(["a", "b", "c"]) def __getitem__(duck, key): return dict(a=1, b=2, c=3).get(key) + self._ok(duck3()) def test_duck_4(self): class duck4(object): def iterkeys(duck): - return iter(['a', 'b', 'c']) + return iter(["a", "b", "c"]) + self._notok(duck4()) def test_duck_5(self): class duck5(object): def keys(duck): - return ['a', 'b', 'c'] + return ["a", "b", "c"] def get(duck, key): return dict(a=1, b=2, c=3).get(key) + self._ok(duck5()) def test_duck_6(self): class duck6(object): def keys(duck): - return ['a', 'b', 'c'] + return ["a", "b", "c"] + self._notok(duck6()) class DuckTypeCollectionTest(fixtures.TestBase): - def test_sets(self): class SetLike(object): def add(self): @@ -1481,21 +1502,18 @@ class DuckTypeCollectionTest(fixtures.TestBase): class ForcedSet(list): __emulates__ = set - for type_ in (set, - SetLike, - ForcedSet): + for type_ in (set, SetLike, ForcedSet): eq_(util.duck_type_collection(type_), set) instance = type_() eq_(util.duck_type_collection(instance), set) - for type_ in (frozenset, ): + for type_ in (frozenset,): is_(util.duck_type_collection(type_), None) instance = type_() is_(util.duck_type_collection(instance), None) class PublicFactoryTest(fixtures.TestBase): - def _fixture(self): class Thingy(object): def __init__(self, value): @@ -1511,8 +1529,7 @@ class PublicFactoryTest(fixtures.TestBase): def test_classmethod(self): Thingy = self._fixture() - foob = langhelpers.public_factory( - Thingy.foobar, ".sql.elements.foob") + foob = langhelpers.public_factory(Thingy.foobar, ".sql.elements.foob") eq_(foob(3, 4).value, 7) eq_(foob(x=3, y=4).value, 7) eq_(foob.__doc__, "do the foobar") @@ -1521,20 +1538,18 @@ class PublicFactoryTest(fixtures.TestBase): def test_constructor(self): Thingy = self._fixture() - foob = langhelpers.public_factory( - Thingy, ".sql.elements.foob") + foob = langhelpers.public_factory(Thingy, ".sql.elements.foob") eq_(foob(7).value, 7) eq_(foob(value=7).value, 7) eq_(foob.__doc__, "make a thingy") eq_(foob.__module__, "sqlalchemy.sql.elements") assert Thingy.__init__.__doc__.startswith( - "Construct a new :class:`.Thingy` object.") + "Construct a new :class:`.Thingy` object." + ) class ArgInspectionTest(fixtures.TestBase): - def test_get_cls_kwargs(self): - class A(object): def __init__(self, a): pass @@ -1600,26 +1615,25 @@ class ArgInspectionTest(fixtures.TestBase): def test(cls, *expected): eq_(set(util.get_cls_kwargs(cls)), set(expected)) - test(A, 'a') - test(A1, 'a1') - test(A11, 'a11', 'a1') - test(B, 'b') - test(B1, 'b1', 'b') - test(AB, 'ab') - test(BA, 'ba', 'b', 'a') - test(BA1, 'ba', 'b', 'a') - test(CAB, 'a') - test(CBA, 'b', 'a') - test(CAB1, 'a') - test(CB1A, 'b1', 'b', 'a') - test(CB2A, 'b2') + test(A, "a") + test(A1, "a1") + test(A11, "a11", "a1") + test(B, "b") + test(B1, "b1", "b") + test(AB, "ab") + test(BA, "ba", "b", "a") + test(BA1, "ba", "b", "a") + test(CAB, "a") + test(CBA, "b", "a") + test(CAB1, "a") + test(CB1A, "b1", "b", "a") + test(CB2A, "b2") test(CB1A1, "a1", "b1", "b") test(D) test(BA2, "a", "b") test(A11B1, "a1", "a11", "b", "b1") def test_get_func_kwargs(self): - def f1(): pass @@ -1636,74 +1650,72 @@ class ArgInspectionTest(fixtures.TestBase): eq_(set(util.get_func_kwargs(fn)), set(expected)) test(f1) - test(f2, 'foo') + test(f2, "foo") test(f3) test(f4) def test_callable_argspec_fn(self): def foo(x, y, **kw): pass - eq_( - get_callable_argspec(foo), - (['x', 'y'], None, 'kw', None) - ) + + eq_(get_callable_argspec(foo), (["x", "y"], None, "kw", None)) def test_callable_argspec_fn_no_self(self): def foo(x, y, **kw): pass + eq_( get_callable_argspec(foo, no_self=True), - (['x', 'y'], None, 'kw', None) + (["x", "y"], None, "kw", None), ) def test_callable_argspec_fn_no_self_but_self(self): def foo(self, x, y, **kw): pass + eq_( get_callable_argspec(foo, no_self=True), - (['self', 'x', 'y'], None, 'kw', None) + (["self", "x", "y"], None, "kw", None), ) - @fails_if(lambda: util.pypy, "pypy returns plain *arg, **kw") + @fails_if(lambda: util.pypy, "pypy returns plain *arg, **kw") def test_callable_argspec_py_builtin(self): import datetime - assert_raises( - TypeError, - get_callable_argspec, datetime.datetime.now - ) - @fails_if(lambda: util.pypy, "pypy returns plain *arg, **kw") + assert_raises(TypeError, get_callable_argspec, datetime.datetime.now) + + @fails_if(lambda: util.pypy, "pypy returns plain *arg, **kw") def test_callable_argspec_obj_init(self): - assert_raises( - TypeError, - get_callable_argspec, object - ) + assert_raises(TypeError, get_callable_argspec, object) def test_callable_argspec_method(self): class Foo(object): def foo(self, x, y, **kw): pass + eq_( get_callable_argspec(Foo.foo), - (['self', 'x', 'y'], None, 'kw', None) + (["self", "x", "y"], None, "kw", None), ) def test_callable_argspec_instance_method_no_self(self): class Foo(object): def foo(self, x, y, **kw): pass + eq_( get_callable_argspec(Foo().foo, no_self=True), - (['x', 'y'], None, 'kw', None) + (["x", "y"], None, "kw", None), ) def test_callable_argspec_unbound_method_no_self(self): class Foo(object): def foo(self, x, y, **kw): pass + eq_( get_callable_argspec(Foo.foo, no_self=True), - (['self', 'x', 'y'], None, 'kw', None) + (["self", "x", "y"], None, "kw", None), ) def test_callable_argspec_init(self): @@ -1711,10 +1723,7 @@ class ArgInspectionTest(fixtures.TestBase): def __init__(self, x, y): pass - eq_( - get_callable_argspec(Foo), - (['self', 'x', 'y'], None, None, None) - ) + eq_(get_callable_argspec(Foo), (["self", "x", "y"], None, None, None)) def test_callable_argspec_init_no_self(self): class Foo(object): @@ -1723,58 +1732,56 @@ class ArgInspectionTest(fixtures.TestBase): eq_( get_callable_argspec(Foo, no_self=True), - (['x', 'y'], None, None, None) + (["x", "y"], None, None, None), ) def test_callable_argspec_call(self): class Foo(object): def __call__(self, x, y): pass + eq_( - get_callable_argspec(Foo()), - (['self', 'x', 'y'], None, None, None) + get_callable_argspec(Foo()), (["self", "x", "y"], None, None, None) ) def test_callable_argspec_call_no_self(self): class Foo(object): def __call__(self, x, y): pass + eq_( get_callable_argspec(Foo(), no_self=True), - (['x', 'y'], None, None, None) + (["x", "y"], None, None, None), ) - @fails_if(lambda: util.pypy, "pypy returns plain *arg, **kw") + @fails_if(lambda: util.pypy, "pypy returns plain *arg, **kw") def test_callable_argspec_partial(self): from functools import partial def foo(x, y, z, **kw): pass + bar = partial(foo, 5) - assert_raises( - TypeError, - get_callable_argspec, bar - ) + assert_raises(TypeError, get_callable_argspec, bar) class SymbolTest(fixtures.TestBase): - def test_basic(self): - sym1 = util.symbol('foo') - assert sym1.name == 'foo' - sym2 = util.symbol('foo') + sym1 = util.symbol("foo") + assert sym1.name == "foo" + sym2 = util.symbol("foo") assert sym1 is sym2 assert sym1 == sym2 - sym3 = util.symbol('bar') + sym3 = util.symbol("bar") assert sym1 is not sym3 assert sym1 != sym3 def test_pickle(self): - sym1 = util.symbol('foo') - sym2 = util.symbol('foo') + sym1 = util.symbol("foo") + sym2 = util.symbol("foo") assert sym1 is sym2 @@ -1790,18 +1797,18 @@ class SymbolTest(fixtures.TestBase): assert rt is sym2 def test_bitflags(self): - sym1 = util.symbol('sym1', canonical=1) - sym2 = util.symbol('sym2', canonical=2) + sym1 = util.symbol("sym1", canonical=1) + sym2 = util.symbol("sym2", canonical=2) assert sym1 & sym1 assert not sym1 & sym2 assert not sym1 & sym1 & sym2 def test_composites(self): - sym1 = util.symbol('sym1', canonical=1) - sym2 = util.symbol('sym2', canonical=2) - sym3 = util.symbol('sym3', canonical=4) - sym4 = util.symbol('sym4', canonical=8) + sym1 = util.symbol("sym1", canonical=1) + sym2 = util.symbol("sym2", canonical=2) + sym3 = util.symbol("sym3", canonical=4) + sym4 = util.symbol("sym4", canonical=8) assert sym1 & (sym2 | sym1 | sym4) assert not sym1 & (sym2 | sym3) @@ -1811,7 +1818,6 @@ class SymbolTest(fixtures.TestBase): class TestFormatArgspec(fixtures.TestBase): - def test_specs(self): def test(fn, wanted, grouped=None): if grouped is None: @@ -1820,91 +1826,184 @@ class TestFormatArgspec(fixtures.TestBase): parsed = util.format_argspec_plus(fn, grouped=grouped) eq_(parsed, wanted) - test(lambda: None, - {'args': '()', 'self_arg': None, - 'apply_kw': '()', 'apply_pos': '()'}) + test( + lambda: None, + { + "args": "()", + "self_arg": None, + "apply_kw": "()", + "apply_pos": "()", + }, + ) - test(lambda: None, - {'args': '', 'self_arg': None, - 'apply_kw': '', 'apply_pos': ''}, - grouped=False) + test( + lambda: None, + {"args": "", "self_arg": None, "apply_kw": "", "apply_pos": ""}, + grouped=False, + ) - test(lambda self: None, - {'args': '(self)', 'self_arg': 'self', - 'apply_kw': '(self)', 'apply_pos': '(self)'}) + test( + lambda self: None, + { + "args": "(self)", + "self_arg": "self", + "apply_kw": "(self)", + "apply_pos": "(self)", + }, + ) - test(lambda self: None, - {'args': 'self', 'self_arg': 'self', - 'apply_kw': 'self', 'apply_pos': 'self'}, - grouped=False) + test( + lambda self: None, + { + "args": "self", + "self_arg": "self", + "apply_kw": "self", + "apply_pos": "self", + }, + grouped=False, + ) - test(lambda *a: None, - {'args': '(*a)', 'self_arg': 'a[0]', - 'apply_kw': '(*a)', 'apply_pos': '(*a)'}) + test( + lambda *a: None, + { + "args": "(*a)", + "self_arg": "a[0]", + "apply_kw": "(*a)", + "apply_pos": "(*a)", + }, + ) - test(lambda **kw: None, - {'args': '(**kw)', 'self_arg': None, - 'apply_kw': '(**kw)', 'apply_pos': '(**kw)'}) + test( + lambda **kw: None, + { + "args": "(**kw)", + "self_arg": None, + "apply_kw": "(**kw)", + "apply_pos": "(**kw)", + }, + ) - test(lambda *a, **kw: None, - {'args': '(*a, **kw)', 'self_arg': 'a[0]', - 'apply_kw': '(*a, **kw)', 'apply_pos': '(*a, **kw)'}) + test( + lambda *a, **kw: None, + { + "args": "(*a, **kw)", + "self_arg": "a[0]", + "apply_kw": "(*a, **kw)", + "apply_pos": "(*a, **kw)", + }, + ) - test(lambda a, *b: None, - {'args': '(a, *b)', 'self_arg': 'a', - 'apply_kw': '(a, *b)', 'apply_pos': '(a, *b)'}) + test( + lambda a, *b: None, + { + "args": "(a, *b)", + "self_arg": "a", + "apply_kw": "(a, *b)", + "apply_pos": "(a, *b)", + }, + ) - test(lambda a, **b: None, - {'args': '(a, **b)', 'self_arg': 'a', - 'apply_kw': '(a, **b)', 'apply_pos': '(a, **b)'}) + test( + lambda a, **b: None, + { + "args": "(a, **b)", + "self_arg": "a", + "apply_kw": "(a, **b)", + "apply_pos": "(a, **b)", + }, + ) - test(lambda a, *b, **c: None, - {'args': '(a, *b, **c)', 'self_arg': 'a', - 'apply_kw': '(a, *b, **c)', 'apply_pos': '(a, *b, **c)'}) + test( + lambda a, *b, **c: None, + { + "args": "(a, *b, **c)", + "self_arg": "a", + "apply_kw": "(a, *b, **c)", + "apply_pos": "(a, *b, **c)", + }, + ) - test(lambda a, b=1, **c: None, - {'args': '(a, b=1, **c)', 'self_arg': 'a', - 'apply_kw': '(a, b=b, **c)', 'apply_pos': '(a, b, **c)'}) + test( + lambda a, b=1, **c: None, + { + "args": "(a, b=1, **c)", + "self_arg": "a", + "apply_kw": "(a, b=b, **c)", + "apply_pos": "(a, b, **c)", + }, + ) - test(lambda a=1, b=2: None, - {'args': '(a=1, b=2)', 'self_arg': 'a', - 'apply_kw': '(a=a, b=b)', 'apply_pos': '(a, b)'}) + test( + lambda a=1, b=2: None, + { + "args": "(a=1, b=2)", + "self_arg": "a", + "apply_kw": "(a=a, b=b)", + "apply_pos": "(a, b)", + }, + ) - test(lambda a=1, b=2: None, - {'args': 'a=1, b=2', 'self_arg': 'a', - 'apply_kw': 'a=a, b=b', 'apply_pos': 'a, b'}, - grouped=False) + test( + lambda a=1, b=2: None, + { + "args": "a=1, b=2", + "self_arg": "a", + "apply_kw": "a=a, b=b", + "apply_pos": "a, b", + }, + grouped=False, + ) - @testing.fails_if(lambda: util.pypy, - "pypy doesn't report Obj.__init__ as object.__init__") + @testing.fails_if( + lambda: util.pypy, + "pypy doesn't report Obj.__init__ as object.__init__", + ) def test_init_grouped(self): object_spec = { - 'args': '(self)', 'self_arg': 'self', - 'apply_pos': '(self)', 'apply_kw': '(self)'} + "args": "(self)", + "self_arg": "self", + "apply_pos": "(self)", + "apply_kw": "(self)", + } wrapper_spec = { - 'args': '(self, *args, **kwargs)', 'self_arg': 'self', - 'apply_pos': '(self, *args, **kwargs)', - 'apply_kw': '(self, *args, **kwargs)'} + "args": "(self, *args, **kwargs)", + "self_arg": "self", + "apply_pos": "(self, *args, **kwargs)", + "apply_kw": "(self, *args, **kwargs)", + } custom_spec = { - 'args': '(slef, a=123)', 'self_arg': 'slef', # yes, slef - 'apply_pos': '(slef, a)', 'apply_kw': '(slef, a=a)'} + "args": "(slef, a=123)", + "self_arg": "slef", # yes, slef + "apply_pos": "(slef, a)", + "apply_kw": "(slef, a=a)", + } self._test_init(None, object_spec, wrapper_spec, custom_spec) self._test_init(True, object_spec, wrapper_spec, custom_spec) - @testing.fails_if(lambda: util.pypy, - "pypy doesn't report Obj.__init__ as object.__init__") + @testing.fails_if( + lambda: util.pypy, + "pypy doesn't report Obj.__init__ as object.__init__", + ) def test_init_bare(self): object_spec = { - 'args': 'self', 'self_arg': 'self', - 'apply_pos': 'self', 'apply_kw': 'self'} + "args": "self", + "self_arg": "self", + "apply_pos": "self", + "apply_kw": "self", + } wrapper_spec = { - 'args': 'self, *args, **kwargs', 'self_arg': 'self', - 'apply_pos': 'self, *args, **kwargs', - 'apply_kw': 'self, *args, **kwargs'} + "args": "self, *args, **kwargs", + "self_arg": "self", + "apply_pos": "self, *args, **kwargs", + "apply_kw": "self, *args, **kwargs", + } custom_spec = { - 'args': 'slef, a=123', 'self_arg': 'slef', # yes, slef - 'apply_pos': 'slef, a', 'apply_kw': 'slef, a=a'} + "args": "slef, a=123", + "self_arg": "slef", # yes, slef + "apply_pos": "slef, a", + "apply_kw": "slef, a=a", + } self._test_init(False, object_spec, wrapper_spec, custom_spec) @@ -1958,17 +2057,14 @@ class TestFormatArgspec(fixtures.TestBase): class GenericReprTest(fixtures.TestBase): - def test_all_positional(self): class Foo(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c - eq_( - util.generic_repr(Foo(1, 2, 3)), - "Foo(1, 2, 3)" - ) + + eq_(util.generic_repr(Foo(1, 2, 3)), "Foo(1, 2, 3)") def test_positional_plus_kw(self): class Foo(object): @@ -1977,10 +2073,8 @@ class GenericReprTest(fixtures.TestBase): self.b = b self.c = c self.d = d - eq_( - util.generic_repr(Foo(1, 2, 3, 6)), - "Foo(1, 2, c=3, d=6)" - ) + + eq_(util.generic_repr(Foo(1, 2, 3, 6)), "Foo(1, 2, c=3, d=6)") def test_kw_defaults(self): class Foo(object): @@ -1989,10 +2083,8 @@ class GenericReprTest(fixtures.TestBase): self.b = b self.c = c self.d = d - eq_( - util.generic_repr(Foo(1, 5, 3, 7)), - "Foo(b=5, d=7)" - ) + + eq_(util.generic_repr(Foo(1, 5, 3, 7)), "Foo(b=5, d=7)") def test_multi_kw(self): class Foo(object): @@ -2011,18 +2103,14 @@ class GenericReprTest(fixtures.TestBase): eq_( util.generic_repr( - Bar('e', 'f', g=7, a=6, b=5, d=9), - to_inspect=[Bar, Foo] + Bar("e", "f", g=7, a=6, b=5, d=9), to_inspect=[Bar, Foo] ), - "Bar('e', 'f', g=7, a=6, b=5, d=9)" + "Bar('e', 'f', g=7, a=6, b=5, d=9)", ) eq_( - util.generic_repr( - Bar('e', 'f', a=6, b=5), - to_inspect=[Bar, Foo] - ), - "Bar('e', 'f', a=6, b=5)" + util.generic_repr(Bar("e", "f", a=6, b=5), to_inspect=[Bar, Foo]), + "Bar('e', 'f', a=6, b=5)", ) def test_multi_kw_repeated(self): @@ -2037,11 +2125,8 @@ class GenericReprTest(fixtures.TestBase): super(Bar, self).__init__(b=b, **kw) eq_( - util.generic_repr( - Bar(a='a', b='b', c='c'), - to_inspect=[Bar, Foo] - ), - "Bar(b='b', c='c', a='a')" + util.generic_repr(Bar(a="a", b="b", c="c"), to_inspect=[Bar, Foo]), + "Bar(b='b', c='c', a='a')", ) def test_discard_vargs(self): @@ -2050,10 +2135,8 @@ class GenericReprTest(fixtures.TestBase): self.a = a self.b = b self.c, self.d = args[0:2] - eq_( - util.generic_repr(Foo(1, 2, 3, 4)), - "Foo(1, 2)" - ) + + eq_(util.generic_repr(Foo(1, 2, 3, 4)), "Foo(1, 2)") def test_discard_vargs_kwargs(self): class Foo(object): @@ -2061,10 +2144,8 @@ class GenericReprTest(fixtures.TestBase): self.a = a self.b = b self.c, self.d = args[0:2] - eq_( - util.generic_repr(Foo(1, 2, 3, 4, x=7, y=4)), - "Foo(1, 2)" - ) + + eq_(util.generic_repr(Foo(1, 2, 3, 4, x=7, y=4)), "Foo(1, 2)") def test_significant_vargs(self): class Foo(object): @@ -2072,33 +2153,25 @@ class GenericReprTest(fixtures.TestBase): self.a = a self.b = b self.args = args - eq_( - util.generic_repr(Foo(1, 2, 3, 4)), - "Foo(1, 2, 3, 4)" - ) + + eq_(util.generic_repr(Foo(1, 2, 3, 4)), "Foo(1, 2, 3, 4)") def test_no_args(self): class Foo(object): def __init__(self): pass - eq_( - util.generic_repr(Foo()), - "Foo()" - ) + + eq_(util.generic_repr(Foo()), "Foo()") def test_no_init(self): class Foo(object): pass - eq_( - util.generic_repr(Foo()), - "Foo()" - ) + eq_(util.generic_repr(Foo()), "Foo()") -class AsInterfaceTest(fixtures.TestBase): +class AsInterfaceTest(fixtures.TestBase): class Something(object): - def _ignoreme(self): pass @@ -2109,7 +2182,6 @@ class AsInterfaceTest(fixtures.TestBase): pass class Partial(object): - def bar(self): pass @@ -2118,21 +2190,27 @@ class AsInterfaceTest(fixtures.TestBase): def test_instance(self): obj = object() - assert_raises(TypeError, util.as_interface, obj, - cls=self.Something) + assert_raises(TypeError, util.as_interface, obj, cls=self.Something) - assert_raises(TypeError, util.as_interface, obj, - methods=('foo')) + assert_raises(TypeError, util.as_interface, obj, methods=("foo")) - assert_raises(TypeError, util.as_interface, obj, - cls=self.Something, required=('foo')) + assert_raises( + TypeError, + util.as_interface, + obj, + cls=self.Something, + required=("foo"), + ) obj = self.Something() eq_(obj, util.as_interface(obj, cls=self.Something)) - eq_(obj, util.as_interface(obj, methods=('foo',))) + eq_(obj, util.as_interface(obj, methods=("foo",))) eq_( - obj, util.as_interface(obj, cls=self.Something, - required=('outofband',))) + obj, + util.as_interface( + obj, cls=self.Something, required=("outofband",) + ), + ) partial = self.Partial() slotted = self.Object() @@ -2140,57 +2218,79 @@ class AsInterfaceTest(fixtures.TestBase): for obj in partial, slotted: eq_(obj, util.as_interface(obj, cls=self.Something)) - assert_raises(TypeError, util.as_interface, obj, - methods=('foo')) - eq_(obj, util.as_interface(obj, methods=('bar',))) - eq_(obj, util.as_interface(obj, cls=self.Something, - required=('bar',))) - assert_raises(TypeError, util.as_interface, obj, - cls=self.Something, required=('foo',)) - - assert_raises(TypeError, util.as_interface, obj, - cls=self.Something, required=self.Something) + assert_raises(TypeError, util.as_interface, obj, methods=("foo")) + eq_(obj, util.as_interface(obj, methods=("bar",))) + eq_( + obj, + util.as_interface(obj, cls=self.Something, required=("bar",)), + ) + assert_raises( + TypeError, + util.as_interface, + obj, + cls=self.Something, + required=("foo",), + ) + + assert_raises( + TypeError, + util.as_interface, + obj, + cls=self.Something, + required=self.Something, + ) def test_dict(self): obj = {} - assert_raises(TypeError, util.as_interface, obj, - cls=self.Something) - assert_raises(TypeError, util.as_interface, obj, methods='foo') - assert_raises(TypeError, util.as_interface, obj, - cls=self.Something, required='foo') + assert_raises(TypeError, util.as_interface, obj, cls=self.Something) + assert_raises(TypeError, util.as_interface, obj, methods="foo") + assert_raises( + TypeError, + util.as_interface, + obj, + cls=self.Something, + required="foo", + ) def assertAdapted(obj, *methods): assert isinstance(obj, type) - found = set([m for m in dir(obj) if not m.startswith('_')]) + found = set([m for m in dir(obj) if not m.startswith("_")]) for method in methods: assert method in found found.remove(method) assert not found - def fn(self): return 123 - obj = {'foo': fn, 'bar': fn} + def fn(self): + return 123 + + obj = {"foo": fn, "bar": fn} res = util.as_interface(obj, cls=self.Something) - assertAdapted(res, 'foo', 'bar') - res = util.as_interface(obj, cls=self.Something, - required=self.Something) - assertAdapted(res, 'foo', 'bar') - res = util.as_interface(obj, cls=self.Something, required=('foo',)) - assertAdapted(res, 'foo', 'bar') - res = util.as_interface(obj, methods=('foo', 'bar')) - assertAdapted(res, 'foo', 'bar') - res = util.as_interface(obj, methods=('foo', 'bar', 'baz')) - assertAdapted(res, 'foo', 'bar') - res = util.as_interface(obj, methods=('foo', 'bar'), required=('foo',)) - assertAdapted(res, 'foo', 'bar') - assert_raises(TypeError, util.as_interface, obj, methods=('foo',)) - assert_raises(TypeError, util.as_interface, obj, - methods=('foo', 'bar', 'baz'), required=('baz', )) - obj = {'foo': 123} + assertAdapted(res, "foo", "bar") + res = util.as_interface( + obj, cls=self.Something, required=self.Something + ) + assertAdapted(res, "foo", "bar") + res = util.as_interface(obj, cls=self.Something, required=("foo",)) + assertAdapted(res, "foo", "bar") + res = util.as_interface(obj, methods=("foo", "bar")) + assertAdapted(res, "foo", "bar") + res = util.as_interface(obj, methods=("foo", "bar", "baz")) + assertAdapted(res, "foo", "bar") + res = util.as_interface(obj, methods=("foo", "bar"), required=("foo",)) + assertAdapted(res, "foo", "bar") + assert_raises(TypeError, util.as_interface, obj, methods=("foo",)) + assert_raises( + TypeError, + util.as_interface, + obj, + methods=("foo", "bar", "baz"), + required=("baz",), + ) + obj = {"foo": 123} assert_raises(TypeError, util.as_interface, obj, cls=self.Something) class TestClassHierarchy(fixtures.TestBase): - def test_object(self): eq_(set(util.class_hierarchy(object)), set((object,))) @@ -2211,6 +2311,7 @@ class TestClassHierarchy(fixtures.TestBase): eq_(set(util.class_hierarchy(B)), set((A, B, C, object))) if util.py2k: + def test_oldstyle_mixin(self): class A(object): pass @@ -2255,11 +2356,7 @@ class ReraiseTest(fixtures.TestBase): type_, value, tb = sys.exc_info() util.reraise(type_, err, tb, value) - assert_raises_message( - AssertionError, - "Same cause emitted", - go - ) + assert_raises_message(AssertionError, "Same cause emitted", go) def test_raise_from_cause(self): class MyException(Exception): @@ -2317,26 +2414,23 @@ class ReraiseTest(fixtures.TestBase): class TestClassProperty(fixtures.TestBase): - def test_simple(self): class A(object): - something = {'foo': 1} + something = {"foo": 1} class B(A): - @classproperty def something(cls): d = dict(super(B, cls).something) - d.update({'bazz': 2}) + d.update({"bazz": 2}) return d - eq_(B.something, {'foo': 1, 'bazz': 2}) + eq_(B.something, {"foo": 1, "bazz": 2}) class TestProperties(fixtures.TestBase): - def test_pickle(self): - data = {'hello': 'bla'} + data = {"hello": "bla"} props = util.Properties(data) for loader, dumper in picklers(): @@ -2347,12 +2441,12 @@ class TestProperties(fixtures.TestBase): eq_(props.keys(), p.keys()) def test_keys_in_dir(self): - data = {'hello': 'bla'} + data = {"hello": "bla"} props = util.Properties(data) - in_('hello', dir(props)) + in_("hello", dir(props)) def test_pickle_immuatbleprops(self): - data = {'hello': 'bla'} + data = {"hello": "bla"} props = util.Properties(data).as_immutable() for loader, dumper in picklers(): @@ -2363,7 +2457,7 @@ class TestProperties(fixtures.TestBase): eq_(props.keys(), p.keys()) def test_pickle_orderedprops(self): - data = {'hello': 'bla'} + data = {"hello": "bla"} props = util.OrderedProperties() props.update(data) @@ -2377,118 +2471,70 @@ class TestProperties(fixtures.TestBase): class QuotedTokenParserTest(fixtures.TestBase): def _test(self, string, expected): - eq_( - langhelpers.quoted_token_parser(string), - expected - ) + eq_(langhelpers.quoted_token_parser(string), expected) def test_single(self): - self._test( - "name", - ["name"] - ) + self._test("name", ["name"]) def test_dotted(self): - self._test( - "schema.name", ["schema", "name"] - ) + self._test("schema.name", ["schema", "name"]) def test_dotted_quoted_left(self): - self._test( - '"Schema".name', ["Schema", "name"] - ) + self._test('"Schema".name', ["Schema", "name"]) def test_dotted_quoted_left_w_quote_left_edge(self): - self._test( - '"""Schema".name', ['"Schema', "name"] - ) + self._test('"""Schema".name', ['"Schema', "name"]) def test_dotted_quoted_left_w_quote_right_edge(self): - self._test( - '"Schema""".name', ['Schema"', "name"] - ) + self._test('"Schema""".name', ['Schema"', "name"]) def test_dotted_quoted_left_w_quote_middle(self): - self._test( - '"Sch""ema".name', ['Sch"ema', "name"] - ) + self._test('"Sch""ema".name', ['Sch"ema', "name"]) def test_dotted_quoted_right(self): - self._test( - 'schema."SomeName"', ["schema", "SomeName"] - ) + self._test('schema."SomeName"', ["schema", "SomeName"]) def test_dotted_quoted_right_w_quote_left_edge(self): - self._test( - 'schema."""name"', ['schema', '"name'] - ) + self._test('schema."""name"', ["schema", '"name']) def test_dotted_quoted_right_w_quote_right_edge(self): - self._test( - 'schema."name"""', ['schema', 'name"'] - ) + self._test('schema."name"""', ["schema", 'name"']) def test_dotted_quoted_right_w_quote_middle(self): - self._test( - 'schema."na""me"', ['schema', 'na"me'] - ) + self._test('schema."na""me"', ["schema", 'na"me']) def test_quoted_single_w_quote_left_edge(self): - self._test( - '"""name"', ['"name'] - ) + self._test('"""name"', ['"name']) def test_quoted_single_w_quote_right_edge(self): - self._test( - '"name"""', ['name"'] - ) + self._test('"name"""', ['name"']) def test_quoted_single_w_quote_middle(self): - self._test( - '"na""me"', ['na"me'] - ) + self._test('"na""me"', ['na"me']) def test_dotted_quoted_left_w_dot_left_edge(self): - self._test( - '".Schema".name', ['.Schema', "name"] - ) + self._test('".Schema".name', [".Schema", "name"]) def test_dotted_quoted_left_w_dot_right_edge(self): - self._test( - '"Schema.".name', ['Schema.', "name"] - ) + self._test('"Schema.".name', ["Schema.", "name"]) def test_dotted_quoted_left_w_dot_middle(self): - self._test( - '"Sch.ema".name', ['Sch.ema', "name"] - ) + self._test('"Sch.ema".name', ["Sch.ema", "name"]) def test_dotted_quoted_right_w_dot_left_edge(self): - self._test( - 'schema.".name"', ['schema', '.name'] - ) + self._test('schema.".name"', ["schema", ".name"]) def test_dotted_quoted_right_w_dot_right_edge(self): - self._test( - 'schema."name."', ['schema', 'name.'] - ) + self._test('schema."name."', ["schema", "name."]) def test_dotted_quoted_right_w_dot_middle(self): - self._test( - 'schema."na.me"', ['schema', 'na.me'] - ) + self._test('schema."na.me"', ["schema", "na.me"]) def test_quoted_single_w_dot_left_edge(self): - self._test( - '".name"', ['.name'] - ) + self._test('".name"', [".name"]) def test_quoted_single_w_dot_right_edge(self): - self._test( - '"name."', ['name.'] - ) + self._test('"name."', ["name."]) def test_quoted_single_w_dot_middle(self): - self._test( - '"na.me"', ['na.me'] - ) + self._test('"na.me"', ["na.me"]) |
