2 Tests that rely on a server running
8 from heavydb
import connect, ProgrammingError, DatabaseError
16 TDBException.__hash__ =
lambda x: id(x)
17 heavydb_host = os.environ.get(
'HEAVYDB_HOST',
'localhost')
20 @pytest.mark.usefixtures(
"heavydb_server")
25 password=
'HyperInteractive',
31 assert con
is not None
36 password=
'HyperInteractive',
42 assert con
is not None
46 'heavydb://admin:HyperInteractive@{0}:6274/heavyai?'
47 'protocol=binary'.format(heavydb_host)
50 assert con._user ==
'admin'
51 assert con._password ==
'HyperInteractive'
52 assert con._host == heavydb_host
53 assert con._port == 6274
54 assert con._dbname ==
'heavyai'
55 assert con._protocol ==
'binary'
59 'heavydb://admin:HyperInteractive@{0}:6274/heavyai?'
60 'protocol=binary'.format(heavydb_host)
62 with pytest.raises(TypeError):
63 connect(username=
'heavyai', uri=uri)
66 with pytest.raises(ProgrammingError)
as r:
67 con.cursor().execute(
"this is invalid;")
71 with pytest.raises(DatabaseError)
as r:
72 con.cursor().execute(
"select it from fake_table;")
73 r.match(
"Table 'FAKE_TABLE' does not exist|Object 'fake_table' not")
76 result = con.execute(
"drop table if exists FOO;")
77 result = con.execute(
"create table FOO (a int);")
78 assert isinstance(result, Cursor)
79 con.execute(
"drop table if exists FOO;")
84 c.execute(
'drop table if exists stocks;')
86 'create table stocks (date_ text, trans text, symbol text, '
87 'qty int, price float, vol float);'
90 i1 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);"
91 i2 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);"
96 c.execute(
"select * from stocks")
98 Description(
'date_', 6,
None,
None,
None,
None,
True),
99 Description(
'trans', 6,
None,
None,
None,
None,
True),
100 Description(
'symbol', 6,
None,
None,
None,
None,
True),
101 Description(
'qty', 1,
None,
None,
None,
None,
True),
102 Description(
'price', 3,
None,
None,
None,
None,
True),
103 Description(
'vol', 3,
None,
None,
None,
None,
True),
105 assert c.description == expected
106 c.execute(
'drop table if exists stocks;')
111 c.execute(
'drop table if exists stocks;')
113 'create table stocks (date_ text, trans text, symbol text, '
114 'qty int, price float, vol float);'
117 i1 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);"
118 i2 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);"
124 'select symbol, qty from stocks where symbol = :symbol',
131 assert result == expected
132 c.execute(
'drop table if exists stocks;')
137 c.execute(
'drop table if exists stocks;')
139 'create table stocks (date_ text, trans text, symbol text, '
140 'qty int, price float, vol float);'
143 i1 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);"
144 i2 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);"
149 parameters = [{
'symbol':
'GOOG'}, {
'symbol':
"RHAT"}]
150 expected = [[(
'GOOG', 100)], [(
'RHAT', 100)]]
151 query =
'select symbol, qty from stocks where symbol = :symbol'
153 result = c.executemany(query, parameters)
154 assert result == expected
155 c.execute(
'drop table if exists stocks;')
160 c.execute(
'drop table if exists stocks;')
162 'create table stocks (date_ text, trans text, symbol text, '
163 'qty int, price float, vol float);'
166 i1 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);"
167 i2 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);"
173 c.execute(
"drop table if exists stocks2;")
175 c.execute(
'CREATE TABLE stocks2 (symbol text, qty int);')
176 params = [{
"symbol":
"GOOG",
"qty": 10}, {
"symbol":
"AAPL",
"qty": 20}]
177 query =
"INSERT INTO stocks2 VALUES (:symbol, :qty);"
178 result = c.executemany(query, params)
179 assert result == [[], []]
180 c.execute(
"drop table stocks2;")
181 c.execute(
'drop table if exists stocks;')
186 c.execute(
'drop table if exists stocks;')
188 'create table stocks (date_ text, trans text, symbol text, '
189 'qty int, price float, vol float);'
192 i1 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);"
193 i2 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);"
198 c.execute(
"select symbol, qty from stocks")
199 result = c.fetchone()
200 expected = (
'RHAT', 100)
201 assert result == expected
202 c.execute(
'drop table if exists stocks;')
207 c.execute(
'drop table if exists stocks;')
209 'create table stocks (date_ text, trans text, symbol text, '
210 'qty int, price float, vol float);'
213 i1 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);"
214 i2 =
"INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);"
219 c.execute(
"select symbol, qty from stocks")
220 result = c.fetchmany()
221 expected = [(
'RHAT', 100)]
222 assert result == expected
224 c.execute(
"select symbol, qty from stocks")
225 result = c.fetchmany(size=10)
226 expected = [(
'RHAT', 100), (
'GOOG', 100)]
227 assert result == expected
228 c.execute(
'drop table if exists stocks;')
233 c.execute(
'drop table if exists dates;')
235 'create table dates (date_ DATE, datetime_ TIMESTAMP, '
239 "INSERT INTO dates VALUES ('2006-01-05','2006-01-01T12:00:00',"
243 "INSERT INTO dates VALUES ('1901-12-14','1901-12-13T20:45:53',"
249 result = list(c.execute(
"select * from dates"))
252 datetime.date(2006, 1, 5),
253 datetime.datetime(2006, 1, 1, 12),
257 datetime.date(1901, 12, 14),
258 datetime.datetime(1901, 12, 13, 20, 45, 53),
259 datetime.time(23, 59),
262 assert result == expected
263 c.execute(
'drop table if exists dates;')
271 c.execute(
'drop table if exists stocks;')
273 'create table stocks (date_ text, trans text, symbol text, '
274 'qty int, price float, vol float);'
278 q =
"select * from stocks"
279 results = con._client.sql_validate(con._session, q)
280 col_names = sorted([r.col_name
for r
in results])
281 col_types = [r.col_type
for r
in results]
283 expected_col_names = [
355 assert col_types == expected_types
356 assert col_names == expected_col_names
def test_nonexistant_table
def test_select_sets_description
def test_connection_execute
def test_executemany_parametrized
def test_select_parametrized
def test_executemany_parametrized_insert
def test_connect_uri_and_others_raises