diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ce22026..8211432 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,27 +14,22 @@ jobs: # image: datafuselabs/databend-query image: datafuselabs/databend env: - DATABEND_DEFAULT_USER: databend - DATABEND_DEFAULT_PASSWORD: databend - # options: >- - # --health-cmd "curl -fs http://localhost:8000/v1/health || exit 1" - # --health-interval 10s - # --health-timeout 5s - # --health-retries 5 + QUERY_DEFAULT_USER: databend + QUERY_DEFAULT_PASSWORD: databend ports: - 8000:8000 steps: - name: Checkout uses: actions/checkout@v2 - - name: Setup Go - uses: actions/setup-go@v2 + - name: Setup Python-3.10 + uses: actions/setup-python@v4 with: - go-version: "1.18" + python-version: '3.10' - - name: Unittest + - name: Pip Install run: | - make test + make install - name: Verify Service Running run: | @@ -44,6 +39,6 @@ jobs: - name: Test env: - TEST_DATABEND_DSN: "databend://databend:databend@localhost:8000/default?idle_timeout=1h&presigned_url_disabled=1&sslmode=disable" + TEST_DATABEND_DSN: "http://databend:databend@localhost:8000/default" run: | make ci diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..23cd3fa --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +test: + python tests/test_client.py + +ci: + python tests/test_client.py + +install: + pip install -r requirements.txt + pip install -e . \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9aa8fd3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +environs==9.5.0 +mysql_connector_repackaged==0.3.1 +pytz==2022.5 +requests==2.28.1 +setuptools==62.3.2 diff --git a/tests/__pycache__/testcase.cpython-39.pyc b/tests/__pycache__/testcase.cpython-39.pyc deleted file mode 100644 index 40d70a5..0000000 Binary files a/tests/__pycache__/testcase.cpython-39.pyc and /dev/null differ diff --git a/tests/log.py b/tests/log.py deleted file mode 100644 index bbb4241..0000000 --- a/tests/log.py +++ /dev/null @@ -1,27 +0,0 @@ -from logging.config import dictConfig - - -def configure(level): - dictConfig({ - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - 'standard': { - 'format': '%(asctime)s %(levelname)-8s %(name)s: %(message)s' - }, - }, - 'handlers': { - 'default': { - 'level': level, - 'formatter': 'standard', - 'class': 'logging.StreamHandler', - }, - }, - 'loggers': { - '': { - 'handlers': ['default'], - 'level': level, - 'propagate': True - }, - } - }) diff --git a/tests/test_client.py b/tests/test_client.py index 24af56c..259f2c3 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,9 +1,13 @@ -from databend_py.client import Client -from tests.testcase import TestCase +from databend_py import Client +from unittest import TestCase import types, os -class ClientFromUrlTestCase(TestCase): +class DatabendPyTestCase(TestCase): + def __init__(self, databend_url): + super().__init__() + self.databend_url = databend_url + def assertHostsEqual(self, client, another, msg=None): self.assertEqual(client.connection.host, another, msg=msg) @@ -21,7 +25,7 @@ def test_simple(self): self.assertEqual(c.connection.password, '') def test_ordinary_query(self): - ss = ''' + select_test = ''' select null as db, name as name, @@ -32,32 +36,32 @@ def test_ordinary_query(self): ''' # if use the host from databend cloud, must set the 'ADDITIONAL_HEADERS': # os.environ['ADDITIONAL_HEADERS'] = 'X-DATABENDCLOUD-TENANT=TENANT,X-DATABENDCLOUD-WAREHOUSE=WAREHOUSE' - c = Client.from_url('http://root:@localhost:8081') - # r = c.execute("select 1", with_column_types=False) - # self.assertEqual(r, [('1',)]) - column_types, r = c.execute(ss, with_column_types=True) - print(r) - print(column_types) + c = Client.from_url(self.databend_url) + _, r = c.execute("select 1", with_column_types=False) + self.assertEqual(r, ([(1,)])) + column_types, _ = c.execute(select_test, with_column_types=True) + self.assertEqual(column_types, [('db', 'NULL'), ('name', 'String'), ('schema', 'String'), ('type', 'String')]) # test with_column_types=True - # r = c.execute("select 1", with_column_types=True) - # self.assertEqual(r, [('1', 'UInt8'), ('1',)]) - # + r = c.execute("select 1", with_column_types=True) + self.assertEqual(r, ([('1', 'UInt8')], [(1,)])) + + def test_batch_insert(self): + c = Client.from_url(self.databend_url) + c.execute('DROP TABLE IF EXISTS test') c.execute('CREATE TABLE if not exists test (x Int32,y VARCHAR)') - # c.execute('DESC test') - r1 = c.execute('INSERT INTO test (x,y) VALUES (%,%)', [1, 'yy', 2, 'xx']) + c.execute('DESC test') + _, r1 = c.execute('INSERT INTO test (x,y) VALUES (%,%)', [1, 'yy', 2, 'xx']) # # insert_rows = 1 - # self.assertEqual(r1, 1) + self.assertEqual(r1, 2) _, ss = c.execute('select * from test') print(ss) - # self.assertEqual(ss, [('1', 'yy')]) + self.assertEqual(ss, [(1, 'yy'), (2, 'xx')]) def test_iter_query(self): - c = Client.from_url('http://root:@localhost:8081') - self.assertEqual(c.connection.user, 'root') - - result = c.execute_iter("select 1", with_column_types=False) + client = Client.from_url(self.databend_url) + result = client.execute_iter("select 1", with_column_types=False) self.assertIsInstance(result, types.GeneratorType) result_list = [i for i in result] @@ -65,3 +69,20 @@ def test_iter_query(self): self.assertEqual(result_list, [1]) self.assertEqual(list(result), []) + + def tearDown(self) -> None: + client = Client.from_url(self.databend_url) + client.execute('DROP TABLE IF EXISTS test') + client.disconnect() + + +if __name__ == '__main__': + print("start test......") + # os.environ['TEST_DATABEND_DSN'] = "http://root:@localhost:8002" + dt = DatabendPyTestCase(databend_url=os.getenv("TEST_DATABEND_DSN")) + dt.test_simple() + dt.test_ordinary_query() + # dt.test_batch_insert() + dt.test_iter_query() + dt.tearDown() + print("end test.....") diff --git a/tests/testcase.py b/tests/testcase.py deleted file mode 100644 index d41e03b..0000000 --- a/tests/testcase.py +++ /dev/null @@ -1,39 +0,0 @@ -import configparser -from contextlib import contextmanager -import subprocess -from unittest import TestCase - -from databend_py.client import Client -from tests import log - -file_config = configparser.ConfigParser() -file_config.read(['../setup.cfg']) - -log.configure(file_config.get('log', 'level')) - - -class BaseTestCase(TestCase): - required_server_version = None - server_version = None - - host = file_config.get('db', 'host') - port = file_config.getint('db', 'port') - database = file_config.get('db', 'database') - user = file_config.get('db', 'user') - password = file_config.get('db', 'password') - - client = None - client_kwargs = None - - def _create_client(self, **kwargs): - client_kwargs = { - 'port': self.port, - 'database': self.database, - 'user': self.user, - 'password': self.password - } - client_kwargs.update(kwargs) - return Client(self.host, **client_kwargs) - - def created_client(self, **kwargs): - return self._create_client(**kwargs)