ready for tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
package lazy
|
package lazy
|
||||||
|
|
||||||
//driver.Result does not play well with creation, overriding it here so we can return the results without
|
//driver.Result does not play well when manually creating them, overriding it here so we can return the results without
|
||||||
//requiring sqlmock
|
//requiring sqlmock
|
||||||
type sqlResult struct {
|
type sqlResult struct {
|
||||||
insertID int64
|
insertID int64
|
||||||
|
77
lazy.go
77
lazy.go
@@ -18,9 +18,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrBadExample = errors.New("example field must be a non-nil struct")
|
ErrBadExample = errors.New("example field must be a non-nil struct")
|
||||||
ErrMissingKeys = errors.New("there must be a key for each row requested with RowCount")
|
ErrMissingKeys = errors.New("there must be a key for each row requested with RowCount")
|
||||||
ErrUnsupportedType = errors.New("")
|
//contains placeholder to inject type on runtime
|
||||||
|
ErrUnsupportedType = errors.New("type: %s is not yet supported")
|
||||||
)
|
)
|
||||||
|
|
||||||
// mock data generated based on config
|
// mock data generated based on config
|
||||||
@@ -34,20 +35,15 @@ type RowMock struct {
|
|||||||
type RowConfig struct {
|
type RowConfig struct {
|
||||||
Query string
|
Query string
|
||||||
Example any
|
Example any
|
||||||
Keys []any //if set, length of keys must = RowCount
|
Keys []any
|
||||||
RowCount int
|
RowCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
type StructMock struct {
|
type StructMock struct {
|
||||||
Query string
|
Query string
|
||||||
Args []any
|
Args []any
|
||||||
MockStruct any
|
MockStructs reflect.Value
|
||||||
Result driver.Result
|
Result driver.Result
|
||||||
}
|
|
||||||
|
|
||||||
type StructConfig struct {
|
|
||||||
Query string
|
|
||||||
Example any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generates mock data based on configuration to be used for sqlmock
|
// generates mock data based on configuration to be used for sqlmock
|
||||||
@@ -102,7 +98,7 @@ func RandomRows(m RowConfig) (*RowMock, error) {
|
|||||||
//generate random values
|
//generate random values
|
||||||
nv := kindToRandom(field)
|
nv := kindToRandom(field)
|
||||||
if nv == nil {
|
if nv == nil {
|
||||||
return nil, ErrUnsupportedType
|
return nil, fmt.Errorf(ErrUnsupportedType.Error(), field.Type.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
rows[y] = append(rows[y], nv)
|
rows[y] = append(rows[y], nv)
|
||||||
@@ -117,9 +113,7 @@ func RandomRows(m RowConfig) (*RowMock, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// so this would "work" but only accounts for inserting one row and doesn't allow for hardcoded ID's
|
func RandomStruct(c RowConfig) (*StructMock, error) {
|
||||||
// it would also have to account in the results struct these changes for rowsAffected, etc.
|
|
||||||
func RandomStruct(c StructConfig) (*StructMock, error) {
|
|
||||||
if c.Example == nil {
|
if c.Example == nil {
|
||||||
return nil, ErrBadExample
|
return nil, ErrBadExample
|
||||||
}
|
}
|
||||||
@@ -127,37 +121,46 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
|
|||||||
return nil, ErrBadExample
|
return nil, ErrBadExample
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.RowCount <= 0 {
|
||||||
|
c.RowCount = 1
|
||||||
|
}
|
||||||
|
|
||||||
retType := reflect.TypeOf(c.Example)
|
retType := reflect.TypeOf(c.Example)
|
||||||
maxFieldCount := retType.NumField()
|
maxFieldCount := retType.NumField()
|
||||||
filled := reflect.New(retType).Elem()
|
//create slice of structs
|
||||||
|
ft := reflect.SliceOf(retType)
|
||||||
|
filled := reflect.MakeSlice(ft, 0, c.RowCount).Elem()
|
||||||
args := make([]any, 0, maxFieldCount)
|
args := make([]any, 0, maxFieldCount)
|
||||||
|
|
||||||
for y := 0; y < maxFieldCount; y++ {
|
for x := 0; x < c.RowCount; x++ {
|
||||||
field := retType.Field(y)
|
for y := 0; y < maxFieldCount; y++ {
|
||||||
dbTag := field.Tag.Get(DB_TAG)
|
field := retType.Field(y)
|
||||||
if dbTag == "" {
|
dbTag := field.Tag.Get(DB_TAG)
|
||||||
continue
|
if dbTag == "" {
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
nv := kindToRandom(field)
|
nv := kindToRandom(field)
|
||||||
if nv == nil {
|
if nv == nil {
|
||||||
return nil, ErrUnsupportedType
|
return nil, fmt.Errorf(ErrUnsupportedType.Error(), field.Type.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
args[y] = nv
|
args[y] = nv
|
||||||
nf := filled.Field(y)
|
if x == 0 {
|
||||||
if nf.CanSet() {
|
nf := filled.Index(x).Field(y)
|
||||||
filled.Field(y).Set(reflect.ValueOf(nv))
|
if nf.CanSet() {
|
||||||
|
filled.Index(x).Field(y).Set(reflect.ValueOf(nv))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &StructMock{
|
return &StructMock{
|
||||||
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)),
|
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)),
|
||||||
Args: args,
|
Args: args,
|
||||||
MockStruct: filled,
|
MockStructs: filled,
|
||||||
Result: &sqlResult{
|
Result: &sqlResult{
|
||||||
insertID: 1,
|
rowsAffected: int64(c.RowCount),
|
||||||
rowsAffected: 1,
|
|
||||||
err: nil,
|
err: nil,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
|
Reference in New Issue
Block a user