| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
|
| 5 |
from tempfile import NamedTemporaryFile |
| 6 |
import sqlite3 |
| 7 |
|
| 8 |
|
| 9 |
from petl.test.helpers import ieq |
| 10 |
from petl.io.db import fromdb, todb, appenddb |
| 11 |
|
| 12 |
|
| 13 |
def test_fromsqlite3(): |
| 14 |
|
| 15 |
# initial data |
| 16 |
f = NamedTemporaryFile(delete=False) |
| 17 |
f.close() |
| 18 |
data = (('a', 1), |
| 19 |
('b', 2), |
| 20 |
('c', 2.0)) |
| 21 |
connection = sqlite3.connect(f.name) |
| 22 |
c = connection.cursor() |
| 23 |
c.execute('CREATE TABLE foobar (foo, bar)') |
| 24 |
for row in data: |
| 25 |
c.execute('INSERT INTO foobar VALUES (?, ?)', row) |
| 26 |
connection.commit() |
| 27 |
c.close() |
| 28 |
connection.close() |
| 29 |
|
| 30 |
# test the function |
| 31 |
actual = fromdb(f.name, 'SELECT * FROM foobar') |
| 32 |
expect = (('foo', 'bar'), |
| 33 |
('a', 1), |
| 34 |
('b', 2), |
| 35 |
('c', 2.0)) |
| 36 |
|
| 37 |
ieq(expect, actual, cast=tuple) |
| 38 |
ieq(expect, actual, cast=tuple) # verify can iterate twice |
| 39 |
|
| 40 |
|
| 41 |
def test_fromsqlite3_connection(): |
| 42 |
|
| 43 |
# initial data |
| 44 |
data = (('a', 1), |
| 45 |
('b', 2), |
| 46 |
('c', 2.0)) |
| 47 |
connection = sqlite3.connect(':memory:') |
| 48 |
c = connection.cursor() |
| 49 |
c.execute('CREATE TABLE foobar (foo, bar)') |
| 50 |
for row in data: |
| 51 |
c.execute('INSERT INTO foobar VALUES (?, ?)', row) |
| 52 |
connection.commit() |
| 53 |
c.close() |
| 54 |
|
| 55 |
# test the function |
| 56 |
actual = fromdb(connection, 'SELECT * FROM foobar') |
| 57 |
expect = (('foo', 'bar'), |
| 58 |
('a', 1), |
| 59 |
('b', 2), |
| 60 |
('c', 2.0)) |
| 61 |
|
| 62 |
ieq(expect, actual, cast=tuple) |
| 63 |
ieq(expect, actual, cast=tuple) # verify can iterate twice |
| 64 |
connection.close() |
| 65 |
|
| 66 |
|
| 67 |
def test_fromsqlite3_withargs(): |
| 68 |
|
| 69 |
# initial data |
| 70 |
data = (('a', 1), |
| 71 |
('b', 2), |
| 72 |
('c', 2.0)) |
| 73 |
connection = sqlite3.connect(':memory:') |
| 74 |
c = connection.cursor() |
| 75 |
c.execute('CREATE TABLE foobar (foo, bar)') |
| 76 |
for row in data: |
| 77 |
c.execute('INSERT INTO foobar VALUES (?, ?)', row) |
| 78 |
connection.commit() |
| 79 |
c.close() |
| 80 |
|
| 81 |
# test the function |
| 82 |
actual = fromdb( |
| 83 |
connection, |
| 84 |
'SELECT * FROM foobar WHERE bar > ? AND bar < ?', |
| 85 |
(1, 3) |
| 86 |
) |
| 87 |
expect = (('foo', 'bar'), |
| 88 |
('b', 2), |
| 89 |
('c', 2.0)) |
| 90 |
ieq(expect, actual) |
| 91 |
ieq(expect, actual) # verify can iterate twice |
| 92 |
connection.close() |
| 93 |
|
| 94 |
|
| 95 |
def test_tosqlite3_appendsqlite3(): |
| 96 |
|
| 97 |
# exercise function |
| 98 |
table = (('foo', 'bar'), |
| 99 |
('a', 1), |
| 100 |
('b', 2), |
| 101 |
('c', 2)) |
| 102 |
f = NamedTemporaryFile(delete=False) |
| 103 |
f.close() |
| 104 |
conn = sqlite3.connect(f.name) |
| 105 |
conn.execute('CREATE TABLE foobar (foo TEXT, bar INT)') |
| 106 |
conn.close() |
| 107 |
todb(table, f.name, 'foobar') |
| 108 |
|
| 109 |
# check what it did |
| 110 |
conn = sqlite3.connect(f.name) |
| 111 |
actual = conn.execute('SELECT * FROM foobar') |
| 112 |
expect = (('a', 1), |
| 113 |
('b', 2), |
| 114 |
('c', 2)) |
| 115 |
ieq(expect, actual) |
| 116 |
|
| 117 |
# check appending |
| 118 |
table2 = (('foo', 'bar'), |
| 119 |
('d', 7), |
| 120 |
('e', 9), |
| 121 |
('f', 1)) |
| 122 |
appenddb(table2, f.name, 'foobar') |
| 123 |
conn.close() |
| 124 |
|
| 125 |
# check what it did |
| 126 |
conn = sqlite3.connect(f.name) |
| 127 |
actual = conn.execute('SELECT * FROM foobar') |
| 128 |
expect = (('a', 1), |
| 129 |
('b', 2), |
| 130 |
('c', 2), |
| 131 |
('d', 7), |
| 132 |
('e', 9), |
| 133 |
('f', 1)) |
| 134 |
ieq(expect, actual) |
| 135 |
conn.close() |
| 136 |
|
| 137 |
|
| 138 |
def test_tosqlite3_appendsqlite3_connection(): |
| 139 |
|
| 140 |
conn = sqlite3.connect(':memory:') |
| 141 |
conn.execute('CREATE TABLE foobar (foo TEXT, bar INT)') |
| 142 |
|
| 143 |
# exercise function |
| 144 |
table = (('foo', 'bar'), |
| 145 |
('a', 1), |
| 146 |
('b', 2), |
| 147 |
('c', 2)) |
| 148 |
todb(table, conn, 'foobar') |
| 149 |
|
| 150 |
# check what it did |
| 151 |
actual = conn.execute('SELECT * FROM foobar') |
| 152 |
expect = (('a', 1), |
| 153 |
('b', 2), |
| 154 |
('c', 2)) |
| 155 |
ieq(expect, actual) |
| 156 |
|
| 157 |
# check appending |
| 158 |
table2 = (('foo', 'bar'), |
| 159 |
('d', 7), |
| 160 |
('e', 9), |
| 161 |
('f', 1)) |
| 162 |
appenddb(table2, conn, 'foobar') |
| 163 |
|
| 164 |
# check what it did |
| 165 |
actual = conn.execute('SELECT * FROM foobar') |
| 166 |
expect = (('a', 1), |
| 167 |
('b', 2), |
| 168 |
('c', 2), |
| 169 |
('d', 7), |
| 170 |
('e', 9), |
| 171 |
('f', 1)) |
| 172 |
ieq(expect, actual) |
| 173 |
conn.close() |
| 174 |
|
| 175 |
|
| 176 |
def test_tosqlite3_identifiers(): |
| 177 |
|
| 178 |
# exercise function |
| 179 |
table = (('foo foo', 'bar.baz.spong`'), |
| 180 |
('a', 1), |
| 181 |
('b', 2), |
| 182 |
('c', 2)) |
| 183 |
f = NamedTemporaryFile(delete=False) |
| 184 |
f.close() |
| 185 |
conn = sqlite3.connect(f.name) |
| 186 |
conn.execute('CREATE TABLE "foo "" bar`" ' |
| 187 |
'("foo foo" TEXT, "bar.baz.spong`" INT)') |
| 188 |
conn.close() |
| 189 |
todb(table, f.name, 'foo " bar`') |
| 190 |
|
| 191 |
# check what it did |
| 192 |
conn = sqlite3.connect(f.name) |
| 193 |
actual = conn.execute('SELECT * FROM `foo " bar```') |
| 194 |
expect = (('a', 1), |
| 195 |
('b', 2), |
| 196 |
('c', 2)) |
| 197 |
ieq(expect, actual) |
| 198 |
conn.close() |
| 199 |
|
| 200 |
|
| 201 |
# TODO test uneven rows |