summaryrefslogtreecommitdiff
path: root/test/engine/test_processors.py
blob: 83b5a79489c781ed067c0f9ae131c4c40ae8004d (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
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
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import assert_raises_message, eq_


class _BooleanProcessorTest(fixtures.TestBase):
    def test_int_to_bool_none(self):
        eq_(
            self.module.int_to_boolean(None),
            None
        )

    def test_int_to_bool_zero(self):
        eq_(
            self.module.int_to_boolean(0),
            False
        )

    def test_int_to_bool_one(self):
        eq_(
            self.module.int_to_boolean(1),
            True
        )

    def test_int_to_bool_positive_int(self):
        eq_(
            self.module.int_to_boolean(12),
            True
        )

    def test_int_to_bool_negative_int(self):
        eq_(
            self.module.int_to_boolean(-4),
            True
        )


class PyBooleanProcessorTest(_BooleanProcessorTest):
    @classmethod
    def setup_class(cls):
        from sqlalchemy import processors
        cls.module = type(
            "util", (object,),
            dict(
                (k, staticmethod(v))
                for k, v in list(processors.py_fallback().items())
            )
        )

    def test_bool_to_int_false(self):
        from sqlalchemy import processors
        eq_(processors.boolean_to_int(False), 0)

    def test_bool_to_int_true(self):
        from sqlalchemy import processors
        eq_(processors.boolean_to_int(True), 1)

    def test_bool_to_int_positive_int(self):
        from sqlalchemy import processors
        eq_(processors.boolean_to_int(5), 1)

    def test_bool_to_int_negative_int(self):
        from sqlalchemy import processors
        eq_(processors.boolean_to_int(-10), 1)

    def test_bool_to_int_zero(self):
        from sqlalchemy import processors
        eq_(processors.boolean_to_int(0), 0)

    def test_bool_to_int_one(self):
        from sqlalchemy import processors
        eq_(processors.boolean_to_int(1), 1)


class CBooleanProcessorTest(_BooleanProcessorTest):
    __requires__ = ('cextensions',)

    @classmethod
    def setup_class(cls):
        from sqlalchemy import cprocessors
        cls.module = cprocessors


class _DateProcessorTest(fixtures.TestBase):
    def test_date_no_string(self):
        assert_raises_message(
            ValueError,
            "Couldn't parse date string '2012' - value is not a string",
            self.module.str_to_date, 2012
        )

    def test_datetime_no_string(self):
        assert_raises_message(
            ValueError,
            "Couldn't parse datetime string '2012' - value is not a string",
            self.module.str_to_datetime, 2012
        )

    def test_time_no_string(self):
        assert_raises_message(
            ValueError,
            "Couldn't parse time string '2012' - value is not a string",
            self.module.str_to_time, 2012
        )

    def test_date_invalid_string(self):
        assert_raises_message(
            ValueError,
            "Couldn't parse date string: '5:a'",
            self.module.str_to_date, "5:a"
        )

    def test_datetime_invalid_string(self):
        assert_raises_message(
            ValueError,
            "Couldn't parse datetime string: '5:a'",
            self.module.str_to_datetime, "5:a"
        )

    def test_time_invalid_string(self):
        assert_raises_message(
            ValueError,
            "Couldn't parse time string: '5:a'",
            self.module.str_to_time, "5:a"
        )


class PyDateProcessorTest(_DateProcessorTest):
    @classmethod
    def setup_class(cls):
        from sqlalchemy import processors
        cls.module = type(
            "util",
            (object,),
            dict((k, staticmethod(v))
                 for k, v in list(processors.py_fallback().items()))
        )


class CDateProcessorTest(_DateProcessorTest):
    __requires__ = ('cextensions',)

    @classmethod
    def setup_class(cls):
        from sqlalchemy import cprocessors
        cls.module = cprocessors


class _DistillArgsTest(fixtures.TestBase):
    def test_distill_none(self):
        eq_(
            self.module._distill_params(None, None),
            []
        )

    def test_distill_no_multi_no_param(self):
        eq_(
            self.module._distill_params((), {}),
            []
        )

    def test_distill_dict_multi_none_param(self):
        eq_(
            self.module._distill_params(None, {"foo": "bar"}),
            [{"foo": "bar"}]
        )

    def test_distill_dict_multi_empty_param(self):
        eq_(
            self.module._distill_params((), {"foo": "bar"}),
            [{"foo": "bar"}]
        )

    def test_distill_single_dict(self):
        eq_(
            self.module._distill_params(({"foo": "bar"},), {}),
            [{"foo": "bar"}]
        )

    def test_distill_single_list_strings(self):
        eq_(
            self.module._distill_params((["foo", "bar"],), {}),
            [["foo", "bar"]]
        )

    def test_distill_single_list_tuples(self):
        eq_(
            self.module._distill_params(
                ([("foo", "bar"), ("bat", "hoho")],), {}),
            [('foo', 'bar'), ('bat', 'hoho')]
        )

    def test_distill_single_list_tuple(self):
        eq_(
            self.module._distill_params(([("foo", "bar")],), {}),
            [('foo', 'bar')]
        )

    def test_distill_multi_list_tuple(self):
        eq_(
            self.module._distill_params(
                ([("foo", "bar")], [("bar", "bat")]), {}),
            ([('foo', 'bar')], [('bar', 'bat')])
        )

    def test_distill_multi_strings(self):
        eq_(
            self.module._distill_params(("foo", "bar"), {}),
            [('foo', 'bar')]
        )

    def test_distill_single_list_dicts(self):
        eq_(
            self.module._distill_params(
                ([{"foo": "bar"}, {"foo": "hoho"}],), {}),
            [{'foo': 'bar'}, {'foo': 'hoho'}]
        )

    def test_distill_single_string(self):
        eq_(
            self.module._distill_params(("arg",), {}),
            [["arg"]]
        )

    def test_distill_multi_string_tuple(self):
        eq_(
            self.module._distill_params((("arg", "arg"),), {}),
            [("arg", "arg")]
        )


class PyDistillArgsTest(_DistillArgsTest):
    @classmethod
    def setup_class(cls):
        from sqlalchemy.engine import util
        cls.module = type(
            "util",
            (object,),
            dict((k, staticmethod(v))
                 for k, v in list(util.py_fallback().items()))
        )


class CDistillArgsTest(_DistillArgsTest):
    __requires__ = ('cextensions', )

    @classmethod
    def setup_class(cls):
        from sqlalchemy import cutils as util
        cls.module = util