From bb1397837d265bc09b855b7484c63078bc40acca Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 16:31:38 +0800 Subject: [PATCH 1/8] add ci and tests --- .github/workflows/ci.yaml | 17 +++--- Makefile | 8 +++ requirements.txt | 5 ++ tests/__pycache__/testcase.cpython-39.pyc | Bin 1281 -> 0 bytes tests/log.py | 27 ---------- tests/test_client.py | 61 +++++++++++++++------- tests/testcase.py | 39 -------------- 7 files changed, 61 insertions(+), 96 deletions(-) create mode 100644 Makefile create mode 100644 requirements.txt delete mode 100644 tests/__pycache__/testcase.cpython-39.pyc delete mode 100644 tests/log.py delete mode 100644 tests/testcase.py diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ce22026..b93bf75 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,25 +16,20 @@ jobs: 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 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..1439740 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +test: + python tests/test_client.py + +ci: + python tests/test_client.py + +install: + pip install -r requirements.txt \ 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 40d70a56ad83b60d3f6e393e7895fa91a21b8001..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1281 zcmZuw%Wl&^6rCAA;y6zF;3XasJESbyNbHahLUh3bsX_vTG#ktHOj5Uwlg`*Jt;&k7 z`2rTH#D)bwG+S2u0%F0LaVn*#w(j+r>pSCn&b`k1{g~i-{PSb(`Gowki`6T{;w4NU z!wDj2Nw|Hol1>?Cw2gV(L(D7vX}|*)^UHAB;hp9Ch)0qvx|f8UTL2Bx3Fos$D=6vkj6@y-9t&e_60cUsp0 zqT!epb z4G(3r9FNxB!V+WXsuIhxo*<^}(d7S9uICLmOq8ZoHkD~=;xwJkL{nOvr0G$UmF*jT zPsyXEP*S9j?O3W5ri)p{2iMo8H!iRpzr?t}q)%k+y23-CqN;E1VJoECK(UUdx?_>; z(~7esRR_*0pzp#(R&JKiQS5StYdmRdm}klkY`pwQ%VJMiRZOg#y}6p@QfnSH zRZ-hUD?7nv?W+H&Pz89l!(|7XCp(VVTX(3sZ?k>KE_k>7TBD?TfT$~CrFh51wJCbu VD*VF!INHV|5P*S5#O!m^`v(FbHGu#C 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..fef7f1a 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 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,34 @@ 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('https://cloudapp:d4xu46fzvp6f@tnc7yee14--reload.ch.datafusecloud.com:443') + 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') + client = Client.from_url(self.databend_url) + self.assertEqual(client.connection.user, 'root') - result = c.execute_iter("select 1", with_column_types=False) + result = client.execute_iter("select 1", with_column_types=False) self.assertIsInstance(result, types.GeneratorType) result_list = [i for i in result] @@ -65,3 +71,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) From cbb489db21a177dd248e7fc27815ddfb8557bf2f Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 16:35:07 +0800 Subject: [PATCH 2/8] fix --- Makefile | 6 +++--- tests/test_client.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 1439740..8e30ce7 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ test: - python tests/test_client.py + python tests/test_client.py ci: - python tests/test_client.py + python tests/test_client.py install: - pip install -r requirements.txt \ No newline at end of file + pip install -r requirements.txt \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py index fef7f1a..a95ff26 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -47,7 +47,7 @@ def test_ordinary_query(self): self.assertEqual(r, ([('1', 'UInt8')], [(1,)])) def test_batch_insert(self): - c = Client.from_url('https://cloudapp:d4xu46fzvp6f@tnc7yee14--reload.ch.datafusecloud.com:443') + 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)') From 46b48089ae8ff2a7a6843f28ee8efa40cdc1db41 Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 16:40:13 +0800 Subject: [PATCH 3/8] fix --- tests/test_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index a95ff26..b175f43 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,4 +1,4 @@ -from databend_py.client import Client +from databend_py import Client from unittest import TestCase import types, os @@ -80,7 +80,7 @@ def tearDown(self) -> None: if __name__ == '__main__': print("start test......") - # os.environ['TEST_DATABEND_DSN'] = "http://root:@localhost:8002" + 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() From 8dfbe7682e74bbf2924e0ce257ca074549b7b453 Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 16:47:45 +0800 Subject: [PATCH 4/8] run --- tests/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index b175f43..8ead9bd 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -80,7 +80,7 @@ def tearDown(self) -> None: if __name__ == '__main__': print("start test......") - os.environ['TEST_DATABEND_DSN'] = "http://root:@localhost:8002" + # 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() From a1d77fdefe5fb9036b29380dab89a44d9bb7623c Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 16:52:26 +0800 Subject: [PATCH 5/8] fix ci --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8e30ce7..23cd3fa 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,5 @@ ci: python tests/test_client.py install: - pip install -r requirements.txt \ No newline at end of file + pip install -r requirements.txt + pip install -e . \ No newline at end of file From 616bd90a326e97cf0faee27e85901bb78cb5abb9 Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 17:23:57 +0800 Subject: [PATCH 6/8] neat --- .github/workflows/ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b93bf75..8211432 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,8 +14,8 @@ jobs: # image: datafuselabs/databend-query image: datafuselabs/databend env: - DATABEND_DEFAULT_USER: databend - DATABEND_DEFAULT_PASSWORD: databend + QUERY_DEFAULT_USER: databend + QUERY_DEFAULT_PASSWORD: databend ports: - 8000:8000 steps: From 6a0de975021625ea5f363e4514b7b6766b5ed24f Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 17:28:40 +0800 Subject: [PATCH 7/8] skip --- tests/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client.py b/tests/test_client.py index 8ead9bd..7828df8 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -84,7 +84,7 @@ def tearDown(self) -> None: dt = DatabendPyTestCase(databend_url=os.getenv("TEST_DATABEND_DSN")) dt.test_simple() dt.test_ordinary_query() - dt.test_batch_insert() + # dt.test_batch_insert() dt.test_iter_query() dt.tearDown() print("end test.....") From b0a4e033573ea09d06c627cc9f394131bdccf2d9 Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 23 Feb 2023 17:30:39 +0800 Subject: [PATCH 8/8] neat --- tests/test_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_client.py b/tests/test_client.py index 7828df8..259f2c3 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -61,8 +61,6 @@ def test_batch_insert(self): def test_iter_query(self): client = Client.from_url(self.databend_url) - self.assertEqual(client.connection.user, 'root') - result = client.execute_iter("select 1", with_column_types=False) self.assertIsInstance(result, types.GeneratorType)