summaryrefslogtreecommitdiff
path: root/test/orm/memusage.py
blob: b456a1abbc715b562d4167f56122519e28a74d09 (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
79
from sqlalchemy import *
from sqlalchemy.orm import mapperlib, session, unitofwork, attributes
Mapper = mapperlib.Mapper
import gc
import testbase
import tables

class A(object):pass
class B(object):pass

class MapperCleanoutTest(testbase.AssertMixin):
    """test that clear_mappers() removes everything related to the class.
    
    does not include classes that use the assignmapper extension."""
    def setUp(self):
        global engine
        engine = testbase.db
    
    def test_mapper_cleanup(self):
        for x in range(0, 5):
            self.do_test()
            gc.collect()
            for o in gc.get_objects():
                if isinstance(o, Mapper):
                    # the classes in the 'tables' package have assign_mapper called on them
                    # which is particularly sticky
                    # if getattr(tables, o.class_.__name__, None) is o.class_:
                    #    continue
                    # well really we are just testing our own classes here
                    if (o.class_ not in [A,B]):
                        continue
                    assert False
        assert True
        
    def do_test(self):
        metadata = BoundMetaData(engine)

        table1 = Table("mytable", metadata, 
            Column('col1', Integer, primary_key=True),
            Column('col2', String(30))
            )

        table2 = Table("mytable2", metadata, 
            Column('col1', Integer, primary_key=True),
            Column('col2', String(30)),
            Column('col3', Integer, ForeignKey("mytable.col1"))
            )
    
        metadata.create_all()


        m1 = mapper(A, table1, properties={
            "bs":relation(B)
        })
        m2 = mapper(B, table2)

        m3 = mapper(A, table1, non_primary=True)
        
        sess = create_session()
        a1 = A()
        a2 = A()
        a3 = A()
        a1.bs.append(B())
        a1.bs.append(B())
        a3.bs.append(B())
        for x in [a1,a2,a3]:
            sess.save(x)
        sess.flush()
        sess.clear()

        alist = sess.query(A).select()
        for a in alist:
            print "A", a, "BS", [b for b in a.bs]
    
        metadata.drop_all()
        clear_mappers()
    
if __name__ == '__main__':
    testbase.main()