583 lines
14 KiB
Go
583 lines
14 KiB
Go
// Code generated by BobGen sql (devel). DO NOT EDIT.
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
|
|
|
package factory
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
"github.com/jaswdr/faker/v2"
|
|
models "github.com/spotdemo4/trevstack/server/internal/models"
|
|
"github.com/stephenafamo/bob"
|
|
)
|
|
|
|
type ItemMod interface {
|
|
Apply(*ItemTemplate)
|
|
}
|
|
|
|
type ItemModFunc func(*ItemTemplate)
|
|
|
|
func (f ItemModFunc) Apply(n *ItemTemplate) {
|
|
f(n)
|
|
}
|
|
|
|
type ItemModSlice []ItemMod
|
|
|
|
func (mods ItemModSlice) Apply(n *ItemTemplate) {
|
|
for _, f := range mods {
|
|
f.Apply(n)
|
|
}
|
|
}
|
|
|
|
// ItemTemplate is an object representing the database table.
|
|
// all columns are optional and should be set by mods
|
|
type ItemTemplate struct {
|
|
ID func() int64
|
|
Name func() string
|
|
Added func() time.Time
|
|
Description func() string
|
|
Price func() float32
|
|
Quantity func() int64
|
|
UserID func() int64
|
|
|
|
r itemR
|
|
f *Factory
|
|
}
|
|
|
|
type itemR struct {
|
|
User *itemRUserR
|
|
}
|
|
|
|
type itemRUserR struct {
|
|
o *UserTemplate
|
|
}
|
|
|
|
// Apply mods to the ItemTemplate
|
|
func (o *ItemTemplate) Apply(mods ...ItemMod) {
|
|
for _, mod := range mods {
|
|
mod.Apply(o)
|
|
}
|
|
}
|
|
|
|
// toModel returns an *models.Item
|
|
// this does nothing with the relationship templates
|
|
func (o ItemTemplate) toModel() *models.Item {
|
|
m := &models.Item{}
|
|
|
|
if o.ID != nil {
|
|
m.ID = o.ID()
|
|
}
|
|
if o.Name != nil {
|
|
m.Name = o.Name()
|
|
}
|
|
if o.Added != nil {
|
|
m.Added = o.Added()
|
|
}
|
|
if o.Description != nil {
|
|
m.Description = o.Description()
|
|
}
|
|
if o.Price != nil {
|
|
m.Price = o.Price()
|
|
}
|
|
if o.Quantity != nil {
|
|
m.Quantity = o.Quantity()
|
|
}
|
|
if o.UserID != nil {
|
|
m.UserID = o.UserID()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// toModels returns an models.ItemSlice
|
|
// this does nothing with the relationship templates
|
|
func (o ItemTemplate) toModels(number int) models.ItemSlice {
|
|
m := make(models.ItemSlice, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.toModel()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// setModelRels creates and sets the relationships on *models.Item
|
|
// according to the relationships in the template. Nothing is inserted into the db
|
|
func (t ItemTemplate) setModelRels(o *models.Item) {
|
|
if t.r.User != nil {
|
|
rel := t.r.User.o.toModel()
|
|
rel.R.Items = append(rel.R.Items, o)
|
|
o.UserID = rel.ID
|
|
o.R.User = rel
|
|
}
|
|
}
|
|
|
|
// BuildSetter returns an *models.ItemSetter
|
|
// this does nothing with the relationship templates
|
|
func (o ItemTemplate) BuildSetter() *models.ItemSetter {
|
|
m := &models.ItemSetter{}
|
|
|
|
if o.ID != nil {
|
|
m.ID = omit.From(o.ID())
|
|
}
|
|
if o.Name != nil {
|
|
m.Name = omit.From(o.Name())
|
|
}
|
|
if o.Added != nil {
|
|
m.Added = omit.From(o.Added())
|
|
}
|
|
if o.Description != nil {
|
|
m.Description = omit.From(o.Description())
|
|
}
|
|
if o.Price != nil {
|
|
m.Price = omit.From(o.Price())
|
|
}
|
|
if o.Quantity != nil {
|
|
m.Quantity = omit.From(o.Quantity())
|
|
}
|
|
if o.UserID != nil {
|
|
m.UserID = omit.From(o.UserID())
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildManySetter returns an []*models.ItemSetter
|
|
// this does nothing with the relationship templates
|
|
func (o ItemTemplate) BuildManySetter(number int) []*models.ItemSetter {
|
|
m := make([]*models.ItemSetter, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.BuildSetter()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// Build returns an *models.Item
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use ItemTemplate.Create
|
|
func (o ItemTemplate) Build() *models.Item {
|
|
m := o.toModel()
|
|
o.setModelRels(m)
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildMany returns an models.ItemSlice
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use ItemTemplate.CreateMany
|
|
func (o ItemTemplate) BuildMany(number int) models.ItemSlice {
|
|
m := make(models.ItemSlice, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.Build()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func ensureCreatableItem(m *models.ItemSetter) {
|
|
if m.Name.IsUnset() {
|
|
m.Name = omit.From(random_string(nil))
|
|
}
|
|
if m.Added.IsUnset() {
|
|
m.Added = omit.From(random_time_Time(nil))
|
|
}
|
|
if m.Description.IsUnset() {
|
|
m.Description = omit.From(random_string(nil))
|
|
}
|
|
if m.Price.IsUnset() {
|
|
m.Price = omit.From(random_float32(nil))
|
|
}
|
|
if m.Quantity.IsUnset() {
|
|
m.Quantity = omit.From(random_int64(nil))
|
|
}
|
|
if m.UserID.IsUnset() {
|
|
m.UserID = omit.From(random_int64(nil))
|
|
}
|
|
}
|
|
|
|
// insertOptRels creates and inserts any optional the relationships on *models.Item
|
|
// according to the relationships in the template.
|
|
// any required relationship should have already exist on the model
|
|
func (o *ItemTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Item) (context.Context, error) {
|
|
var err error
|
|
|
|
return ctx, err
|
|
}
|
|
|
|
// Create builds a item and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o *ItemTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Item, error) {
|
|
_, m, err := o.create(ctx, exec)
|
|
return m, err
|
|
}
|
|
|
|
// MustCreate builds a item and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o *ItemTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Item {
|
|
_, m, err := o.create(ctx, exec)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateOrFail builds a item and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
|
func (o *ItemTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Item {
|
|
tb.Helper()
|
|
_, m, err := o.create(ctx, exec)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// create builds a item and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// this returns a context that includes the newly inserted model
|
|
func (o *ItemTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.Item, error) {
|
|
var err error
|
|
opt := o.BuildSetter()
|
|
ensureCreatableItem(opt)
|
|
|
|
var rel0 *models.User
|
|
if o.r.User == nil {
|
|
var ok bool
|
|
rel0, ok = userCtx.Value(ctx)
|
|
if !ok {
|
|
ItemMods.WithNewUser().Apply(o)
|
|
}
|
|
}
|
|
if o.r.User != nil {
|
|
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
}
|
|
opt.UserID = omit.From(rel0.ID)
|
|
|
|
m, err := models.Items.Insert(opt).One(ctx, exec)
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
ctx = itemCtx.WithValue(ctx, m)
|
|
|
|
m.R.User = rel0
|
|
|
|
ctx, err = o.insertOptRels(ctx, exec, m)
|
|
return ctx, m, err
|
|
}
|
|
|
|
// CreateMany builds multiple items and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o ItemTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.ItemSlice, error) {
|
|
_, m, err := o.createMany(ctx, exec, number)
|
|
return m, err
|
|
}
|
|
|
|
// MustCreateMany builds multiple items and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o ItemTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.ItemSlice {
|
|
_, m, err := o.createMany(ctx, exec, number)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateManyOrFail builds multiple items and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// It calls `tb.Fatal(err)` on the test/benchmark if an error occurs
|
|
func (o ItemTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.ItemSlice {
|
|
tb.Helper()
|
|
_, m, err := o.createMany(ctx, exec, number)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// createMany builds multiple items and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// this returns a context that includes the newly inserted models
|
|
func (o ItemTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.ItemSlice, error) {
|
|
var err error
|
|
m := make(models.ItemSlice, number)
|
|
|
|
for i := range m {
|
|
ctx, m[i], err = o.create(ctx, exec)
|
|
if err != nil {
|
|
return ctx, nil, err
|
|
}
|
|
}
|
|
|
|
return ctx, m, nil
|
|
}
|
|
|
|
// Item has methods that act as mods for the ItemTemplate
|
|
var ItemMods itemMods
|
|
|
|
type itemMods struct{}
|
|
|
|
func (m itemMods) RandomizeAllColumns(f *faker.Faker) ItemMod {
|
|
return ItemModSlice{
|
|
ItemMods.RandomID(f),
|
|
ItemMods.RandomName(f),
|
|
ItemMods.RandomAdded(f),
|
|
ItemMods.RandomDescription(f),
|
|
ItemMods.RandomPrice(f),
|
|
ItemMods.RandomQuantity(f),
|
|
ItemMods.RandomUserID(f),
|
|
}
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) ID(val int64) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.ID = func() int64 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) IDFunc(f func() int64) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.ID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetID() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.ID = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomID(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.ID = func() int64 {
|
|
return random_int64(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) Name(val string) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Name = func() string { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) NameFunc(f func() string) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Name = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetName() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Name = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomName(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Name = func() string {
|
|
return random_string(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) Added(val time.Time) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Added = func() time.Time { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) AddedFunc(f func() time.Time) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Added = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetAdded() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Added = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomAdded(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Added = func() time.Time {
|
|
return random_time_Time(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) Description(val string) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Description = func() string { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) DescriptionFunc(f func() string) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Description = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetDescription() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Description = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomDescription(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Description = func() string {
|
|
return random_string(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) Price(val float32) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Price = func() float32 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) PriceFunc(f func() float32) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Price = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetPrice() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Price = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomPrice(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Price = func() float32 {
|
|
return random_float32(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) Quantity(val int64) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Quantity = func() int64 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) QuantityFunc(f func() int64) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Quantity = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetQuantity() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Quantity = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomQuantity(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.Quantity = func() int64 {
|
|
return random_int64(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m itemMods) UserID(val int64) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.UserID = func() int64 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m itemMods) UserIDFunc(f func() int64) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.UserID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m itemMods) UnsetUserID() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.UserID = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m itemMods) RandomUserID(f *faker.Faker) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.UserID = func() int64 {
|
|
return random_int64(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m itemMods) WithUser(rel *UserTemplate) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.r.User = &itemRUserR{
|
|
o: rel,
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m itemMods) WithNewUser(mods ...UserMod) ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
related := o.f.NewUser(mods...)
|
|
|
|
m.WithUser(related).Apply(o)
|
|
})
|
|
}
|
|
|
|
func (m itemMods) WithoutUser() ItemMod {
|
|
return ItemModFunc(func(o *ItemTemplate) {
|
|
o.r.User = nil
|
|
})
|
|
}
|