Skip to content

Commit

Permalink
fix(tests): added tests of SetMap
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Feb 4, 2024
1 parent 4377a66 commit 957d1e7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 36 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- added `BuilderOption` on `SetMap` on `InsertBuilder` and `UpdateBuilder` (#1)
- added `WithDbColumns` filter BuilderOption (#2)
- added `WithAllow` BuilderOption (#2)

### Fixed
- fixed `sql.Scanner` support on `bind` (#2)

## [1.0.0] - 2024-01-31
18 changes: 0 additions & 18 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ func (r *Rows) Bind(dest any) error {

}

case reflect.String:

for r.Rows.Next() {
fields := make([]string, n)

values := make([]interface{}, n)

for i := range fields {
values[i] = &fields[i]
}

err = r.Rows.Scan(values...)
if err != nil {
return err
}
ev = reflect.Append(ev, reflect.ValueOf(fields))
}

case reflect.Struct:
b = getStructBinder(iValue.Type(), iValue)

Expand Down
16 changes: 11 additions & 5 deletions sqlbuilder_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,28 @@ func (ib *InsertBuilder) SetMap(m map[string]any, opts ...BuilderOption) *Insert
return ib
}

bo := &BuilderOptions{
DbColumns: make(map[string]bool),
}
bo := &BuilderOptions{}
for _, opt := range opts {
opt(bo)
}

for n, v := range m {
if bo.ToSnake != nil {
n = bo.ToSnake(n)
sn := bo.ToSnake(n)
if sn != n {
delete(m, n)
m[sn] = v
}
}
}

if _, ok := bo.DbColumns[n]; ok {
for _, n := range bo.Columns {
v, ok := m[n]
if ok {
ib.Set(n, v)
}
}

return ib
}

Expand Down
13 changes: 6 additions & 7 deletions sqlbuilder_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package sqle
type BuilderOption func(opts *BuilderOptions)

type BuilderOptions struct {
ToSnake func(string) string // db column name converter
DbColumns map[string]bool // db column filter
ToSnake func(string) string // db column name converter
Columns []string // db column filter
}

func WithToSnake(fn func(string) string) BuilderOption {
Expand All @@ -13,13 +13,12 @@ func WithToSnake(fn func(string) string) BuilderOption {
}
}

func WithDbColumns(columns []string) BuilderOption {
// WithFiler only allowed columns can be written to db
func WithAllow(columns []string) BuilderOption {
return func(opts *BuilderOptions) {
if opts.DbColumns == nil {
opts.DbColumns = map[string]bool{}
}

for _, c := range columns {
opts.DbColumns[c] = true
opts.Columns = append(opts.Columns, c)
}
}
}
64 changes: 64 additions & 0 deletions sqlbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/iancoleman/strcase"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -240,6 +241,41 @@ func TestBuilder(t *testing.T) {
require.Equal(t, now, vars[5])
},
},
{
name: "build_update_map",
build: func() *Builder {
m := map[string]any{
"MemberID": "id123",
"Amount": 100,
"Created_time": now,
"alias": "alias",
}

b := New()
b.Update("orders").
SetMap(m, WithToSnake(strcase.ToSnake), WithAllow([]string{"member_id", "amount", "created_time"}))
b.Where("cancelled>={now}").
If(true).SQL("AND", "id={order_id}")

b.SQL(" AND created_time>={now}")
b.Param("order_id", "order_123456")
b.Param("now", now)

return b
},
assert: func(t *testing.T, b *Builder) {
s, vars, err := b.Build()
require.NoError(t, err)
require.Equal(t, "UPDATE orders SET `member_id`=?, `amount`=?, `created_time`=? WHERE cancelled>=? AND id=? AND created_time>=?", s)
require.Len(t, vars, 6)
require.Equal(t, "id123", vars[0])
require.Equal(t, 100, vars[1])
require.Equal(t, now, vars[2])
require.Equal(t, now, vars[3])
require.Equal(t, "order_123456", vars[4])
require.Equal(t, now, vars[5])
},
},
{
name: "build_update_if",
build: func() *Builder {
Expand Down Expand Up @@ -332,6 +368,34 @@ func TestBuilder(t *testing.T) {

},
},
{
name: "build_insert_map",
build: func() *Builder {
m := map[string]any{
"ID": "id123",
"Amount": 100,
"CreatedTime": now,
"Alias": "alias",
}

b := New()
b.Insert("orders").
SetMap(m, WithToSnake(strcase.ToSnake), WithAllow([]string{"id", "amount", "created_time"})).
End()

return b
},
assert: func(t *testing.T, b *Builder) {
s, vars, err := b.Build()
require.NoError(t, err)
require.Equal(t, "INSERT INTO orders (`id`, `amount`, `created_time`) VALUES (?, ?, ?);", s)
require.Len(t, vars, 3)
require.Equal(t, "id123", vars[0])
require.Equal(t, 100, vars[1])
require.Equal(t, now, vars[2])

},
},
{
name: "build_insert_if",
build: func() *Builder {
Expand Down
16 changes: 11 additions & 5 deletions sqlbuilder_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,28 @@ func (ub *UpdateBuilder) SetMap(m map[string]any, opts ...BuilderOption) *Update
return ub
}

bo := &BuilderOptions{
DbColumns: make(map[string]bool),
}
bo := &BuilderOptions{}
for _, opt := range opts {
opt(bo)
}

for n, v := range m {
if bo.ToSnake != nil {
n = bo.ToSnake(n)
sn := bo.ToSnake(n)
if sn != n {
delete(m, n)
m[sn] = v
}
}
}

if _, ok := bo.DbColumns[n]; ok {
for _, n := range bo.Columns {
v, ok := m[n]
if ok {
ub.Set(n, v)
}
}

return ub
}

Expand Down

0 comments on commit 957d1e7

Please sign in to comment.