|
| 1 | +#!python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import time |
| 5 | +import shutil |
| 6 | +import unittest |
| 7 | +import psutil |
| 8 | +from chdb import session as chs |
| 9 | + |
| 10 | +tmp_dir = ".state_tmp_auxten_issue104" |
| 11 | + |
| 12 | + |
| 13 | +class TestIssue104(unittest.TestCase): |
| 14 | + def setUp(self) -> None: |
| 15 | + # shutil.rmtree(tmp_dir, ignore_errors=True) |
| 16 | + return super().setUp() |
| 17 | + |
| 18 | + def tearDown(self): |
| 19 | + # shutil.rmtree(tmp_dir, ignore_errors=True) |
| 20 | + return super().tearDown() |
| 21 | + |
| 22 | + def test_issue104(self): |
| 23 | + sess = chs.Session(tmp_dir) |
| 24 | + |
| 25 | + sess.query("CREATE DATABASE IF NOT EXISTS test_db ENGINE = Atomic;") |
| 26 | + # sess.query("CREATE DATABASE IF NOT EXISTS test_db ENGINE = Atomic;", "Debug") |
| 27 | + sess.query("CREATE TABLE IF NOT EXISTS test_db.test_table (x String, y String) ENGINE = MergeTree ORDER BY tuple()") |
| 28 | + sess.query("INSERT INTO test_db.test_table (x, y) VALUES ('A', 'B'), ('C', 'D');") |
| 29 | + |
| 30 | + # show final thread count |
| 31 | + print("Final thread count:", len(psutil.Process().threads())) |
| 32 | + |
| 33 | + print("Original values:") |
| 34 | + ret = sess.query("SELECT * FROM test_db.test_table", "Debug") |
| 35 | + print(ret) |
| 36 | + # self.assertEqual(str(ret), '"A","B"\n"C","D"\n') |
| 37 | + |
| 38 | + # show final thread count |
| 39 | + print("Final thread count:", len(psutil.Process().threads())) |
| 40 | + |
| 41 | + print('Values after ALTER UPDATE in same query expected:') |
| 42 | + ret = sess.query( |
| 43 | + "ALTER TABLE test_db.test_table UPDATE y = 'updated1' WHERE x = 'A';" |
| 44 | + "SELECT * FROM test_db.test_table WHERE x = 'A';") |
| 45 | + print(ret) |
| 46 | + self.assertEqual(str(ret), '"A","updated1"\n') |
| 47 | + |
| 48 | + # show final thread count |
| 49 | + print("Final thread count:", len(psutil.Process().threads())) |
| 50 | + |
| 51 | + # print("Values after UPDATE in same query (expected 'A', 'updated'):") |
| 52 | + # ret = sess.query( |
| 53 | + # "UPDATE test_db.test_table SET y = 'updated2' WHERE x = 'A';" |
| 54 | + # "SELECT * FROM test_db.test_table WHERE x = 'A';") |
| 55 | + # print(ret) |
| 56 | + # self.assertEqual(str(ret), '"A","updated2"\n') |
| 57 | + |
| 58 | + print('Values after UPDATE expected:') |
| 59 | + sess.query("ALTER TABLE test_db.test_table UPDATE y = 'updated2' WHERE x = 'A';" |
| 60 | + "ALTER TABLE test_db.test_table UPDATE y = 'updated3' WHERE x = 'A'") |
| 61 | + ret = sess.query("SELECT * FROM test_db.test_table WHERE x = 'A'") |
| 62 | + print(ret) |
| 63 | + self.assertEqual(str(ret), '"A","updated3"\n') |
| 64 | + |
| 65 | + # show final thread count |
| 66 | + print("Final thread count:", len(psutil.Process().threads())) |
| 67 | + |
| 68 | + print("Values after DELETE expected:") |
| 69 | + sess.query("ALTER TABLE test_db.test_table DELETE WHERE x = 'A'") |
| 70 | + ret = sess.query("SELECT * FROM test_db.test_table") |
| 71 | + print(ret) |
| 72 | + self.assertEqual(str(ret), '"C","D"\n') |
| 73 | + |
| 74 | + # show final thread count |
| 75 | + print("Final thread count:", len(psutil.Process().threads())) |
| 76 | + |
| 77 | + print("Values after ALTER then OPTIMIZE expected:") |
| 78 | + sess.query("ALTER TABLE test_db.test_table DELETE WHERE x = 'C'; OPTIMIZE TABLE test_db.test_table FINAL") |
| 79 | + ret = sess.query("SELECT * FROM test_db.test_table") |
| 80 | + print(ret) |
| 81 | + self.assertEqual(str(ret), "") |
| 82 | + |
| 83 | + print("Inserting 1000 rows") |
| 84 | + sess.query("INSERT INTO test_db.test_table (x, y) SELECT toString(number), toString(number) FROM numbers(1000);") |
| 85 | + ret = sess.query("SELECT count() FROM test_db.test_table", "Debug") |
| 86 | + count = str(ret).count("\n") |
| 87 | + print("Number of newline characters:", count) |
| 88 | + |
| 89 | + # show final thread count |
| 90 | + print("Final thread count:", len(psutil.Process().threads())) |
| 91 | + |
| 92 | + time.sleep(3) |
| 93 | + print("Final thread count after 3s:", len(psutil.Process().threads())) |
| 94 | + self.assertEqual(len(psutil.Process().threads()), 1) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + unittest.main() |
0 commit comments