@@ -20,21 +20,13 @@ package tests
20
20
import (
21
21
"context"
22
22
"crypto/tls"
23
+ "database/sql"
23
24
"encoding/json"
24
25
"errors"
25
26
"fmt"
26
- "github.com/ClickHouse/clickhouse-go/v2"
27
- "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
28
- "github.com/ClickHouse/clickhouse-go/v2/lib/proto"
29
- "github.com/docker/docker/api/types/container"
30
- "github.com/docker/go-connections/nat"
31
- "github.com/docker/go-units"
32
- "github.com/google/uuid"
33
- "github.com/stretchr/testify/require"
34
- "github.com/testcontainers/testcontainers-go"
35
- "github.com/testcontainers/testcontainers-go/wait"
36
27
"math/rand"
37
28
"net"
29
+ "net/url"
38
30
"os"
39
31
"path"
40
32
"path/filepath"
@@ -43,6 +35,17 @@ import (
43
35
"strings"
44
36
"testing"
45
37
"time"
38
+
39
+ "github.com/ClickHouse/clickhouse-go/v2"
40
+ "github.com/ClickHouse/clickhouse-go/v2/lib/driver"
41
+ "github.com/ClickHouse/clickhouse-go/v2/lib/proto"
42
+ "github.com/docker/docker/api/types/container"
43
+ "github.com/docker/go-connections/nat"
44
+ "github.com/docker/go-units"
45
+ "github.com/google/uuid"
46
+ "github.com/stretchr/testify/require"
47
+ "github.com/testcontainers/testcontainers-go"
48
+ "github.com/testcontainers/testcontainers-go/wait"
46
49
)
47
50
48
51
var testUUID = uuid .NewString ()[0 :12 ]
@@ -291,7 +294,7 @@ func testClientWithDefaultOptions(env ClickHouseTestEnvironment, settings clickh
291
294
return clickhouse .Open (& opts )
292
295
}
293
296
294
- func TestClientWithDefaultSettings (env ClickHouseTestEnvironment ) (driver. Conn , error ) {
297
+ func TestClientDefaultSettings (env ClickHouseTestEnvironment ) clickhouse. Settings {
295
298
settings := clickhouse.Settings {}
296
299
297
300
if proto .CheckMinVersion (proto.Version {
@@ -305,7 +308,20 @@ func TestClientWithDefaultSettings(env ClickHouseTestEnvironment) (driver.Conn,
305
308
settings ["insert_quorum_parallel" ] = 0
306
309
settings ["select_sequential_consistency" ] = 1
307
310
308
- return testClientWithDefaultOptions (env , settings )
311
+ return settings
312
+ }
313
+
314
+ func TestClientWithDefaultSettings (env ClickHouseTestEnvironment ) (driver.Conn , error ) {
315
+ return testClientWithDefaultOptions (env , TestClientDefaultSettings (env ))
316
+ }
317
+
318
+ func TestDatabaseSQLClientWithDefaultOptions (env ClickHouseTestEnvironment , settings clickhouse.Settings ) (* sql.DB , error ) {
319
+ opts := ClientOptionsFromEnv (env , settings )
320
+ return sql .Open ("clickhouse" , optionsToDSN (& opts ))
321
+ }
322
+
323
+ func TestDatabaseSQLClientWithDefaultSettings (env ClickHouseTestEnvironment ) (* sql.DB , error ) {
324
+ return TestDatabaseSQLClientWithDefaultOptions (env , TestClientDefaultSettings (env ))
309
325
}
310
326
311
327
func GetConnection (testSet string , settings clickhouse.Settings , tlsConfig * tls.Config , compression * clickhouse.Compression ) (driver.Conn , error ) {
@@ -627,3 +643,109 @@ func CreateTinyProxyTestEnvironment(t *testing.T) (TinyProxyTestEnvironment, err
627
643
Container : container ,
628
644
}, nil
629
645
}
646
+
647
+ func optionsToDSN (o * clickhouse.Options ) string {
648
+ var u url.URL
649
+
650
+ if o .Protocol == clickhouse .Native {
651
+ u .Scheme = "clickhouse"
652
+ } else {
653
+ if o .TLS != nil {
654
+ u .Scheme = "https"
655
+ } else {
656
+ u .Scheme = "http"
657
+ }
658
+ }
659
+
660
+ u .Host = strings .Join (o .Addr , "," )
661
+ u .User = url .UserPassword (o .Auth .Username , o .Auth .Password )
662
+ u .Path = fmt .Sprintf ("/%s" , o .Auth .Database )
663
+
664
+ params := u .Query ()
665
+
666
+ if o .TLS != nil {
667
+ params .Set ("secure" , "true" )
668
+ }
669
+
670
+ if o .TLS != nil && o .TLS .InsecureSkipVerify {
671
+ params .Set ("skip_verify" , "true" )
672
+ }
673
+
674
+ if o .Debug {
675
+ params .Set ("debug" , "true" )
676
+ }
677
+
678
+ if o .Compression != nil {
679
+ params .Set ("compress" , o .Compression .Method .String ())
680
+ if o .Compression .Level > 0 {
681
+ params .Set ("compress_level" , strconv .Itoa (o .Compression .Level ))
682
+ }
683
+ }
684
+
685
+ if o .MaxCompressionBuffer > 0 {
686
+ params .Set ("max_compression_buffer" , strconv .Itoa (o .MaxCompressionBuffer ))
687
+ }
688
+
689
+ if o .DialTimeout > 0 {
690
+ params .Set ("dial_timeout" , o .DialTimeout .String ())
691
+ }
692
+
693
+ if o .BlockBufferSize > 0 {
694
+ params .Set ("block_buffer_size" , strconv .Itoa (int (o .BlockBufferSize )))
695
+ }
696
+
697
+ if o .ReadTimeout > 0 {
698
+ params .Set ("read_timeout" , o .ReadTimeout .String ())
699
+ }
700
+
701
+ if o .ConnOpenStrategy != 0 {
702
+ var strategy string
703
+ switch o .ConnOpenStrategy {
704
+ case clickhouse .ConnOpenInOrder :
705
+ strategy = "in_order"
706
+ case clickhouse .ConnOpenRoundRobin :
707
+ strategy = "round_robin"
708
+ }
709
+
710
+ params .Set ("connection_open_strategy" , strategy )
711
+ }
712
+
713
+ if o .MaxOpenConns > 0 {
714
+ params .Set ("max_open_conns" , strconv .Itoa (o .MaxOpenConns ))
715
+ }
716
+
717
+ if o .MaxIdleConns > 0 {
718
+ params .Set ("max_idle_conns" , strconv .Itoa (o .MaxIdleConns ))
719
+ }
720
+
721
+ if o .ConnMaxLifetime > 0 {
722
+ params .Set ("conn_max_lifetime" , o .ConnMaxLifetime .String ())
723
+ }
724
+
725
+ if o .ClientInfo .Products != nil {
726
+ var products []string
727
+ for _ , product := range o .ClientInfo .Products {
728
+ products = append (products , fmt .Sprintf ("%s/%s" , product .Name , product .Version ))
729
+ }
730
+ params .Set ("client_info_product" , strings .Join (products , "," ))
731
+ }
732
+
733
+ for k , v := range o .Settings {
734
+ switch v := v .(type ) {
735
+ case bool :
736
+ if v {
737
+ params .Set (k , "true" )
738
+ } else {
739
+ params .Set (k , "false" )
740
+ }
741
+ case int :
742
+ params .Set (k , strconv .Itoa (v ))
743
+ case string :
744
+ params .Set (k , v )
745
+ }
746
+ }
747
+
748
+ u .RawQuery = params .Encode ()
749
+
750
+ return u .String ()
751
+ }
0 commit comments