Skip to content

Commit 6bd1634

Browse files
authored
Merge pull request #461 - blocking mocks for responses and add v2.4.x support
Blocking mocks for responses and add v2.4.x support
2 parents da19f5e + 67dd3b5 commit 6bd1634

24 files changed

+929
-149
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
language: go
22

33
go:
4-
- 1.7.x
5-
- 1.8.x
64
- 1.9.x
75
- 1.10.x
86
- 1.11.x
9-
- master
7+
- 1.12.x
8+
- 1.13.x
109

1110
cache: apt
1211

README.md

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

11-
Current version: v5.0.1 (RethinkDB v2.3)
11+
Current version: v5.1.0 (RethinkDB v2.4)
1212

1313
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).
1414

@@ -456,6 +456,43 @@ func TestSomething(t *testing.T) {
456456
}
457457
```
458458

459+
If you want the cursor to block on some of the response values, you can pass in
460+
a value of type `chan interface{}` and the cursor will block until a value is
461+
available to read on the channel. Or you can pass in a function with signature
462+
`func() interface{}`: the cursor will call the function (which may block). Here
463+
is the example above adapted to use a channel.
464+
465+
```go
466+
func TestSomething(t *testing.T) {
467+
mock := r.NewMock()
468+
ch := make(chan []interface{})
469+
mock.On(r.Table("people")).Return(ch, nil)
470+
go func() {
471+
ch <- []interface{}{
472+
map[string]interface{}{"id": 1, "name": "John Smith"},
473+
map[string]interface{}{"id": 2, "name": "Jane Smith"},
474+
}
475+
ch <- []interface{}{map[string]interface{}{"id": 3, "name": "Jack Smith"}}
476+
close(ch)
477+
}()
478+
cursor, err := r.Table("people").Run(mock)
479+
if err != nil {
480+
t.Errorf("err is: %v", err)
481+
}
482+
483+
var rows []interface{}
484+
err = cursor.All(&rows)
485+
if err != nil {
486+
t.Errorf("err is: %v", err)
487+
}
488+
489+
// Test result of rows
490+
491+
mock.AssertExpectations(t)
492+
}
493+
494+
```
495+
459496
The mocking implementation is based on amazing https://github.com/stretchr/testify library, thanks to @stretchr for their awesome work!
460497

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

465502
Thanks to @jaredfolkins for the contribution.
466503

467-
| Type | Value |
468-
| --- | --- |
469-
| **Model Name** | MacBook Pro |
470-
| **Model Identifier** | MacBookPro11,3 |
471-
| **Processor Name** | Intel Core i7 |
472-
| **Processor Speed** | 2.3 GHz |
473-
| **Number of Processors** | 1 |
474-
| **Total Number of Cores** | 4 |
475-
| **L2 Cache (per Core)** | 256 KB |
476-
| **L3 Cache** | 6 MB |
477-
| **Memory** | 16 GB |
504+
| Type | Value |
505+
| ------------------------- | -------------- |
506+
| **Model Name** | MacBook Pro |
507+
| **Model Identifier** | MacBookPro11,3 |
508+
| **Processor Name** | Intel Core i7 |
509+
| **Processor Speed** | 2.3 GHz |
510+
| **Number of Processors** | 1 |
511+
| **Total Number of Cores** | 4 |
512+
| **L2 Cache (per Core)** | 256 KB |
513+
| **L3 Cache** | 6 MB |
514+
| **Memory** | 16 GB |
478515

479516
```bash
480517
BenchmarkBatch200RandomWrites 20 557227775 ns/op

connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func (c *Connection) processResponse(ctx context.Context, q Query, response *Res
464464
case p.Response_WAIT_COMPLETE:
465465
return c.processWaitResponse(response)
466466
default:
467-
return nil, nil, RQLDriverError{rqlError("Unexpected response type: %v")}
467+
return nil, nil, RQLDriverError{rqlError(fmt.Sprintf("Unexpected response type: %v", response.Type.String()))}
468468
}
469469
}
470470

cursor.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ func (c *Cursor) nextLocked(dest interface{}, progressCursor bool) (bool, error)
227227
if progressCursor {
228228
c.buffer = c.buffer[1:]
229229
}
230-
231230
err := encoding.Decode(dest, data)
232231
if err != nil {
233232
return false, err
@@ -494,7 +493,6 @@ func (c *Cursor) Listen(channel interface{}) {
494493
if !c.Next(elemp.Interface()) {
495494
break
496495
}
497-
498496
channelv.Send(elemp.Elem())
499497
}
500498

cursor_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package rethinkdb
2+
3+
import (
4+
test "gopkg.in/check.v1"
5+
"gopkg.in/rethinkdb/rethinkdb-go.v5/internal/integration/tests"
6+
)
7+
8+
type CursorSuite struct{}
9+
10+
var _ = test.Suite(&CursorSuite{})
11+
12+
func (s *CursorSuite) TestCursor_One_Ok(c *test.C) {
13+
data := map[string]interface{}{
14+
"A": 1,
15+
"B": true,
16+
}
17+
18+
mock := NewMock()
19+
ch := make(chan []interface{})
20+
mock.On(DB("test").Table("test")).Return(ch, nil)
21+
go func() {
22+
ch <- []interface{}{data}
23+
close(ch)
24+
}()
25+
res, err := DB("test").Table("test").Run(mock)
26+
c.Assert(err, test.IsNil)
27+
28+
var response interface{}
29+
err = res.One(&response)
30+
31+
c.Assert(err, test.IsNil)
32+
c.Assert(response, tests.JsonEquals, data)
33+
mock.AssertExpectations(c)
34+
}

encoding/decoder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ func TestDecodeUnmarshalerPointer(t *testing.T) {
433433
t.Errorf("got error %v, expected nil", err)
434434
}
435435
if !jsonEqual(out, want) {
436-
t.Errorf("got %q, want %q", out, want)
436+
t.Errorf("got %+v, want %+v", out, want)
437437
}
438438
}
439439

@@ -587,7 +587,7 @@ func TestDecodeCustomTypeEncodingPointer(t *testing.T) {
587587
t.Errorf("got error %v, expected nil", err)
588588
}
589589
if !jsonEqual(out, want) {
590-
t.Errorf("got %q, want %q", out, want)
590+
t.Errorf("got %+v, want %+v", out, want)
591591
}
592592
}
593593

internal/integration/reql_tests/gorethink_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ package reql_tests
55
import (
66
"flag"
77
"os"
8+
"runtime"
89

910
r "gopkg.in/rethinkdb/rethinkdb-go.v5"
1011
)
1112

1213
var url string
1314

1415
func init() {
15-
flag.Parse()
16+
// Fixing test.testlogfile parsing error on Go 1.13+.
17+
if runtime.Version() < "go1.13" {
18+
flag.Parse()
19+
}
20+
1621
r.SetVerbose(true)
1722

1823
// If the test is being run by wercker look for the rethink url
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package reql_tests
2+
3+
import (
4+
"github.com/stretchr/testify/suite"
5+
r "gopkg.in/rethinkdb/rethinkdb-go.v5"
6+
"testing"
7+
)
8+
9+
// Test edge cases of bitwise operations
10+
func TestBitwiseSuite(t *testing.T) {
11+
suite.Run(t, new(BitwiseSuite))
12+
}
13+
14+
type BitwiseSuite struct {
15+
suite.Suite
16+
17+
session *r.Session
18+
}
19+
20+
func (suite *BitwiseSuite) SetupTest() {
21+
suite.T().Log("Setting up BitwiseSuite")
22+
23+
session, err := r.Connect(r.ConnectOpts{
24+
Address: url,
25+
})
26+
suite.Require().NoError(err, "Error returned when connecting to server")
27+
suite.session = session
28+
}
29+
30+
func (suite *BitwiseSuite) TearDownSuite() {
31+
suite.T().Log("Tearing down BitwiseSuite")
32+
33+
if suite.session != nil {
34+
suite.session.Close()
35+
}
36+
}
37+
38+
func (suite *BitwiseSuite) TestCases() {
39+
suite.T().Log("Running BitwiseSuite: Test edge cases of bitwise operations")
40+
41+
runOpts := r.RunOpts{
42+
GeometryFormat: "raw",
43+
GroupFormat: "map",
44+
}
45+
46+
{
47+
var q = r.BitAnd(3, 5)
48+
var expected_ = 3 & 5
49+
50+
suite.T().Logf("About to run line #1: %v", q)
51+
52+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
53+
suite.T().Log("Finished running line #1")
54+
}
55+
56+
{
57+
var q = r.Expr(3).BitAnd(5)
58+
var expected_ = 3 & 5
59+
60+
suite.T().Logf("About to run line #2: %v", q)
61+
62+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
63+
suite.T().Log("Finished running line #2")
64+
}
65+
66+
{
67+
var q = r.BitOr(3, 5)
68+
var expected_ = 3 | 5
69+
70+
suite.T().Logf("About to run line #3: %v", q)
71+
72+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
73+
suite.T().Log("Finished running line #3")
74+
}
75+
76+
{
77+
var q = r.Expr(3).BitOr(5)
78+
var expected_ = 3 | 5
79+
80+
suite.T().Logf("About to run line #4: %v", q)
81+
82+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
83+
suite.T().Log("Finished running line #4")
84+
}
85+
86+
{
87+
var q = r.BitXor(3, 5)
88+
var expected_ = 3 ^ 5
89+
90+
suite.T().Logf("About to run line #5: %v", q)
91+
92+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
93+
suite.T().Log("Finished running line #5")
94+
}
95+
96+
{
97+
var q = r.Expr(3).BitXor(5)
98+
var expected_ = 3 ^ 5
99+
100+
suite.T().Logf("About to run line #6: %v", q)
101+
102+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
103+
suite.T().Log("Finished running line #6")
104+
}
105+
106+
{
107+
var q = r.BitNot(3)
108+
var expected_ = ^3
109+
110+
suite.T().Logf("About to run line #7: %v", q)
111+
112+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
113+
suite.T().Log("Finished running line #7")
114+
}
115+
116+
{
117+
var q = r.Expr(3).BitNot()
118+
var expected_ = ^3
119+
120+
suite.T().Logf("About to run line #8: %v", q)
121+
122+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
123+
suite.T().Log("Finished running line #8")
124+
}
125+
126+
{
127+
var q = r.BitSal(3, 5)
128+
var expected_ = 3 << 5
129+
130+
suite.T().Logf("About to run line #9: %v", q)
131+
132+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
133+
suite.T().Log("Finished running line #9")
134+
}
135+
136+
{
137+
var q = r.Expr(3).BitSal(5)
138+
var expected_ = 3 << 5
139+
140+
suite.T().Logf("About to run line #10: %v", q)
141+
142+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
143+
suite.T().Log("Finished running line #10")
144+
}
145+
146+
{
147+
var q = r.BitSar(3, 5)
148+
var expected_ = 3 >> 5
149+
150+
suite.T().Logf("About to run line #11: %v", q)
151+
152+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
153+
suite.T().Log("Finished running line #11")
154+
}
155+
156+
{
157+
var q = r.Expr(3).BitSar(5)
158+
var expected_ = 3 >> 5
159+
160+
suite.T().Logf("About to run line #12: %v", q)
161+
162+
runAndAssert(suite.Suite, expected_, q, suite.session, runOpts)
163+
suite.T().Log("Finished running line #12")
164+
}
165+
}

internal/integration/reql_tests/reql_meta_table_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/integration/reql_tests/reql_selection_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)