Skip to content

Commit 03954c2

Browse files
committed
Add tests for UDF and with session
1 parent 8177040 commit 03954c2

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

tests/test_stateful.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ def test_tmp(self):
9393
ret = sess2.query("SELECT chdb_xxx()", "CSV")
9494
self.assertEqual(str(ret), "")
9595

96+
def test_context_mgr(self):
97+
with session.Session() as sess:
98+
sess.query("CREATE FUNCTION chdb_xxx AS () -> '0.12.0'", "CSV")
99+
ret = sess.query("SELECT chdb_xxx()", "CSV")
100+
self.assertEqual(str(ret), '"0.12.0"\n')
101+
102+
with session.Session() as sess:
103+
ret = sess.query("SELECT chdb_xxx()", "CSV")
104+
self.assertEqual(str(ret), "")
105+
96106
def test_zfree_thread_count(self):
97107
time.sleep(3)
98108
thread_count = current_process.num_threads()

tests/test_udf.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
11
#!python3
22

3-
from chdb.udf import *
3+
import unittest
4+
from chdb.udf import chdb_udf
5+
from chdb.session import Session
6+
from chdb import query
47

5-
@to_clickhouse_udf()
8+
9+
@chdb_udf()
610
def sum_udf(lhs, rhs):
7-
import time
8-
time.sleep(1)
911
return int(lhs) + int(rhs)
1012

11-
@to_clickhouse_udf(return_type="Int32")
13+
14+
@chdb_udf(return_type="Int32")
1215
def mul_udf(lhs, rhs):
1316
return int(lhs) * int(rhs)
17+
18+
19+
class TestUDF(unittest.TestCase):
20+
def test_sum_udf(self):
21+
ret = query("select sum_udf(12,22)", "Debug")
22+
self.assertEqual(str(ret), '"34"\n')
23+
24+
def test_return_Int32(self):
25+
ret = query("select mul_udf(12,22) + 1", "Debug")
26+
self.assertEqual(str(ret), "265\n")
27+
28+
def test_define_in_function(self):
29+
@chdb_udf()
30+
def sum_udf2(lhs, rhs):
31+
return int(lhs) + int(rhs)
32+
33+
ret = query("select sum_udf2(11, 22)", "Debug")
34+
self.assertEqual(str(ret), '"33"\n')
35+
36+
37+
class TestUDFinSession(unittest.TestCase):
38+
def test_sum_udf(self):
39+
with Session() as session:
40+
ret = session.query("select sum_udf(12,22)", "Debug")
41+
self.assertEqual(str(ret), '"34"\n')
42+
43+
def test_return_Int32(self):
44+
with Session() as session:
45+
ret = session.query("select mul_udf(12,22) + 1", "Debug")
46+
self.assertEqual(str(ret), "265\n")
47+
48+
def test_define_in_function(self):
49+
@chdb_udf()
50+
def sum_udf2(lhs, rhs):
51+
return int(lhs) + int(rhs)
52+
53+
with Session() as session:
54+
ret = session.query("select sum_udf2(11, 22)", "Debug")
55+
self.assertEqual(str(ret), '"33"\n')
56+
57+
if __name__ == "__main__":
58+
unittest.main()

0 commit comments

Comments
 (0)