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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy.orm import relationship
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.fixtures import fixture_session
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
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
@testing.combinations(
(True, False),
(False, False),
(False, True),
argnames="_legacy_inactive_history_style, active_history",
)
def test_basic(self, _legacy_inactive_history_style, active_history):
Port, port, jack, Jack = (
self.classes.Port,
self.tables.port,
self.tables.jack,
self.classes.Jack,
)
self.mapper_registry.map_imperatively(Port, port)
self.mapper_registry.map_imperatively(
Jack,
jack,
properties=dict(
port=relationship(
Port,
backref="jack",
uselist=False,
active_history=active_history,
_legacy_inactive_history_style=(
_legacy_inactive_history_style
),
)
),
)
session = fixture_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.get(Jack, jid)
p = session.get(Port, 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.get(Jack, jid)
p = session.get(Port, pid)
j.port = None
if not active_history and not _legacy_inactive_history_style:
session.flush()
self.assert_(p.jack is None)
else:
self.assert_(p.jack is None)
session.flush()
session.delete(j)
session.flush()
@testing.combinations(
(True,), (False,), argnames="_legacy_inactive_history_style"
)
def test_simple_replace(self, _legacy_inactive_history_style):
Port, port, jack, Jack = (
self.classes.Port,
self.tables.port,
self.tables.jack,
self.classes.Jack,
)
self.mapper_registry.map_imperatively(Port, port)
self.mapper_registry.map_imperatively(
Jack,
jack,
properties=dict(
port=relationship(
Port,
uselist=False,
_legacy_inactive_history_style=(
_legacy_inactive_history_style
),
)
),
)
s = fixture_session()
p1 = Port(name="p1")
j1 = Jack(number="j1", port=p1)
s.add(j1)
s.commit()
j1.port = Port(name="p2")
s.commit()
assert s.query(Port).filter_by(name="p1").one().jack_id is None
@testing.combinations(
(True,), (False,), argnames="_legacy_inactive_history_style"
)
def test_simple_del(self, _legacy_inactive_history_style):
Port, port, jack, Jack = (
self.classes.Port,
self.tables.port,
self.tables.jack,
self.classes.Jack,
)
self.mapper_registry.map_imperatively(Port, port)
self.mapper_registry.map_imperatively(
Jack,
jack,
properties=dict(
port=relationship(
Port,
uselist=False,
_legacy_inactive_history_style=(
_legacy_inactive_history_style
),
)
),
)
s = fixture_session()
p1 = Port(name="p1")
j1 = Jack(number="j1", port=p1)
s.add(j1)
s.commit()
del j1.port
s.commit()
assert s.query(Port).filter_by(name="p1").one().jack_id is None
|