Skip to content

Commit 8b2fd83

Browse files
ianrose14shantanuraj
authored andcommitted
add TestBindJsonSlice
1 parent b385498 commit 8b2fd83

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

queries/reflect_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/aarondl/null/v8"
1616
"github.com/aarondl/sqlboiler/v4/drivers"
17+
"github.com/aarondl/sqlboiler/v4/types"
1718

1819
"github.com/DATA-DOG/go-sqlmock"
1920
)
@@ -181,6 +182,57 @@ func TestBindPtrSlice(t *testing.T) {
181182
}
182183
}
183184

185+
func TestBindJsonSlice(t *testing.T) {
186+
t.Parallel()
187+
188+
type siteInfoItem struct {
189+
ID int `boil:"id" json:"id" toml:"id" yaml:"id"`
190+
Fields types.JSON `boil:"test" json:"test" toml:"test" yaml:"test"`
191+
}
192+
193+
query := &Query{
194+
from: []string{"fun"},
195+
dialect: &drivers.Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
196+
}
197+
198+
db, mock, err := sqlmock.New()
199+
if err != nil {
200+
t.Error(err)
201+
}
202+
203+
ret := sqlmock.NewRows([]string{"id", "test"})
204+
ret.AddRow(driver.Value(int64(35)), driver.Value(`{"foo": "bar"}`))
205+
ret.AddRow(driver.Value(int64(12)), driver.Value("{}"))
206+
mock.ExpectQuery(`SELECT \* FROM "fun";`).WillReturnRows(ret)
207+
208+
var testResults []siteInfoItem
209+
err = query.Bind(nil, db, &testResults)
210+
if err != nil {
211+
t.Error(err)
212+
}
213+
214+
if len(testResults) != 2 {
215+
t.Fatal("wrong number of results:", len(testResults))
216+
}
217+
if id := testResults[0].ID; id != 35 {
218+
t.Error("wrong ID:", id)
219+
}
220+
if name := testResults[0].Fields; name.String() != `{"foo": "bar"}` {
221+
t.Error("wrong name:", name)
222+
}
223+
224+
if id := testResults[1].ID; id != 12 {
225+
t.Error("wrong ID:", id)
226+
}
227+
if name := testResults[1].Fields; name.String() != "{}" {
228+
t.Error("wrong name:", name)
229+
}
230+
231+
if err := mock.ExpectationsWereMet(); err != nil {
232+
t.Error(err)
233+
}
234+
}
235+
184236
func testMakeMapping(byt ...byte) uint64 {
185237
var x uint64
186238
for i, b := range byt {

0 commit comments

Comments
 (0)