ready for tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
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
|
||||
type sqlResult struct {
|
||||
insertID int64
|
||||
|
41
lazy.go
41
lazy.go
@@ -20,7 +20,8 @@ const (
|
||||
var (
|
||||
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")
|
||||
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
|
||||
@@ -34,22 +35,17 @@ type RowMock struct {
|
||||
type RowConfig struct {
|
||||
Query string
|
||||
Example any
|
||||
Keys []any //if set, length of keys must = RowCount
|
||||
Keys []any
|
||||
RowCount int
|
||||
}
|
||||
|
||||
type StructMock struct {
|
||||
Query string
|
||||
Args []any
|
||||
MockStruct any
|
||||
MockStructs reflect.Value
|
||||
Result driver.Result
|
||||
}
|
||||
|
||||
type StructConfig struct {
|
||||
Query string
|
||||
Example any
|
||||
}
|
||||
|
||||
// generates mock data based on configuration to be used for sqlmock
|
||||
func RandomRows(m RowConfig) (*RowMock, error) {
|
||||
//example struct cannot be nil and must be a struct
|
||||
@@ -102,7 +98,7 @@ func RandomRows(m RowConfig) (*RowMock, error) {
|
||||
//generate random values
|
||||
nv := kindToRandom(field)
|
||||
if nv == nil {
|
||||
return nil, ErrUnsupportedType
|
||||
return nil, fmt.Errorf(ErrUnsupportedType.Error(), field.Type.Name())
|
||||
}
|
||||
|
||||
rows[y] = append(rows[y], nv)
|
||||
@@ -117,9 +113,7 @@ func RandomRows(m RowConfig) (*RowMock, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
// so this would "work" but only accounts for inserting one row and doesn't allow for hardcoded ID's
|
||||
// it would also have to account in the results struct these changes for rowsAffected, etc.
|
||||
func RandomStruct(c StructConfig) (*StructMock, error) {
|
||||
func RandomStruct(c RowConfig) (*StructMock, error) {
|
||||
if c.Example == nil {
|
||||
return nil, ErrBadExample
|
||||
}
|
||||
@@ -127,11 +121,18 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
|
||||
return nil, ErrBadExample
|
||||
}
|
||||
|
||||
if c.RowCount <= 0 {
|
||||
c.RowCount = 1
|
||||
}
|
||||
|
||||
retType := reflect.TypeOf(c.Example)
|
||||
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)
|
||||
|
||||
for x := 0; x < c.RowCount; x++ {
|
||||
for y := 0; y < maxFieldCount; y++ {
|
||||
field := retType.Field(y)
|
||||
dbTag := field.Tag.Get(DB_TAG)
|
||||
@@ -141,23 +142,25 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
|
||||
|
||||
nv := kindToRandom(field)
|
||||
if nv == nil {
|
||||
return nil, ErrUnsupportedType
|
||||
return nil, fmt.Errorf(ErrUnsupportedType.Error(), field.Type.Name())
|
||||
}
|
||||
|
||||
args[y] = nv
|
||||
nf := filled.Field(y)
|
||||
if x == 0 {
|
||||
nf := filled.Index(x).Field(y)
|
||||
if nf.CanSet() {
|
||||
filled.Field(y).Set(reflect.ValueOf(nv))
|
||||
filled.Index(x).Field(y).Set(reflect.ValueOf(nv))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &StructMock{
|
||||
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)),
|
||||
Args: args,
|
||||
MockStruct: filled,
|
||||
MockStructs: filled,
|
||||
Result: &sqlResult{
|
||||
insertID: 1,
|
||||
rowsAffected: 1,
|
||||
rowsAffected: int64(c.RowCount),
|
||||
err: nil,
|
||||
},
|
||||
}, nil
|
||||
|
Reference in New Issue
Block a user