~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to test/base/test_utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ożarowski
  • Date: 2011-08-01 23:18:16 UTC
  • mfrom: (1.4.15 upstream) (16.1.14 experimental)
  • Revision ID: james.westby@ubuntu.com-20110801231816-6lx797pi3q1fpqst
Tags: 0.7.2-1
* New upstream release
* Bump minimum required python-mako version to 0.4.1 (closes: 635898)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from sqlalchemy.test.testing import assert_raises, assert_raises_message
 
1
from test.lib.testing import assert_raises, assert_raises_message
2
2
import copy, threading
3
3
from sqlalchemy import util, sql, exc
4
 
from sqlalchemy.test import TestBase
5
 
from sqlalchemy.test.testing import eq_, is_, ne_
6
 
from sqlalchemy.test.util import gc_collect, picklers
 
4
from test.lib.testing import eq_, is_, ne_, fails_if
 
5
from test.lib.util import gc_collect, picklers
7
6
from sqlalchemy.util import classproperty
8
 
 
9
 
 
10
 
class OrderedDictTest(TestBase):
 
7
from test.lib import fixtures
 
8
 
 
9
class OrderedDictTest(fixtures.TestBase):
11
10
    def test_odict(self):
12
11
        o = util.OrderedDict()
13
12
        o['a'] = 1
71
70
        o3 = copy.copy(o)
72
71
        eq_(o3.keys(), o.keys())
73
72
 
74
 
class OrderedSetTest(TestBase):
 
73
class OrderedSetTest(fixtures.TestBase):
75
74
    def test_mutators_against_iter(self):
76
75
        # testing a set modified against an iterator
77
76
        o = util.OrderedSet([3,2, 4, 5])
80
79
        eq_(o.intersection(iter([3,4, 6])), util.OrderedSet([3, 4]))
81
80
        eq_(o.union(iter([3,4, 6])), util.OrderedSet([2, 3, 4, 5, 6]))
82
81
 
83
 
class FrozenDictTest(TestBase):
 
82
class FrozenDictTest(fixtures.TestBase):
84
83
    def test_serialize(self):
85
 
        d = util.frozendict({1:2, 3:4})
 
84
        d = util.immutabledict({1:2, 3:4})
86
85
        for loads, dumps in picklers():
87
86
            print loads(dumps(d))
88
87
 
89
88
 
90
 
class MemoizedAttrTest(TestBase):
 
89
class MemoizedAttrTest(fixtures.TestBase):
91
90
    def test_memoized_property(self):
92
91
        val = [20]
93
92
        class Foo(object):
121
120
        eq_(f1.bar(), 20)
122
121
        eq_(val[0], 21)
123
122
 
124
 
class ColumnCollectionTest(TestBase):
 
123
class ColumnCollectionTest(fixtures.TestBase):
125
124
    def test_in(self):
126
125
        cc = sql.ColumnCollection()
127
126
        cc.add(sql.column('col1'))
149
148
        assert (cc1==cc2).compare(c1 == c2)
150
149
        assert not (cc1==cc3).compare(c2 == c3)
151
150
 
152
 
class LRUTest(TestBase):
 
151
class LRUTest(fixtures.TestBase):
153
152
 
154
153
    def test_lru(self):
155
154
        class item(object):
196
195
class ImmutableSubclass(str):
197
196
    pass
198
197
 
199
 
class FlattenIteratorTest(TestBase):
 
198
class FlattenIteratorTest(fixtures.TestBase):
200
199
 
201
200
    def test_flatten(self):
202
201
        assert list(util.flatten_iterator([[1, 2, 3], [4, 5, 6], 7,
207
206
        PyPy) is not interpreted as an iterable.
208
207
 
209
208
        """
 
209
 
210
210
        class IterString(str):
211
211
            def __iter__(self):
212
212
                return iter(self + '')
253
253
            return True
254
254
 
255
255
 
256
 
class IdentitySetTest(TestBase):
 
256
class IdentitySetTest(fixtures.TestBase):
257
257
    def assert_eq(self, identityset, expected_iterable):
258
258
        expected = sorted([id(o) for o in expected_iterable])
259
259
        found = sorted([id(o) for o in identityset])
370
370
        assert_raises(TypeError, lambda: s1 - os2)
371
371
        assert_raises(TypeError, lambda: s1 - [3, 4, 5])
372
372
 
373
 
class OrderedIdentitySetTest(TestBase):
 
373
class OrderedIdentitySetTest(fixtures.TestBase):
374
374
 
375
375
    def assert_eq(self, identityset, expected_iterable):
376
376
        expected = [id(o) for o in expected_iterable]
398
398
        eq_(s1.union(s2).intersection(s3), [a, d, f])
399
399
 
400
400
 
401
 
class DictlikeIteritemsTest(TestBase):
 
401
class DictlikeIteritemsTest(fixtures.TestBase):
402
402
    baseline = set([('a', 1), ('b', 2), ('c', 3)])
403
403
 
404
404
    def _ok(self, instance):
475
475
        self._notok(duck6())
476
476
 
477
477
 
478
 
class DuckTypeCollectionTest(TestBase):
 
478
class DuckTypeCollectionTest(fixtures.TestBase):
479
479
    def test_sets(self):
480
480
        # Py2K
481
481
        import sets
506
506
            instance = type_()
507
507
            is_(util.duck_type_collection(instance), None)
508
508
 
509
 
class ArgInspectionTest(TestBase):
 
509
class ArgInspectionTest(fixtures.TestBase):
510
510
    def test_get_cls_kwargs(self):
511
511
        class A(object):
512
512
            def __init__(self, a):
573
573
        test(f3)
574
574
        test(f4)
575
575
 
576
 
class SymbolTest(TestBase):
 
576
class SymbolTest(fixtures.TestBase):
577
577
    def test_basic(self):
578
578
        sym1 = util.symbol('foo')
579
579
        assert sym1.name == 'foo'
603
603
            assert rt is sym1
604
604
            assert rt is sym2
605
605
 
606
 
class WeakIdentityMappingTest(TestBase):
 
606
class WeakIdentityMappingTest(fixtures.TestBase):
607
607
    class Data(object):
608
608
        pass
609
609
 
796
796
        eq_(wim._weakrefs, {})
797
797
 
798
798
 
799
 
class TestFormatArgspec(TestBase):
 
799
class TestFormatArgspec(fixtures.TestBase):
800
800
    def test_specs(self):
801
801
        def test(fn, wanted, grouped=None):
802
802
            if grouped is None:
860
860
            'apply_kw': 'a=a, b=b', 'apply_pos': 'a, b' },
861
861
           grouped=False)
862
862
 
 
863
    @fails_if(lambda: util.pypy, "object.__init__ is introspectable")
863
864
    def test_init_grouped(self):
864
865
        object_spec = {
865
866
            'args': '(self)', 'self_arg': 'self',
875
876
        self._test_init(None, object_spec, wrapper_spec, custom_spec)
876
877
        self._test_init(True, object_spec, wrapper_spec, custom_spec)
877
878
 
 
879
    @fails_if(lambda: util.pypy,  "object.__init__ can be introspected")
878
880
    def test_init_bare(self):
879
881
        object_spec = {
880
882
            'args': 'self', 'self_arg': 'self',
935
937
 
936
938
        test(O.__init__, custom_spec)
937
939
 
938
 
class AsInterfaceTest(TestBase):
 
940
 
 
941
class GenericReprTest(fixtures.TestBase):
 
942
    def test_all_positional(self):
 
943
        class Foo(object):
 
944
            def __init__(self, a, b, c):
 
945
                self.a = a
 
946
                self.b = b
 
947
                self.c = c
 
948
        eq_(
 
949
            util.generic_repr(Foo(1, 2, 3)),
 
950
            "Foo(1, 2, 3)"
 
951
        )
 
952
 
 
953
    def test_positional_plus_kw(self):
 
954
        class Foo(object):
 
955
            def __init__(self, a, b, c=5, d=4):
 
956
                self.a = a
 
957
                self.b = b
 
958
                self.c = c
 
959
                self.d = d
 
960
        eq_(
 
961
            util.generic_repr(Foo(1, 2, 3, 6)),
 
962
            "Foo(1, 2, c=3, d=6)"
 
963
        )
 
964
 
 
965
    def test_kw_defaults(self):
 
966
        class Foo(object):
 
967
            def __init__(self, a=1, b=2, c=3, d=4):
 
968
                self.a = a
 
969
                self.b = b
 
970
                self.c = c
 
971
                self.d = d
 
972
        eq_(
 
973
            util.generic_repr(Foo(1, 5, 3, 7)),
 
974
            "Foo(b=5, d=7)"
 
975
        )
 
976
 
 
977
    def test_discard_vargs(self):
 
978
        class Foo(object):
 
979
            def __init__(self, a, b, *args):
 
980
                self.a = a
 
981
                self.b = b
 
982
                self.c, self.d = args[0:2]
 
983
        eq_(
 
984
            util.generic_repr(Foo(1, 2, 3, 4)),
 
985
            "Foo(1, 2)"
 
986
        )
 
987
 
 
988
    def test_discard_vargs_kwargs(self):
 
989
        class Foo(object):
 
990
            def __init__(self, a, b, *args, **kw):
 
991
                self.a = a
 
992
                self.b = b
 
993
                self.c, self.d = args[0:2]
 
994
        eq_(
 
995
            util.generic_repr(Foo(1, 2, 3, 4, x=7, y=4)),
 
996
            "Foo(1, 2)"
 
997
        )
 
998
 
 
999
    def test_significant_vargs(self):
 
1000
        class Foo(object):
 
1001
            def __init__(self, a, b, *args):
 
1002
                self.a = a
 
1003
                self.b = b
 
1004
                self.args = args
 
1005
        eq_(
 
1006
            util.generic_repr(Foo(1, 2, 3, 4)),
 
1007
            "Foo(1, 2, 3, 4)"
 
1008
        )
 
1009
 
 
1010
    def test_no_args(self):
 
1011
        class Foo(object):
 
1012
            def __init__(self):
 
1013
                pass
 
1014
        eq_(
 
1015
            util.generic_repr(Foo()),
 
1016
            "Foo()"
 
1017
        )
 
1018
 
 
1019
    def test_no_init(self):
 
1020
        class Foo(object):
 
1021
            pass
 
1022
        eq_(
 
1023
            util.generic_repr(Foo()),
 
1024
            "Foo()"
 
1025
        )
 
1026
 
 
1027
class AsInterfaceTest(fixtures.TestBase):
939
1028
 
940
1029
    class Something(object):
941
1030
        def _ignoreme(self): pass
1024
1113
                      cls=self.Something)
1025
1114
 
1026
1115
 
1027
 
class TestClassHierarchy(TestBase):
 
1116
class TestClassHierarchy(fixtures.TestBase):
1028
1117
    def test_object(self):
1029
1118
        eq_(set(util.class_hierarchy(object)), set((object,)))
1030
1119
 
1061
1150
    # end Py2K
1062
1151
 
1063
1152
 
1064
 
class TestClassProperty(TestBase):
 
1153
class TestClassProperty(fixtures.TestBase):
1065
1154
 
1066
1155
    def test_simple(self):
1067
1156
        class A(object):