removing driver, just generating mock struct args would need an order
This commit is contained in:
17
driver.go
17
driver.go
@@ -1,17 +0,0 @@
|
|||||||
package lazy
|
|
||||||
|
|
||||||
//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
|
|
||||||
rowsAffected int64
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *sqlResult) LastInsertId() (int64, error) {
|
|
||||||
return r.insertID, r.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *sqlResult) RowsAffected() (int64, error) {
|
|
||||||
return r.rowsAffected, r.err
|
|
||||||
}
|
|
16
lazy.go
16
lazy.go
@@ -47,9 +47,7 @@ type StructConfig struct {
|
|||||||
|
|
||||||
type StructMock struct {
|
type StructMock struct {
|
||||||
Query string
|
Query string
|
||||||
Args []driver.Value
|
|
||||||
MockStructs []any
|
MockStructs []any
|
||||||
Result driver.Result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generates mock data based on configuration to be used for sqlmock
|
// generates mock data based on configuration to be used for sqlmock
|
||||||
@@ -142,18 +140,12 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
|
|||||||
//create slice of example structs
|
//create slice of example structs
|
||||||
ft := reflect.SliceOf(retType)
|
ft := reflect.SliceOf(retType)
|
||||||
filled := reflect.MakeSlice(ft, 0, c.RowCount)
|
filled := reflect.MakeSlice(ft, 0, c.RowCount)
|
||||||
args := make([]driver.Value, 0) //args for sqlmock WithArgs
|
|
||||||
|
|
||||||
for x := 0; x < c.RowCount; x++ {
|
for x := 0; x < c.RowCount; x++ {
|
||||||
//add empty example struct
|
//add empty example struct
|
||||||
filled = reflect.Append(filled, reflect.ValueOf(c.Example))
|
filled = reflect.Append(filled, reflect.ValueOf(c.Example))
|
||||||
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)
|
|
||||||
// no db tag, we skip
|
|
||||||
if dbTag == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
//get random value
|
//get random value
|
||||||
nv := kindToRandom(field)
|
nv := kindToRandom(field)
|
||||||
@@ -161,9 +153,6 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
|
|||||||
return nil, fmt.Errorf(ErrUnsupportedType.Error(), field.Type.Name())
|
return nil, fmt.Errorf(ErrUnsupportedType.Error(), field.Type.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
//add to sqlmock args
|
|
||||||
args = append(args, nv)
|
|
||||||
|
|
||||||
if filled.Index(x).Field(y).CanSet() {
|
if filled.Index(x).Field(y).CanSet() {
|
||||||
filled.Index(x).Field(y).Set(reflect.ValueOf(nv))
|
filled.Index(x).Field(y).Set(reflect.ValueOf(nv))
|
||||||
}
|
}
|
||||||
@@ -179,12 +168,7 @@ func RandomStruct(c StructConfig) (*StructMock, error) {
|
|||||||
|
|
||||||
return &StructMock{
|
return &StructMock{
|
||||||
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)),
|
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(c.Query)),
|
||||||
Args: args,
|
|
||||||
MockStructs: retStructs,
|
MockStructs: retStructs,
|
||||||
Result: &sqlResult{
|
|
||||||
rowsAffected: int64(c.RowCount),
|
|
||||||
err: nil,
|
|
||||||
},
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user