Fix strmangle's AutoIncPrimaryKey function.

- It's test was also a dead unicorn of unhappiness.
This commit is contained in:
Aaron L 2016-06-19 17:17:09 -07:00
parent c07d21cffc
commit 30ece150e7
2 changed files with 92 additions and 53 deletions

View file

@ -302,15 +302,18 @@ func AutoIncPrimaryKey(cols []dbdrivers.Column, pkey *dbdrivers.PrimaryKey) stri
return ""
}
for _, c := range cols {
if rgxAutoIncColumn.MatchString(c.Default) &&
c.IsNullable == false &&
(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) {
for _, p := range pkey.Columns {
if c.Name == p {
return p
}
for _, pkeyColumn := range pkey.Columns {
for _, c := range cols {
if c.Name != pkeyColumn {
continue
}
if !rgxAutoIncColumn.MatchString(c.Default) || c.IsNullable ||
!(strings.HasPrefix(c.Type, "int") || strings.HasPrefix(c.Type, "uint")) {
continue
}
return pkeyColumn
}
}

View file

@ -90,57 +90,93 @@ func TestCamelCaseCommaList(t *testing.T) {
func TestAutoIncPrimaryKey(t *testing.T) {
t.Parallel()
var pkey *dbdrivers.PrimaryKey
var cols []dbdrivers.Column
r := AutoIncPrimaryKey(cols, pkey)
if r != "" {
t.Errorf("Expected empty string, got %s", r)
}
pkey = &dbdrivers.PrimaryKey{
Columns: []string{
"col1", "auto",
tests := map[string]struct {
Expect string
Pkey *dbdrivers.PrimaryKey
Columns []dbdrivers.Column
}{
"easycase": {
Expect: "one",
Pkey: &dbdrivers.PrimaryKey{
Name: "pkey",
Columns: []string{"one"},
},
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "int32",
IsNullable: false,
Default: `nextval('abc'::regclass)`,
},
},
},
Name: "",
}
cols = []dbdrivers.Column{
{
Name: "thing",
IsNullable: true,
Type: "int64",
Default: "nextval('abc'::regclass)",
"missingcase": {
Expect: "",
Pkey: &dbdrivers.PrimaryKey{
Name: "pkey",
Columns: []string{"two"},
},
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "int32",
IsNullable: false,
Default: `nextval('abc'::regclass)`,
},
},
},
{
Name: "stuff",
IsNullable: false,
Type: "string",
Default: "nextval('abc'::regclass)",
"wrongtype": {
Expect: "",
Pkey: &dbdrivers.PrimaryKey{
Name: "pkey",
Columns: []string{"one"},
},
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "string",
IsNullable: false,
Default: `nextval('abc'::regclass)`,
},
},
},
{
Name: "other",
IsNullable: false,
Type: "int64",
Default: "nextval",
"nodefault": {
Expect: "",
Pkey: &dbdrivers.PrimaryKey{
Name: "pkey",
Columns: []string{"one"},
},
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "string",
IsNullable: false,
Default: ``,
},
},
},
"nullable": {
Expect: "",
Pkey: &dbdrivers.PrimaryKey{
Name: "pkey",
Columns: []string{"one"},
},
Columns: []dbdrivers.Column{
dbdrivers.Column{
Name: "one",
Type: "string",
IsNullable: true,
Default: `nextval('abc'::regclass)`,
},
},
},
}
r = AutoIncPrimaryKey(cols, pkey)
if r != "" {
t.Errorf("Expected empty string, got %s", r)
}
cols = append(cols, dbdrivers.Column{
Name: "auto",
IsNullable: false,
Type: "int64",
Default: "nextval('abc'::regclass)",
})
r = AutoIncPrimaryKey(cols, pkey)
if r != "auto" {
t.Errorf("Expected empty string, got %s", r)
for testName, test := range tests {
primaryKey := AutoIncPrimaryKey(test.Columns, test.Pkey)
if primaryKey != test.Expect {
t.Errorf("%s) wrong primary key, want: %q, got %q", testName, test.Expect, primaryKey)
}
}
}