Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit 335976e

Browse files
committed
Minor improvement to ClusterConfigToConfigString
Added tests
1 parent 258856e commit 335976e

File tree

6 files changed

+36
-49
lines changed

6 files changed

+36
-49
lines changed

config.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewClusterConfig(hosts ...string) *gocql.ClusterConfig {
2222
// https://godoc.org/github.com/gocql/gocql#ClusterConfig
2323
func ClusterConfigToConfigString(clusterConfig *gocql.ClusterConfig) string {
2424
clusterConfigDefault := gocql.NewCluster()
25-
stringConfig := ""
25+
stringConfig := strings.Join(clusterConfig.Hosts, ",") + "?"
2626

2727
if clusterConfig.Consistency != clusterConfigDefault.Consistency {
2828
consistency, ok := DbConsistency[clusterConfig.Consistency]
@@ -43,9 +43,6 @@ func ClusterConfigToConfigString(clusterConfig *gocql.ClusterConfig) string {
4343
if clusterConfig.NumConns > 1 {
4444
stringConfig += "numConns=" + strconv.FormatInt(int64(clusterConfig.NumConns), 10) + "&"
4545
}
46-
if clusterConfig.NumConns > 1 {
47-
stringConfig += "numConns=" + strconv.FormatInt(int64(clusterConfig.NumConns), 10) + "&"
48-
}
4946
if clusterConfig.IgnorePeerAddr != clusterConfigDefault.IgnorePeerAddr {
5047
stringConfig += "ignorePeerAddr=" + fmt.Sprint(clusterConfig.IgnorePeerAddr) + "&"
5148
}
@@ -68,13 +65,7 @@ func ClusterConfigToConfigString(clusterConfig *gocql.ClusterConfig) string {
6865
}
6966
}
7067

71-
if stringConfig == "" {
72-
stringConfig = strings.Join(clusterConfig.Hosts, ",")
73-
} else {
74-
stringConfig = strings.Join(clusterConfig.Hosts, ",") + "?" + stringConfig[:len(stringConfig)-1]
75-
}
76-
77-
return stringConfig
68+
return stringConfig[:len(stringConfig)-1]
7869
}
7970

8071
// ConfigStringToClusterConfig converts a config string to a gocql ClusterConfig

config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ func TestClusterConfigToConfigString(t *testing.T) {
4040
{info: "ConnectTimeout > 0", clusterConfig: &gocql.ClusterConfig{ConnectTimeout: 10 * time.Second}, configString: "?consistency=any&timeout=0s&connectTimeout=10s&writeCoalesceWaitTime=0s"},
4141
{info: "Keyspace", clusterConfig: &gocql.ClusterConfig{Keyspace: "system"}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&keyspace=system&writeCoalesceWaitTime=0s"},
4242
{info: "NumConns < 2", clusterConfig: &gocql.ClusterConfig{NumConns: 1}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s"},
43+
{info: "NumConns > 1", clusterConfig: &gocql.ClusterConfig{NumConns: 2}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&numConns=2&writeCoalesceWaitTime=0s"},
4344
{info: "IgnorePeerAddr false DisableInitialHostLookup false", clusterConfig: &gocql.ClusterConfig{IgnorePeerAddr: false, DisableInitialHostLookup: false}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s"},
4445
{info: "IgnorePeerAddr true DisableInitialHostLookup false", clusterConfig: &gocql.ClusterConfig{IgnorePeerAddr: true, DisableInitialHostLookup: false}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&ignorePeerAddr=true&writeCoalesceWaitTime=0s"},
4546
{info: "IgnorePeerAddr false DisableInitialHostLookup true", clusterConfig: &gocql.ClusterConfig{IgnorePeerAddr: false, DisableInitialHostLookup: true}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&disableInitialHostLookup=true&writeCoalesceWaitTime=0s"},
4647
{info: "IgnorePeerAddr true DisableInitialHostLookup true", clusterConfig: &gocql.ClusterConfig{IgnorePeerAddr: true, DisableInitialHostLookup: true}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&ignorePeerAddr=true&disableInitialHostLookup=true&writeCoalesceWaitTime=0s"},
4748
{info: "WriteCoalesceWaitTime 1s", clusterConfig: &gocql.ClusterConfig{WriteCoalesceWaitTime: time.Second}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=1s"},
49+
{info: "Authenticator empty", clusterConfig: &gocql.ClusterConfig{Authenticator: gocql.PasswordAuthenticator{}}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s"},
50+
{info: "Authenticator username", clusterConfig: &gocql.ClusterConfig{Authenticator: gocql.PasswordAuthenticator{Username: "username"}}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s&username=username"},
51+
{info: "Authenticator username password", clusterConfig: &gocql.ClusterConfig{Authenticator: gocql.PasswordAuthenticator{Username: "username", Password: "password"}}, configString: "?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s&username=username&password=password"},
4852
{info: "Host", clusterConfig: &gocql.ClusterConfig{Hosts: []string{"one"}}, configString: "one?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s"},
4953
{info: "Hosts", clusterConfig: &gocql.ClusterConfig{Hosts: []string{"one", "two", "three"}}, configString: "one,two,three?consistency=any&timeout=0s&connectTimeout=0s&writeCoalesceWaitTime=0s"},
5054
}
@@ -60,6 +64,7 @@ func TestConfigStringToClusterConfig(t *testing.T) {
6064
tests := []TestStringToConfigStruct{
6165
{info: "missing =", configString: "?consistency", err: fmt.Errorf("missing =")},
6266
{info: "failed consistency", configString: "?consistency=", err: fmt.Errorf("failed for: consistency = ")},
67+
{info: "failed keyspace", configString: "?keyspace=", err: fmt.Errorf("failed for: keyspace = ")},
6368
{info: "failed timeout", configString: "?timeout=", err: fmt.Errorf("failed for: timeout = ")},
6469
{info: "failed connectTimeout", configString: "?connectTimeout=", err: fmt.Errorf("failed for: connectTimeout = ")},
6570
{info: "failed numConns", configString: "?numConns=", err: fmt.Errorf("failed for: numConns = ")},

cqlSql_test.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
165165
if rows.Next() {
166166
t.Fatal("has Next rows")
167167
}
168-
err = rows.Err()
169-
if err != nil {
170-
t.Fatal("Err error: ", err)
171-
}
172168
err = rows.Close()
173169
if err != nil {
174170
t.Fatal("Close error: ", err)
@@ -211,10 +207,6 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
211207
if rows.Next() {
212208
t.Fatal("has Next rows")
213209
}
214-
err = rows.Err()
215-
if err != nil {
216-
t.Fatal("Err error: ", err)
217-
}
218210
err = rows.Close()
219211
if err != nil {
220212
t.Fatal("Close error: ", err)
@@ -257,10 +249,6 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
257249
if rows.Next() {
258250
t.Fatal("has Next rows")
259251
}
260-
err = rows.Err()
261-
if err != nil {
262-
t.Fatal("Err error: ", err)
263-
}
264252
err = rows.Close()
265253
if err != nil {
266254
t.Fatal("Close error: ", err)
@@ -290,10 +278,6 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
290278
if rows.Next() {
291279
t.Fatal("has Next rows")
292280
}
293-
err = rows.Err()
294-
if err != nil {
295-
t.Fatal("Err error: ", err)
296-
}
297281
err = rows.Close()
298282
if err != nil {
299283
t.Fatal("Close error: ", err)
@@ -311,7 +295,7 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
311295
t.Fatal("result is nil")
312296
}
313297

314-
// select error
298+
// select errors
315299
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
316300
rows, err = db.QueryContext(ctx, "select int_data from "+KeyspaceName+"."+TableName+" group by int_data")
317301
if err != nil {
@@ -322,6 +306,16 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
322306
}
323307
cancel()
324308

309+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
310+
rows, err = db.QueryContext(ctx, "select int_data from "+KeyspaceName+"."+TableName+" where int_data = ?")
311+
if err != nil {
312+
t.Fatal("QueryContext error: ", err)
313+
}
314+
if rows.Close() == nil {
315+
t.Fatal("QueryContext no error")
316+
}
317+
cancel()
318+
325319
err = db.Close()
326320
if err != nil {
327321
t.Fatal("Close error: ", err)
@@ -434,10 +428,6 @@ func TestSqTruncate(t *testing.T) {
434428
if rows.Next() {
435429
t.Fatal("has Next rows")
436430
}
437-
err = rows.Err()
438-
if err != nil {
439-
t.Fatal("Err error: ", err)
440-
}
441431
err = rows.Close()
442432
if err != nil {
443433
t.Fatal("Close error: ", err)

exampleSqlGo110_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ func Example_sqlConnector() {
7676
return
7777
}
7878

79-
err = rows.Err()
80-
if err != nil {
81-
fmt.Println("Err error is not nil:", err)
82-
return
83-
}
8479
err = rows.Close()
8580
if err != nil {
8681
fmt.Println("Close error is not nil:", err)

exampleSql_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ func Example_sqlSelect() {
7070
return
7171
}
7272

73-
err = rows.Err()
74-
if err != nil {
75-
fmt.Println("Err error is not nil:", err)
76-
return
77-
}
7873
err = rows.Close()
7974
if err != nil {
8075
fmt.Println("Close error is not nil:", err)
@@ -166,11 +161,6 @@ func Example_sqlStatement() {
166161
return
167162
}
168163

169-
err = rows.Err()
170-
if err != nil {
171-
fmt.Println("Err error is not nil:", err)
172-
return
173-
}
174164
err = rows.Close()
175165
if err != nil {
176166
fmt.Println("Close error is not nil:", err)

statement_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestStatementExecContext(t *testing.T) {
149149
}
150150

151151
func TestStatementQuery(t *testing.T) {
152-
conn, stmt := testGetStatementHostValid(t, "select release_version from system.local}")
152+
conn, stmt := testGetStatementHostValid(t, "select release_version from system.local")
153153
if stmt == nil {
154154
t.Fatal("stmt is nil")
155155
}
@@ -161,6 +161,10 @@ func TestStatementQuery(t *testing.T) {
161161
if rows == nil {
162162
t.Fatal("rows is nil")
163163
}
164+
err = rows.Close()
165+
if err != nil {
166+
t.Fatalf("Close error - received: %v - expected: %v ", err, nil)
167+
}
164168

165169
rows, err = stmt.Query([]driver.Value{driver.Value(1)})
166170
if err != nil {
@@ -169,6 +173,10 @@ func TestStatementQuery(t *testing.T) {
169173
if rows == nil {
170174
t.Fatal("rows is nil")
171175
}
176+
err = rows.Close()
177+
if err == nil {
178+
t.Fatalf("Close error expected")
179+
}
172180

173181
err = stmt.Close()
174182
if err != nil {
@@ -181,7 +189,7 @@ func TestStatementQuery(t *testing.T) {
181189
}
182190

183191
func TestStatementQueryContext(t *testing.T) {
184-
conn, stmt := testGetStatementHostValid(t, "select release_version from system.local}")
192+
conn, stmt := testGetStatementHostValid(t, "select release_version from system.local")
185193
if stmt == nil {
186194
t.Fatal("stmt is nil")
187195
}
@@ -194,6 +202,10 @@ func TestStatementQueryContext(t *testing.T) {
194202
if rows == nil {
195203
t.Fatal("rows is nil")
196204
}
205+
err = rows.Close()
206+
if err != nil {
207+
t.Fatalf("Close error - received: %v - expected: %v ", err, nil)
208+
}
197209

198210
rows, err = cqlStmt.QueryContext(context.Background(), []driver.NamedValue{{Ordinal: 1, Value: 1}})
199211
if err != nil {
@@ -202,6 +214,10 @@ func TestStatementQueryContext(t *testing.T) {
202214
if rows == nil {
203215
t.Fatal("rows is nil")
204216
}
217+
err = rows.Close()
218+
if err == nil {
219+
t.Fatalf("Close error expected")
220+
}
205221

206222
rows, err = cqlStmt.QueryContext(context.Background(), []driver.NamedValue{{Name: "a"}})
207223
if err == nil || err != ErrNamedValuesNotSupported {

0 commit comments

Comments
 (0)