Big refactor of generated code
- Stop generating helper functions for each model - Move Insert/Update/Upsert query generation helpers to strmangle - Add tests for query generation helpers - Delete a lot of the Insert/Upsert tests that test the query generation helpers. - Use tx for more of the tests.
This commit is contained in:
parent
8a7a9a35e8
commit
efa1fbb80a
13 changed files with 231 additions and 227 deletions
strmangle
|
@ -1,5 +1,43 @@
|
|||
package strmangle
|
||||
|
||||
// UpdateColumnSet generates the set of columns to update for an update statement.
|
||||
// if a whitelist is supplied, it's returned
|
||||
// if a whitelist is missing then we begin with all columns
|
||||
// then we remove the primary key columns
|
||||
func UpdateColumnSet(allColumns, pkeyCols, whitelist []string) []string {
|
||||
if len(whitelist) != 0 {
|
||||
return whitelist
|
||||
}
|
||||
|
||||
return SetComplement(allColumns, pkeyCols)
|
||||
}
|
||||
|
||||
// InsertColumnSet generates the set of columns to insert and return for an insert statement
|
||||
// the return columns are used to get values that are assigned within the database during the
|
||||
// insert to keep the struct in sync with what's in the db.
|
||||
// with a whitelist:
|
||||
// - the whitelist is used for the insert columns
|
||||
// - the return columns are the result of (columns with default values - the whitelist)
|
||||
// without a whitelist:
|
||||
// - start with columns without a default as these always need to be inserted
|
||||
// - add all columns that have a default in the database but that are non-zero in the struct
|
||||
// - the return columns are the result of (columns with default values - the previous set)
|
||||
func InsertColumnSet(cols, defaults, noDefaults, nonZeroDefaults, whitelist []string) ([]string, []string) {
|
||||
if len(whitelist) > 0 {
|
||||
return whitelist, SetComplement(defaults, whitelist)
|
||||
}
|
||||
|
||||
var wl []string
|
||||
wl = append(wl, noDefaults...)
|
||||
wl = SetMerge(nonZeroDefaults, wl)
|
||||
wl = SortByKeys(cols, wl)
|
||||
|
||||
// Only return the columns with default values that are not in the insert whitelist
|
||||
rc := SetComplement(defaults, wl)
|
||||
|
||||
return wl, rc
|
||||
}
|
||||
|
||||
// SetInclude checks to see if the string is found in the string slice
|
||||
func SetInclude(str string, slice []string) bool {
|
||||
for _, s := range slice {
|
||||
|
|
|
@ -5,6 +5,86 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestUpdateColumnSet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
Cols []string
|
||||
PKeys []string
|
||||
Whitelist []string
|
||||
Out []string
|
||||
}{
|
||||
{Cols: []string{"a", "b"}, PKeys: []string{"a"}, Out: []string{"b"}},
|
||||
{Cols: []string{"a", "b"}, PKeys: []string{"a"}, Whitelist: []string{"a"}, Out: []string{"a"}},
|
||||
{Cols: []string{"a", "b"}, PKeys: []string{"a"}, Whitelist: []string{"a", "b"}, Out: []string{"a", "b"}},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
set := UpdateColumnSet(test.Cols, test.PKeys, test.Whitelist)
|
||||
|
||||
if !reflect.DeepEqual(set, test.Out) {
|
||||
t.Errorf("%d) set was wrong\nwant: %v\ngot: %v", i, test.Out, set)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertColumnSet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
columns := []string{"a", "b", "c"}
|
||||
defaults := []string{"a", "c"}
|
||||
nodefaults := []string{"b"}
|
||||
|
||||
tests := []struct {
|
||||
Cols []string
|
||||
Defaults []string
|
||||
NoDefaults []string
|
||||
NonZeroDefaults []string
|
||||
Whitelist []string
|
||||
Set []string
|
||||
Ret []string
|
||||
}{
|
||||
// No whitelist
|
||||
{Set: []string{"b"}, Ret: []string{"a", "c"}},
|
||||
{Defaults: []string{}, NoDefaults: []string{"a", "b", "c"}, Set: []string{"a", "b", "c"}, Ret: []string{}},
|
||||
|
||||
// No whitelist + Nonzero defaults
|
||||
{NonZeroDefaults: []string{"a"}, Set: []string{"a", "b"}, Ret: []string{"c"}},
|
||||
{NonZeroDefaults: []string{"c"}, Set: []string{"b", "c"}, Ret: []string{"a"}},
|
||||
|
||||
// Whitelist
|
||||
{Whitelist: []string{"a"}, Set: []string{"a"}, Ret: []string{"c"}},
|
||||
{Whitelist: []string{"c"}, Set: []string{"c"}, Ret: []string{"a"}},
|
||||
{Whitelist: []string{"a", "c"}, Set: []string{"a", "c"}, Ret: []string{}},
|
||||
{Whitelist: []string{"a", "b", "c"}, Set: []string{"a", "b", "c"}, Ret: []string{}},
|
||||
|
||||
// Whitelist + Nonzero defaults (shouldn't care, same results as above)
|
||||
{Whitelist: []string{"a"}, NonZeroDefaults: []string{"c"}, Set: []string{"a"}, Ret: []string{"c"}},
|
||||
{Whitelist: []string{"c"}, NonZeroDefaults: []string{"b"}, Set: []string{"c"}, Ret: []string{"a"}},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
if test.Cols == nil {
|
||||
test.Cols = columns
|
||||
}
|
||||
if test.Defaults == nil {
|
||||
test.Defaults = defaults
|
||||
}
|
||||
if test.NoDefaults == nil {
|
||||
test.NoDefaults = nodefaults
|
||||
}
|
||||
|
||||
set, ret := InsertColumnSet(test.Cols, test.Defaults, test.NoDefaults, test.NonZeroDefaults, test.Whitelist)
|
||||
|
||||
if !reflect.DeepEqual(set, test.Set) {
|
||||
t.Errorf("%d) set was wrong\nwant: %v\ngot: %v", i, test.Set, set)
|
||||
}
|
||||
if !reflect.DeepEqual(ret, test.Ret) {
|
||||
t.Errorf("%d) ret was wrong\nwant: %v\ngot: %v", i, test.Ret, ret)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetInclude(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue