OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_integration.py
Go to the documentation of this file.
1 """
2 Tests that rely on a server running
3 """
4 import datetime
5 import os
6 
7 import pytest
8 from heavydb import connect, ProgrammingError, DatabaseError
9 from heavydb.cursor import Cursor
10 from heavydb._parsers import Description
11 from heavydb.thrift.ttypes import TDBException
12 
13 # XXX: Make it hashable to silence warnings; see if this can be done upstream
14 # This isn't a huge deal, but our testing context mangers for asserting
15 # exceptions need hashability
16 TDBException.__hash__ = lambda x: id(x)
17 heavydb_host = os.environ.get('HEAVYDB_HOST', 'localhost')
18 
19 
20 @pytest.mark.usefixtures("heavydb_server")
23  con = connect(
24  user="admin",
25  password='HyperInteractive',
26  host=heavydb_host,
27  port=6274,
28  protocol='binary',
29  dbname='heavyai',
30  )
31  assert con is not None
32 
33  def test_connect_http(self):
34  con = connect(
35  user="admin",
36  password='HyperInteractive',
37  host=heavydb_host,
38  port=6278,
39  protocol='http',
40  dbname='heavyai',
41  )
42  assert con is not None
43 
44  def test_connect_uri(self):
45  uri = (
46  'heavydb://admin:HyperInteractive@{0}:6274/heavyai?'
47  'protocol=binary'.format(heavydb_host)
48  )
49  con = connect(uri=uri)
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'
56 
58  uri = (
59  'heavydb://admin:HyperInteractive@{0}:6274/heavyai?'
60  'protocol=binary'.format(heavydb_host)
61  )
62  with pytest.raises(TypeError):
63  connect(username='heavyai', uri=uri)
64 
65  def test_invalid_sql(self, con):
66  with pytest.raises(ProgrammingError) as r:
67  con.cursor().execute("this is invalid;")
68  r.match("SQL Error:")
69 
70  def test_nonexistant_table(self, con):
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")
74 
75  def test_connection_execute(self, con):
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;")
80 
82 
83  c = con.cursor()
84  c.execute('drop table if exists stocks;')
85  create = (
86  'create table stocks (date_ text, trans text, symbol text, '
87  'qty int, price float, vol float);'
88  )
89  c.execute(create)
90  i1 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);" # noqa
91  i2 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);" # noqa
92 
93  c.execute(i1)
94  c.execute(i2)
95 
96  c.execute("select * from stocks")
97  expected = [
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),
104  ]
105  assert c.description == expected
106  c.execute('drop table if exists stocks;')
107 
108  def test_select_parametrized(self, con):
109 
110  c = con.cursor()
111  c.execute('drop table if exists stocks;')
112  create = (
113  'create table stocks (date_ text, trans text, symbol text, '
114  'qty int, price float, vol float);'
115  )
116  c.execute(create)
117  i1 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);" # noqa
118  i2 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);" # noqa
119 
120  c.execute(i1)
121  c.execute(i2)
122 
123  c.execute(
124  'select symbol, qty from stocks where symbol = :symbol',
125  {'symbol': 'GOOG'},
126  )
127  result = list(c)
128  expected = [
129  ('GOOG', 100),
130  ] # noqa
131  assert result == expected
132  c.execute('drop table if exists stocks;')
133 
135 
136  c = con.cursor()
137  c.execute('drop table if exists stocks;')
138  create = (
139  'create table stocks (date_ text, trans text, symbol text, '
140  'qty int, price float, vol float);'
141  )
142  c.execute(create)
143  i1 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);" # noqa
144  i2 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);" # noqa
145 
146  c.execute(i1)
147  c.execute(i2)
148 
149  parameters = [{'symbol': 'GOOG'}, {'symbol': "RHAT"}]
150  expected = [[('GOOG', 100)], [('RHAT', 100)]]
151  query = 'select symbol, qty from stocks where symbol = :symbol'
152  c = con.cursor()
153  result = c.executemany(query, parameters)
154  assert result == expected
155  c.execute('drop table if exists stocks;')
156 
158 
159  c = con.cursor()
160  c.execute('drop table if exists stocks;')
161  create = (
162  'create table stocks (date_ text, trans text, symbol text, '
163  'qty int, price float, vol float);'
164  )
165  c.execute(create)
166  i1 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);" # noqa
167  i2 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);" # noqa
168 
169  c.execute(i1)
170  c.execute(i2)
171 
172  c = con.cursor()
173  c.execute("drop table if exists stocks2;")
174  # Create table
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 == [[], []] # TODO: not sure if this is standard
180  c.execute("drop table stocks2;")
181  c.execute('drop table if exists stocks;')
182 
183  def test_fetchone(self, con):
184 
185  c = con.cursor()
186  c.execute('drop table if exists stocks;')
187  create = (
188  'create table stocks (date_ text, trans text, symbol text, '
189  'qty int, price float, vol float);'
190  )
191  c.execute(create)
192  i1 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);" # noqa
193  i2 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);" # noqa
194 
195  c.execute(i1)
196  c.execute(i2)
197 
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;')
203 
204  def test_fetchmany(self, con):
205 
206  c = con.cursor()
207  c.execute('drop table if exists stocks;')
208  create = (
209  'create table stocks (date_ text, trans text, symbol text, '
210  'qty int, price float, vol float);'
211  )
212  c.execute(create)
213  i1 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14,1.1);" # noqa
214  i2 = "INSERT INTO stocks VALUES ('2006-01-05','BUY','GOOG',100,12.14,1.2);" # noqa
215 
216  c.execute(i1)
217  c.execute(i2)
218 
219  c.execute("select symbol, qty from stocks")
220  result = c.fetchmany()
221  expected = [('RHAT', 100)]
222  assert result == expected
223 
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;')
229 
230  def test_select_dates(self, con):
231 
232  c = con.cursor()
233  c.execute('drop table if exists dates;')
234  c.execute(
235  'create table dates (date_ DATE, datetime_ TIMESTAMP, '
236  'time_ TIME);'
237  )
238  i1 = (
239  "INSERT INTO dates VALUES ('2006-01-05','2006-01-01T12:00:00',"
240  "'12:00:00');"
241  )
242  i2 = (
243  "INSERT INTO dates VALUES ('1901-12-14','1901-12-13T20:45:53',"
244  "'23:59:00');"
245  )
246  c.execute(i1)
247  c.execute(i2)
248 
249  result = list(c.execute("select * from dates"))
250  expected = [
251  (
252  datetime.date(2006, 1, 5),
253  datetime.datetime(2006, 1, 1, 12),
254  datetime.time(12),
255  ),
256  (
257  datetime.date(1901, 12, 14),
258  datetime.datetime(1901, 12, 13, 20, 45, 53),
259  datetime.time(23, 59),
260  ),
261  ]
262  assert result == expected
263  c.execute('drop table if exists dates;')
264 
265 
267  def test_sql_validate(self, con):
268  from heavydb.common.ttypes import TTypeInfo
269 
270  c = con.cursor()
271  c.execute('drop table if exists stocks;')
272  create = (
273  'create table stocks (date_ text, trans text, symbol text, '
274  'qty int, price float, vol float);'
275  )
276  c.execute(create)
277 
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]
282 
283  expected_col_names = [
284  'date_',
285  'price',
286  'qty',
287  'symbol',
288  'trans',
289  'vol',
290  ]
291 
292  expected_types = [
293  TTypeInfo(
294  type=6,
295  encoding=4,
296  nullable=True,
297  is_array=False,
298  precision=0,
299  scale=0,
300  comp_param=32,
301  size=-1,
302  ),
303  TTypeInfo(
304  type=6,
305  encoding=4,
306  nullable=True,
307  is_array=False,
308  precision=0,
309  scale=0,
310  comp_param=32,
311  size=-1,
312  ),
313  TTypeInfo(
314  type=6,
315  encoding=4,
316  nullable=True,
317  is_array=False,
318  precision=0,
319  scale=0,
320  comp_param=32,
321  size=-1,
322  ),
323  TTypeInfo(
324  type=1,
325  encoding=0,
326  nullable=True,
327  is_array=False,
328  precision=0,
329  scale=0,
330  comp_param=0,
331  size=-1,
332  ),
333  TTypeInfo(
334  type=3,
335  encoding=0,
336  nullable=True,
337  is_array=False,
338  precision=0,
339  scale=0,
340  comp_param=0,
341  size=-1,
342  ),
343  TTypeInfo(
344  type=3,
345  encoding=0,
346  nullable=True,
347  is_array=False,
348  precision=0,
349  scale=0,
350  comp_param=0,
351  size=-1,
352  ),
353  ]
354 
355  assert col_types == expected_types
356  assert col_names == expected_col_names
tuple Description
Definition: _parsers.py:11