Skip to content

Commit

Permalink
Added more tests for insert and update
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Sep 1, 2015
1 parent 54c8a8d commit d2494e1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
66 changes: 65 additions & 1 deletion dataset_insert_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package goqu

import (
"gopkg.in/DATA-DOG/go-sqlmock.v1"
"github.com/c2fo/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"

"database/sql"
"time"
Expand Down Expand Up @@ -543,3 +543,67 @@ func (me *datasetTest) TestPreparedInsertSqlWithEmbeddedStructPtr() {
})
assert.Equal(t, sql, `INSERT INTO "items" ("primary_phone", "home_phone", "address", "name") VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)`)
}

func (me *datasetTest) TestPreparedInsertSqlWithValuer() {
t := me.T()
ds1 := From("items")

type item struct {
Address string `db:"address"`
Name string `db:"name"`
Valuer sql.NullInt64 `db:"valuer"`
}
sqlString, args, err := ds1.Prepared(true).ToInsertSql(item{Name: "Test", Address: "111 Test Addr", Valuer: sql.NullInt64{Int64: 10, Valid: true}})
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{
"111 Test Addr", "Test", 10,
})
assert.Equal(t, sqlString, `INSERT INTO "items" ("address", "name", "valuer") VALUES (?, ?, ?)`)

sqlString, args, err = ds1.Prepared(true).ToInsertSql(
item{Address: "111 Test Addr", Name: "Test1", Valuer: sql.NullInt64{Int64: 10, Valid: true}},
item{Address: "211 Test Addr", Name: "Test2", Valuer: sql.NullInt64{Int64: 20, Valid: true}},
item{Address: "311 Test Addr", Name: "Test3", Valuer: sql.NullInt64{Int64: 30, Valid: true}},
item{Address: "411 Test Addr", Name: "Test4", Valuer: sql.NullInt64{Int64: 40, Valid: true}},
)
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{
"111 Test Addr", "Test1", 10,
"211 Test Addr", "Test2", 20,
"311 Test Addr", "Test3", 30,
"411 Test Addr", "Test4", 40,
})
assert.Equal(t, sqlString, `INSERT INTO "items" ("address", "name", "valuer") VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?)`)
}

func (me *datasetTest) TestPreparedInsertSqlWithValuerNull() {
t := me.T()
ds1 := From("items")

type item struct {
Address string `db:"address"`
Name string `db:"name"`
Valuer sql.NullInt64 `db:"valuer"`
}
sqlString, args, err := ds1.Prepared(true).ToInsertSql(item{Name: "Test", Address: "111 Test Addr"})
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{
"111 Test Addr", "Test", nil,
})
assert.Equal(t, sqlString, `INSERT INTO "items" ("address", "name", "valuer") VALUES (?, ?, ?)`)

sqlString, args, err = ds1.Prepared(true).ToInsertSql(
item{Address: "111 Test Addr", Name: "Test1"},
item{Address: "211 Test Addr", Name: "Test2"},
item{Address: "311 Test Addr", Name: "Test3"},
item{Address: "411 Test Addr", Name: "Test4"},
)
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{
"111 Test Addr", "Test1", nil,
"211 Test Addr", "Test2", nil,
"311 Test Addr", "Test3", nil,
"411 Test Addr", "Test4", nil,
})
assert.Equal(t, sqlString, `INSERT INTO "items" ("address", "name", "valuer") VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?)`)
}
11 changes: 6 additions & 5 deletions dataset_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"time"

"gopkg.in/DATA-DOG/go-sqlmock.v1"
"github.com/c2fo/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

func (me *datasetTest) TestUpdateSqlWithNoSources() {
Expand Down Expand Up @@ -145,9 +145,10 @@ func (me *datasetTest) TestUpdateSqlWithEmbeddedStruct() {
}
type item struct {
phone
Address string `db:"address" goqu:"skipupdate"`
Name string `db:"name"`
Created time.Time `db:"created"`
Address string `db:"address" goqu:"skipupdate"`
Name string `db:"name"`
Created time.Time `db:"created"`
NilPointer interface{} `db:"nil_pointer"`
}
created, _ := time.Parse("2006-01-02", "2015-01-01")

Expand All @@ -158,7 +159,7 @@ func (me *datasetTest) TestUpdateSqlWithEmbeddedStruct() {
}})
assert.NoError(t, err)
assert.Equal(t, args, []interface{}{})
assert.Equal(t, sql, `UPDATE "items" SET "primary_phone"='456456',"home_phone"='123123',"phone_created"='2015-01-01T00:00:00Z',"name"='Test',"created"='2015-01-01T00:00:00Z'`)
assert.Equal(t, sql, `UPDATE "items" SET "primary_phone"='456456',"home_phone"='123123',"phone_created"='2015-01-01T00:00:00Z',"name"='Test',"created"='2015-01-01T00:00:00Z',"nil_pointer"=NULL`)
}

func (me *datasetTest) TestUpdateSqlWithEmbeddedStructPtr() {
Expand Down

0 comments on commit d2494e1

Please sign in to comment.