1
1
package chdb
2
2
3
3
import (
4
- "os"
5
- "path/filepath"
6
4
"testing"
7
5
)
8
6
9
7
func TestQueryToBuffer (t * testing.T ) {
10
8
// 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 ()
13
14
14
15
// Define test cases
15
16
testCases := []struct {
16
- name string
17
- queryStr string
18
- outputFormat string
19
- path string
17
+ name string
18
+ queryStr string
19
+ outputFormat string
20
+
20
21
udfPath string
21
22
expectedErrMsg string
22
23
expectedResult string
23
24
}{
24
25
{
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
+
29
30
udfPath : "" ,
30
31
expectedErrMsg : "" ,
31
32
expectedResult : "123\n " ,
@@ -35,35 +36,35 @@ func TestQueryToBuffer(t *testing.T) {
35
36
name : "Session Query 1" ,
36
37
queryStr : "CREATE DATABASE IF NOT EXISTS testdb; " +
37
38
"CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;" ,
38
- outputFormat : "CSV" ,
39
- path : tempDir ,
39
+ outputFormat : "CSV" ,
40
+
40
41
udfPath : "" ,
41
42
expectedErrMsg : "" ,
42
43
expectedResult : "" ,
43
44
},
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
- // },
62
45
{
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
+
67
68
udfPath : "" ,
68
69
expectedErrMsg : "Code: 60. DB::Exception: Unknown table expression identifier 'nonexist' in scope SELECT * FROM nonexist. (UNKNOWN_TABLE)" ,
69
70
expectedResult : "" ,
@@ -73,23 +74,24 @@ func TestQueryToBuffer(t *testing.T) {
73
74
for _ , tc := range testCases {
74
75
t .Run (tc .name , func (t * testing.T ) {
75
76
// Call queryToBuffer
76
- result , err := queryToBuffer (tc .queryStr , tc .outputFormat , tc .path , tc .udfPath )
77
+
78
+ result , err := sess .Query (tc .queryStr , tc .outputFormat )
77
79
78
80
// Verify
79
81
if tc .expectedErrMsg != "" {
80
82
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 )
83
85
} else {
84
86
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 ())
87
89
}
88
90
}
89
91
} else {
90
92
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 ()))
93
95
}
94
96
}
95
97
})
0 commit comments