~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to test/dialect/test_sqlite.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""SQLite-specific tests."""
2
2
 
3
 
from test.lib.testing import eq_, assert_raises, \
 
3
from sqlalchemy.testing import eq_, assert_raises, \
4
4
    assert_raises_message
5
5
import datetime
6
 
from sqlalchemy import *
 
6
from sqlalchemy import Table, String, select, Text, CHAR, bindparam, Column,\
 
7
    Unicode, Date, MetaData, UnicodeText, Time, Integer, TIMESTAMP, \
 
8
    Boolean, func, NUMERIC, DateTime, extract, ForeignKey, text, Numeric,\
 
9
    DefaultClause, and_, DECIMAL, TypeDecorator, create_engine, Float, \
 
10
    INTEGER, UniqueConstraint, DATETIME, DATE, TIME, BOOLEAN, BIGINT
7
11
from sqlalchemy import exc, sql, schema, pool, types as sqltypes
8
12
from sqlalchemy.dialects.sqlite import base as sqlite, \
9
13
    pysqlite as pysqlite_dialect
10
14
from sqlalchemy.engine.url import make_url
11
 
from test.lib import *
 
15
from sqlalchemy.testing import fixtures, AssertsCompiledSQL, \
 
16
    AssertsExecutionResults, engines
 
17
from sqlalchemy import testing
12
18
import os
13
19
from sqlalchemy.schema import CreateTable
14
20
 
63
69
                ).scalar()
64
70
            )
65
71
 
66
 
    def test_time_microseconds(self):
67
 
        dt = datetime.datetime(2008, 6, 27, 12, 0, 0, 125, )
68
 
        eq_(str(dt), '2008-06-27 12:00:00.000125')
69
 
        sldt = sqlite.DATETIME()
70
 
        bp = sldt.bind_processor(None)
71
 
        eq_(bp(dt), '2008-06-27 12:00:00.000125')
72
 
        rp = sldt.result_processor(None, None)
73
 
        eq_(rp(bp(dt)), dt)
74
 
 
75
72
    def test_native_datetime(self):
76
73
        dbapi = testing.db.dialect.dbapi
77
74
        connect_args = {'detect_types': dbapi.PARSE_DECLTYPES \
137
134
            (Numeric(10, 2), NUMERIC(10, 2)),
138
135
            (DECIMAL, DECIMAL()),
139
136
            (DECIMAL(10, 2), DECIMAL(10, 2)),
 
137
            (INTEGER, INTEGER()),
 
138
            (BIGINT, BIGINT()),
140
139
            (Float, Float()),
141
140
            (NUMERIC(), ),
142
141
            (TIMESTAMP, TIMESTAMP()),
183
182
        assert isinstance(t2.c.y.type, sqltypes.NullType)
184
183
 
185
184
 
 
185
class DateTimeTest(fixtures.TestBase, AssertsCompiledSQL):
 
186
 
 
187
    def test_time_microseconds(self):
 
188
        dt = datetime.datetime(2008, 6, 27, 12, 0, 0, 125, )
 
189
        eq_(str(dt), '2008-06-27 12:00:00.000125')
 
190
        sldt = sqlite.DATETIME()
 
191
        bp = sldt.bind_processor(None)
 
192
        eq_(bp(dt), '2008-06-27 12:00:00.000125')
 
193
        rp = sldt.result_processor(None, None)
 
194
        eq_(rp(bp(dt)), dt)
 
195
 
 
196
    def test_truncate_microseconds(self):
 
197
        dt = datetime.datetime(2008, 6, 27, 12, 0, 0, 125)
 
198
        dt_out = datetime.datetime(2008, 6, 27, 12, 0, 0)
 
199
        eq_(str(dt), '2008-06-27 12:00:00.000125')
 
200
        sldt = sqlite.DATETIME(truncate_microseconds=True)
 
201
        bp = sldt.bind_processor(None)
 
202
        eq_(bp(dt), '2008-06-27 12:00:00')
 
203
        rp = sldt.result_processor(None, None)
 
204
        eq_(rp(bp(dt)), dt_out)
 
205
 
 
206
    def test_custom_format_compact(self):
 
207
        dt = datetime.datetime(2008, 6, 27, 12, 0, 0, 125)
 
208
        eq_(str(dt), '2008-06-27 12:00:00.000125')
 
209
        sldt = sqlite.DATETIME(
 
210
            storage_format=(
 
211
                "%(year)04d%(month)02d%(day)02d"
 
212
                "%(hour)02d%(minute)02d%(second)02d%(microsecond)06d"
 
213
            ),
 
214
            regexp="(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{6})",
 
215
        )
 
216
        bp = sldt.bind_processor(None)
 
217
        eq_(bp(dt), '20080627120000000125')
 
218
        rp = sldt.result_processor(None, None)
 
219
        eq_(rp(bp(dt)), dt)
 
220
 
 
221
 
 
222
class DateTest(fixtures.TestBase, AssertsCompiledSQL):
 
223
 
 
224
    def test_default(self):
 
225
        dt = datetime.date(2008, 6, 27)
 
226
        eq_(str(dt), '2008-06-27')
 
227
        sldt = sqlite.DATE()
 
228
        bp = sldt.bind_processor(None)
 
229
        eq_(bp(dt), '2008-06-27')
 
230
        rp = sldt.result_processor(None, None)
 
231
        eq_(rp(bp(dt)), dt)
 
232
 
 
233
    def test_custom_format(self):
 
234
        dt = datetime.date(2008, 6, 27)
 
235
        eq_(str(dt), '2008-06-27')
 
236
        sldt = sqlite.DATE(
 
237
            storage_format="%(month)02d/%(day)02d/%(year)04d",
 
238
            regexp="(?P<month>\d+)/(?P<day>\d+)/(?P<year>\d+)",
 
239
        )
 
240
        bp = sldt.bind_processor(None)
 
241
        eq_(bp(dt), '06/27/2008')
 
242
        rp = sldt.result_processor(None, None)
 
243
        eq_(rp(bp(dt)), dt)
 
244
 
 
245
class TimeTest(fixtures.TestBase, AssertsCompiledSQL):
 
246
 
 
247
    def test_default(self):
 
248
        dt = datetime.date(2008, 6, 27)
 
249
        eq_(str(dt), '2008-06-27')
 
250
        sldt = sqlite.DATE()
 
251
        bp = sldt.bind_processor(None)
 
252
        eq_(bp(dt), '2008-06-27')
 
253
        rp = sldt.result_processor(None, None)
 
254
        eq_(rp(bp(dt)), dt)
 
255
 
 
256
    def test_truncate_microseconds(self):
 
257
        dt = datetime.time(12, 0, 0, 125)
 
258
        dt_out = datetime.time(12, 0, 0)
 
259
        eq_(str(dt), '12:00:00.000125')
 
260
        sldt = sqlite.TIME(truncate_microseconds=True)
 
261
        bp = sldt.bind_processor(None)
 
262
        eq_(bp(dt), '12:00:00')
 
263
        rp = sldt.result_processor(None, None)
 
264
        eq_(rp(bp(dt)), dt_out)
 
265
 
 
266
    def test_custom_format(self):
 
267
        dt = datetime.date(2008, 6, 27)
 
268
        eq_(str(dt), '2008-06-27')
 
269
        sldt = sqlite.DATE(
 
270
            storage_format="%(year)04d%(month)02d%(day)02d",
 
271
            regexp="(\d{4})(\d{2})(\d{2})",
 
272
        )
 
273
        bp = sldt.bind_processor(None)
 
274
        eq_(bp(dt), '20080627')
 
275
        rp = sldt.result_processor(None, None)
 
276
        eq_(rp(bp(dt)), dt)
 
277
 
 
278
 
186
279
class DefaultsTest(fixtures.TestBase, AssertsCompiledSQL):
187
280
 
188
281
    __only_on__ = 'sqlite'
314
407
            meta.drop_all()
315
408
 
316
409
    @testing.provide_metadata
317
 
    def test_quoted_identifiers_one(self):
 
410
    def test_quoted_identifiers_functional_one(self):
318
411
        """Tests autoload of tables created with quoted column names."""
319
412
 
320
413
        metadata = self.metadata
340
433
                == table2.c.id)
341
434
 
342
435
    @testing.provide_metadata
343
 
    def test_quoted_identifiers_two(self):
 
436
    def test_quoted_identifiers_functional_two(self):
344
437
        """"test the edgiest of edge cases, quoted table/col names
345
438
        that start and end with quotes.
346
439
 
375
468
        #assert j.onclause.compare(table1.c['"id"']
376
469
        #        == table2.c['"aid"'])
377
470
 
 
471
    def test_legacy_quoted_identifiers_unit(self):
 
472
        dialect = sqlite.dialect()
 
473
        dialect._broken_fk_pragma_quotes = True
 
474
 
 
475
 
 
476
        for row in [
 
477
            (0, 'target', 'tid', 'id'),
 
478
            (0, '"target"', 'tid', 'id'),
 
479
            (0, '[target]', 'tid', 'id'),
 
480
            (0, "'target'", 'tid', 'id'),
 
481
            (0, '`target`', 'tid', 'id'),
 
482
        ]:
 
483
            fks = {}
 
484
            fkeys = []
 
485
            dialect._parse_fk(fks, fkeys, *row)
 
486
            eq_(fkeys, [{
 
487
                    'referred_table': 'target',
 
488
                    'referred_columns': ['id'],
 
489
                    'referred_schema': None,
 
490
                    'name': None,
 
491
                    'constrained_columns': ['tid']
 
492
                }])
 
493
 
 
494
 
378
495
    def test_attached_as_schema(self):
379
496
        cx = testing.db.connect()
380
497
        try: