406 lines
11 KiB
Go
406 lines
11 KiB
Go
// Code generated by BobGen psql v0.41.1. DO NOT EDIT.
|
|
// This file is meant to be re-generated in place and/or deleted at any time.
|
|
|
|
package factory
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
models "github.com/Gleipnir-Technology/nidus-sync/models"
|
|
"github.com/aarondl/opt/null"
|
|
"github.com/aarondl/opt/omit"
|
|
"github.com/aarondl/opt/omitnull"
|
|
"github.com/jaswdr/faker/v2"
|
|
"github.com/stephenafamo/bob"
|
|
)
|
|
|
|
type OrganizationMod interface {
|
|
Apply(context.Context, *OrganizationTemplate)
|
|
}
|
|
|
|
type OrganizationModFunc func(context.Context, *OrganizationTemplate)
|
|
|
|
func (f OrganizationModFunc) Apply(ctx context.Context, n *OrganizationTemplate) {
|
|
f(ctx, n)
|
|
}
|
|
|
|
type OrganizationModSlice []OrganizationMod
|
|
|
|
func (mods OrganizationModSlice) Apply(ctx context.Context, n *OrganizationTemplate) {
|
|
for _, f := range mods {
|
|
f.Apply(ctx, n)
|
|
}
|
|
}
|
|
|
|
// OrganizationTemplate is an object representing the database table.
|
|
// all columns are optional and should be set by mods
|
|
type OrganizationTemplate struct {
|
|
ID func() int32
|
|
Name func() null.Val[string]
|
|
|
|
r organizationR
|
|
f *Factory
|
|
|
|
alreadyPersisted bool
|
|
}
|
|
|
|
type organizationR struct {
|
|
User []*organizationRUserR
|
|
}
|
|
|
|
type organizationRUserR struct {
|
|
number int
|
|
o *UserTemplate
|
|
}
|
|
|
|
// Apply mods to the OrganizationTemplate
|
|
func (o *OrganizationTemplate) Apply(ctx context.Context, mods ...OrganizationMod) {
|
|
for _, mod := range mods {
|
|
mod.Apply(ctx, o)
|
|
}
|
|
}
|
|
|
|
// setModelRels creates and sets the relationships on *models.Organization
|
|
// according to the relationships in the template. Nothing is inserted into the db
|
|
func (t OrganizationTemplate) setModelRels(o *models.Organization) {
|
|
if t.r.User != nil {
|
|
rel := models.UserSlice{}
|
|
for _, r := range t.r.User {
|
|
related := r.o.BuildMany(r.number)
|
|
for _, rel := range related {
|
|
rel.OrganizationID = null.From(o.ID) // h2
|
|
rel.R.Organization = o
|
|
}
|
|
rel = append(rel, related...)
|
|
}
|
|
o.R.User = rel
|
|
}
|
|
}
|
|
|
|
// BuildSetter returns an *models.OrganizationSetter
|
|
// this does nothing with the relationship templates
|
|
func (o OrganizationTemplate) BuildSetter() *models.OrganizationSetter {
|
|
m := &models.OrganizationSetter{}
|
|
|
|
if o.ID != nil {
|
|
val := o.ID()
|
|
m.ID = omit.From(val)
|
|
}
|
|
if o.Name != nil {
|
|
val := o.Name()
|
|
m.Name = omitnull.FromNull(val)
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildManySetter returns an []*models.OrganizationSetter
|
|
// this does nothing with the relationship templates
|
|
func (o OrganizationTemplate) BuildManySetter(number int) []*models.OrganizationSetter {
|
|
m := make([]*models.OrganizationSetter, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.BuildSetter()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// Build returns an *models.Organization
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use OrganizationTemplate.Create
|
|
func (o OrganizationTemplate) Build() *models.Organization {
|
|
m := &models.Organization{}
|
|
|
|
if o.ID != nil {
|
|
m.ID = o.ID()
|
|
}
|
|
if o.Name != nil {
|
|
m.Name = o.Name()
|
|
}
|
|
|
|
o.setModelRels(m)
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildMany returns an models.OrganizationSlice
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use OrganizationTemplate.CreateMany
|
|
func (o OrganizationTemplate) BuildMany(number int) models.OrganizationSlice {
|
|
m := make(models.OrganizationSlice, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.Build()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func ensureCreatableOrganization(m *models.OrganizationSetter) {
|
|
}
|
|
|
|
// insertOptRels creates and inserts any optional the relationships on *models.Organization
|
|
// according to the relationships in the template.
|
|
// any required relationship should have already exist on the model
|
|
func (o *OrganizationTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Organization) error {
|
|
var err error
|
|
|
|
isUserDone, _ := organizationRelUserCtx.Value(ctx)
|
|
if !isUserDone && o.r.User != nil {
|
|
ctx = organizationRelUserCtx.WithValue(ctx, true)
|
|
for _, r := range o.r.User {
|
|
if r.o.alreadyPersisted {
|
|
m.R.User = append(m.R.User, r.o.Build())
|
|
} else {
|
|
rel0, err := r.o.CreateMany(ctx, exec, r.number)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = m.AttachUser(ctx, exec, rel0...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// Create builds a organization and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o *OrganizationTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Organization, error) {
|
|
var err error
|
|
opt := o.BuildSetter()
|
|
ensureCreatableOrganization(opt)
|
|
|
|
m, err := models.Organizations.Insert(opt).One(ctx, exec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := o.insertOptRels(ctx, exec, m); err != nil {
|
|
return nil, err
|
|
}
|
|
return m, err
|
|
}
|
|
|
|
// MustCreate builds a organization and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o *OrganizationTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Organization {
|
|
m, err := o.Create(ctx, exec)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateOrFail builds a organization 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 *OrganizationTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Organization {
|
|
tb.Helper()
|
|
m, err := o.Create(ctx, exec)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateMany builds multiple organizations and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o OrganizationTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.OrganizationSlice, error) {
|
|
var err error
|
|
m := make(models.OrganizationSlice, number)
|
|
|
|
for i := range m {
|
|
m[i], err = o.Create(ctx, exec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
// MustCreateMany builds multiple organizations and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o OrganizationTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.OrganizationSlice {
|
|
m, err := o.CreateMany(ctx, exec, number)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateManyOrFail builds multiple organizations 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 OrganizationTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.OrganizationSlice {
|
|
tb.Helper()
|
|
m, err := o.CreateMany(ctx, exec, number)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Organization has methods that act as mods for the OrganizationTemplate
|
|
var OrganizationMods organizationMods
|
|
|
|
type organizationMods struct{}
|
|
|
|
func (m organizationMods) RandomizeAllColumns(f *faker.Faker) OrganizationMod {
|
|
return OrganizationModSlice{
|
|
OrganizationMods.RandomID(f),
|
|
OrganizationMods.RandomName(f),
|
|
}
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m organizationMods) ID(val int32) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.ID = func() int32 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m organizationMods) IDFunc(f func() int32) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.ID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m organizationMods) UnsetID() OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
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 organizationMods) RandomID(f *faker.Faker) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.ID = func() int32 {
|
|
return random_int32(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m organizationMods) Name(val null.Val[string]) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.Name = func() null.Val[string] { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m organizationMods) NameFunc(f func() null.Val[string]) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.Name = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m organizationMods) UnsetName() OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.Name = 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 organizationMods) RandomName(f *faker.Faker) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.Name = func() null.Val[string] {
|
|
if f == nil {
|
|
f = &defaultFaker
|
|
}
|
|
|
|
val := random_string(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 organizationMods) RandomNameNotNull(f *faker.Faker) OrganizationMod {
|
|
return OrganizationModFunc(func(_ context.Context, o *OrganizationTemplate) {
|
|
o.Name = func() null.Val[string] {
|
|
if f == nil {
|
|
f = &defaultFaker
|
|
}
|
|
|
|
val := random_string(f)
|
|
return null.From(val)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) WithParentsCascading() OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
if isDone, _ := organizationWithParentsCascadingCtx.Value(ctx); isDone {
|
|
return
|
|
}
|
|
ctx = organizationWithParentsCascadingCtx.WithValue(ctx, true)
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) WithUser(number int, related *UserTemplate) OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
o.r.User = []*organizationRUserR{{
|
|
number: number,
|
|
o: related,
|
|
}}
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) WithNewUser(number int, mods ...UserMod) OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
related := o.f.NewUserWithContext(ctx, mods...)
|
|
m.WithUser(number, related).Apply(ctx, o)
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) AddUser(number int, related *UserTemplate) OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
o.r.User = append(o.r.User, &organizationRUserR{
|
|
number: number,
|
|
o: related,
|
|
})
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) AddNewUser(number int, mods ...UserMod) OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
related := o.f.NewUserWithContext(ctx, mods...)
|
|
m.AddUser(number, related).Apply(ctx, o)
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) AddExistingUser(existingModels ...*models.User) OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
for _, em := range existingModels {
|
|
o.r.User = append(o.r.User, &organizationRUserR{
|
|
o: o.f.FromExistingUser(em),
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m organizationMods) WithoutUser() OrganizationMod {
|
|
return OrganizationModFunc(func(ctx context.Context, o *OrganizationTemplate) {
|
|
o.r.User = nil
|
|
})
|
|
}
|