Skip to content

Commit 342e4b5

Browse files
committed
fix tests
1 parent 7a5fd4e commit 342e4b5

File tree

2 files changed

+7
-78
lines changed

2 files changed

+7
-78
lines changed

chdb/wrapper.go

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,22 @@ import (
44
chdbpurego "github.com/chdb-io/chdb-go/chdb-purego"
55
)
66

7-
// Query calls queryToBuffer with a default output format of "CSV" if not provided.
7+
// Query calls query_conn with a default in-memory session and default output format of "CSV" if not provided.
88
func Query(queryStr string, outputFormats ...string) (result chdbpurego.ChdbResult, err error) {
99
outputFormat := "CSV" // Default value
1010
if len(outputFormats) > 0 {
1111
outputFormat = outputFormats[0]
1212
}
13-
return queryToBuffer(queryStr, outputFormat, "", "")
14-
}
15-
16-
// queryToBuffer constructs the arguments for QueryStable and calls it.
17-
func queryToBuffer(queryStr, outputFormat, path, udfPath string) (result chdbpurego.ChdbResult, err error) {
18-
argv := []string{"clickhouse", "--multiquery"}
19-
20-
// Handle output format
21-
if outputFormat == "Debug" || outputFormat == "debug" {
22-
argv = append(argv, "--verbose", "--log-level=trace", "--output-format=CSV")
23-
} else {
24-
argv = append(argv, "--output-format="+outputFormat)
25-
}
26-
27-
// Handle path
28-
if path != "" {
29-
argv = append(argv, "--path="+path)
30-
}
31-
32-
// Add query string
33-
argv = append(argv, "--query="+queryStr)
34-
35-
// Handle user-defined functions path
36-
if udfPath != "" {
37-
argv = append(argv, "--", "--user_scripts_path="+udfPath, "--user_defined_executable_functions_config="+udfPath+"/*.xml")
13+
tempSession, err := initConnection(":memory:")
14+
if err != nil {
15+
return nil, err
3816
}
39-
40-
// Call QueryStable with the constructed arguments
41-
return chdbpurego.RawQuery(len(argv), argv)
17+
defer tempSession.Close()
18+
return tempSession.Query(queryStr, outputFormat)
4219
}
4320

4421
func initConnection(connStr string) (result chdbpurego.ChdbConn, err error) {
4522
argv := []string{connStr}
4623
// Call NewConnection with the constructed arguments
4724
return chdbpurego.NewConnection(len(argv), argv)
4825
}
49-
50-
func connQueryToBuffer(conn chdbpurego.ChdbConn, queryStr, outputFormat string) (result chdbpurego.ChdbResult, err error) {
51-
if outputFormat == "" {
52-
outputFormat = "CSV"
53-
}
54-
return conn.Query(queryStr, outputFormat)
55-
}

chdb/wrapper_test.go

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import (
66

77
func TestQueryToBuffer(t *testing.T) {
88
// Create a temporary directory
9-
sess, err := NewSession()
10-
if err != nil {
11-
t.Fatalf("could not create session: %s", err)
12-
}
13-
defer sess.Close()
149

1510
// Define test cases
1611
testCases := []struct {
@@ -31,43 +26,7 @@ func TestQueryToBuffer(t *testing.T) {
3126
expectedErrMsg: "",
3227
expectedResult: "123\n",
3328
},
34-
// Session
35-
{
36-
name: "Session Query 1",
37-
queryStr: "CREATE DATABASE IF NOT EXISTS testdb; ",
38-
outputFormat: "CSV",
39-
40-
udfPath: "",
41-
expectedErrMsg: "",
42-
expectedResult: "",
43-
},
44-
{
45-
name: "Session Query 1 bis",
46-
queryStr: "CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;",
47-
outputFormat: "CSV",
4829

49-
udfPath: "",
50-
expectedErrMsg: "",
51-
expectedResult: "",
52-
},
53-
{
54-
name: "Session Query 2",
55-
queryStr: "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);",
56-
outputFormat: "CSV",
57-
58-
udfPath: "",
59-
expectedErrMsg: "",
60-
expectedResult: "",
61-
},
62-
{
63-
name: "Session Query 3",
64-
queryStr: "SELECT * FROM testtable;",
65-
outputFormat: "CSV",
66-
67-
udfPath: "",
68-
expectedErrMsg: "",
69-
expectedResult: "1\n2\n3\n",
70-
},
7130
{
7231
name: "Error Query",
7332
queryStr: "SELECT * FROM nonexist; ",
@@ -83,7 +42,7 @@ func TestQueryToBuffer(t *testing.T) {
8342
t.Run(tc.name, func(t *testing.T) {
8443
// Call queryToBuffer
8544

86-
result, err := sess.Query(tc.queryStr, tc.outputFormat)
45+
result, err := Query(tc.queryStr, tc.outputFormat)
8746

8847
// Verify
8948
if tc.expectedErrMsg != "" {

0 commit comments

Comments
 (0)