| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
import sqlite3 |
| 6 |
from tempfile import NamedTemporaryFile |
| 7 |
from petl.compat import next |
| 8 |
|
| 9 |
|
| 10 |
import petl.io.db as db |
| 11 |
from petl.test.helpers import ieq, eq_ |
| 12 |
from petl.io.db import fromdb, todb, appenddb |
| 13 |
from petl.io.db_create import drop_table |
| 14 |
|
| 15 |
|
| 16 |
# N.B., this file only tests the DB-related functions using sqlite3, |
| 17 |
# as anything else requires database connection configuration. See |
| 18 |
# docs/dbtests.py for a script to exercise the DB-related functions with |
| 19 |
# MySQL and Postgres. |
| 20 |
|
| 21 |
|
| 22 |
class LoggingConnection(object): |
| 23 |
|
| 24 |
def __init__(self): |
| 25 |
self.sql = [] |
| 26 |
|
| 27 |
def cursor(self): |
| 28 |
return self |
| 29 |
|
| 30 |
def execute(self, sql): |
| 31 |
self.sql.append(sql) |
| 32 |
|
| 33 |
def close(self): |
| 34 |
pass |
| 35 |
|
| 36 |
def commit(self): |
| 37 |
pass |
| 38 |
|
| 39 |
|
| 40 |
def test_drop_table_if_exists_sql(): |
| 41 |
dbo = LoggingConnection() |
| 42 |
|
| 43 |
drop_table(dbo, 'test_create', commit=False) |
| 44 |
drop_table(dbo, 'test_create', commit=False, if_exists=True) |
| 45 |
|
| 46 |
eq_([u'DROP TABLE "test_create"', |
| 47 |
u'DROP TABLE IF EXISTS "test_create"'], |
| 48 |
dbo.sql) |
| 49 |
|
| 50 |
|
| 51 |
def _record_todb_create_calls(monkeypatch, drop): |
| 52 |
calls = [] |
| 53 |
|
| 54 |
def fake_drop_table(dbo, tablename, schema=None, commit=True, |
| 55 |
if_exists=False): |
| 56 |
calls.append(('drop', tablename, if_exists)) |
| 57 |
|
| 58 |
def fake_create_table(table, dbo, tablename, schema=None, commit=True, |
| 59 |
constraints=True, metadata=None, dialect=None, |
| 60 |
sample=1000): |
| 61 |
calls.append(('create', tablename)) |
| 62 |
|
| 63 |
def fake_todb(table, dbo, tablename, schema=None, commit=True, |
| 64 |
truncate=False): |
| 65 |
calls.append(('load', tablename, truncate)) |
| 66 |
|
| 67 |
monkeypatch.setattr(db, 'drop_table', fake_drop_table) |
| 68 |
monkeypatch.setattr(db, 'create_table', fake_create_table) |
| 69 |
monkeypatch.setattr(db, '_todb', fake_todb) |
| 70 |
|
| 71 |
table = (('foo',), ('a',)) |
| 72 |
todb(table, object(), 'foobar', create=True, drop=drop) |
| 73 |
return calls |
| 74 |
|
| 75 |
|
| 76 |
def test_todb_drop_false_skips_drop(monkeypatch): |
| 77 |
calls = _record_todb_create_calls(monkeypatch, False) |
| 78 |
|
| 79 |
eq_([('create', 'foobar'), |
| 80 |
('load', 'foobar', True)], |
| 81 |
calls) |
| 82 |
|
| 83 |
|
| 84 |
def test_todb_drop_true_uses_plain_drop(monkeypatch): |
| 85 |
calls = _record_todb_create_calls(monkeypatch, True) |
| 86 |
|
| 87 |
eq_([('drop', 'foobar', False), |
| 88 |
('create', 'foobar'), |
| 89 |
('load', 'foobar', True)], |
| 90 |
calls) |
| 91 |
|
| 92 |
|
| 93 |
def test_todb_drop_if_exists_uses_if_exists(monkeypatch): |
| 94 |
calls = _record_todb_create_calls(monkeypatch, 'if_exists') |
| 95 |
|
| 96 |
eq_([('drop', 'foobar', True), |
| 97 |
('create', 'foobar'), |
| 98 |
('load', 'foobar', True)], |
| 99 |
calls) |
| 100 |
|
| 101 |
|
| 102 |
def test_fromdb(): |
| 103 |
|
| 104 |
# initial data |
| 105 |
data = (('a', 1), |
| 106 |
('b', 2), |
| 107 |
('c', 2.0)) |
| 108 |
connection = sqlite3.connect(':memory:') |
| 109 |
c = connection.cursor() |
| 110 |
c.execute('create table foobar (foo, bar)') |
| 111 |
for row in data: |
| 112 |
c.execute('insert into foobar values (?, ?)', row) |
| 113 |
connection.commit() |
| 114 |
c.close() |
| 115 |
|
| 116 |
# test the function |
| 117 |
actual = fromdb(connection, 'select * from foobar', ownsdb=True) |
| 118 |
expect = (('foo', 'bar'), |
| 119 |
('a', 1), |
| 120 |
('b', 2), |
| 121 |
('c', 2.0)) |
| 122 |
ieq(expect, actual) |
| 123 |
ieq(expect, actual) # verify can iterate twice |
| 124 |
|
| 125 |
# test iterators are isolated |
| 126 |
i1 = iter(actual) |
| 127 |
i2 = iter(actual) |
| 128 |
eq_(('foo', 'bar'), next(i1)) |
| 129 |
eq_(('a', 1), next(i1)) |
| 130 |
eq_(('foo', 'bar'), next(i2)) |
| 131 |
eq_(('b', 2), next(i1)) |
| 132 |
|
| 133 |
|
| 134 |
def test_fromdb_mkcursor(): |
| 135 |
|
| 136 |
# initial data |
| 137 |
data = (('a', 1), |
| 138 |
('b', 2), |
| 139 |
('c', 2.0)) |
| 140 |
connection = sqlite3.connect(':memory:') |
| 141 |
c = connection.cursor() |
| 142 |
c.execute('create table foobar (foo, bar)') |
| 143 |
for row in data: |
| 144 |
c.execute('insert into foobar values (?, ?)', row) |
| 145 |
connection.commit() |
| 146 |
c.close() |
| 147 |
|
| 148 |
# test the function |
| 149 |
mkcursor = lambda: connection.cursor() |
| 150 |
actual = fromdb(mkcursor, 'select * from foobar') |
| 151 |
expect = (('foo', 'bar'), |
| 152 |
('a', 1), |
| 153 |
('b', 2), |
| 154 |
('c', 2.0)) |
| 155 |
ieq(expect, actual) |
| 156 |
ieq(expect, actual) # verify can iterate twice |
| 157 |
|
| 158 |
# test iterators are isolated |
| 159 |
i1 = iter(actual) |
| 160 |
i2 = iter(actual) |
| 161 |
eq_(('foo', 'bar'), next(i1)) |
| 162 |
eq_(('a', 1), next(i1)) |
| 163 |
eq_(('foo', 'bar'), next(i2)) |
| 164 |
eq_(('b', 2), next(i1)) |
| 165 |
|
| 166 |
|
| 167 |
def test_fromdb_withargs(): |
| 168 |
|
| 169 |
# initial data |
| 170 |
data = (('a', 1), |
| 171 |
('b', 2), |
| 172 |
('c', 2.0)) |
| 173 |
connection = sqlite3.connect(':memory:') |
| 174 |
c = connection.cursor() |
| 175 |
c.execute('create table foobar (foo, bar)') |
| 176 |
for row in data: |
| 177 |
c.execute('insert into foobar values (?, ?)', row) |
| 178 |
connection.commit() |
| 179 |
c.close() |
| 180 |
|
| 181 |
# test the function |
| 182 |
actual = fromdb( |
| 183 |
connection, |
| 184 |
'select * from foobar where bar > ? and bar < ?', |
| 185 |
(1, 3) |
| 186 |
) |
| 187 |
expect = (('foo', 'bar'), |
| 188 |
('b', 2), |
| 189 |
('c', 2.0)) |
| 190 |
ieq(expect, actual) |
| 191 |
ieq(expect, actual) # verify can iterate twice |
| 192 |
connection.close() |
| 193 |
|
| 194 |
|
| 195 |
def test_todb_appenddb(): |
| 196 |
|
| 197 |
f = NamedTemporaryFile(delete=False) |
| 198 |
conn = sqlite3.connect(f.name) |
| 199 |
conn.execute('create table foobar (foo, bar)') |
| 200 |
conn.commit() |
| 201 |
|
| 202 |
# exercise function |
| 203 |
table = (('foo', 'bar'), |
| 204 |
('a', 1), |
| 205 |
('b', 2), |
| 206 |
('c', 2)) |
| 207 |
todb(table, conn, 'foobar') |
| 208 |
|
| 209 |
# check what it did |
| 210 |
actual = conn.execute('select * from foobar') |
| 211 |
expect = (('a', 1), |
| 212 |
('b', 2), |
| 213 |
('c', 2)) |
| 214 |
ieq(expect, actual) |
| 215 |
|
| 216 |
# try appending |
| 217 |
table2 = (('foo', 'bar'), |
| 218 |
('d', 7), |
| 219 |
('e', 9), |
| 220 |
('f', 1)) |
| 221 |
appenddb(table2, conn, 'foobar') |
| 222 |
|
| 223 |
# check what it did |
| 224 |
actual = conn.execute('select * from foobar') |
| 225 |
expect = (('a', 1), |
| 226 |
('b', 2), |
| 227 |
('c', 2), |
| 228 |
('d', 7), |
| 229 |
('e', 9), |
| 230 |
('f', 1)) |
| 231 |
ieq(expect, actual) |
| 232 |
conn.close() |
| 233 |
|
| 234 |
|
| 235 |
def test_todb_appenddb_cursor(): |
| 236 |
|
| 237 |
f = NamedTemporaryFile(delete=False) |
| 238 |
conn = sqlite3.connect(f.name) |
| 239 |
conn.execute('create table foobar (foo, bar)') |
| 240 |
conn.commit() |
| 241 |
|
| 242 |
# exercise function |
| 243 |
table = (('foo', 'bar'), |
| 244 |
('a', 1), |
| 245 |
('b', 2), |
| 246 |
('c', 2)) |
| 247 |
cursor = conn.cursor() |
| 248 |
todb(table, cursor, 'foobar') |
| 249 |
|
| 250 |
# check what it did |
| 251 |
actual = conn.execute('select * from foobar') |
| 252 |
expect = (('a', 1), |
| 253 |
('b', 2), |
| 254 |
('c', 2)) |
| 255 |
ieq(expect, actual) |
| 256 |
|
| 257 |
# try appending |
| 258 |
table2 = (('foo', 'bar'), |
| 259 |
('d', 7), |
| 260 |
('e', 9), |
| 261 |
('f', 1)) |
| 262 |
appenddb(table2, cursor, 'foobar') |
| 263 |
|
| 264 |
# check what it did |
| 265 |
actual = conn.execute('select * from foobar') |
| 266 |
expect = (('a', 1), |
| 267 |
('b', 2), |
| 268 |
('c', 2), |
| 269 |
('d', 7), |
| 270 |
('e', 9), |
| 271 |
('f', 1)) |
| 272 |
ieq(expect, actual) |
| 273 |
conn.close() |