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