519 lines
13 KiB
Go
519 lines
13 KiB
Go
// Code generated by BobGen psql v0.42.5. 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/Gleipnir-Technology/bob"
|
|
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
models "github.com/Gleipnir-Technology/nidus-sync/db/models"
|
|
"github.com/aarondl/opt/null"
|
|
"github.com/aarondl/opt/omit"
|
|
"github.com/aarondl/opt/omitnull"
|
|
"github.com/jaswdr/faker/v2"
|
|
)
|
|
|
|
type PoolMod interface {
|
|
Apply(context.Context, *PoolTemplate)
|
|
}
|
|
|
|
type PoolModFunc func(context.Context, *PoolTemplate)
|
|
|
|
func (f PoolModFunc) Apply(ctx context.Context, n *PoolTemplate) {
|
|
f(ctx, n)
|
|
}
|
|
|
|
type PoolModSlice []PoolMod
|
|
|
|
func (mods PoolModSlice) Apply(ctx context.Context, n *PoolTemplate) {
|
|
for _, f := range mods {
|
|
f.Apply(ctx, n)
|
|
}
|
|
}
|
|
|
|
// PoolTemplate is an object representing the database table.
|
|
// all columns are optional and should be set by mods
|
|
type PoolTemplate struct {
|
|
Condition func() enums.Poolconditiontype
|
|
Created func() time.Time
|
|
CreatorID func() int32
|
|
ID func() int32
|
|
SiteID func() null.Val[int32]
|
|
|
|
r poolR
|
|
f *Factory
|
|
|
|
alreadyPersisted bool
|
|
}
|
|
|
|
type poolR struct {
|
|
CreatorUser *poolRCreatorUserR
|
|
}
|
|
|
|
type poolRCreatorUserR struct {
|
|
o *UserTemplate
|
|
}
|
|
|
|
// Apply mods to the PoolTemplate
|
|
func (o *PoolTemplate) Apply(ctx context.Context, mods ...PoolMod) {
|
|
for _, mod := range mods {
|
|
mod.Apply(ctx, o)
|
|
}
|
|
}
|
|
|
|
// setModelRels creates and sets the relationships on *models.Pool
|
|
// according to the relationships in the template. Nothing is inserted into the db
|
|
func (t PoolTemplate) setModelRels(o *models.Pool) {
|
|
if t.r.CreatorUser != nil {
|
|
rel := t.r.CreatorUser.o.Build()
|
|
rel.R.CreatorPools = append(rel.R.CreatorPools, o)
|
|
o.CreatorID = rel.ID // h2
|
|
o.R.CreatorUser = rel
|
|
}
|
|
}
|
|
|
|
// BuildSetter returns an *models.PoolSetter
|
|
// this does nothing with the relationship templates
|
|
func (o PoolTemplate) BuildSetter() *models.PoolSetter {
|
|
m := &models.PoolSetter{}
|
|
|
|
if o.Condition != nil {
|
|
val := o.Condition()
|
|
m.Condition = omit.From(val)
|
|
}
|
|
if o.Created != nil {
|
|
val := o.Created()
|
|
m.Created = omit.From(val)
|
|
}
|
|
if o.CreatorID != nil {
|
|
val := o.CreatorID()
|
|
m.CreatorID = omit.From(val)
|
|
}
|
|
if o.ID != nil {
|
|
val := o.ID()
|
|
m.ID = omit.From(val)
|
|
}
|
|
if o.SiteID != nil {
|
|
val := o.SiteID()
|
|
m.SiteID = omitnull.FromNull(val)
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildManySetter returns an []*models.PoolSetter
|
|
// this does nothing with the relationship templates
|
|
func (o PoolTemplate) BuildManySetter(number int) []*models.PoolSetter {
|
|
m := make([]*models.PoolSetter, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.BuildSetter()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// Build returns an *models.Pool
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use PoolTemplate.Create
|
|
func (o PoolTemplate) Build() *models.Pool {
|
|
m := &models.Pool{}
|
|
|
|
if o.Condition != nil {
|
|
m.Condition = o.Condition()
|
|
}
|
|
if o.Created != nil {
|
|
m.Created = o.Created()
|
|
}
|
|
if o.CreatorID != nil {
|
|
m.CreatorID = o.CreatorID()
|
|
}
|
|
if o.ID != nil {
|
|
m.ID = o.ID()
|
|
}
|
|
if o.SiteID != nil {
|
|
m.SiteID = o.SiteID()
|
|
}
|
|
|
|
o.setModelRels(m)
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildMany returns an models.PoolSlice
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use PoolTemplate.CreateMany
|
|
func (o PoolTemplate) BuildMany(number int) models.PoolSlice {
|
|
m := make(models.PoolSlice, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.Build()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func ensureCreatablePool(m *models.PoolSetter) {
|
|
if !(m.Condition.IsValue()) {
|
|
val := random_enums_Poolconditiontype(nil)
|
|
m.Condition = omit.From(val)
|
|
}
|
|
if !(m.Created.IsValue()) {
|
|
val := random_time_Time(nil)
|
|
m.Created = omit.From(val)
|
|
}
|
|
if !(m.CreatorID.IsValue()) {
|
|
val := random_int32(nil)
|
|
m.CreatorID = omit.From(val)
|
|
}
|
|
}
|
|
|
|
// insertOptRels creates and inserts any optional the relationships on *models.Pool
|
|
// according to the relationships in the template.
|
|
// any required relationship should have already exist on the model
|
|
func (o *PoolTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Pool) error {
|
|
var err error
|
|
|
|
return err
|
|
}
|
|
|
|
// Create builds a pool and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o *PoolTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Pool, error) {
|
|
var err error
|
|
opt := o.BuildSetter()
|
|
ensureCreatablePool(opt)
|
|
|
|
if o.r.CreatorUser == nil {
|
|
PoolMods.WithNewCreatorUser().Apply(ctx, o)
|
|
}
|
|
|
|
var rel0 *models.User
|
|
|
|
if o.r.CreatorUser.o.alreadyPersisted {
|
|
rel0 = o.r.CreatorUser.o.Build()
|
|
} else {
|
|
rel0, err = o.r.CreatorUser.o.Create(ctx, exec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
opt.CreatorID = omit.From(rel0.ID)
|
|
|
|
m, err := models.Pools.Insert(opt).One(ctx, exec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
m.R.CreatorUser = rel0
|
|
|
|
if err := o.insertOptRels(ctx, exec, m); err != nil {
|
|
return nil, err
|
|
}
|
|
return m, err
|
|
}
|
|
|
|
// MustCreate builds a pool and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o *PoolTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Pool {
|
|
m, err := o.Create(ctx, exec)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateOrFail builds a pool 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 *PoolTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Pool {
|
|
tb.Helper()
|
|
m, err := o.Create(ctx, exec)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateMany builds multiple pools and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o PoolTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.PoolSlice, error) {
|
|
var err error
|
|
m := make(models.PoolSlice, number)
|
|
|
|
for i := range m {
|
|
m[i], err = o.Create(ctx, exec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
// MustCreateMany builds multiple pools and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o PoolTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.PoolSlice {
|
|
m, err := o.CreateMany(ctx, exec, number)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateManyOrFail builds multiple pools 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 PoolTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.PoolSlice {
|
|
tb.Helper()
|
|
m, err := o.CreateMany(ctx, exec, number)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Pool has methods that act as mods for the PoolTemplate
|
|
var PoolMods poolMods
|
|
|
|
type poolMods struct{}
|
|
|
|
func (m poolMods) RandomizeAllColumns(f *faker.Faker) PoolMod {
|
|
return PoolModSlice{
|
|
PoolMods.RandomCondition(f),
|
|
PoolMods.RandomCreated(f),
|
|
PoolMods.RandomCreatorID(f),
|
|
PoolMods.RandomID(f),
|
|
PoolMods.RandomSiteID(f),
|
|
}
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m poolMods) Condition(val enums.Poolconditiontype) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Condition = func() enums.Poolconditiontype { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m poolMods) ConditionFunc(f func() enums.Poolconditiontype) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Condition = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m poolMods) UnsetCondition() PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Condition = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m poolMods) RandomCondition(f *faker.Faker) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Condition = func() enums.Poolconditiontype {
|
|
return random_enums_Poolconditiontype(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m poolMods) Created(val time.Time) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Created = func() time.Time { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m poolMods) CreatedFunc(f func() time.Time) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Created = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m poolMods) UnsetCreated() PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Created = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m poolMods) RandomCreated(f *faker.Faker) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.Created = func() time.Time {
|
|
return random_time_Time(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m poolMods) CreatorID(val int32) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.CreatorID = func() int32 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m poolMods) CreatorIDFunc(f func() int32) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.CreatorID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m poolMods) UnsetCreatorID() PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.CreatorID = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m poolMods) RandomCreatorID(f *faker.Faker) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.CreatorID = func() int32 {
|
|
return random_int32(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m poolMods) ID(val int32) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.ID = func() int32 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m poolMods) IDFunc(f func() int32) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.ID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m poolMods) UnsetID() PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
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 poolMods) RandomID(f *faker.Faker) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.ID = func() int32 {
|
|
return random_int32(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m poolMods) SiteID(val null.Val[int32]) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.SiteID = func() null.Val[int32] { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m poolMods) SiteIDFunc(f func() null.Val[int32]) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.SiteID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m poolMods) UnsetSiteID() PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.SiteID = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
// The generated value is sometimes null
|
|
func (m poolMods) RandomSiteID(f *faker.Faker) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.SiteID = func() null.Val[int32] {
|
|
if f == nil {
|
|
f = &defaultFaker
|
|
}
|
|
|
|
val := random_int32(f)
|
|
return null.From(val)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
// The generated value is never null
|
|
func (m poolMods) RandomSiteIDNotNull(f *faker.Faker) PoolMod {
|
|
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
|
o.SiteID = func() null.Val[int32] {
|
|
if f == nil {
|
|
f = &defaultFaker
|
|
}
|
|
|
|
val := random_int32(f)
|
|
return null.From(val)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m poolMods) WithParentsCascading() PoolMod {
|
|
return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
|
|
if isDone, _ := poolWithParentsCascadingCtx.Value(ctx); isDone {
|
|
return
|
|
}
|
|
ctx = poolWithParentsCascadingCtx.WithValue(ctx, true)
|
|
{
|
|
|
|
related := o.f.NewUserWithContext(ctx, UserMods.WithParentsCascading())
|
|
m.WithCreatorUser(related).Apply(ctx, o)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m poolMods) WithCreatorUser(rel *UserTemplate) PoolMod {
|
|
return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
|
|
o.r.CreatorUser = &poolRCreatorUserR{
|
|
o: rel,
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m poolMods) WithNewCreatorUser(mods ...UserMod) PoolMod {
|
|
return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
|
|
related := o.f.NewUserWithContext(ctx, mods...)
|
|
|
|
m.WithCreatorUser(related).Apply(ctx, o)
|
|
})
|
|
}
|
|
|
|
func (m poolMods) WithExistingCreatorUser(em *models.User) PoolMod {
|
|
return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
|
|
o.r.CreatorUser = &poolRCreatorUserR{
|
|
o: o.f.FromExistingUser(em),
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m poolMods) WithoutCreatorUser() PoolMod {
|
|
return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
|
|
o.r.CreatorUser = nil
|
|
})
|
|
}
|