gocqlxtest: test helpers extracted to separate package
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"github.com/gocql/gocql"
|
||||
"github.com/scylladb/gocqlx"
|
||||
. "github.com/scylladb/gocqlx/gocqlxtest"
|
||||
"github.com/scylladb/gocqlx/qb"
|
||||
)
|
||||
|
||||
@@ -64,10 +65,10 @@ func loadFixtures() []*benchPerson {
|
||||
// BenchmarkE2EGocqlInsert performs standard insert.
|
||||
func BenchmarkE2EGocqlInsert(b *testing.B) {
|
||||
people := loadFixtures()
|
||||
session := createSession(b)
|
||||
session := CreateSession(b)
|
||||
defer session.Close()
|
||||
|
||||
if err := createTable(session, benchPersonSchema); err != nil {
|
||||
if err := ExecStmt(session, benchPersonSchema); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -92,10 +93,10 @@ func BenchmarkE2EGocqlInsert(b *testing.B) {
|
||||
// BenchmarkE2EGocqlInsert performs insert with struct binding.
|
||||
func BenchmarkE2EGocqlxInsert(b *testing.B) {
|
||||
people := loadFixtures()
|
||||
session := createSession(b)
|
||||
session := CreateSession(b)
|
||||
defer session.Close()
|
||||
|
||||
if err := createTable(session, benchPersonSchema); err != nil {
|
||||
if err := ExecStmt(session, benchPersonSchema); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -120,7 +121,7 @@ func BenchmarkE2EGocqlxInsert(b *testing.B) {
|
||||
// BenchmarkE2EGocqlGet performs standard scan.
|
||||
func BenchmarkE2EGocqlGet(b *testing.B) {
|
||||
people := loadFixtures()
|
||||
session := createSession(b)
|
||||
session := CreateSession(b)
|
||||
defer session.Close()
|
||||
|
||||
initTable(b, session, people)
|
||||
@@ -145,7 +146,7 @@ func BenchmarkE2EGocqlGet(b *testing.B) {
|
||||
// BenchmarkE2EGocqlxGet performs get.
|
||||
func BenchmarkE2EGocqlxGet(b *testing.B) {
|
||||
people := loadFixtures()
|
||||
session := createSession(b)
|
||||
session := CreateSession(b)
|
||||
defer session.Close()
|
||||
|
||||
initTable(b, session, people)
|
||||
@@ -170,7 +171,7 @@ func BenchmarkE2EGocqlxGet(b *testing.B) {
|
||||
// BenchmarkE2EGocqlSelect performs standard loop scan.
|
||||
func BenchmarkE2EGocqlSelect(b *testing.B) {
|
||||
people := loadFixtures()
|
||||
session := createSession(b)
|
||||
session := CreateSession(b)
|
||||
defer session.Close()
|
||||
|
||||
initTable(b, session, people)
|
||||
@@ -200,7 +201,7 @@ func BenchmarkE2EGocqlSelect(b *testing.B) {
|
||||
// BenchmarkE2EGocqlSelect performs select.
|
||||
func BenchmarkE2EGocqlxSelect(b *testing.B) {
|
||||
people := loadFixtures()
|
||||
session := createSession(b)
|
||||
session := CreateSession(b)
|
||||
defer session.Close()
|
||||
|
||||
initTable(b, session, people)
|
||||
@@ -220,7 +221,7 @@ func BenchmarkE2EGocqlxSelect(b *testing.B) {
|
||||
}
|
||||
|
||||
func initTable(b *testing.B, session *gocql.Session, people []*benchPerson) {
|
||||
if err := createTable(session, benchPersonSchema); err != nil {
|
||||
if err := ExecStmt(session, benchPersonSchema); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/scylladb/gocqlx"
|
||||
. "github.com/scylladb/gocqlx/gocqlxtest"
|
||||
"github.com/scylladb/gocqlx/qb"
|
||||
)
|
||||
|
||||
@@ -31,10 +32,10 @@ type Person struct {
|
||||
}
|
||||
|
||||
func TestExample(t *testing.T) {
|
||||
session := createSession(t)
|
||||
session := CreateSession(t)
|
||||
defer session.Close()
|
||||
|
||||
if err := createTable(session, personSchema); err != nil {
|
||||
if err := ExecStmt(session, personSchema); err != nil {
|
||||
t.Fatal("create table:", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Use of this source code is governed by a ALv2-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build all integration
|
||||
|
||||
package gocqlx_test
|
||||
package gocqlxtest
|
||||
|
||||
import (
|
||||
"flag"
|
||||
@@ -38,13 +36,10 @@ func init() {
|
||||
|
||||
var initOnce sync.Once
|
||||
|
||||
func createTable(s *gocql.Session, table string) error {
|
||||
if err := s.Query(table).RetryPolicy(nil).Exec(); err != nil {
|
||||
log.Printf("error creating table table=%q err=%v\n", table, err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
// CreateSession creates a new gocql session from flags.
|
||||
func CreateSession(tb testing.TB) *gocql.Session {
|
||||
cluster := createCluster()
|
||||
return createSessionFromCluster(cluster, tb)
|
||||
}
|
||||
|
||||
func createCluster() *gocql.ClusterConfig {
|
||||
@@ -69,32 +64,6 @@ func createCluster() *gocql.ClusterConfig {
|
||||
return cluster
|
||||
}
|
||||
|
||||
func createKeyspace(tb testing.TB, cluster *gocql.ClusterConfig, keyspace string) {
|
||||
c := *cluster
|
||||
c.Keyspace = "system"
|
||||
c.Timeout = 30 * time.Second
|
||||
session, err := c.CreateSession()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
err = createTable(session, `DROP KEYSPACE IF EXISTS `+keyspace)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to drop keyspace: %v", err))
|
||||
}
|
||||
|
||||
err = createTable(session, fmt.Sprintf(`CREATE KEYSPACE %s
|
||||
WITH replication = {
|
||||
'class' : 'SimpleStrategy',
|
||||
'replication_factor' : %d
|
||||
}`, keyspace, *flagRF))
|
||||
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to create keyspace: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
func createSessionFromCluster(cluster *gocql.ClusterConfig, tb testing.TB) *gocql.Session {
|
||||
// Drop and re-create the keyspace once. Different tests should use their own
|
||||
// individual tables, but can assume that the table does not exist before.
|
||||
@@ -105,13 +74,41 @@ func createSessionFromCluster(cluster *gocql.ClusterConfig, tb testing.TB) *gocq
|
||||
cluster.Keyspace = "gocqlx_test"
|
||||
session, err := cluster.CreateSession()
|
||||
if err != nil {
|
||||
tb.Fatal("createSession:", err)
|
||||
tb.Fatal("CreateSession:", err)
|
||||
}
|
||||
|
||||
return session
|
||||
}
|
||||
|
||||
func createSession(tb testing.TB) *gocql.Session {
|
||||
cluster := createCluster()
|
||||
return createSessionFromCluster(cluster, tb)
|
||||
func createKeyspace(tb testing.TB, cluster *gocql.ClusterConfig, keyspace string) {
|
||||
c := *cluster
|
||||
c.Keyspace = "system"
|
||||
c.Timeout = 30 * time.Second
|
||||
session, err := c.CreateSession()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
err = ExecStmt(session, `DROP KEYSPACE IF EXISTS `+keyspace)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to drop keyspace: %v", err))
|
||||
}
|
||||
|
||||
err = ExecStmt(session, fmt.Sprintf(`CREATE KEYSPACE %s
|
||||
WITH replication = {
|
||||
'class' : 'SimpleStrategy',
|
||||
'replication_factor' : %d
|
||||
}`, keyspace, *flagRF))
|
||||
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("unable to create keyspace: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// ExecStmt executes a statement string.
|
||||
func ExecStmt(s *gocql.Session, stmt string) error {
|
||||
q := s.Query(stmt).RetryPolicy(nil)
|
||||
defer q.Release()
|
||||
return q.Exec()
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
"github.com/gocql/gocql"
|
||||
"github.com/scylladb/gocqlx"
|
||||
|
||||
. "github.com/scylladb/gocqlx/gocqlxtest"
|
||||
"gopkg.in/inf.v0"
|
||||
)
|
||||
|
||||
@@ -35,9 +35,9 @@ func (n *FullName) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
|
||||
}
|
||||
|
||||
func TestStruct(t *testing.T) {
|
||||
session := createSession(t)
|
||||
session := CreateSession(t)
|
||||
defer session.Close()
|
||||
if err := createTable(session, `CREATE TABLE gocqlx_test.struct_table (
|
||||
if err := ExecStmt(session, `CREATE TABLE gocqlx_test.struct_table (
|
||||
testuuid timeuuid PRIMARY KEY,
|
||||
testtimestamp timestamp,
|
||||
testvarchar varchar,
|
||||
@@ -165,9 +165,9 @@ func TestStruct(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestScannable(t *testing.T) {
|
||||
session := createSession(t)
|
||||
session := CreateSession(t)
|
||||
defer session.Close()
|
||||
if err := createTable(session, `CREATE TABLE gocqlx_test.scannable_table (testfullname text PRIMARY KEY)`); err != nil {
|
||||
if err := ExecStmt(session, `CREATE TABLE gocqlx_test.scannable_table (testfullname text PRIMARY KEY)`); err != nil {
|
||||
t.Fatal("create table:", err)
|
||||
}
|
||||
m := FullName{"John", "Doe"}
|
||||
@@ -219,9 +219,9 @@ func TestScannable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnsafe(t *testing.T) {
|
||||
session := createSession(t)
|
||||
session := CreateSession(t)
|
||||
defer session.Close()
|
||||
if err := createTable(session, `CREATE TABLE gocqlx_test.unsafe_table (testtext text PRIMARY KEY, testtextunbound text)`); err != nil {
|
||||
if err := ExecStmt(session, `CREATE TABLE gocqlx_test.unsafe_table (testtext text PRIMARY KEY, testtextunbound text)`); err != nil {
|
||||
t.Fatal("create table:", err)
|
||||
}
|
||||
if err := session.Query(`INSERT INTO unsafe_table (testtext, testtextunbound) values (?, ?)`, "test", "test").Exec(); err != nil {
|
||||
@@ -278,9 +278,9 @@ func TestUnsafe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNotFound(t *testing.T) {
|
||||
session := createSession(t)
|
||||
session := CreateSession(t)
|
||||
defer session.Close()
|
||||
if err := createTable(session, `CREATE TABLE gocqlx_test.not_found_table (testtext text PRIMARY KEY)`); err != nil {
|
||||
if err := ExecStmt(session, `CREATE TABLE gocqlx_test.not_found_table (testtext text PRIMARY KEY)`); err != nil {
|
||||
t.Fatal("create table:", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user