aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_db_enginestrategy.py
blob: d969397913629fe86630606f7d14abadc881a47c (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
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
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from twisted.trial import unittest
from twisted.python import runtime
from sqlalchemy.engine import url
from sqlalchemy.pool import NullPool
from buildbot.db import enginestrategy

class BuildbotEngineStrategy_special_cases(unittest.TestCase):
    "Test the special case methods, without actually creating a db"

    # used several times below
    mysql_kwargs = dict(basedir='my-base-dir',
            connect_args=dict(init_command='SET storage_engine=MyISAM'),
            pool_recycle=3600)
    sqlite_kwargs = dict(basedir='/my-base-dir', poolclass=NullPool)

    def setUp(self):
        self.strat = enginestrategy.BuildbotEngineStrategy()

    # utility

    def filter_kwargs(self, kwargs):
        # filter out the listeners list to just include the class name
        if 'listeners' in kwargs:
            kwargs['listeners'] = [ lstnr.__class__.__name__
                            for lstnr in kwargs['listeners'] ]
        return kwargs

    # tests

    def test_sqlite_pct_sub(self):
        u = url.make_url("sqlite:///%(basedir)s/x/state.sqlite")
        kwargs = dict(basedir='/my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_sqlite(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
            [ "sqlite:////my-base-dir/x/state.sqlite", None,
              self.sqlite_kwargs ])

    def test_sqlite_relpath(self):
        url_src = "sqlite:///x/state.sqlite"
        basedir = "/my-base-dir"
        expected_url = "sqlite:////my-base-dir/x/state.sqlite"

        # this looks a whole lot different on windows
        if runtime.platformType == 'win32':
            url_src = r'sqlite:///X\STATE.SQLITE'
            basedir = r'C:\MYBASE~1'
            expected_url = r'sqlite:///C:\MYBASE~1\X\STATE.SQLITE'

        exp_kwargs = self.sqlite_kwargs.copy()
        exp_kwargs['basedir'] = basedir

        u = url.make_url(url_src)
        kwargs = dict(basedir=basedir)
        u, kwargs, max_conns = self.strat.special_case_sqlite(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
            [ expected_url, None, exp_kwargs ])

    def test_sqlite_abspath(self):
        u = url.make_url("sqlite:////x/state.sqlite")
        kwargs = dict(basedir='/my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_sqlite(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
            [ "sqlite:////x/state.sqlite", None, self.sqlite_kwargs ])

    def test_sqlite_memory(self):
        u = url.make_url("sqlite://")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_sqlite(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
            [ "sqlite://", 1, # only one conn at a time
              dict(basedir='my-base-dir',
                   # note: no poolclass= argument
                   pool_size=1) ]) # extra in-memory args

    def test_mysql_simple(self):
        u = url.make_url("mysql://host/dbname")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql://host/dbname?charset=utf8&use_unicode=True", None,
                  self.mysql_kwargs ])

    def test_mysql_userport(self):
        u = url.make_url("mysql://user:pass@host:1234/dbname")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql://user:pass@host:1234/dbname?"
                  "charset=utf8&use_unicode=True", None, self.mysql_kwargs ])

    def test_mysql_local(self):
        u = url.make_url("mysql:///dbname")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql:///dbname?charset=utf8&use_unicode=True", None,
                  self.mysql_kwargs ])

    def test_mysql_args(self):
        u = url.make_url("mysql:///dbname?foo=bar")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql:///dbname?charset=utf8&foo=bar&use_unicode=True",
                  None, self.mysql_kwargs ])

    def test_mysql_max_idle(self):
        u = url.make_url("mysql:///dbname?max_idle=1234")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        exp = self.mysql_kwargs.copy()
        exp['pool_recycle'] = 1234
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql:///dbname?charset=utf8&use_unicode=True", None,
                  exp ])

    def test_mysql_good_charset(self):
        u = url.make_url("mysql:///dbname?charset=utf8")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql:///dbname?charset=utf8&use_unicode=True", None,
                  self.mysql_kwargs ])

    def test_mysql_bad_charset(self):
        u = url.make_url("mysql:///dbname?charset=ebcdic")
        kwargs = dict(basedir='my-base-dir')
        self.assertRaises(TypeError,
                lambda : self.strat.special_case_mysql(u, kwargs))

    def test_mysql_good_use_unicode(self):
        u = url.make_url("mysql:///dbname?use_unicode=True")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql:///dbname?charset=utf8&use_unicode=True", None,
                  self.mysql_kwargs ])

    def test_mysql_bad_use_unicode(self):
        u = url.make_url("mysql:///dbname?use_unicode=maybe")
        kwargs = dict(basedir='my-base-dir')
        self.assertRaises(TypeError,
                lambda : self.strat.special_case_mysql(u, kwargs))

    def test_mysql_storage_engine(self):
        u = url.make_url("mysql:///dbname?storage_engine=foo")
        kwargs = dict(basedir='my-base-dir')
        u, kwargs, max_conns = self.strat.special_case_mysql(u, kwargs)
        exp = self.mysql_kwargs.copy()
        exp['connect_args'] = dict(init_command='SET storage_engine=foo')
        self.assertEqual([ str(u), max_conns, self.filter_kwargs(kwargs) ],
                [ "mysql:///dbname?charset=utf8&use_unicode=True", None,
                  exp ])


class BuildbotEngineStrategy(unittest.TestCase):
    "Test create_engine by creating a sqlite in-memory db"

    def test_create_engine(self):
        engine = enginestrategy.create_engine('sqlite://', basedir="/base")
        self.assertEqual(engine.scalar("SELECT 13 + 14"), 27)