11package chdb
22
33import (
4- "os"
5- "path/filepath"
64 "testing"
75)
86
97func TestQueryToBuffer (t * testing.T ) {
108 // Create a temporary directory
11- tempDir := filepath .Join (os .TempDir (), "chdb_test" )
12- defer os .RemoveAll (tempDir )
9+ sess , err := NewSession ()
10+ if err != nil {
11+ t .Fatalf ("could not create session: %s" , err )
12+ }
13+ defer sess .Close ()
1314
1415 // Define test cases
1516 testCases := []struct {
16- name string
17- queryStr string
18- outputFormat string
19- path string
17+ name string
18+ queryStr string
19+ outputFormat string
20+
2021 udfPath string
2122 expectedErrMsg string
2223 expectedResult string
2324 }{
2425 {
25- name : "Basic Query" ,
26- queryStr : "SELECT 123" ,
27- outputFormat : "CSV" ,
28- path : "" ,
26+ name : "Basic Query" ,
27+ queryStr : "SELECT 123" ,
28+ outputFormat : "CSV" ,
29+
2930 udfPath : "" ,
3031 expectedErrMsg : "" ,
3132 expectedResult : "123\n " ,
@@ -35,35 +36,35 @@ func TestQueryToBuffer(t *testing.T) {
3536 name : "Session Query 1" ,
3637 queryStr : "CREATE DATABASE IF NOT EXISTS testdb; " +
3738 "CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;" ,
38- outputFormat : "CSV" ,
39- path : tempDir ,
39+ outputFormat : "CSV" ,
40+
4041 udfPath : "" ,
4142 expectedErrMsg : "" ,
4243 expectedResult : "" ,
4344 },
44- // {
45- // name: "Session Query 2",
46- // queryStr: "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);",
47- // outputFormat: "CSV",
48- // path: tempDir,
49- // udfPath: "",
50- // expectedErrMsg: "",
51- // expectedResult: "",
52- // },
53- // {
54- // name: "Session Query 3",
55- // queryStr: "SELECT * FROM testtable;",
56- // outputFormat: "CSV",
57- // path: tempDir,
58- // udfPath: "",
59- // expectedErrMsg: "",
60- // expectedResult: "1\n2\n3\n",
61- // },
6245 {
63- name : "Error Query" ,
64- queryStr : "SELECT * FROM nonexist; " ,
65- outputFormat : "CSV" ,
66- path : tempDir ,
46+ name : "Session Query 2" ,
47+ queryStr : "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);" ,
48+ outputFormat : "CSV" ,
49+
50+ udfPath : "" ,
51+ expectedErrMsg : "" ,
52+ expectedResult : "" ,
53+ },
54+ {
55+ name : "Session Query 3" ,
56+ queryStr : "SELECT * FROM testtable;" ,
57+ outputFormat : "CSV" ,
58+
59+ udfPath : "" ,
60+ expectedErrMsg : "" ,
61+ expectedResult : "1\n 2\n 3\n " ,
62+ },
63+ {
64+ name : "Error Query" ,
65+ queryStr : "SELECT * FROM nonexist; " ,
66+ outputFormat : "CSV" ,
67+
6768 udfPath : "" ,
6869 expectedErrMsg : "Code: 60. DB::Exception: Unknown table expression identifier 'nonexist' in scope SELECT * FROM nonexist. (UNKNOWN_TABLE)" ,
6970 expectedResult : "" ,
@@ -73,23 +74,24 @@ func TestQueryToBuffer(t *testing.T) {
7374 for _ , tc := range testCases {
7475 t .Run (tc .name , func (t * testing.T ) {
7576 // Call queryToBuffer
76- result , err := queryToBuffer (tc .queryStr , tc .outputFormat , tc .path , tc .udfPath )
77+
78+ result , err := sess .Query (tc .queryStr , tc .outputFormat )
7779
7880 // Verify
7981 if tc .expectedErrMsg != "" {
8082 if err == nil {
81- t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect error message: %v, got no error" ,
82- tc .name , tc .queryStr , tc .outputFormat , tc .path , tc . udfPath , tc .expectedErrMsg )
83+ t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, udfPath %v, expect error message: %v, got no error" ,
84+ tc .name , tc .queryStr , tc .outputFormat , tc .udfPath , tc .expectedErrMsg )
8385 } else {
8486 if err .Error () != tc .expectedErrMsg {
85- t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect error message: %v, got error message: %v" ,
86- tc .name , tc .queryStr , tc .outputFormat , tc .path , tc . udfPath , tc .expectedErrMsg , err .Error ())
87+ t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, udfPath %v, expect error message: %v, got error message: %v" ,
88+ tc .name , tc .queryStr , tc .outputFormat , tc .udfPath , tc .expectedErrMsg , err .Error ())
8789 }
8890 }
8991 } else {
9092 if string (result .Buf ()) != tc .expectedResult {
91- t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect result: %v, got result: %v" ,
92- tc .name , tc .queryStr , tc .outputFormat , tc .path , tc . udfPath , tc .expectedResult , string (result .Buf ()))
93+ t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, udfPath %v, expect result: %v, got result: %v" ,
94+ tc .name , tc .queryStr , tc .outputFormat , tc .udfPath , tc .expectedResult , string (result .Buf ()))
9395 }
9496 }
9597 })
0 commit comments