OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
conftest.py
Go to the documentation of this file.
1 import os
2 import subprocess
3 import time
4 from uuid import uuid4
5 
6 import pytest
7 from thrift.transport import TSocket, TTransport
8 from thrift.transport.TSocket import TTransportException
9 from omnisci import connect
10 import random
11 import string
12 
13 omniscihost = os.environ.get('OMNISCI_HOST', 'localhost')
14 
16  """
17  Test to see if OmniSci running on localhost and socket open
18  """
19  socket = TSocket.TSocket(omniscihost, 6274)
20  transport = TTransport.TBufferedTransport(socket)
21 
22  try:
23  transport.open()
24  return True
25  except TTransportException:
26  return False
27 
28 
29 @pytest.fixture(scope='session')
31  """Ensure an omnisci server is running, optionally starting one if none"""
32  if _check_open():
33  # already running before pytest started
34  pass
35  else:
36  # not yet running...
37  subprocess.check_output(
38  [
39  'docker',
40  'run',
41  '-d',
42  '--ipc=host',
43  '-v',
44  '/dev:/dev',
45  '-p',
46  '6274:6274',
47  '-p',
48  '9092:9092',
49  '--name=mapd',
50  'omnisci/core-os-cpu:latest',
51  ]
52  )
53  # yield and stop afterwards?
54  assert _check_open()
55  # Takes some time to start up. Unfortunately even trying to connect
56  # will cause it to hang.
57  time.sleep(5)
58 
59 
60 @pytest.fixture(scope='session')
61 def con(omnisci_server):
62  """
63  Fixture to provide Connection for tests run against live OmniSci instance
64  """
65  return connect(
66  user="admin",
67  password='HyperInteractive',
68  host=omniscihost,
69  port=6274,
70  protocol='binary',
71  dbname='omnisci',
72  )
73 
74 
75 @pytest.fixture
76 def mock_client(mocker):
77  """A magicmock for omnisci.connection.Client"""
78  return mocker.patch("omnisci.connection.Client")
79 
80 
81 def no_gpu():
82  """Check for the required GPU dependencies"""
83  try:
84  from numba import cuda
85  import cudf # noqa
86 
87  try:
88  cuda.select_device(0)
89  except cuda.cudadrv.error.CudaDriverError:
90  return True
91  except ImportError:
92  return True
93  return False
94 
95 
96 def gen_string():
97  """Generate a random string sequence for use in _tests_table_no_nulls"""
98  return ''.join(
99  [
100  random.choice(string.ascii_letters + string.digits)
101  for n in range(10)
102  ]
103  )
104 
105 
106 @pytest.fixture
107 def tmp_table(con) -> str:
108  table_name = 'table_{}'.format(uuid4().hex)
109  con.execute("drop table if exists {};".format(table_name))
110 
111  try:
112  yield table_name
113  finally:
114  con.execute("drop table if exists {};".format(table_name))
def omnisci_server
Definition: conftest.py:30
std::string join(T const &container, std::string const &delim)
def gen_string
Definition: conftest.py:96
def _check_open
Definition: conftest.py:15
def mock_client
Definition: conftest.py:76