Skip to content

Commit

Permalink
Merge pull request #461 - blocking mocks for responses and add v2.4.x…
Browse files Browse the repository at this point in the history
… support

Blocking mocks for responses and add v2.4.x support
  • Loading branch information
gabor-boros authored Dec 20, 2019
2 parents da19f5e + 67dd3b5 commit 6bd1634
Show file tree
Hide file tree
Showing 24 changed files with 929 additions and 149 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: go

go:
- 1.7.x
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x
- master
- 1.12.x
- 1.13.x

cache: apt

Expand Down
61 changes: 49 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

![RethinkDB-go Logo](https://raw.github.com/wiki/rethinkdb/rethinkdb-go/gopher-and-thinker-s.png "Golang Gopher and RethinkDB Thinker")

Current version: v5.0.1 (RethinkDB v2.3)
Current version: v5.1.0 (RethinkDB v2.4)

Please note that this version of the driver only supports versions of RethinkDB using the v0.4 protocol (any versions of the driver older than RethinkDB 2.0 will not work).

Expand Down Expand Up @@ -456,6 +456,43 @@ func TestSomething(t *testing.T) {
}
```

If you want the cursor to block on some of the response values, you can pass in
a value of type `chan interface{}` and the cursor will block until a value is
available to read on the channel. Or you can pass in a function with signature
`func() interface{}`: the cursor will call the function (which may block). Here
is the example above adapted to use a channel.

```go
func TestSomething(t *testing.T) {
mock := r.NewMock()
ch := make(chan []interface{})
mock.On(r.Table("people")).Return(ch, nil)
go func() {
ch <- []interface{}{
map[string]interface{}{"id": 1, "name": "John Smith"},
map[string]interface{}{"id": 2, "name": "Jane Smith"},
}
ch <- []interface{}{map[string]interface{}{"id": 3, "name": "Jack Smith"}}
close(ch)
}()
cursor, err := r.Table("people").Run(mock)
if err != nil {
t.Errorf("err is: %v", err)
}

var rows []interface{}
err = cursor.All(&rows)
if err != nil {
t.Errorf("err is: %v", err)
}

// Test result of rows

mock.AssertExpectations(t)
}

```

The mocking implementation is based on amazing https://github.com/stretchr/testify library, thanks to @stretchr for their awesome work!

## Benchmarks
Expand All @@ -464,17 +501,17 @@ Everyone wants their project's benchmarks to be speedy. And while we know that R

Thanks to @jaredfolkins for the contribution.

| Type | Value |
| --- | --- |
| **Model Name** | MacBook Pro |
| **Model Identifier** | MacBookPro11,3 |
| **Processor Name** | Intel Core i7 |
| **Processor Speed** | 2.3 GHz |
| **Number of Processors** | 1 |
| **Total Number of Cores** | 4 |
| **L2 Cache (per Core)** | 256 KB |
| **L3 Cache** | 6 MB |
| **Memory** | 16 GB |
| Type | Value |
| ------------------------- | -------------- |
| **Model Name** | MacBook Pro |
| **Model Identifier** | MacBookPro11,3 |
| **Processor Name** | Intel Core i7 |
| **Processor Speed** | 2.3 GHz |
| **Number of Processors** | 1 |
| **Total Number of Cores** | 4 |
| **L2 Cache (per Core)** | 256 KB |
| **L3 Cache** | 6 MB |
| **Memory** | 16 GB |

```bash
BenchmarkBatch200RandomWrites 20 557227775 ns/op
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func (c *Connection) processResponse(ctx context.Context, q Query, response *Res
case p.Response_WAIT_COMPLETE:
return c.processWaitResponse(response)
default:
return nil, nil, RQLDriverError{rqlError("Unexpected response type: %v")}
return nil, nil, RQLDriverError{rqlError(fmt.Sprintf("Unexpected response type: %v", response.Type.String()))}
}
}

Expand Down
2 changes: 0 additions & 2 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (c *Cursor) nextLocked(dest interface{}, progressCursor bool) (bool, error)
if progressCursor {
c.buffer = c.buffer[1:]
}

err := encoding.Decode(dest, data)
if err != nil {
return false, err
Expand Down Expand Up @@ -494,7 +493,6 @@ func (c *Cursor) Listen(channel interface{}) {
if !c.Next(elemp.Interface()) {
break
}

channelv.Send(elemp.Elem())
}

Expand Down
34 changes: 34 additions & 0 deletions cursor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package rethinkdb

import (
test "gopkg.in/check.v1"
"gopkg.in/rethinkdb/rethinkdb-go.v5/internal/integration/tests"
)

type CursorSuite struct{}

var _ = test.Suite(&CursorSuite{})

func (s *CursorSuite) TestCursor_One_Ok(c *test.C) {
data := map[string]interface{}{
"A": 1,
"B": true,
}

mock := NewMock()
ch := make(chan []interface{})
mock.On(DB("test").Table("test")).Return(ch, nil)
go func() {
ch <- []interface{}{data}
close(ch)
}()
res, err := DB("test").Table("test").Run(mock)
c.Assert(err, test.IsNil)

var response interface{}
err = res.One(&response)

c.Assert(err, test.IsNil)
c.Assert(response, tests.JsonEquals, data)
mock.AssertExpectations(c)
}
4 changes: 2 additions & 2 deletions encoding/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func TestDecodeUnmarshalerPointer(t *testing.T) {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
t.Errorf("got %+v, want %+v", out, want)
}
}

Expand Down Expand Up @@ -587,7 +587,7 @@ func TestDecodeCustomTypeEncodingPointer(t *testing.T) {
t.Errorf("got error %v, expected nil", err)
}
if !jsonEqual(out, want) {
t.Errorf("got %q, want %q", out, want)
t.Errorf("got %+v, want %+v", out, want)
}
}

Expand Down
7 changes: 6 additions & 1 deletion internal/integration/reql_tests/gorethink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ package reql_tests
import (
"flag"
"os"
"runtime"

r "gopkg.in/rethinkdb/rethinkdb-go.v5"
)

var url string

func init() {
flag.Parse()
// Fixing test.testlogfile parsing error on Go 1.13+.
if runtime.Version() < "go1.13" {
flag.Parse()
}

r.SetVerbose(true)

// If the test is being run by wercker look for the rethink url
Expand Down
165 changes: 165 additions & 0 deletions internal/integration/reql_tests/reql_bitwise_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package reql_tests

import (
"github.com/stretchr/testify/suite"
r "gopkg.in/rethinkdb/rethinkdb-go.v5"
"testing"
)

// Test edge cases of bitwise operations
func TestBitwiseSuite(t *testing.T) {
suite.Run(t, new(BitwiseSuite))
}

type BitwiseSuite struct {
suite.Suite

session *r.Session
}

func (suite *BitwiseSuite) SetupTest() {
suite.T().Log("Setting up BitwiseSuite")

session, err := r.Connect(r.ConnectOpts{
Address: url,
})
suite.Require().NoError(err, "Error returned when connecting to server")
suite.session = session
}

func (suite *BitwiseSuite) TearDownSuite() {
suite.T().Log("Tearing down BitwiseSuite")

if suite.session != nil {
suite.session.Close()
}
}

func (suite *BitwiseSuite) TestCases() {
suite.T().Log("Running BitwiseSuite: Test edge cases of bitwise operations")

runOpts := r.RunOpts{
GeometryFormat: "raw",
GroupFormat: "map",
}

{
var q = r.BitAnd(3, 5)
var expected_ = 3 & 5

suite.T().Logf("About to run line #1: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #1")
}

{
var q = r.Expr(3).BitAnd(5)
var expected_ = 3 & 5

suite.T().Logf("About to run line #2: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #2")
}

{
var q = r.BitOr(3, 5)
var expected_ = 3 | 5

suite.T().Logf("About to run line #3: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #3")
}

{
var q = r.Expr(3).BitOr(5)
var expected_ = 3 | 5

suite.T().Logf("About to run line #4: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #4")
}

{
var q = r.BitXor(3, 5)
var expected_ = 3 ^ 5

suite.T().Logf("About to run line #5: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #5")
}

{
var q = r.Expr(3).BitXor(5)
var expected_ = 3 ^ 5

suite.T().Logf("About to run line #6: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #6")
}

{
var q = r.BitNot(3)
var expected_ = ^3

suite.T().Logf("About to run line #7: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #7")
}

{
var q = r.Expr(3).BitNot()
var expected_ = ^3

suite.T().Logf("About to run line #8: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #8")
}

{
var q = r.BitSal(3, 5)
var expected_ = 3 << 5

suite.T().Logf("About to run line #9: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #9")
}

{
var q = r.Expr(3).BitSal(5)
var expected_ = 3 << 5

suite.T().Logf("About to run line #10: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #10")
}

{
var q = r.BitSar(3, 5)
var expected_ = 3 >> 5

suite.T().Logf("About to run line #11: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #11")
}

{
var q = r.Expr(3).BitSar(5)
var expected_ = 3 >> 5

suite.T().Logf("About to run line #12: %v", q)

runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
suite.T().Log("Finished running line #12")
}
}
4 changes: 2 additions & 2 deletions internal/integration/reql_tests/reql_meta_table_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/integration/reql_tests/reql_selection_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6bd1634

Please sign in to comment.