summaryrefslogtreecommitdiff
path: root/test/base/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/base/test_utils.py')
-rw-r--r--test/base/test_utils.py114
1 files changed, 111 insertions, 3 deletions
diff --git a/test/base/test_utils.py b/test/base/test_utils.py
index aefc6d421..86e4b190a 100644
--- a/test/base/test_utils.py
+++ b/test/base/test_utils.py
@@ -1,10 +1,10 @@
import copy
-from sqlalchemy import util, sql, exc
+from sqlalchemy import util, sql, exc, testing
from sqlalchemy.testing import assert_raises, assert_raises_message, fixtures
from sqlalchemy.testing import eq_, is_, ne_, fails_if
-from sqlalchemy.testing.util import picklers
-from sqlalchemy.util import classproperty
+from sqlalchemy.testing.util import picklers, gc_collect
+from sqlalchemy.util import classproperty, WeakSequence, get_callable_argspec
class KeyedTupleTest():
@@ -115,6 +115,36 @@ class KeyedTupleTest():
keyed_tuple[0] = 100
assert_raises(TypeError, should_raise)
+class WeakSequenceTest(fixtures.TestBase):
+ @testing.requires.predictable_gc
+ def test_cleanout_elements(self):
+ class Foo(object):
+ pass
+ f1, f2, f3 = Foo(), Foo(), Foo()
+ w = WeakSequence([f1, f2, f3])
+ eq_(len(w), 3)
+ eq_(len(w._storage), 3)
+ del f2
+ gc_collect()
+ eq_(len(w), 2)
+ eq_(len(w._storage), 2)
+
+ @testing.requires.predictable_gc
+ def test_cleanout_appended(self):
+ class Foo(object):
+ pass
+ f1, f2, f3 = Foo(), Foo(), Foo()
+ w = WeakSequence()
+ w.append(f1)
+ w.append(f2)
+ w.append(f3)
+ eq_(len(w), 3)
+ eq_(len(w._storage), 3)
+ del f2
+ gc_collect()
+ eq_(len(w), 2)
+ eq_(len(w._storage), 2)
+
class OrderedDictTest(fixtures.TestBase):
@@ -1154,6 +1184,33 @@ class ArgInspectionTest(fixtures.TestBase):
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)
+ )
+
+ 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)
+ )
+
+ def test_callable_argspec_partial(self):
+ from functools import partial
+ def foo(x, y, z, **kw):
+ pass
+ bar = partial(foo, 5)
+
+ assert_raises(
+ ValueError,
+ get_callable_argspec, bar
+ )
class SymbolTest(fixtures.TestBase):
@@ -1389,6 +1446,55 @@ class GenericReprTest(fixtures.TestBase):
"Foo(b=5, d=7)"
)
+ def test_multi_kw(self):
+ class Foo(object):
+ def __init__(self, a, b, c=3, d=4):
+ self.a = a
+ self.b = b
+ self.c = c
+ self.d = d
+ class Bar(Foo):
+ def __init__(self, e, f, g=5, **kw):
+ self.e = e
+ self.f = f
+ self.g = g
+ super(Bar, self).__init__(**kw)
+
+ 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)"
+ )
+
+ eq_(
+ 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):
+ class Foo(object):
+ def __init__(self, a=1, b=2):
+ self.a = a
+ self.b = b
+ class Bar(Foo):
+ def __init__(self, b=3, c=4, **kw):
+ self.c = c
+ 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')"
+ )
+
+
def test_discard_vargs(self):
class Foo(object):
def __init__(self, a, b, *args):
@@ -1586,3 +1692,5 @@ class TestClassProperty(fixtures.TestBase):
return d
eq_(B.something, {'foo': 1, 'bazz': 2})
+
+