Add RandomizeEnforcedStruct
* Fix broken Select QM test
This commit is contained in:
parent
775c5ba369
commit
39fe91f2cb
4 changed files with 74 additions and 4 deletions
|
@ -255,7 +255,7 @@ func (p *PostgresDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
||||||
c.Type = "null.Float64"
|
c.Type = "null.Float64"
|
||||||
case "real":
|
case "real":
|
||||||
c.Type = "null.Float32"
|
c.Type = "null.Float32"
|
||||||
case "bit", "interval", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
case "bit", "interval", "uuint", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
||||||
c.Type = "null.String"
|
c.Type = "null.String"
|
||||||
case "bytea":
|
case "bytea":
|
||||||
c.Type = "[]byte"
|
c.Type = "[]byte"
|
||||||
|
@ -278,7 +278,7 @@ func (p *PostgresDriver) TranslateColumnType(c bdb.Column) bdb.Column {
|
||||||
c.Type = "float64"
|
c.Type = "float64"
|
||||||
case "real":
|
case "real":
|
||||||
c.Type = "float32"
|
c.Type = "float32"
|
||||||
case "bit", "interval", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
case "bit", "interval", "uuint", "bit varying", "character", "character varying", "cidr", "inet", "json", "macaddr", "text", "uuid", "xml":
|
||||||
c.Type = "string"
|
c.Type = "string"
|
||||||
case "bytea":
|
case "bytea":
|
||||||
c.Type = "[]byte"
|
c.Type = "[]byte"
|
||||||
|
|
|
@ -252,7 +252,7 @@ func TestSetSelect(t *testing.T) {
|
||||||
t.Errorf("Expected selectCols len 2, got %d", len(q.selectCols))
|
t.Errorf("Expected selectCols len 2, got %d", len(q.selectCols))
|
||||||
}
|
}
|
||||||
|
|
||||||
if q.selectCols[0] != `"col1"` && q.selectCols[1] != `"col2"` {
|
if q.selectCols[0] != `col1` && q.selectCols[1] != `col2` {
|
||||||
t.Errorf("select cols value mismatch: %#v", q.selectCols)
|
t.Errorf("select cols value mismatch: %#v", q.selectCols)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,8 @@ var (
|
||||||
typeTime = reflect.TypeOf(time.Time{})
|
typeTime = reflect.TypeOf(time.Time{})
|
||||||
|
|
||||||
rgxValidTime = regexp.MustCompile(`[2-9]+`)
|
rgxValidTime = regexp.MustCompile(`[2-9]+`)
|
||||||
|
|
||||||
|
enforcedTypes = []string{"uuid"}
|
||||||
)
|
)
|
||||||
|
|
||||||
type seed int
|
type seed int
|
||||||
|
@ -193,7 +195,7 @@ func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldDBType := colTypes[typ.Field(i).Name]
|
fieldDBType := colTypes[fieldTyp.Name]
|
||||||
if err := randomizeField(fieldVal, fieldDBType, includeInvalid); err != nil {
|
if err := randomizeField(fieldVal, fieldDBType, includeInvalid); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -202,6 +204,37 @@ func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandomizeEnforcedStruct(obj interface{}, colTypes map[string]string) error {
|
||||||
|
value := reflect.Indirect(reflect.ValueOf(obj))
|
||||||
|
if !value.IsValid() {
|
||||||
|
return fmt.Errorf("Invalid object provided: %T", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
kind := value.Kind()
|
||||||
|
if kind != reflect.Struct {
|
||||||
|
return fmt.Errorf("Inner element should be a struct, given a non-struct: %T", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
typ := value.Type()
|
||||||
|
nFields := value.NumField()
|
||||||
|
|
||||||
|
// Iterate through fields
|
||||||
|
for i := 0; i < nFields; i++ {
|
||||||
|
fieldVal := value.Field(i)
|
||||||
|
fieldTyp := typ.Field(i)
|
||||||
|
for _, v := range enforcedTypes {
|
||||||
|
if colTypes[fieldTyp.Name] == v {
|
||||||
|
if err := randomizeField(fieldVal, v, false); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// randDate generates a random time.Time between 1850 and 2050.
|
// randDate generates a random time.Time between 1850 and 2050.
|
||||||
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
|
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
|
||||||
// not cause mismatches in the test data comparisons.
|
// not cause mismatches in the test data comparisons.
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsZeroValue(t *testing.T) {
|
func TestIsZeroValue(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
o := struct {
|
o := struct {
|
||||||
A []byte
|
A []byte
|
||||||
B time.Time
|
B time.Time
|
||||||
|
@ -49,6 +51,8 @@ func TestIsZeroValue(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsValueMatch(t *testing.T) {
|
func TestIsValueMatch(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
var values []interface{}
|
var values []interface{}
|
||||||
|
|
||||||
|
@ -117,6 +121,8 @@ func TestIsValueMatch(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRandomizeStruct(t *testing.T) {
|
func TestRandomizeStruct(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
var testStruct = struct {
|
var testStruct = struct {
|
||||||
Int int
|
Int int
|
||||||
Int64 int64
|
Int64 int64
|
||||||
|
@ -184,3 +190,34 @@ func TestRandomizeStruct(t *testing.T) {
|
||||||
t.Errorf("the null values are not being randomized: %#v", testStruct)
|
t.Errorf("the null values are not being randomized: %#v", testStruct)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRandomizeEnforcedStruct(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
var testStruct = struct {
|
||||||
|
Int1 int
|
||||||
|
NullInt1 null.Int
|
||||||
|
UUID1 string
|
||||||
|
UUID2 string
|
||||||
|
}{}
|
||||||
|
|
||||||
|
fieldTypes := map[string]string{
|
||||||
|
"Int": "integer",
|
||||||
|
"NullInt": "integer",
|
||||||
|
"UUID1": "uuid",
|
||||||
|
"UUID2": "uuid",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := RandomizeEnforcedStruct(&testStruct, fieldTypes)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if testStruct.Int1 != 0 || testStruct.NullInt1.Int != 0 ||
|
||||||
|
testStruct.NullInt1.Valid != false {
|
||||||
|
t.Errorf("the regular values are being randomized when they should be zero vals: %#v", testStruct)
|
||||||
|
}
|
||||||
|
|
||||||
|
if testStruct.UUID1 == "" || testStruct.UUID2 == "" {
|
||||||
|
t.Errorf("the enforced values should be set: %#v", testStruct)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue