Skip to content

Commit bfa9b39

Browse files
committed
cursor.one test
1 parent 80d9fe4 commit bfa9b39

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

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+
}

mock.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ func (m *Mock) Query(ctx context.Context, q Query) (*Cursor, error) {
372372
} else if conn != nil {
373373
conn.cursors[query.Query.Token] = c
374374
c.finished = false
375+
c.releaseConn = func() error {
376+
return conn.Close()
377+
}
378+
c.mu.Lock()
379+
err := c.fetchMore()
380+
c.mu.Unlock()
381+
if err != nil {
382+
return nil, err
383+
}
375384
} else {
376385
c.buffer = append(c.buffer, query.Response)
377386
}
@@ -499,7 +508,7 @@ func (c *mockConn) Write(b []byte) (n int, err error) {
499508
c.tokens <- token
500509
return len(b), nil
501510
}
502-
func (c *mockConn) Close() error { panic("not implemented") }
511+
func (c *mockConn) Close() error { return nil }
503512
func (c *mockConn) LocalAddr() net.Addr { panic("not implemented") }
504513
func (c *mockConn) RemoteAddr() net.Addr { panic("not implemented") }
505514
func (c *mockConn) SetDeadline(t time.Time) error { panic("not implemented") }

0 commit comments

Comments
 (0)