1- from databend_py . client import Client
2- from tests . testcase import TestCase
1+ from databend_py import Client
2+ from unittest import TestCase
33import types , os
44
55
6- class ClientFromUrlTestCase (TestCase ):
6+ class DatabendPyTestCase (TestCase ):
7+ def __init__ (self , databend_url ):
8+ super ().__init__ ()
9+ self .databend_url = databend_url
10+
711 def assertHostsEqual (self , client , another , msg = None ):
812 self .assertEqual (client .connection .host , another , msg = msg )
913
@@ -21,7 +25,7 @@ def test_simple(self):
2125 self .assertEqual (c .connection .password , '' )
2226
2327 def test_ordinary_query (self ):
24- ss = '''
28+ select_test = '''
2529 select
2630 null as db,
2731 name as name,
@@ -32,36 +36,53 @@ def test_ordinary_query(self):
3236 '''
3337 # if use the host from databend cloud, must set the 'ADDITIONAL_HEADERS':
3438 # os.environ['ADDITIONAL_HEADERS'] = 'X-DATABENDCLOUD-TENANT=TENANT,X-DATABENDCLOUD-WAREHOUSE=WAREHOUSE'
35- c = Client .from_url ('http://root:@localhost:8081' )
36- # r = c.execute("select 1", with_column_types=False)
37- # self.assertEqual(r, [('1',)])
38- column_types , r = c .execute (ss , with_column_types = True )
39- print (r )
40- print (column_types )
39+ c = Client .from_url (self .databend_url )
40+ _ , r = c .execute ("select 1" , with_column_types = False )
41+ self .assertEqual (r , ([(1 ,)]))
42+ column_types , _ = c .execute (select_test , with_column_types = True )
43+ self .assertEqual (column_types , [('db' , 'NULL' ), ('name' , 'String' ), ('schema' , 'String' ), ('type' , 'String' )])
4144
4245 # test with_column_types=True
43- # r = c.execute("select 1", with_column_types=True)
44- # self.assertEqual(r, [('1', 'UInt8'), ('1',)])
45- #
46+ r = c .execute ("select 1" , with_column_types = True )
47+ self .assertEqual (r , ([('1' , 'UInt8' )], [(1 ,)]))
48+
49+ def test_batch_insert (self ):
50+ c = Client .from_url (self .databend_url )
51+
4652 c .execute ('DROP TABLE IF EXISTS test' )
4753 c .execute ('CREATE TABLE if not exists test (x Int32,y VARCHAR)' )
48- # c.execute('DESC test')
49- r1 = c .execute ('INSERT INTO test (x,y) VALUES (%,%)' , [1 , 'yy' , 2 , 'xx' ])
54+ c .execute ('DESC test' )
55+ _ , r1 = c .execute ('INSERT INTO test (x,y) VALUES (%,%)' , [1 , 'yy' , 2 , 'xx' ])
5056 # # insert_rows = 1
51- # self.assertEqual(r1, 1 )
57+ self .assertEqual (r1 , 2 )
5258 _ , ss = c .execute ('select * from test' )
5359 print (ss )
54- # self.assertEqual(ss, [('1' , 'yy')])
60+ self .assertEqual (ss , [(1 , 'yy' ), ( 2 , 'xx ' )])
5561
5662 def test_iter_query (self ):
57- c = Client .from_url ('http://root:@localhost:8081' )
58- self .assertEqual (c .connection .user , 'root' )
59-
60- result = c .execute_iter ("select 1" , with_column_types = False )
63+ client = Client .from_url (self .databend_url )
64+ result = client .execute_iter ("select 1" , with_column_types = False )
6165
6266 self .assertIsInstance (result , types .GeneratorType )
6367 result_list = [i for i in result ]
6468 print (result_list )
6569 self .assertEqual (result_list , [1 ])
6670
6771 self .assertEqual (list (result ), [])
72+
73+ def tearDown (self ) -> None :
74+ client = Client .from_url (self .databend_url )
75+ client .execute ('DROP TABLE IF EXISTS test' )
76+ client .disconnect ()
77+
78+
79+ if __name__ == '__main__' :
80+ print ("start test......" )
81+ # os.environ['TEST_DATABEND_DSN'] = "http://root:@localhost:8002"
82+ dt = DatabendPyTestCase (databend_url = os .getenv ("TEST_DATABEND_DSN" ))
83+ dt .test_simple ()
84+ dt .test_ordinary_query ()
85+ # dt.test_batch_insert()
86+ dt .test_iter_query ()
87+ dt .tearDown ()
88+ print ("end test....." )
0 commit comments