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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
from sqlalchemy.test.testing import assert_raises, assert_raises_message
import sqlalchemy as sa
from sqlalchemy.test import testing
from sqlalchemy.orm import scoped_session
from sqlalchemy import Integer, String, ForeignKey
from sqlalchemy.test.schema import Table, Column
from sqlalchemy.orm import mapper, relationship, query
from sqlalchemy.test.testing import eq_
from test.orm import _base
class _ScopedTest(_base.MappedTest):
"""Adds another lookup bucket to emulate Session globals."""
run_setup_mappers = 'once'
_artifact_registries = (
_base.MappedTest._artifact_registries + ('scoping',))
@classmethod
def setup_class(cls):
cls.scoping = _base.adict()
super(_ScopedTest, cls).setup_class()
@classmethod
def teardown_class(cls):
cls.scoping.clear()
super(_ScopedTest, cls).teardown_class()
class ScopedSessionTest(_base.MappedTest):
@classmethod
def define_tables(cls, metadata):
Table('table1', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('data', String(30)))
Table('table2', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('someid', None, ForeignKey('table1.id')))
@testing.resolve_artifact_names
def test_basic(self):
Session = scoped_session(sa.orm.sessionmaker())
class CustomQuery(query.Query):
pass
class SomeObject(_base.ComparableEntity):
query = Session.query_property()
class SomeOtherObject(_base.ComparableEntity):
query = Session.query_property()
custom_query = Session.query_property(query_cls=CustomQuery)
mapper(SomeObject, table1, properties={
'options':relationship(SomeOtherObject)})
mapper(SomeOtherObject, table2)
s = SomeObject(id=1, data="hello")
sso = SomeOtherObject()
s.options.append(sso)
Session.add(s)
Session.commit()
Session.refresh(sso)
Session.remove()
eq_(SomeObject(id=1, data="hello", options=[SomeOtherObject(someid=1)]),
Session.query(SomeObject).one())
eq_(SomeObject(id=1, data="hello", options=[SomeOtherObject(someid=1)]),
SomeObject.query.one())
eq_(SomeOtherObject(someid=1),
SomeOtherObject.query.filter(
SomeOtherObject.someid == sso.someid).one())
assert isinstance(SomeOtherObject.query, query.Query)
assert not isinstance(SomeOtherObject.query, CustomQuery)
assert isinstance(SomeOtherObject.custom_query, query.Query)
class ScopedMapperTest(_ScopedTest):
@classmethod
def define_tables(cls, metadata):
Table('table1', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('data', String(30)))
Table('table2', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('someid', None, ForeignKey('table1.id')))
@classmethod
def setup_classes(cls):
class SomeObject(_base.ComparableEntity):
pass
class SomeOtherObject(_base.ComparableEntity):
pass
@classmethod
@testing.uses_deprecated()
@testing.resolve_artifact_names
def setup_mappers(cls):
Session = scoped_session(sa.orm.create_session)
Session.mapper(SomeObject, table1, properties={
'options':relationship(SomeOtherObject)
})
Session.mapper(SomeOtherObject, table2)
cls.scoping['Session'] = Session
@classmethod
@testing.resolve_artifact_names
def insert_data(cls):
s = SomeObject()
s.id = 1
s.data = 'hello'
sso = SomeOtherObject()
s.options.append(sso)
Session.flush()
Session.expunge_all()
@testing.resolve_artifact_names
def test_query(self):
sso = SomeOtherObject.query().first()
assert SomeObject.query.filter_by(id=1).one().options[0].id == sso.id
@testing.uses_deprecated()
@testing.resolve_artifact_names
def test_query_compiles(self):
class Foo(object):
pass
Session.mapper(Foo, table2)
assert hasattr(Foo, 'query')
ext = sa.orm.MapperExtension()
class Bar(object):
pass
Session.mapper(Bar, table2, extension=[ext])
assert hasattr(Bar, 'query')
class Baz(object):
pass
Session.mapper(Baz, table2, extension=ext)
assert hasattr(Baz, 'query')
@testing.uses_deprecated()
@testing.resolve_artifact_names
def test_default_constructor_state_not_shared(self):
scope = scoped_session(sa.orm.sessionmaker())
class A(object):
pass
class B(object):
def __init__(self):
pass
scope.mapper(A, table1)
scope.mapper(B, table2)
A(foo='bar')
assert_raises(TypeError, B, foo='bar')
scope = scoped_session(sa.orm.sessionmaker())
class C(object):
def __init__(self):
pass
class D(object):
pass
scope.mapper(C, table1)
scope.mapper(D, table2)
assert_raises(TypeError, C, foo='bar')
D(foo='bar')
@testing.uses_deprecated()
@testing.resolve_artifact_names
def test_validating_constructor(self):
s2 = SomeObject(someid=12)
s3 = SomeOtherObject(someid=123, bogus=345)
class ValidatedOtherObject(object): pass
Session.mapper(ValidatedOtherObject, table2, validate=True)
v1 = ValidatedOtherObject(someid=12)
assert_raises(sa.exc.ArgumentError, ValidatedOtherObject,
someid=12, bogus=345)
@testing.uses_deprecated()
@testing.resolve_artifact_names
def test_dont_clobber_methods(self):
class MyClass(object):
def expunge(self):
return "an expunge !"
Session.mapper(MyClass, table2)
assert MyClass().expunge() == "an expunge !"
class ScopedMapperTest2(_ScopedTest):
@classmethod
def define_tables(cls, metadata):
Table('table1', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('data', String(30)),
Column('type', String(30)))
Table('table2', metadata,
Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
Column('someid', None, ForeignKey('table1.id')),
Column('somedata', String(30)))
@classmethod
def setup_classes(cls):
class BaseClass(_base.ComparableEntity):
pass
class SubClass(BaseClass):
pass
@classmethod
@testing.uses_deprecated()
@testing.resolve_artifact_names
def setup_mappers(cls):
Session = scoped_session(sa.orm.sessionmaker())
Session.mapper(BaseClass, table1,
polymorphic_identity='base',
polymorphic_on=table1.c.type)
Session.mapper(SubClass, table2,
polymorphic_identity='sub',
inherits=BaseClass)
cls.scoping['Session'] = Session
@testing.resolve_artifact_names
def test_inheritance(self):
def expunge_list(l):
for x in l:
Session.expunge(x)
return l
b = BaseClass(data='b1')
s = SubClass(data='s1', somedata='somedata')
Session.commit()
Session.expunge_all()
eq_(expunge_list([BaseClass(data='b1'),
SubClass(data='s1', somedata='somedata')]),
BaseClass.query.all())
eq_(expunge_list([SubClass(data='s1', somedata='somedata')]),
SubClass.query.all())
|