Path: blob/main/singlestoredb/tests/test_http.py
469 views
#!/usr/bin/env python1# type: ignore2"""SingleStoreDB HTTP connection testing."""3import base644import os5import unittest67import singlestoredb.connection as sc8from singlestoredb import config9from singlestoredb import http10from singlestoredb.tests import utils11# import traceback121314class TestHTTP(unittest.TestCase):1516dbname: str = ''17dbexisted: bool = False1819@classmethod20def setUpClass(cls):21sql_file = os.path.join(os.path.dirname(__file__), 'test.sql')22cls.dbname, cls.dbexisted = utils.load_sql(sql_file)2324@classmethod25def tearDownClass(cls):26if not cls.dbexisted:27utils.drop_database(cls.dbname)2829def setUp(self):30self.conn = self._connect()31self.cur = self.conn.cursor()32if self.params['driver'] not in ['http', 'https']:33self.skipTest('Tests must be run using HTTP connection')34self.driver = self.params['driver'] or 'http'3536def _connect(self):37params = sc.build_params(host=config.get_option('host'))38self.params = {39k: v for k, v in dict(40host=params.get('host'),41port=params.get('port'),42user=params.get('user'),43password=params.get('password'),44driver=params.get('driver'),45).items() if v is not None46}47return http.connect(database=type(self).dbname, **self.params)4849def tearDown(self):50try:51if self.cur is not None:52self.cur.close()53except Exception:54# traceback.print_exc()55pass5657try:58if self.conn is not None:59self.conn.close()60except Exception:61# traceback.print_exc()62pass6364def test_get_exc_type(self):65exc = http.connection.get_exc_type(0)66assert exc is http.InterfaceError, exc6768exc = http.connection.get_exc_type(2012)69assert exc is http.InterfaceError, exc7071exc = http.connection.get_exc_type(1230)72assert exc is http.DataError, exc7374exc = http.connection.get_exc_type(1110)75assert exc is http.ProgrammingError, exc7677exc = http.connection.get_exc_type(1452)78assert exc is http.IntegrityError, exc7980exc = http.connection.get_exc_type(9999)81assert exc is http.OperationalError, exc8283exc = http.connection.get_exc_type(222)84assert exc is http.InternalError, exc8586def test_identity(self):87out = http.connection.identity(1)88assert out == 1, out8990out = http.connection.identity('hi')91assert out == 'hi', out9293def test_b64decode_converter(self):94data = base64.b64encode(b'hi there')95assert type(data) is bytes, type(data)9697out = http.connection.b64decode_converter(http.connection.identity, None)98assert out is None, out99100out = http.connection.b64decode_converter(http.connection.identity, data)101assert out == b'hi there', out102103out = http.connection.b64decode_converter(104http.connection.identity,105str(data, 'utf8'),106)107assert out == b'hi there', out108109def test_executemany(self):110self.cur.executemany(111'select * from data where id < %(id)s',112[dict(id='d'), dict(id='e')],113)114115assert self.cur.rownumber == 0, self.cur.rownumber116117# First set118out = self.cur.fetchall()119120assert self.cur.rownumber == 3, self.cur.rownumber121122desc = self.cur.description123rowcount = self.cur.rowcount124lastrowid = self.cur.lastrowid125126assert sorted(out) == sorted([127('a', 'antelopes', 2),128('b', 'bears', 2),129('c', 'cats', 5),130]), out131132assert rowcount == 7, rowcount133assert lastrowid is None, lastrowid134assert len(desc) == 3, desc135assert desc[0][0] == 'id', desc[0][0]136assert desc[0][1] in [253, 15], desc[0][1]137assert desc[1][0] == 'name', desc[1][0]138assert desc[1][1] in [253, 15], desc[1][1]139assert desc[2][0] == 'value', desc[2][0]140assert desc[2][1] == 8, desc[2][1]141142# Second set143self.cur.nextset()144145out = self.cur.fetchall()146147desc = self.cur.description148rowcount = self.cur.rowcount149lastrowid = self.cur.lastrowid150151assert sorted(out) == sorted([152('a', 'antelopes', 2),153('b', 'bears', 2),154('c', 'cats', 5),155('d', 'dogs', 4),156]), out157158assert rowcount == 4, rowcount159assert lastrowid is None, lastrowid160assert len(desc) == 3, desc161assert desc[0][0] == 'id', desc[0][0]162assert desc[0][1] in [253, 15], desc[0][1]163assert desc[1][0] == 'name', desc[1][0]164assert desc[1][1] in [253, 15], desc[1][1]165assert desc[2][0] == 'value', desc[2][0]166assert desc[2][1] == 8, desc[2][1]167168out = self.cur.nextset()169assert out is None, out170171def test_executemany_no_args(self):172self.cur.executemany('select * from data where id < "d"')173174# First set175out = self.cur.fetchall()176177desc = self.cur.description178rowcount = self.cur.rowcount179lastrowid = self.cur.lastrowid180181assert sorted(out) == sorted([182('a', 'antelopes', 2),183('b', 'bears', 2),184('c', 'cats', 5),185]), out186187assert rowcount == 3, rowcount188assert lastrowid is None, lastrowid189assert len(desc) == 3, desc190assert desc[0][0] == 'id', desc[0][0]191assert desc[0][1] in [253, 15], desc[0][1]192assert desc[1][0] == 'name', desc[1][0]193assert desc[1][1] in [253, 15], desc[1][1]194assert desc[2][0] == 'value', desc[2][0]195assert desc[2][1] == 8, desc[2][1]196197out = self.cur.nextset()198assert out is None, out199200def test_is_connected(self):201assert self.cur.is_connected() is True202self.cur.close()203assert self.cur.is_connected() is False204205def test_close(self):206self.cur.close()207assert self.cur.is_connected() is False208209with self.assertRaises(http.ProgrammingError):210self.cur.execute('select 1')211212with self.assertRaises(http.ProgrammingError):213self.cur.executemany('select 1')214215with self.assertRaises(http.ProgrammingError):216self.cur.callproc('get_animal', ['cats'])217218# def test_callproc(self):219# with self.assertRaises(NotImplementedError):220# self.cur.callproc('get_animal', ['cats'])221222def test_iter(self):223self.cur.execute('select * from data')224225out = list(self.cur)226227assert sorted(out) == sorted([228('a', 'antelopes', 2),229('b', 'bears', 2),230('c', 'cats', 5),231('d', 'dogs', 4),232('e', 'elephants', 0),233]), out234235def test_next(self):236self.cur.execute('select * from data')237238out = [next(self.cur) for i in range(5)]239240assert sorted(out) == sorted([241('a', 'antelopes', 2),242('b', 'bears', 2),243('c', 'cats', 5),244('d', 'dogs', 4),245('e', 'elephants', 0),246]), out247248with self.assertRaises(StopIteration):249next(self.cur)250251def test_context_manager(self):252with self._connect() as conn:253with conn.cursor() as cur:254assert conn.is_connected()255assert cur.is_connected()256assert not conn.is_connected()257assert not cur.is_connected()258259def test_commit(self):260self.conn.autocommit(True)261assert self.conn.commit() is None262self.conn.autocommit(False)263with self.assertRaises(http.NotSupportedError):264self.conn.commit()265266def test_rollback(self):267self.conn.autocommit(True)268assert self.conn.rollback() is None269self.conn.autocommit(False)270with self.assertRaises(http.NotSupportedError):271self.conn.rollback()272273def test_http_error(self):274# Break content type275self.conn._sess.headers.update({276'Content-Type': 'GaRbAge',277})278with self.assertRaises(http.InternalError) as cm:279self.cur.execute('select 1')280exc = cm.exception281assert exc.errno == 415, exc.errno282assert 'Content-Type' in exc.msg, exc.msg283284285if __name__ == '__main__':286import nose2287nose2.main()288289290