1
1
package chdb
2
2
3
3
import (
4
- "io/ioutil"
5
4
"os"
5
+ "path/filepath"
6
6
"testing"
7
7
)
8
8
9
9
func TestQueryToBuffer (t * testing.T ) {
10
10
// 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" )
15
12
defer os .RemoveAll (tempDir )
16
13
17
14
// Define test cases
@@ -21,6 +18,7 @@ func TestQueryToBuffer(t *testing.T) {
21
18
outputFormat string
22
19
path string
23
20
udfPath string
21
+ expectedErrMsg string
24
22
expectedResult string
25
23
}{
26
24
{
@@ -29,6 +27,7 @@ func TestQueryToBuffer(t *testing.T) {
29
27
outputFormat : "CSV" ,
30
28
path : "" ,
31
29
udfPath : "" ,
30
+ expectedErrMsg : "" ,
32
31
expectedResult : "123\n " ,
33
32
},
34
33
// Session
@@ -39,35 +38,59 @@ func TestQueryToBuffer(t *testing.T) {
39
38
outputFormat : "CSV" ,
40
39
path : tempDir ,
41
40
udfPath : "" ,
41
+ expectedErrMsg : "" ,
42
42
expectedResult : "" ,
43
43
},
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
+ // },
44
62
{
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; " ,
47
65
outputFormat : "CSV" ,
48
66
path : tempDir ,
49
67
udfPath : "" ,
68
+ expectedErrMsg : "Code: 60. DB::Exception: Table _local.nonexist does not exist. (UNKNOWN_TABLE)" ,
50
69
expectedResult : "" ,
51
70
},
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
- },
60
71
}
61
72
62
73
for _ , tc := range testCases {
63
74
t .Run (tc .name , func (t * testing.T ) {
64
75
// 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 )
66
77
67
78
// 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
+ }
71
94
}
72
95
})
73
96
}
0 commit comments