Skip to content

Commit 914fc2c

Browse files
authored
Merge pull request #467 from DivPro/ISSUE-466
ISSUE-466 test and decode source pointers support
2 parents 95e85ce + c06ab3b commit 914fc2c

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

encoding/decoder.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ func valueDecoder(dv, sv reflect.Value, blank bool) decoderFunc {
7878

7979
if dv.IsValid() {
8080
dv = indirect(dv, false)
81-
if blank {
81+
if sv.Kind() == reflect.Ptr {
82+
sv = indirect(sv, false)
83+
dv.Set(sv)
84+
} else if blank {
8285
dv.Set(reflect.Zero(dv.Type()))
8386
}
8487
}

mock_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package rethinkdb
22

33
import (
44
"fmt"
5-
65
"testing"
76

87
test "gopkg.in/check.v1"
@@ -350,6 +349,54 @@ func (s *MockSuite) TestMockAnything(c *test.C) {
350349
mock.AssertExpectations(c)
351350
}
352351

352+
func (s *MockSuite) TestMockMapSliceResultOk(c *test.C) {
353+
type Some struct {
354+
Id string
355+
}
356+
357+
result := []map[string]interface{}{
358+
{"Id": "test1"},
359+
{"Id": "test2"},
360+
}
361+
362+
mock := NewMock()
363+
q := DB("test").Table("test").GetAll()
364+
mock.On(q).Return(result, nil)
365+
res, err := q.Run(mock)
366+
c.Assert(err, test.IsNil)
367+
368+
var casted []*Some
369+
err = res.All(&casted)
370+
c.Assert(err, test.IsNil)
371+
372+
c.Assert(casted[0].Id, test.Equals, "test1")
373+
c.Assert(casted[1].Id, test.Equals, "test2")
374+
}
375+
376+
func (s *MockSuite) TestMockPointerSliceResultOk(c *test.C) {
377+
type Some struct {
378+
Id string
379+
}
380+
381+
result := []*Some{
382+
{Id: "test1"},
383+
{Id: "test2"},
384+
}
385+
386+
mock := NewMock()
387+
q := DB("test").Table("test").GetAll()
388+
mock.On(q).Return(result, nil)
389+
res, err := q.Run(mock)
390+
c.Assert(err, test.IsNil)
391+
392+
var casted []*Some
393+
err = res.All(&casted)
394+
c.Assert(err, test.IsNil)
395+
396+
c.Assert(casted[0].Id, test.Equals, "test1")
397+
c.Assert(casted[1].Id, test.Equals, "test2")
398+
}
399+
353400
func (s *MockSuite) TestMockRethinkStructsRunWrite(c *test.C) {
354401
mock := NewMock()
355402
mock.On(DB("test").Table("test").Update(map[string]int{"val": 1})).Return(WriteResponse{

0 commit comments

Comments
 (0)