Skip to content

Commit

Permalink
Some refactoring of test setup/teardown methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dancannon committed May 17, 2015
1 parent 7cc01b2 commit 51a9644
Show file tree
Hide file tree
Showing 24 changed files with 481 additions and 496 deletions.
32 changes: 14 additions & 18 deletions benchmarks_test.go
Expand Up @@ -8,10 +8,6 @@ import (
"time"
)

var bSess *Session
var bDbName string
var bTableName string

func BenchmarkBatch200RandomWrites(b *testing.B) {

var term Term
Expand All @@ -28,10 +24,10 @@ func BenchmarkBatch200RandomWrites(b *testing.B) {
}

// Insert the new item into the database
term = Table(bTableName).Insert(data)
term = DB("benchmarks").Table("benchmarks").Insert(data)

// Insert the new item into the database
_, err := term.RunWrite(bSess, RunOpts{
_, err := term.RunWrite(session, RunOpts{
MinBatchRows: 200,
MaxBatchRows: 200,
})
Expand Down Expand Up @@ -61,10 +57,10 @@ func BenchmarkBatch200RandomWritesParallel10(b *testing.B) {
}

// Insert the new item into the database
term = Table(bTableName).Insert(data)
term = DB("benchmarks").Table("benchmarks").Insert(data)

// Insert the new item into the database
_, err := term.RunWrite(bSess, RunOpts{
_, err := term.RunWrite(session, RunOpts{
MinBatchRows: 200,
MaxBatchRows: 200,
})
Expand Down Expand Up @@ -98,10 +94,10 @@ func BenchmarkBatch200SoftRandomWritesParallel10(b *testing.B) {
}

// Insert the new item into the database
term = Table(bTableName).Insert(data, opts)
term = DB("benchmarks").Table("benchmarks").Insert(data, opts)

// Insert the new item into the database
_, err := term.RunWrite(bSess, RunOpts{
_, err := term.RunWrite(session, RunOpts{
MinBatchRows: 200,
MaxBatchRows: 200,
})
Expand All @@ -121,7 +117,7 @@ func BenchmarkRandomWrites(b *testing.B) {
"customer_id": strconv.FormatInt(r.Int63(), 10),
}
// Insert the new item into the database
_, err := Table(bTableName).Insert(data).RunWrite(bSess)
_, err := DB("benchmarks").Table("benchmarks").Insert(data).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
}
Expand All @@ -141,7 +137,7 @@ func BenchmarkRandomWritesParallel10(b *testing.B) {
"customer_id": strconv.FormatInt(r.Int63(), 10),
}
// Insert the new item into the database
_, err := Table(bTableName).Insert(data).RunWrite(bSess)
_, err := DB("benchmarks").Table("benchmarks").Insert(data).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
}
Expand All @@ -158,7 +154,7 @@ func BenchmarkRandomSoftWrites(b *testing.B) {
}
// Insert the new item into the database
opts := InsertOpts{Durability: "soft"}
_, err := Table(bTableName).Insert(data, opts).RunWrite(bSess)
_, err := DB("benchmarks").Table("benchmarks").Insert(data, opts).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
}
Expand All @@ -180,7 +176,7 @@ func BenchmarkRandomSoftWritesParallel10(b *testing.B) {

// Insert the new item into the database
opts := InsertOpts{Durability: "soft"}
_, err := Table(bTableName).Insert(data, opts).RunWrite(bSess)
_, err := DB("benchmarks").Table("benchmarks").Insert(data, opts).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
}
Expand All @@ -199,7 +195,7 @@ func BenchmarkSequentialWrites(b *testing.B) {
}

// Insert the new item into the database
_, err := Table(bTableName).Insert(data).RunWrite(bSess)
_, err := DB("benchmarks").Table("benchmarks").Insert(data).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
return
Expand All @@ -226,7 +222,7 @@ func BenchmarkSequentialWritesParallel10(b *testing.B) {
}

// Insert the new item into the database
_, err := Table(bTableName).Insert(data).RunWrite(bSess)
_, err := DB("benchmarks").Table("benchmarks").Insert(data).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
return
Expand All @@ -248,7 +244,7 @@ func BenchmarkSequentialSoftWrites(b *testing.B) {
}

// Insert the new item into the database
_, err := Table(bTableName).Insert(data, opts).RunWrite(bSess)
_, err := Table("benchmarks").Insert(data, opts).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
return
Expand Down Expand Up @@ -277,7 +273,7 @@ func BenchmarkSequentialSoftWritesParallel10(b *testing.B) {
opts := InsertOpts{Durability: "soft"}

// Insert the new item into the database
_, err := Table(bTableName).Insert(data, opts).RunWrite(bSess)
_, err := Table("benchmarks").Insert(data, opts).RunWrite(session)
if err != nil {
b.Errorf("insert failed [%s] ", err)
return
Expand Down
62 changes: 31 additions & 31 deletions cursor_test.go
Expand Up @@ -18,7 +18,7 @@ type attr struct {
}

func (s *RethinkSuite) TestCursorLiteral(c *test.C) {
res, err := Expr(5).Run(sess)
res, err := Expr(5).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -29,7 +29,7 @@ func (s *RethinkSuite) TestCursorLiteral(c *test.C) {
}

func (s *RethinkSuite) TestCursorSlice(c *test.C) {
res, err := Expr([]interface{}{1, 2, 3, 4, 5}).Run(sess)
res, err := Expr([]interface{}{1, 2, 3, 4, 5}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -45,7 +45,7 @@ func (s *RethinkSuite) TestCursorPartiallyNilSlice(c *test.C) {
map[string]interface{}{"num": 1},
nil,
},
}).Run(sess)
}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -64,7 +64,7 @@ func (s *RethinkSuite) TestCursorMap(c *test.C) {
res, err := Expr(map[string]interface{}{
"id": 2,
"name": "Object 1",
}).Run(sess)
}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -81,7 +81,7 @@ func (s *RethinkSuite) TestCursorMapIntoInterface(c *test.C) {
res, err := Expr(map[string]interface{}{
"id": 2,
"name": "Object 1",
}).Run(sess)
}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -102,7 +102,7 @@ func (s *RethinkSuite) TestCursorMapNested(c *test.C) {
"name": "attr 1",
"value": "value 1",
}},
}).Run(sess)
}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -127,7 +127,7 @@ func (s *RethinkSuite) TestCursorStruct(c *test.C) {
"Name": "attr 1",
"Value": "value 1",
}},
}).Run(sess)
}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -152,7 +152,7 @@ func (s *RethinkSuite) TestCursorStructPseudoTypes(c *test.C) {
"T": time.Unix(t.Unix(), 0).In(time.UTC),
"Z": zeroTime,
"B": []byte("hello"),
}).Run(sess)
}).Run(session)
c.Assert(err, test.IsNil)

var response PseudoTypes
Expand All @@ -166,7 +166,7 @@ func (s *RethinkSuite) TestCursorStructPseudoTypes(c *test.C) {
}

func (s *RethinkSuite) TestCursorAtomString(c *test.C) {
res, err := Expr("a").Run(sess)
res, err := Expr("a").Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -177,7 +177,7 @@ func (s *RethinkSuite) TestCursorAtomString(c *test.C) {
}

func (s *RethinkSuite) TestCursorAtomArray(c *test.C) {
res, err := Expr([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}).Run(sess)
res, err := Expr([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Type(), test.Equals, "Cursor")

Expand All @@ -188,44 +188,44 @@ func (s *RethinkSuite) TestCursorAtomArray(c *test.C) {
}

func (s *RethinkSuite) TestEmptyResults(c *test.C) {
DBCreate("test").Exec(sess)
DB("test").TableCreate("test").Exec(sess)
res, err := DB("test").Table("test").Get("missing value").Run(sess)
DBCreate("test").Exec(session)
DB("test").TableCreate("test").Exec(session)
res, err := DB("test").Table("test").Get("missing value").Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.IsNil(), test.Equals, true)

res, err = DB("test").Table("test").Get("missing value").Run(sess)
res, err = DB("test").Table("test").Get("missing value").Run(session)
c.Assert(err, test.IsNil)
var response interface{}
err = res.One(&response)
c.Assert(err, test.Equals, ErrEmptyResult)
c.Assert(res.IsNil(), test.Equals, true)

res, err = Expr(nil).Run(sess)
res, err = Expr(nil).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.IsNil(), test.Equals, true)

res, err = DB("test").Table("test").Get("missing value").Run(sess)
res, err = DB("test").Table("test").Get("missing value").Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.IsNil(), test.Equals, true)

res, err = DB("test").Table("test").GetAll("missing value", "another missing value").Run(sess)
res, err = DB("test").Table("test").GetAll("missing value", "another missing value").Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.Next(&response), test.Equals, false)

var obj object
obj.Name = "missing value"
res, err = DB("test").Table("test").Filter(obj).Run(sess)
res, err = DB("test").Table("test").Filter(obj).Run(session)
c.Assert(err, test.IsNil)
c.Assert(res.IsNil(), test.Equals, true)
}

func (s *RethinkSuite) TestCursorAll(c *test.C) {
// Ensure table + database exist
DBCreate("test").Exec(sess)
DB("test").TableDrop("Table3").Exec(sess)
DB("test").TableCreate("Table3").Exec(sess)
DB("test").Table("Table3").IndexCreate("num").Exec(sess)
DBCreate("test").Exec(session)
DB("test").TableDrop("Table3").Exec(session)
DB("test").TableCreate("Table3").Exec(session)
DB("test").Table("Table3").IndexCreate("num").Exec(session)

// Insert rows
DB("test").Table("Table3").Insert([]interface{}{
Expand All @@ -245,11 +245,11 @@ func (s *RethinkSuite) TestCursorAll(c *test.C) {
"Value": "value 1",
}},
},
}).Exec(sess)
}).Exec(session)

// Test query
query := DB("test").Table("Table3").OrderBy("id")
res, err := query.Run(sess)
res, err := query.Run(session)
c.Assert(err, test.IsNil)

var response []object
Expand Down Expand Up @@ -278,10 +278,10 @@ func (s *RethinkSuite) TestCursorAll(c *test.C) {

func (s *RethinkSuite) TestCursorListen(c *test.C) {
// Ensure table + database exist
DBCreate("test").Exec(sess)
DB("test").TableDrop("Table3").Exec(sess)
DB("test").TableCreate("Table3").Exec(sess)
DB("test").Table("Table3").IndexCreate("num").Exec(sess)
DBCreate("test").Exec(session)
DB("test").TableDrop("Table3").Exec(session)
DB("test").TableCreate("Table3").Exec(session)
DB("test").Table("Table3").IndexCreate("num").Exec(session)

// Insert rows
DB("test").Table("Table3").Insert([]interface{}{
Expand All @@ -301,11 +301,11 @@ func (s *RethinkSuite) TestCursorListen(c *test.C) {
"Value": "value 1",
}},
},
}).Exec(sess)
}).Exec(session)

// Test query
query := DB("test").Table("Table3").OrderBy("id")
res, err := query.Run(sess)
res, err := query.Run(session)
c.Assert(err, test.IsNil)

ch := make(chan object)
Expand Down Expand Up @@ -356,7 +356,7 @@ func (s *RethinkSuite) TestCursorReuseResult(c *test.C) {
"B": 1,
},
})
res, err := query.Run(sess)
res, err := query.Run(session)
c.Assert(err, test.IsNil)

var i int
Expand Down
2 changes: 1 addition & 1 deletion example_query_db_test.go
Expand Up @@ -2,5 +2,5 @@ package gorethink

func ExampleDBCreate() {
// Setup database
DBCreate("test").Run(sess)
DBCreate("test").Run(session)
}
20 changes: 10 additions & 10 deletions example_query_select_test.go
Expand Up @@ -13,12 +13,12 @@ func ExampleTerm_Get() {
}

// Setup table
DB("test").TableDrop("table").Run(sess)
DB("test").TableCreate("table").Run(sess)
DB("test").Table("table").Insert(Person{"1", "John", "Smith", "M"}).Run(sess)
DB("test").TableDrop("table").Run(session)
DB("test").TableCreate("table").Run(session)
DB("test").Table("table").Insert(Person{"1", "John", "Smith", "M"}).Run(session)

// Fetch the row from the database
res, err := DB("test").Table("table").Get("1").Run(sess)
res, err := DB("test").Table("table").Get("1").Run(session)
if err != nil {
log.Fatalf("Error finding person: %s", err)
}
Expand Down Expand Up @@ -48,16 +48,16 @@ func ExampleTerm_GetAll_compound() {
}

// Setup table
DB("test").TableDrop("table").Run(sess)
DB("test").TableCreate("table").Run(sess)
DB("test").Table("table").Insert(Person{"1", "John", "Smith", "M"}).Run(sess)
DB("test").TableDrop("table").Run(session)
DB("test").TableCreate("table").Run(session)
DB("test").Table("table").Insert(Person{"1", "John", "Smith", "M"}).Run(session)
DB("test").Table("table").IndexCreateFunc("full_name", func(row Term) interface{} {
return []interface{}{row.Field("first_name"), row.Field("last_name")}
}).Run(sess)
DB("test").Table("table").IndexWait().Run(sess)
}).Run(session)
DB("test").Table("table").IndexWait().Run(session)

// Fetch the row from the database
res, err := DB("test").Table("table").GetAllByIndex("full_name", []interface{}{"John", "Smith"}).Run(sess)
res, err := DB("test").Table("table").GetAllByIndex("full_name", []interface{}{"John", "Smith"}).Run(session)
if err != nil {
log.Fatalf("Error finding person: %s", err)
}
Expand Down

0 comments on commit 51a9644

Please sign in to comment.