Merge branch 'randimize-enums-with-single-value' into dev

This commit is contained in:
Aaron L 2017-06-14 20:23:40 -07:00
commit 1e9753091b
2 changed files with 14 additions and 2 deletions

View file

@ -627,6 +627,8 @@ func randEnumValue(enum string) (string, error) {
vals := strmangle.ParseEnumVals(enum)
if vals == nil || len(vals) == 0 {
return "", fmt.Errorf("unable to parse enum string: %s", enum)
} else if len(vals) == 1 {
return vals[0], nil
}
return vals[rand.Intn(len(vals)-1)], nil

View file

@ -150,6 +150,7 @@ func TestRandEnumValue(t *testing.T) {
enum1 := "enum.workday('monday','tuesday')"
enum2 := "enum('monday','tuesday')"
enum3 := "enum('monday')"
r1, err := randEnumValue(enum1)
if err != nil {
@ -157,7 +158,7 @@ func TestRandEnumValue(t *testing.T) {
}
if r1 != "monday" && r1 != "tuesday" {
t.Errorf("Expected monday or tueday, got: %q", r1)
t.Errorf("Expected monday or tuesday, got: %q", r1)
}
r2, err := randEnumValue(enum2)
@ -166,6 +167,15 @@ func TestRandEnumValue(t *testing.T) {
}
if r2 != "monday" && r2 != "tuesday" {
t.Errorf("Expected monday or tueday, got: %q", r2)
t.Errorf("Expected monday or tuesday, got: %q", r2)
}
r3, err := randEnumValue(enum3)
if err != nil {
t.Error(err)
}
if r3 != "monday" {
t.Errorf("Expected monday got: %q", r3)
}
}