Fix bugs for table only containing composite pkey
* Add more tables to test schema * Add string slice match helper
This commit is contained in:
parent
56faec6476
commit
cd7e45efaa
6 changed files with 126 additions and 10 deletions
|
@ -318,3 +318,27 @@ func JoinSlices(sep string, a, b []string) []string {
|
|||
|
||||
return ret
|
||||
}
|
||||
|
||||
// StringSliceMatch returns true if the length of both
|
||||
// slices is the same, and the elements of both slices are the same.
|
||||
// The elements can be in any order.
|
||||
func StringSliceMatch(a []string, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, aval := range a {
|
||||
found := false
|
||||
for _, bval := range b {
|
||||
if bval == aval {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -297,3 +297,60 @@ func TestJoinSlicesFail(t *testing.T) {
|
|||
|
||||
JoinSlices("", nil, []string{"hello"})
|
||||
}
|
||||
|
||||
func TestStringSliceMatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
a []string
|
||||
b []string
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
a: []string{},
|
||||
b: []string{},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
a: []string{"a"},
|
||||
b: []string{},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
a: []string{"a"},
|
||||
b: []string{"a"},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
a: []string{},
|
||||
b: []string{"b"},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
a: []string{"c", "d"},
|
||||
b: []string{"b", "d"},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
a: []string{"b", "d"},
|
||||
b: []string{"c", "d"},
|
||||
expect: false,
|
||||
},
|
||||
{
|
||||
a: []string{"a", "b", "c"},
|
||||
b: []string{"c", "b", "a"},
|
||||
expect: true,
|
||||
},
|
||||
{
|
||||
a: []string{"a", "b", "c"},
|
||||
b: []string{"a", "b", "c"},
|
||||
expect: true,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
if StringSliceMatch(test.a, test.b) != test.expect {
|
||||
t.Errorf("%d) Expected match to return %v, but got %v", i, test.expect, !test.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ func generateUpsertQuery(tableName string, updateOnConflict bool, ret, update, c
|
|||
strmangle.Placeholders(len(whitelist), 1, 1),
|
||||
)
|
||||
|
||||
if !updateOnConflict {
|
||||
if !updateOnConflict || len(update) == 0 {
|
||||
buf.WriteString("DO NOTHING")
|
||||
} else {
|
||||
buf.WriteByte('(')
|
||||
|
|
|
@ -49,11 +49,13 @@ func test{{$tableNamePlural}}SliceInPrimaryKeyArgs(t *testing.T) {
|
|||
t.Errorf("Expected args to be len %d, but got %d", len({{$varNameSingular}}PrimaryKeyColumns) * 3, len(args))
|
||||
}
|
||||
|
||||
for i := 0; i < len({{$varNameSingular}}PrimaryKeyColumns) * 3; i++ {
|
||||
argC := 0
|
||||
for i := 0; i < 3; i++ {
|
||||
{{range $key, $value := .Table.PKey.Columns}}
|
||||
if o[i].{{titleCase $value}} != args[i] {
|
||||
if o[i].{{titleCase $value}} != args[argC] {
|
||||
t.Errorf("Expected args[%d] to be value of o.{{titleCase $value}}, but got %#v", i, args[i])
|
||||
}
|
||||
argC++
|
||||
{{- end}}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,10 +31,20 @@ func test{{$tableNamePlural}}Update(t *testing.T) {
|
|||
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
||||
}
|
||||
|
||||
// If table only contains primary key columns, we need to pass
|
||||
// them into a whitelist to get a valid test result,
|
||||
// otherwise the Update method will error because it will not be able to
|
||||
// generate a whitelist (due to it excluding primary key columns).
|
||||
if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) {
|
||||
if err = {{$varNameSingular}}.Update(tx, {{$varNameSingular}}PrimaryKeyColumns...); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
} else {
|
||||
if err = {{$varNameSingular}}.Update(tx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
@ -66,10 +76,15 @@ func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
|||
}
|
||||
|
||||
// Remove Primary keys and unique columns from what we plan to update
|
||||
fields := strmangle.SetComplement(
|
||||
var fields []string
|
||||
if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) {
|
||||
fields = {{$varNameSingular}}Columns
|
||||
} else {
|
||||
fields = strmangle.SetComplement(
|
||||
{{$varNameSingular}}Columns,
|
||||
{{$varNameSingular}}PrimaryKeyColumns,
|
||||
)
|
||||
}
|
||||
|
||||
value := reflect.Indirect(reflect.ValueOf({{$varNameSingular}}))
|
||||
updateMap := M{}
|
||||
|
|
18
testdata/test_schema.sql
vendored
18
testdata/test_schema.sql
vendored
|
@ -117,3 +117,21 @@ create table cat_toys (
|
|||
toy_id int not null references toys (id),
|
||||
primary key (cat_id, toy_id)
|
||||
);
|
||||
|
||||
create table dog_toys (
|
||||
dog_id int not null,
|
||||
toy_id int not null,
|
||||
primary key (dog_id, toy_id)
|
||||
);
|
||||
|
||||
create table dragon_toys (
|
||||
dragon_id uuid,
|
||||
toy_id uuid,
|
||||
primary key (dragon_id, toy_id)
|
||||
);
|
||||
|
||||
create table spider_toys (
|
||||
spider_id uuid,
|
||||
name character varying,
|
||||
primary key (spider_id)
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue