summaryrefslogtreecommitdiff
path: root/test/orm/selectable.py
blob: 30771ec2b199ed693fb1d03cac8f999ddd076a30 (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
"""all tests involving generic mapping to Select statements"""

import testenv; testenv.configure_for_tests()
from sqlalchemy import *
from sqlalchemy import exceptions
from sqlalchemy.orm import *
from testlib import *
from testlib.fixtures import *
from query import QueryTest

class SelectableNoFromsTest(ORMTest):
    def define_tables(self, metadata):
        global common_table
        common_table = Table('common', metadata,
            Column('id', Integer, primary_key=True),
            Column('data', Integer),
            Column('extra', String(45)),
        )

    def test_no_tables(self):
        class Subset(object):
            pass
        selectable = select(["x", "y", "z"]).alias('foo')
        try:
            mapper(Subset, selectable)
            compile_mappers()
            assert False
        except exceptions.InvalidRequestError, e:
            assert str(e) == "Could not find any Table objects in mapped table 'SELECT x, y, z'", str(e)

    def test_basic(self):
        class Subset(Base):
            pass

        subset_select = select([common_table.c.id, common_table.c.data]).alias('subset')
        subset_mapper = mapper(Subset, subset_select)

        sess = create_session(bind=testing.db)
        l = Subset()
        l.data = 1
        sess.save(l)
        sess.flush()
        sess.clear()

        assert [Subset(data=1)] == sess.query(Subset).all()

    # TODO: more tests mapping to selects

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