adding support for returning more than one mock row
This commit is contained in:
18
lazy.go
18
lazy.go
@@ -14,19 +14,24 @@ import (
|
|||||||
type MockResults struct {
|
type MockResults struct {
|
||||||
Query string
|
Query string
|
||||||
Columns []string
|
Columns []string
|
||||||
Rows []driver.Value
|
Rows [][]driver.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateRandomResults(query string, exampleObj any, keyVal any) (*MockResults, error) {
|
func GenerateRandomResults(query string, exampleObj any, keyVal any, rowCount int) (*MockResults, error) {
|
||||||
if exampleObj == nil {
|
if exampleObj == nil {
|
||||||
return nil, errors.New("exampleObj cannot be nil")
|
return nil, errors.New("exampleObj cannot be nil")
|
||||||
}
|
}
|
||||||
|
if rowCount == 0 {
|
||||||
|
rowCount = 1
|
||||||
|
}
|
||||||
|
|
||||||
retType := reflect.TypeOf(exampleObj)
|
retType := reflect.TypeOf(exampleObj)
|
||||||
maxFieldCount := retType.NumField()
|
maxFieldCount := retType.NumField()
|
||||||
columns := make([]string, 0, maxFieldCount)
|
columns := make([]string, 0, maxFieldCount)
|
||||||
rows := make([]driver.Value, 0, maxFieldCount)
|
rows := make([][]driver.Value, 0, maxFieldCount)
|
||||||
|
|
||||||
|
//double loop here to allow for multiple rows
|
||||||
|
for y := 0; y < rowCount; y++ {
|
||||||
for x := 0; x < maxFieldCount; x++ {
|
for x := 0; x < maxFieldCount; x++ {
|
||||||
field := retType.Field(x)
|
field := retType.Field(x)
|
||||||
dbTag := field.Tag.Get("db")
|
dbTag := field.Tag.Get("db")
|
||||||
@@ -34,10 +39,12 @@ func GenerateRandomResults(query string, exampleObj any, keyVal any) (*MockResul
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if y == 0 {
|
||||||
columns = append(columns, dbTag)
|
columns = append(columns, dbTag)
|
||||||
|
}
|
||||||
|
|
||||||
if field.Tag.Get("test") == "key" {
|
if field.Tag.Get("test") == "key" {
|
||||||
rows = append(rows, keyVal)
|
rows[y] = append(rows[y], keyVal)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +53,8 @@ func GenerateRandomResults(query string, exampleObj any, keyVal any) (*MockResul
|
|||||||
return nil, fmt.Errorf("could not match type: %s", retType.Name())
|
return nil, fmt.Errorf("could not match type: %s", retType.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
rows = append(rows, nv)
|
rows[y] = append(rows[y], nv)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MockResults{
|
return &MockResults{
|
||||||
|
Reference in New Issue
Block a user