11package chdb
22
33import (
4- "io/ioutil"
54 "os"
5+ "path/filepath"
66 "testing"
77)
88
99func TestQueryToBuffer (t * testing.T ) {
1010 // Create a temporary directory
11- tempDir , err := ioutil .TempDir ("" , "example" )
12- if err != nil {
13- t .Fatalf ("Failed to create temporary directory: %v" , err )
14- }
11+ tempDir := filepath .Join (os .TempDir (), "chdb_test" )
1512 defer os .RemoveAll (tempDir )
1613
1714 // Define test cases
@@ -21,6 +18,7 @@ func TestQueryToBuffer(t *testing.T) {
2118 outputFormat string
2219 path string
2320 udfPath string
21+ expectedErrMsg string
2422 expectedResult string
2523 }{
2624 {
@@ -29,6 +27,7 @@ func TestQueryToBuffer(t *testing.T) {
2927 outputFormat : "CSV" ,
3028 path : "" ,
3129 udfPath : "" ,
30+ expectedErrMsg : "" ,
3231 expectedResult : "123\n " ,
3332 },
3433 // Session
@@ -39,35 +38,59 @@ func TestQueryToBuffer(t *testing.T) {
3938 outputFormat : "CSV" ,
4039 path : tempDir ,
4140 udfPath : "" ,
41+ expectedErrMsg : "" ,
4242 expectedResult : "" ,
4343 },
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+ // },
4462 {
45- name : "Session Query 2 " ,
46- queryStr : "USE testdb; INSERT INTO testtable VALUES (1), (2), (3); " ,
63+ name : "Error Query" ,
64+ queryStr : "SELECT * FROM nonexist; " ,
4765 outputFormat : "CSV" ,
4866 path : tempDir ,
4967 udfPath : "" ,
68+ expectedErrMsg : "Code: 60. DB::Exception: Table _local.nonexist does not exist. (UNKNOWN_TABLE)" ,
5069 expectedResult : "" ,
5170 },
52- {
53- name : "Session Query 3" ,
54- queryStr : "SELECT * FROM testtable;" ,
55- outputFormat : "CSV" ,
56- path : tempDir ,
57- udfPath : "" ,
58- expectedResult : "1\n 2\n 3\n " ,
59- },
6071 }
6172
6273 for _ , tc := range testCases {
6374 t .Run (tc .name , func (t * testing.T ) {
6475 // Call queryToBuffer
65- result := queryToBuffer (tc .queryStr , tc .outputFormat , tc .path , tc .udfPath )
76+ result , err := queryToBuffer (tc .queryStr , tc .outputFormat , tc .path , tc .udfPath )
6677
6778 // Verify
68- if string (result .Buf ()) != tc .expectedResult {
69- t .Errorf ("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect result: %v, got result: %v" ,
70- tc .name , tc .queryStr , tc .outputFormat , tc .path , tc .udfPath , tc .expectedResult , string (result .Buf ()))
79+ if tc .expectedErrMsg != "" {
80+ 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+ } else {
84+ 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+ }
88+ }
89+ } else {
90+ 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+ }
7194 }
7295 })
7396 }
0 commit comments