Skip to content

Commit fc5acec

Browse files
committed
Add yugabyte-specific test file
1 parent f384118 commit fc5acec

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

yugabyte_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//go:build yugabyte
2+
// +build yugabyte
3+
4+
package gocql
5+
6+
import (
7+
"bytes"
8+
"strconv"
9+
"testing"
10+
)
11+
12+
func TestGetKeyspaceMetadata(t *testing.T) {
13+
session := createSession(t)
14+
defer session.Close()
15+
16+
keyspaceMetadata, err := session.KeyspaceMetadata("gocql_test")
17+
if err != nil {
18+
t.Fatalf("failed to query the keyspace metadata with err: %v", err)
19+
}
20+
if keyspaceMetadata == nil {
21+
t.Fatal("failed to query the keyspace metadata, nil returned")
22+
}
23+
if keyspaceMetadata.Name != "gocql_test" {
24+
t.Errorf("Expected keyspace name to be 'gocql' but was '%s'", keyspaceMetadata.Name)
25+
}
26+
if keyspaceMetadata.StrategyClass != "org.apache.cassandra.locator.SimpleStrategy" {
27+
t.Errorf("Expected replication strategy class to be 'org.apache.cassandra.locator.SimpleStrategy' but was '%s'", keyspaceMetadata.StrategyClass)
28+
}
29+
if keyspaceMetadata.StrategyOptions == nil {
30+
t.Error("Expected replication strategy options map but was nil")
31+
}
32+
rfStr, ok := keyspaceMetadata.StrategyOptions["replication_factor"]
33+
if !ok {
34+
t.Fatalf("Expected strategy option 'replication_factor' but was not found in %v", keyspaceMetadata.StrategyOptions)
35+
}
36+
rfInt, err := strconv.Atoi(rfStr.(string))
37+
if err != nil {
38+
t.Fatalf("Error converting string to int with err: %v", err)
39+
}
40+
if rfInt != *flagRF {
41+
t.Errorf("Expected replication factor to be %d but was %d", *flagRF, rfInt)
42+
}
43+
}
44+
45+
func TestJSONB(t *testing.T) {
46+
session := createSession(t)
47+
defer session.Close()
48+
49+
defer func() {
50+
err := createTable(session, "DROP TABLE IF EXISTS gocql_test.jsonb")
51+
if err != nil {
52+
t.Logf("failed to delete jsonb table: %v", err)
53+
}
54+
}()
55+
56+
if err := createTable(session, "CREATE TABLE gocql_test.jsonb (id int, my_jsonb jsonb, PRIMARY KEY (id))"); err != nil {
57+
t.Fatalf("failed to create table with error '%v'", err)
58+
}
59+
60+
b := session.NewBatch(LoggedBatch)
61+
b.Query("INSERT INTO gocql_test.jsonb(id, my_jsonb) VALUES (?,?)", 1, []byte("true"))
62+
b.Query("INSERT INTO gocql_test.jsonb(id, my_jsonb) VALUES (?,?)", 2, []byte(`{"foo":"bar"}`))
63+
64+
if err := session.ExecuteBatch(b); err != nil {
65+
t.Fatalf("query failed. %v", err)
66+
} else {
67+
if b.Attempts() < 1 {
68+
t.Fatal("expected at least 1 attempt, but got 0")
69+
}
70+
if b.Latency() <= 0 {
71+
t.Fatalf("expected latency to be greater than 0, but got %v instead.", b.Latency())
72+
}
73+
}
74+
75+
var id int
76+
var myJSONB []byte
77+
if err := session.Query("SELECT id, my_jsonb FROM gocql_test.jsonb WHERE id = 1;").Scan(&id, &myJSONB); err != nil {
78+
t.Fatalf("Failed to select with err: %v", err)
79+
} else if id != 1 {
80+
t.Fatalf("Expected id = 1, got %v", id)
81+
} else if !bytes.Equal(myJSONB, []byte("true")) {
82+
t.Fatalf("Expected my_jsonb = true, got %v", string(myJSONB))
83+
}
84+
85+
if rd, err := session.Query("SELECT id, my_jsonb FROM gocql_test.jsonb;").Iter().RowData(); err != nil {
86+
t.Fatalf("Failed to select with err: %v", err)
87+
} else if len(rd.Columns) != 2 || rd.Columns[0] != "id" || rd.Columns[1] != "my_jsonb" {
88+
t.Fatalf("Expected [id, my_jsonb], got %v", rd.Columns)
89+
} else if len(rd.Values) != 2 {
90+
t.Fatalf("Expected 2 values, got %v", rd.Values)
91+
} else if _, ok := rd.Values[0].(*int); !ok {
92+
t.Fatalf("Expected values[0] = *int, got %T", rd.Values[0])
93+
} else if _, ok := rd.Values[1].(*[]byte); !ok {
94+
t.Fatalf(`Expected values[1] = *[]byte, got %T`, rd.Values[1])
95+
}
96+
}

0 commit comments

Comments
 (0)