moving to struct as param, readme updates

This commit is contained in:
2025-09-26 16:46:40 -04:00
parent b9a8c5cd47
commit 4573594c85
2 changed files with 36 additions and 18 deletions

View File

@@ -9,10 +9,12 @@ This function expects the SQL query, the example response object, and an optiona
<b>Example Object</b> The example object argument <u>must</u> be a struct and requires "db" tags. The db tags are then parsed and used to calculate the psuedo-random rows and values <b>Example Object</b> The example object argument <u>must</u> be a struct and requires "db" tags. The db tags are then parsed and used to calculate the psuedo-random rows and values
<b>Primary Key</b> The optional primary key argument is used to hardcode a primary key field in the returned mocks, the primary key field in the example struct <u>must</u> have a test tag with the value "key" <b>Primary Key</b> The optional primary key argument is used to hardcode a primary key field in the returned mocks, the primary key field in the example struct <u>must</u> have a lazy tag with the value "key"
<b>Row Count</b> Row count sets the amount of mock rows to generate, if set to 0 then one row will be generated. If primary keys are provided you must provide one per row.
```go ```go
type Mock struct { type Mock struct {
Field1 string `db:"field1" test:"key"` Field1 string `db:"field1" lazy:"key"`
} }
``` ```
@@ -22,17 +24,17 @@ func Test() {
testArg := 123 testArg := 123
query := `SELECT ...` query := `SELECT ...`
type results struct { type results struct {
Field1 string `db:"field1" test:"key"` Field1 string `db:"field1" lazy:"key"`
Field2 int `db:"field2"` Field2 int `db:"field2"`
} }
rando, err := GenerateRandomResults(query, results{}, testArg) rando, err := GenerateRandomResults(query, results{}, []any{testArg}, 1)
if err != nil { if err != nil {
panic(err) panic(err)
} }
//the rando object will now have psuedo random values in all fields except for the field containing the test tag set to "key" that field will be hardcoded with the testArg to allow for unit tests to ensure the requested ID flows through //the rando object will now have psuedo random values in all fields except for the field containing the test tag set to "key" that field will be hardcoded with the testArg to allow for unit tests to ensure the requested ID flows through
rows := sqlmock.NewRows(rando.Columns).AddRow(rando.Rows...) rows := sqlmock.NewRows(rando.Columns).AddRows(rando.Rows...)
mock.ExpectQuery(rando.Query).WithArgs(testArg).WillReturnRows(rows) mock.ExpectQuery(rando.Query).WithArgs(testArg).WillReturnRows(rows)
... ...

42
lazy.go
View File

@@ -11,35 +11,51 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
const (
DB_TAG = "db"
LAZY_TAG = "lazy"
KEY_VALUE = "key"
)
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, rowCount int) (*MockResults, error) { type MockDetails struct {
if exampleObj == nil { Query string
return nil, errors.New("exampleObj cannot be nil") Example any
Keys []any
RowCount int
}
func GenerateRandomResults(m MockDetails) (*MockResults, error) {
if m.Example == nil {
return nil, errors.New("example value cannot be nil")
} }
if rowCount == 0 { if reflect.ValueOf(m.Example).Kind() != reflect.Struct {
rowCount = 1 return nil, errors.New("example value must be a struct")
} }
if len(keyVal) != 0 { if m.RowCount == 0 {
if len(keyVal) != rowCount { m.RowCount = 1
}
if len(m.Keys) != 0 {
if len(m.Keys) != m.RowCount {
return nil, errors.New("you must provide a key for each row") return nil, errors.New("you must provide a key for each row")
} }
} }
retType := reflect.TypeOf(exampleObj) retType := reflect.TypeOf(m.Example)
maxFieldCount := retType.NumField() maxFieldCount := retType.NumField()
columns := make([]string, 0, maxFieldCount) columns := make([]string, 0, maxFieldCount)
rows := make([][]driver.Value, 0) rows := make([][]driver.Value, 0)
for y := 0; y < rowCount; y++ { for y := 0; y < m.RowCount; y++ {
rows = append(rows, make([]driver.Value, 0)) rows = append(rows, make([]driver.Value, 0))
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_TAG)
if dbTag == "" { if dbTag == "" {
continue continue
} }
@@ -48,8 +64,8 @@ func GenerateRandomResults(query string, exampleObj any, keyVal []any, rowCount
columns = append(columns, dbTag) columns = append(columns, dbTag)
} }
if field.Tag.Get("test") == "key" { if field.Tag.Get(LAZY_TAG) == KEY_VALUE {
rows[y] = append(rows[y], keyVal[y]) rows[y] = append(rows[y], m.Keys[y])
continue continue
} }
@@ -63,7 +79,7 @@ func GenerateRandomResults(query string, exampleObj any, keyVal []any, rowCount
} }
return &MockResults{ return &MockResults{
Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(query)), Query: sqlx.Rebind(sqlx.AT, regexp.QuoteMeta(m.Query)),
Columns: columns, Columns: columns,
Rows: rows, Rows: rows,
}, nil }, nil