summaryrefslogtreecommitdiff
path: root/test/orm/test_onetoone.py
blob: 19b13c4ddc07d8e3d7cedb6a33e657f232eac690 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sqlalchemy as sa
from sqlalchemy import testing
from sqlalchemy import Integer, String, ForeignKey
from sqlalchemy.testing.schema import Table, Column
from sqlalchemy.orm import mapper, relationship, create_session
from sqlalchemy.testing import fixtures


class O2OTest(fixtures.MappedTest):
    @classmethod
    def define_tables(cls, metadata):
        Table('jack', metadata,
              Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
              Column('number', String(50)),
              Column('status', String(20)),
              Column('subroom', String(5)))

        Table('port', metadata,
              Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
              Column('name', String(30)),
              Column('description', String(100)),
              Column('jack_id', Integer, ForeignKey("jack.id")))

    @classmethod
    def setup_mappers(cls):
        class Jack(cls.Basic):
            pass
        class Port(cls.Basic):
            pass


    def test_basic(self):
        Port, port, jack, Jack = (self.classes.Port,
                                self.tables.port,
                                self.tables.jack,
                                self.classes.Jack)

        mapper(Port, port)
        mapper(Jack, jack,
               order_by=[jack.c.number],
               properties=dict(
                   port=relationship(Port, backref='jack',
                                 uselist=False,
                                 )),
               )

        session = create_session()

        j = Jack(number='101')
        session.add(j)
        p = Port(name='fa0/1')
        session.add(p)

        j.port=p
        session.flush()
        jid = j.id
        pid = p.id

        j=session.query(Jack).get(jid)
        p=session.query(Port).get(pid)
        assert p.jack is not None
        assert p.jack is  j
        assert j.port is not None
        p.jack = None
        assert j.port is None

        session.expunge_all()

        j = session.query(Jack).get(jid)
        p = session.query(Port).get(pid)

        j.port=None
        self.assert_(p.jack is None)
        session.flush()

        session.delete(j)
        session.flush()