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
|
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"})
|
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,12 +29,12 @@ func generateUpsertQuery(tableName string, updateOnConflict bool, ret, update, c
|
||||||
strmangle.Placeholders(len(whitelist), 1, 1),
|
strmangle.Placeholders(len(whitelist), 1, 1),
|
||||||
)
|
)
|
||||||
|
|
||||||
if !updateOnConflict {
|
if !updateOnConflict || len(update) == 0 {
|
||||||
buf.WriteString("DO NOTHING")
|
buf.WriteString("DO NOTHING")
|
||||||
} else {
|
} else {
|
||||||
buf.WriteByte('(')
|
buf.WriteByte('(')
|
||||||
buf.WriteString(strings.Join(conflict, ", "))
|
buf.WriteString(strings.Join(conflict, ", "))
|
||||||
buf.WriteString(") DO UPDATE SET")
|
buf.WriteString(") DO UPDATE SET ")
|
||||||
|
|
||||||
for i, v := range update {
|
for i, v := range update {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
|
|
|
@ -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))
|
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}}
|
{{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])
|
t.Errorf("Expected args[%d] to be value of o.{{titleCase $value}}, but got %#v", i, args[i])
|
||||||
}
|
}
|
||||||
|
argC++
|
||||||
{{- end}}
|
{{- end}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,8 +31,18 @@ func test{{$tableNamePlural}}Update(t *testing.T) {
|
||||||
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
t.Errorf("Unable to randomize {{$tableNameSingular}} struct: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = {{$varNameSingular}}.Update(tx); err != nil {
|
// If table only contains primary key columns, we need to pass
|
||||||
t.Error(err)
|
// 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,10 +76,15 @@ func test{{$tableNamePlural}}SliceUpdateAll(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove Primary keys and unique columns from what we plan to update
|
// Remove Primary keys and unique columns from what we plan to update
|
||||||
fields := strmangle.SetComplement(
|
var fields []string
|
||||||
{{$varNameSingular}}Columns,
|
if strmangle.StringSliceMatch({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns) {
|
||||||
{{$varNameSingular}}PrimaryKeyColumns,
|
fields = {{$varNameSingular}}Columns
|
||||||
)
|
} else {
|
||||||
|
fields = strmangle.SetComplement(
|
||||||
|
{{$varNameSingular}}Columns,
|
||||||
|
{{$varNameSingular}}PrimaryKeyColumns,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
value := reflect.Indirect(reflect.ValueOf({{$varNameSingular}}))
|
value := reflect.Indirect(reflect.ValueOf({{$varNameSingular}}))
|
||||||
updateMap := M{}
|
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),
|
toy_id int not null references toys (id),
|
||||||
primary key (cat_id, toy_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…
Add table
Reference in a new issue