ready for tests

This commit is contained in:
2025-09-30 11:12:47 -04:00
parent ff62b371c9
commit c62da47a31
2 changed files with 41 additions and 38 deletions

View File

@@ -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

41
lazy.go
View File

@@ -20,7 +20,8 @@ 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,22 +35,17 @@ 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
func RandomRows(m RowConfig) (*RowMock, error) { func RandomRows(m RowConfig) (*RowMock, error) {
//example struct cannot be nil and must be a struct //example struct cannot be nil and must be a struct
@@ -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,11 +121,18 @@ 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 x := 0; x < c.RowCount; x++ {
for y := 0; y < maxFieldCount; y++ { for y := 0; y < maxFieldCount; y++ {
field := retType.Field(y) field := retType.Field(y)
dbTag := field.Tag.Get(DB_TAG) dbTag := field.Tag.Get(DB_TAG)
@@ -141,23 +142,25 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
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 {
nf := filled.Index(x).Field(y)
if nf.CanSet() { if nf.CanSet() {
filled.Field(y).Set(reflect.ValueOf(nv)) 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