File: test_custombase.py

package info (click to toggle)
elixir 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 640 kB
  • ctags: 1,079
  • sloc: python: 4,936; makefile: 10
file content (101 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download
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
"""
test having entities using a custom base class
"""

from elixir import *

def setup():
    metadata.bind = 'sqlite:///'

    global MyBase

    class MyBase(object):
        __metaclass__ = EntityMeta

        def __init__(self, **kwargs):
            for key, value in kwargs.items():
                setattr(self, key, value)

class TestCustomBase(object):
    def teardown(self):
        cleanup_all(True)

    def test_simple(self):
        class A(MyBase):
            name = Field(String(30))

        setup_all(True)

        a1 = A(name="a1")

        session.commit()
        session.clear()

        a = A.query.filter_by(name="a1").one()

        assert a.name == 'a1'

    def test_inherit(self):
        class A(MyBase):
            name = Field(String(30))

        class B(A):
            data = Field(String(30))

        setup_all(True)

        a1 = A(name="a1")
        b1 = B(name="b1", data="-b1-")

        session.commit()
        session.clear()

        b = A.query.filter_by(name="b1").one()

        assert b.data == '-b1-'

    def test_non_object_base(self):
        class BaseParent(object):
            def test(self):
                return "success"

        class InheritedBase(BaseParent):
            __metaclass__ = EntityMeta

        class A(InheritedBase):
            name = Field(String(30))

        setup_all(True)

        a1 = A()
        a1.name = "a1"

        session.commit()
        session.clear()

        a = A.query.filter_by(name="a1").one()

        assert a.name == 'a1'
        assert a.test() == "success"

    def test_base_with_fields(self):
        class FieldBase(object):
            __metaclass__ = EntityMeta

            common = Field(String(32))

            def __init__(self, **kwargs):
                for key, value in kwargs.items():
                    setattr(self, key, value)

        class A(FieldBase):
            name = Field(String(32))

        class B(FieldBase):
            pass

        setup_all(True)

        assert 'name' in A.table.columns.keys()
        assert 'common' in A.table.columns.keys()
        assert 'common' in B.table.columns.keys()