summaryrefslogtreecommitdiff
path: root/test/orm/bind.py
blob: 33d028d22ec74777fc09ee7d2f2acc747720c8e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import testenv; testenv.configure_for_tests()
from testlib.sa import MetaData, Table, Column, Integer
from testlib.sa.orm import mapper, create_session
from testlib import sa, testing
from orm import _base


class BindTest(_base.MappedTest):
    def define_tables(self, metadata):
        Table('test_table', metadata,
              Column('id', Integer, primary_key=True,
                     test_needs_autoincrement=True),
              Column('data', Integer))

    def setup_classes(self):
        class Foo(_base.BasicEntity):
            pass

    @testing.resolve_artifact_names
    def setup_mappers(self):
        meta = MetaData()
        test_table.tometadata(meta)

        assert meta.tables['test_table'].bind is None
        mapper(Foo, meta.tables['test_table'])

    @testing.resolve_artifact_names
    def test_session_bind(self):
        engine = self.metadata.bind

        for bind in (engine, engine.connect()):
            try:
                sess = create_session(bind=bind)
                assert sess.bind is bind
                f = Foo()
                sess.add(f)
                sess.flush()
                assert sess.query(Foo).get(f.id) is f
            finally:
                if hasattr(bind, 'close'):
                    bind.close()

    @testing.resolve_artifact_names
    def test_session_unbound(self):
        sess = create_session()
        sess.add(Foo())
        self.assertRaisesMessage(
            sa.exc.UnboundExecutionError,
            ('Could not locate a bind configured on Mapper|Foo|test_table '
             'or this Session'),
            sess.flush)


if __name__ == '__main__':
    testenv.main()