Skip to content

Commit 5fd2cdf

Browse files
committed
Update example and test code for 2.0 release
This patch updates the existing examples and tests according to the 2.0 changes (mostly deprecations) and adds new examples for new features added in 2.0. Patch by João Reis; reviewed by Bohdan Siryk and Lukasz Antoniak for CASSGO-80
1 parent 759351e commit 5fd2cdf

17 files changed

+689
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
- Add Query and Batch to ObservedQuery and ObservedBatch (CASSGO-73)
3535
- Add way to create HostInfo objects for testing purposes (CASSGO-71)
3636
- Add missing Context methods on Query and Batch (CASSGO-81)
37+
- Update example and test code for 2.0 release (CASSGO-80)
3738

3839
### Changed
3940

batch_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ func TestBatch_WithNowInSeconds(t *testing.T) {
9393
t.Fatal(err)
9494
}
9595

96-
b := session.NewBatch(LoggedBatch)
96+
b := session.Batch(LoggedBatch)
9797
b.WithNowInSeconds(0)
9898
b.Query("INSERT INTO batch_now_in_seconds (id, val) VALUES (?, ?) USING TTL 20", 1, "val")
99-
if err := session.ExecuteBatch(b); err != nil {
99+
if err := b.Exec(); err != nil {
100100
t.Fatal(err)
101101
}
102102

@@ -140,10 +140,10 @@ func TestBatch_SetKeyspace(t *testing.T) {
140140
ids := []int{1, 2}
141141
texts := []string{"val1", "val2"}
142142

143-
b := session.NewBatch(LoggedBatch).SetKeyspace("gocql_keyspace_override_test")
143+
b := session.Batch(LoggedBatch).SetKeyspace("gocql_keyspace_override_test")
144144
b.Query("INSERT INTO batch_keyspace(id, value) VALUES (?, ?)", ids[0], texts[0])
145145
b.Query("INSERT INTO batch_keyspace(id, value) VALUES (?, ?)", ids[1], texts[1])
146-
err = session.ExecuteBatch(b)
146+
err = b.Exec()
147147
if err != nil {
148148
t.Fatal(err)
149149
}

cassandra_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3899,15 +3899,15 @@ func TestStmtCacheUsesOverriddenKeyspace(t *testing.T) {
38993899

39003900
// Inserting data via Batch to ensure that batches
39013901
// properly accounts for keyspace overriding
3902-
b1 := session.NewBatch(LoggedBatch)
3902+
b1 := session.Batch(LoggedBatch)
39033903
b1.Query(insertQuery, 1)
3904-
err = session.ExecuteBatch(b1)
3904+
err = b1.Exec()
39053905
require.NoError(t, err)
39063906

3907-
b2 := session.NewBatch(LoggedBatch)
3907+
b2 := session.Batch(LoggedBatch)
39083908
b2.SetKeyspace("gocql_test_stmt_cache")
39093909
b2.Query(insertQuery, 2)
3910-
err = session.ExecuteBatch(b2)
3910+
err = b2.Exec()
39113911
require.NoError(t, err)
39123912

39133913
var scannedID int

example_batch_test.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,46 @@ func Example_batch() {
4949

5050
ctx := context.Background()
5151

52-
b := session.Batch(gocql.UnloggedBatch).WithContext(ctx)
52+
// Example 1: Simple batch using the Query() method - recommended approach
53+
batch := session.Batch(gocql.LoggedBatch)
54+
batch.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 2, "1.2")
55+
batch.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 3, "1.3")
56+
57+
err = batch.ExecContext(ctx)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
62+
// Example 2: Advanced batch usage with Entries for more control
63+
b := session.Batch(gocql.UnloggedBatch)
5364
b.Entries = append(b.Entries, gocql.BatchEntry{
5465
Stmt: "INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)",
55-
Args: []interface{}{1, 2, "1.2"},
66+
Args: []interface{}{1, 4, "1.4"},
5667
Idempotent: true,
5768
})
5869
b.Entries = append(b.Entries, gocql.BatchEntry{
5970
Stmt: "INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)",
60-
Args: []interface{}{1, 3, "1.3"},
71+
Args: []interface{}{1, 5, "1.5"},
6172
Idempotent: true,
6273
})
6374

64-
err = b.Exec()
75+
err = b.ExecContext(ctx)
6576
if err != nil {
6677
log.Fatal(err)
6778
}
6879

69-
err = b.Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 4, "1.4").
70-
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 5, "1.5").
71-
Exec()
80+
// Example 3: Fluent style chaining
81+
err = session.Batch(gocql.LoggedBatch).
82+
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 6, "1.6").
83+
Query("INSERT INTO example.batches (pk, ck, description) VALUES (?, ?, ?)", 1, 7, "1.7").
84+
ExecContext(ctx)
7285
if err != nil {
7386
log.Fatal(err)
7487
}
7588

76-
scanner := session.Query("SELECT pk, ck, description FROM example.batches").Iter().Scanner()
89+
// Verification: Display all inserted data
90+
fmt.Println("All inserted data:")
91+
scanner := session.Query("SELECT pk, ck, description FROM example.batches").IterContext(ctx).Scanner()
7792
for scanner.Next() {
7893
var pk, ck int32
7994
var description string
@@ -83,8 +98,16 @@ func Example_batch() {
8398
}
8499
fmt.Println(pk, ck, description)
85100
}
101+
102+
if err := scanner.Err(); err != nil {
103+
log.Fatal(err)
104+
}
105+
106+
// All inserted data:
86107
// 1 2 1.2
87108
// 1 3 1.3
88109
// 1 4 1.4
89110
// 1 5 1.5
111+
// 1 6 1.6
112+
// 1 7 1.7
90113
}

example_dynamic_columns_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Example_dynamicColumns() {
5656
defer session.Close()
5757

5858
printQuery := func(ctx context.Context, session *gocql.Session, stmt string, values ...interface{}) error {
59-
iter := session.Query(stmt, values...).WithContext(ctx).Iter()
59+
iter := session.Query(stmt, values...).IterContext(ctx)
6060
fmt.Println(stmt)
6161
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ',
6262
0)

example_lwt_batch_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
gocql "github.com/apache/cassandra-gocql-driver/v2"
3333
)
3434

35-
// ExampleSession_MapExecuteBatchCAS demonstrates how to execute a batch lightweight transaction.
36-
func ExampleSession_MapExecuteBatchCAS() {
35+
// ExampleBatch_MapExecCAS demonstrates how to execute a batch lightweight transaction.
36+
func ExampleBatch_MapExecCAS() {
3737
/* The example assumes the following CQL was used to setup the keyspace:
3838
create keyspace example with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
3939
create table example.my_lwt_batch_table(pk text, ck text, version int, value text, PRIMARY KEY(pk, ck));
@@ -50,13 +50,13 @@ func ExampleSession_MapExecuteBatchCAS() {
5050
ctx := context.Background()
5151

5252
err = session.Query("INSERT INTO example.my_lwt_batch_table (pk, ck, version, value) VALUES (?, ?, ?, ?)",
53-
"pk1", "ck1", 1, "a").WithContext(ctx).Exec()
53+
"pk1", "ck1", 1, "a").ExecContext(ctx)
5454
if err != nil {
5555
log.Fatal(err)
5656
}
5757

5858
err = session.Query("INSERT INTO example.my_lwt_batch_table (pk, ck, version, value) VALUES (?, ?, ?, ?)",
59-
"pk1", "ck2", 1, "A").WithContext(ctx).Exec()
59+
"pk1", "ck2", 1, "A").ExecContext(ctx)
6060
if err != nil {
6161
log.Fatal(err)
6262
}
@@ -72,7 +72,7 @@ func ExampleSession_MapExecuteBatchCAS() {
7272
Args: []interface{}{"B", "pk1", "ck2", ck2Version},
7373
})
7474
m := make(map[string]interface{})
75-
applied, iter, err := b.WithContext(ctx).MapExecCAS(m)
75+
applied, iter, err := b.MapExecCASContext(ctx, m)
7676
if err != nil {
7777
log.Fatal(err)
7878
}
@@ -91,7 +91,7 @@ func ExampleSession_MapExecuteBatchCAS() {
9191

9292
printState := func() {
9393
scanner := session.Query("SELECT ck, value FROM example.my_lwt_batch_table WHERE pk = ?", "pk1").
94-
WithContext(ctx).Iter().Scanner()
94+
IterContext(ctx).Scanner()
9595
for scanner.Next() {
9696
var ck, value string
9797
err = scanner.Scan(&ck, &value)

example_lwt_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,37 @@ func ExampleQuery_MapScanCAS() {
5050
ctx := context.Background()
5151

5252
err = session.Query("INSERT INTO example.my_lwt_table (pk, version, value) VALUES (?, ?, ?)",
53-
1, 1, "a").WithContext(ctx).Exec()
53+
1, 1, "a").ExecContext(ctx)
5454
if err != nil {
5555
log.Fatal(err)
5656
}
5757
m := make(map[string]interface{})
5858
applied, err := session.Query("UPDATE example.my_lwt_table SET value = ? WHERE pk = ? IF version = ?",
59-
"b", 1, 0).WithContext(ctx).MapScanCAS(m)
59+
"b", 1, 0).MapScanCASContext(ctx, m)
6060
if err != nil {
6161
log.Fatal(err)
6262
}
6363
fmt.Println(applied, m)
6464

6565
var value string
66-
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).WithContext(ctx).
67-
Scan(&value)
66+
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).
67+
ScanContext(ctx, &value)
6868
if err != nil {
6969
log.Fatal(err)
7070
}
7171
fmt.Println(value)
7272

7373
m = make(map[string]interface{})
7474
applied, err = session.Query("UPDATE example.my_lwt_table SET value = ? WHERE pk = ? IF version = ?",
75-
"b", 1, 1).WithContext(ctx).MapScanCAS(m)
75+
"b", 1, 1).MapScanCASContext(ctx, m)
7676
if err != nil {
7777
log.Fatal(err)
7878
}
7979
fmt.Println(applied, m)
8080

8181
var value2 string
82-
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).WithContext(ctx).
83-
Scan(&value2)
82+
err = session.Query("SELECT value FROM example.my_lwt_table WHERE pk = ?", 1).
83+
ScanContext(ctx, &value2)
8484
if err != nil {
8585
log.Fatal(err)
8686
}

example_marshaler_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,20 @@ func Example_marshalerUnmarshaler() {
9595
patch: 3,
9696
}
9797
err = session.Query("INSERT INTO example.my_marshaler_table (pk, value) VALUES (?, ?)",
98-
1, value).WithContext(ctx).Exec()
98+
1, value).ExecContext(ctx)
9999
if err != nil {
100100
log.Fatal(err)
101101
}
102102
var stringValue string
103-
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").WithContext(ctx).
104-
Scan(&stringValue)
103+
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").
104+
ScanContext(ctx, &stringValue)
105105
if err != nil {
106106
log.Fatal(err)
107107
}
108108
fmt.Println(stringValue)
109109
var unmarshaledValue MyMarshaler
110-
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").WithContext(ctx).
111-
Scan(&unmarshaledValue)
110+
err = session.Query("SELECT value FROM example.my_marshaler_table WHERE pk = 1").
111+
ScanContext(ctx, &unmarshaledValue)
112112
if err != nil {
113113
log.Fatal(err)
114114
}

0 commit comments

Comments
 (0)