From 9cfd46fcddf289d69d4c9b41cfcfd3832d821e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Matczuk?= Date: Thu, 24 May 2018 10:48:37 +0200 Subject: [PATCH] gocqlxtest: test helpers extracted to separate package --- benchmark_test.go | 19 +++--- example_test.go | 5 +- common_test.go => gocqlxtest/gocqlxtest.go | 77 +++++++++++----------- iterx_test.go | 18 ++--- 4 files changed, 59 insertions(+), 60 deletions(-) rename common_test.go => gocqlxtest/gocqlxtest.go (84%) diff --git a/benchmark_test.go b/benchmark_test.go index 1a1de7f..3b75f0f 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -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) } diff --git a/example_test.go b/example_test.go index ab4df81..5a1d5dc 100644 --- a/example_test.go +++ b/example_test.go @@ -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) } diff --git a/common_test.go b/gocqlxtest/gocqlxtest.go similarity index 84% rename from common_test.go rename to gocqlxtest/gocqlxtest.go index 01044a3..4950946 100644 --- a/common_test.go +++ b/gocqlxtest/gocqlxtest.go @@ -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() } diff --git a/iterx_test.go b/iterx_test.go index 2e52a9c..8298e65 100644 --- a/iterx_test.go +++ b/iterx_test.go @@ -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) }