1
from test.lib.testing import eq_
1
from sqlalchemy.testing import eq_
2
2
from sqlalchemy.orm import mapper, relationship, create_session, \
3
clear_mappers, sessionmaker, class_mapper
3
clear_mappers, sessionmaker, aliased,\
4
5
from sqlalchemy.orm.mapper import _mapper_registry
5
6
from sqlalchemy.orm.session import _sessions
7
from test.lib import testing, engines
7
from sqlalchemy import testing
8
from sqlalchemy.testing import engines
8
9
from sqlalchemy import MetaData, Integer, String, ForeignKey, \
9
PickleType, create_engine, Unicode
10
from test.lib.schema import Table, Column
11
11
import sqlalchemy as sa
12
from sqlalchemy.testing.schema import Table, Column
12
13
from sqlalchemy.sql import column
13
14
from sqlalchemy.processors import to_decimal_processor_factory, \
14
15
to_unicode_processor_factory
15
from test.lib.util import gc_collect
16
from sqlalchemy.util.compat import decimal
16
from sqlalchemy.testing.util import gc_collect
19
from sqlalchemy.testing import fixtures
19
from test.lib import fixtures
21
22
class A(fixtures.ComparableEntity):
23
24
class B(fixtures.ComparableEntity):
26
def profile_memory(func):
27
# run the test 50 times. if length of gc.get_objects()
28
# keeps growing, assert false
32
samples = [0 for x in range(0, 50)]
33
for x in range(0, 50):
29
def profile_memory(times=50):
31
# run the test 50 times. if length of gc.get_objects()
32
# keeps growing, assert false
34
def get_objects_skipping_sqlite_issue():
35
# pysqlite keeps adding weakref objects which only
36
# get reset after 220 iterations, which is too long
37
# to run lots of these tests, so just filter them
39
return [o for o in gc.get_objects()
40
if not isinstance(o, weakref.ref)]
36
samples[x] = len(gc.get_objects())
38
print "sample gc sizes:", samples
40
assert len(_sessions) == 0
42
for x in samples[-4:]:
49
# object count is bigger than when it started
50
if not flatline and samples[-1] > samples[0]:
51
for x in samples[1:-2]:
52
# see if a spike bigger than the endpoint exists
44
samples = [0 for x in range(0, times)]
45
for x in range(0, times):
48
samples[x] = len(get_objects_skipping_sqlite_issue())
50
print "sample gc sizes:", samples
52
assert len(_sessions) == 0
54
for x in samples[-4:]:
56
assert False, repr(samples) + " " + repr(flatline)
61
# object count is bigger than when it started
62
if not flatline and samples[-1] > samples[0]:
63
for x in samples[1:-2]:
64
# see if a spike bigger than the endpoint exists
68
assert False, repr(samples) + " " + repr(flatline)
60
73
def assert_no_mappers():
368
392
metadata.drop_all()
369
393
assert_no_mappers()
395
def test_alias_pathing(self):
396
metadata = MetaData(testing.db)
398
a = Table("a", metadata,
399
Column('id', Integer, primary_key=True,
400
test_needs_autoincrement=True),
401
Column('bid', Integer, ForeignKey('b.id')),
402
Column('type', String(30))
405
asub = Table("asub", metadata,
406
Column('id', Integer, ForeignKey('a.id'),
408
Column('data', String(30)))
410
b = Table("b", metadata,
411
Column('id', Integer, primary_key=True,
412
test_needs_autoincrement=True),
414
mapper(A, a, polymorphic_identity='a',
415
polymorphic_on=a.c.type)
416
mapper(ASub, asub, inherits=A,polymorphic_identity='asub')
417
m1 = mapper(B, b, properties={
418
'as_':relationship(A)
421
metadata.create_all()
426
b1 = B(as_=[a1, a2, a3])
431
# sqlite has a slow enough growth here
432
# that we have to run it more times to see the
434
@profile_memory(times=120)
437
sess.query(B).options(subqueryload(B.as_.of_type(ASub))).all()
445
def test_path_registry(self):
446
metadata = MetaData()
447
a = Table("a", metadata,
448
Column('id', Integer, primary_key=True),
449
Column('foo', Integer),
450
Column('bar', Integer)
455
ma = sa.inspect(aliased(A))
456
m1._path_registry[m1.attrs.foo][ma][m1.attrs.bar]
371
460
def test_with_inheritance(self):
372
461
metadata = MetaData(testing.db)
553
638
metadata.drop_all()
556
def test_mutable_identity(self):
557
metadata = MetaData(testing.db)
559
table1 = Table("mytable", metadata,
560
Column('col1', Integer, primary_key=True,
561
test_needs_autoincrement=True),
562
Column('col2', PickleType(comparator=operator.eq, mutable=True))
566
def __init__(self, col2):
570
metadata.create_all()
572
session = sessionmaker()()
593
testing.eq_(len(session.identity_map._mutable_attrs), 12)
594
testing.eq_(len(session.identity_map), 12)
597
testing.eq_(len(session.identity_map._mutable_attrs), 0)
598
testing.eq_(len(session.identity_map), 0)
605
642
def test_type_compile(self):
606
643
from sqlalchemy.dialects.sqlite.base import dialect as SQLiteDialect
607
644
cast = sa.cast(column('x'), sa.Integer)
610
647
dialect = SQLiteDialect()
611
648
cast.compile(dialect=dialect)