nidus-sync/db/factory/lead.bob.go

841 lines
22 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 LeadMod interface {
Apply(context.Context, *LeadTemplate)
}
type LeadModFunc func(context.Context, *LeadTemplate)
func (f LeadModFunc) Apply(ctx context.Context, n *LeadTemplate) {
f(ctx, n)
}
type LeadModSlice []LeadMod
func (mods LeadModSlice) Apply(ctx context.Context, n *LeadTemplate) {
for _, f := range mods {
f.Apply(ctx, n)
}
}
// LeadTemplate is an object representing the database table.
// all columns are optional and should be set by mods
type LeadTemplate struct {
Created func() time.Time
Creator func() int32
ID func() int32
OrganizationID func() int32
SiteID func() null.Val[int32]
SiteVersion func() null.Val[int32]
Type func() enums.Leadtype
r leadR
f *Factory
alreadyPersisted bool
}
type leadR struct {
ComplianceReportRequests []*leadRComplianceReportRequestsR
CreatorUser *leadRCreatorUserR
Organization *leadROrganizationR
Site *leadRSiteR
}
type leadRComplianceReportRequestsR struct {
number int
o *ComplianceReportRequestTemplate
}
type leadRCreatorUserR struct {
o *UserTemplate
}
type leadROrganizationR struct {
o *OrganizationTemplate
}
type leadRSiteR struct {
o *SiteTemplate
}
// Apply mods to the LeadTemplate
func (o *LeadTemplate) Apply(ctx context.Context, mods ...LeadMod) {
for _, mod := range mods {
mod.Apply(ctx, o)
}
}
// setModelRels creates and sets the relationships on *models.Lead
// according to the relationships in the template. Nothing is inserted into the db
func (t LeadTemplate) setModelRels(o *models.Lead) {
if t.r.ComplianceReportRequests != nil {
rel := models.ComplianceReportRequestSlice{}
for _, r := range t.r.ComplianceReportRequests {
related := r.o.BuildMany(r.number)
for _, rel := range related {
rel.LeadID = null.From(o.ID) // h2
rel.R.Lead = o
}
rel = append(rel, related...)
}
o.R.ComplianceReportRequests = rel
}
if t.r.CreatorUser != nil {
rel := t.r.CreatorUser.o.Build()
rel.R.CreatorLeads = append(rel.R.CreatorLeads, o)
o.Creator = rel.ID // h2
o.R.CreatorUser = rel
}
if t.r.Organization != nil {
rel := t.r.Organization.o.Build()
rel.R.Leads = append(rel.R.Leads, o)
o.OrganizationID = rel.ID // h2
o.R.Organization = rel
}
if t.r.Site != nil {
rel := t.r.Site.o.Build()
rel.R.Leads = append(rel.R.Leads, o)
o.SiteID = null.From(rel.ID) // h2
o.SiteVersion = null.From(rel.Version) // h2
o.R.Site = rel
}
}
// BuildSetter returns an *models.LeadSetter
// this does nothing with the relationship templates
func (o LeadTemplate) BuildSetter() *models.LeadSetter {
m := &models.LeadSetter{}
if o.Created != nil {
val := o.Created()
m.Created = omit.From(val)
}
if o.Creator != nil {
val := o.Creator()
m.Creator = omit.From(val)
}
if o.ID != nil {
val := o.ID()
m.ID = omit.From(val)
}
if o.OrganizationID != nil {
val := o.OrganizationID()
m.OrganizationID = omit.From(val)
}
if o.SiteID != nil {
val := o.SiteID()
m.SiteID = omitnull.FromNull(val)
}
if o.SiteVersion != nil {
val := o.SiteVersion()
m.SiteVersion = omitnull.FromNull(val)
}
if o.Type != nil {
val := o.Type()
m.Type = omit.From(val)
}
return m
}
// BuildManySetter returns an []*models.LeadSetter
// this does nothing with the relationship templates
func (o LeadTemplate) BuildManySetter(number int) []*models.LeadSetter {
m := make([]*models.LeadSetter, number)
for i := range m {
m[i] = o.BuildSetter()
}
return m
}
// Build returns an *models.Lead
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use LeadTemplate.Create
func (o LeadTemplate) Build() *models.Lead {
m := &models.Lead{}
if o.Created != nil {
m.Created = o.Created()
}
if o.Creator != nil {
m.Creator = o.Creator()
}
if o.ID != nil {
m.ID = o.ID()
}
if o.OrganizationID != nil {
m.OrganizationID = o.OrganizationID()
}
if o.SiteID != nil {
m.SiteID = o.SiteID()
}
if o.SiteVersion != nil {
m.SiteVersion = o.SiteVersion()
}
if o.Type != nil {
m.Type = o.Type()
}
o.setModelRels(m)
return m
}
// BuildMany returns an models.LeadSlice
// Related objects are also created and placed in the .R field
// NOTE: Objects are not inserted into the database. Use LeadTemplate.CreateMany
func (o LeadTemplate) BuildMany(number int) models.LeadSlice {
m := make(models.LeadSlice, number)
for i := range m {
m[i] = o.Build()
}
return m
}
func ensureCreatableLead(m *models.LeadSetter) {
if !(m.Created.IsValue()) {
val := random_time_Time(nil)
m.Created = omit.From(val)
}
if !(m.Creator.IsValue()) {
val := random_int32(nil)
m.Creator = omit.From(val)
}
if !(m.OrganizationID.IsValue()) {
val := random_int32(nil)
m.OrganizationID = omit.From(val)
}
if !(m.Type.IsValue()) {
val := random_enums_Leadtype(nil)
m.Type = omit.From(val)
}
}
// insertOptRels creates and inserts any optional the relationships on *models.Lead
// according to the relationships in the template.
// any required relationship should have already exist on the model
func (o *LeadTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Lead) error {
var err error
isComplianceReportRequestsDone, _ := leadRelComplianceReportRequestsCtx.Value(ctx)
if !isComplianceReportRequestsDone && o.r.ComplianceReportRequests != nil {
ctx = leadRelComplianceReportRequestsCtx.WithValue(ctx, true)
for _, r := range o.r.ComplianceReportRequests {
if r.o.alreadyPersisted {
m.R.ComplianceReportRequests = append(m.R.ComplianceReportRequests, r.o.Build())
} else {
rel0, err := r.o.CreateMany(ctx, exec, r.number)
if err != nil {
return err
}
err = m.AttachComplianceReportRequests(ctx, exec, rel0...)
if err != nil {
return err
}
}
}
}
isSiteDone, _ := leadRelSiteCtx.Value(ctx)
if !isSiteDone && o.r.Site != nil {
ctx = leadRelSiteCtx.WithValue(ctx, true)
if o.r.Site.o.alreadyPersisted {
m.R.Site = o.r.Site.o.Build()
} else {
var rel3 *models.Site
rel3, err = o.r.Site.o.Create(ctx, exec)
if err != nil {
return err
}
err = m.AttachSite(ctx, exec, rel3)
if err != nil {
return err
}
}
}
return err
}
// Create builds a lead and inserts it into the database
// Relations objects are also inserted and placed in the .R field
func (o *LeadTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Lead, error) {
var err error
opt := o.BuildSetter()
ensureCreatableLead(opt)
if o.r.CreatorUser == nil {
LeadMods.WithNewCreatorUser().Apply(ctx, o)
}
var rel1 *models.User
if o.r.CreatorUser.o.alreadyPersisted {
rel1 = o.r.CreatorUser.o.Build()
} else {
rel1, err = o.r.CreatorUser.o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
opt.Creator = omit.From(rel1.ID)
if o.r.Organization == nil {
LeadMods.WithNewOrganization().Apply(ctx, o)
}
var rel2 *models.Organization
if o.r.Organization.o.alreadyPersisted {
rel2 = o.r.Organization.o.Build()
} else {
rel2, err = o.r.Organization.o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
opt.OrganizationID = omit.From(rel2.ID)
m, err := models.Leads.Insert(opt).One(ctx, exec)
if err != nil {
return nil, err
}
m.R.CreatorUser = rel1
m.R.Organization = rel2
if err := o.insertOptRels(ctx, exec, m); err != nil {
return nil, err
}
return m, err
}
// MustCreate builds a lead and inserts it into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o *LeadTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Lead {
m, err := o.Create(ctx, exec)
if err != nil {
panic(err)
}
return m
}
// CreateOrFail builds a lead 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 *LeadTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Lead {
tb.Helper()
m, err := o.Create(ctx, exec)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// CreateMany builds multiple leads and inserts them into the database
// Relations objects are also inserted and placed in the .R field
func (o LeadTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.LeadSlice, error) {
var err error
m := make(models.LeadSlice, number)
for i := range m {
m[i], err = o.Create(ctx, exec)
if err != nil {
return nil, err
}
}
return m, nil
}
// MustCreateMany builds multiple leads and inserts them into the database
// Relations objects are also inserted and placed in the .R field
// panics if an error occurs
func (o LeadTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.LeadSlice {
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
panic(err)
}
return m
}
// CreateManyOrFail builds multiple leads 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 LeadTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.LeadSlice {
tb.Helper()
m, err := o.CreateMany(ctx, exec, number)
if err != nil {
tb.Fatal(err)
return nil
}
return m
}
// Lead has methods that act as mods for the LeadTemplate
var LeadMods leadMods
type leadMods struct{}
func (m leadMods) RandomizeAllColumns(f *faker.Faker) LeadMod {
return LeadModSlice{
LeadMods.RandomCreated(f),
LeadMods.RandomCreator(f),
LeadMods.RandomID(f),
LeadMods.RandomOrganizationID(f),
LeadMods.RandomSiteID(f),
LeadMods.RandomSiteVersion(f),
LeadMods.RandomType(f),
}
}
// Set the model columns to this value
func (m leadMods) Created(val time.Time) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Created = func() time.Time { return val }
})
}
// Set the Column from the function
func (m leadMods) CreatedFunc(f func() time.Time) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Created = f
})
}
// Clear any values for the column
func (m leadMods) UnsetCreated() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
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 leadMods) RandomCreated(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Created = func() time.Time {
return random_time_Time(f)
}
})
}
// Set the model columns to this value
func (m leadMods) Creator(val int32) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Creator = func() int32 { return val }
})
}
// Set the Column from the function
func (m leadMods) CreatorFunc(f func() int32) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Creator = f
})
}
// Clear any values for the column
func (m leadMods) UnsetCreator() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Creator = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m leadMods) RandomCreator(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Creator = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m leadMods) ID(val int32) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.ID = func() int32 { return val }
})
}
// Set the Column from the function
func (m leadMods) IDFunc(f func() int32) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.ID = f
})
}
// Clear any values for the column
func (m leadMods) UnsetID() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
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 leadMods) RandomID(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.ID = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m leadMods) OrganizationID(val int32) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.OrganizationID = func() int32 { return val }
})
}
// Set the Column from the function
func (m leadMods) OrganizationIDFunc(f func() int32) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.OrganizationID = f
})
}
// Clear any values for the column
func (m leadMods) UnsetOrganizationID() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.OrganizationID = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m leadMods) RandomOrganizationID(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.OrganizationID = func() int32 {
return random_int32(f)
}
})
}
// Set the model columns to this value
func (m leadMods) SiteID(val null.Val[int32]) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteID = func() null.Val[int32] { return val }
})
}
// Set the Column from the function
func (m leadMods) SiteIDFunc(f func() null.Val[int32]) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteID = f
})
}
// Clear any values for the column
func (m leadMods) UnsetSiteID() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
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 leadMods) RandomSiteID(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
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 leadMods) RandomSiteIDNotNull(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteID = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
val := random_int32(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m leadMods) SiteVersion(val null.Val[int32]) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteVersion = func() null.Val[int32] { return val }
})
}
// Set the Column from the function
func (m leadMods) SiteVersionFunc(f func() null.Val[int32]) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteVersion = f
})
}
// Clear any values for the column
func (m leadMods) UnsetSiteVersion() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteVersion = 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 leadMods) RandomSiteVersion(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteVersion = 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 leadMods) RandomSiteVersionNotNull(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.SiteVersion = func() null.Val[int32] {
if f == nil {
f = &defaultFaker
}
val := random_int32(f)
return null.From(val)
}
})
}
// Set the model columns to this value
func (m leadMods) Type(val enums.Leadtype) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Type = func() enums.Leadtype { return val }
})
}
// Set the Column from the function
func (m leadMods) TypeFunc(f func() enums.Leadtype) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Type = f
})
}
// Clear any values for the column
func (m leadMods) UnsetType() LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Type = nil
})
}
// Generates a random value for the column using the given faker
// if faker is nil, a default faker is used
func (m leadMods) RandomType(f *faker.Faker) LeadMod {
return LeadModFunc(func(_ context.Context, o *LeadTemplate) {
o.Type = func() enums.Leadtype {
return random_enums_Leadtype(f)
}
})
}
func (m leadMods) WithParentsCascading() LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
if isDone, _ := leadWithParentsCascadingCtx.Value(ctx); isDone {
return
}
ctx = leadWithParentsCascadingCtx.WithValue(ctx, true)
{
related := o.f.NewUserWithContext(ctx, UserMods.WithParentsCascading())
m.WithCreatorUser(related).Apply(ctx, o)
}
{
related := o.f.NewOrganizationWithContext(ctx, OrganizationMods.WithParentsCascading())
m.WithOrganization(related).Apply(ctx, o)
}
{
related := o.f.NewSiteWithContext(ctx, SiteMods.WithParentsCascading())
m.WithSite(related).Apply(ctx, o)
}
})
}
func (m leadMods) WithCreatorUser(rel *UserTemplate) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.CreatorUser = &leadRCreatorUserR{
o: rel,
}
})
}
func (m leadMods) WithNewCreatorUser(mods ...UserMod) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
related := o.f.NewUserWithContext(ctx, mods...)
m.WithCreatorUser(related).Apply(ctx, o)
})
}
func (m leadMods) WithExistingCreatorUser(em *models.User) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.CreatorUser = &leadRCreatorUserR{
o: o.f.FromExistingUser(em),
}
})
}
func (m leadMods) WithoutCreatorUser() LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.CreatorUser = nil
})
}
func (m leadMods) WithOrganization(rel *OrganizationTemplate) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.Organization = &leadROrganizationR{
o: rel,
}
})
}
func (m leadMods) WithNewOrganization(mods ...OrganizationMod) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
related := o.f.NewOrganizationWithContext(ctx, mods...)
m.WithOrganization(related).Apply(ctx, o)
})
}
func (m leadMods) WithExistingOrganization(em *models.Organization) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.Organization = &leadROrganizationR{
o: o.f.FromExistingOrganization(em),
}
})
}
func (m leadMods) WithoutOrganization() LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.Organization = nil
})
}
func (m leadMods) WithSite(rel *SiteTemplate) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.Site = &leadRSiteR{
o: rel,
}
})
}
func (m leadMods) WithNewSite(mods ...SiteMod) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
related := o.f.NewSiteWithContext(ctx, mods...)
m.WithSite(related).Apply(ctx, o)
})
}
func (m leadMods) WithExistingSite(em *models.Site) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.Site = &leadRSiteR{
o: o.f.FromExistingSite(em),
}
})
}
func (m leadMods) WithoutSite() LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.Site = nil
})
}
func (m leadMods) WithComplianceReportRequests(number int, related *ComplianceReportRequestTemplate) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.ComplianceReportRequests = []*leadRComplianceReportRequestsR{{
number: number,
o: related,
}}
})
}
func (m leadMods) WithNewComplianceReportRequests(number int, mods ...ComplianceReportRequestMod) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
related := o.f.NewComplianceReportRequestWithContext(ctx, mods...)
m.WithComplianceReportRequests(number, related).Apply(ctx, o)
})
}
func (m leadMods) AddComplianceReportRequests(number int, related *ComplianceReportRequestTemplate) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.ComplianceReportRequests = append(o.r.ComplianceReportRequests, &leadRComplianceReportRequestsR{
number: number,
o: related,
})
})
}
func (m leadMods) AddNewComplianceReportRequests(number int, mods ...ComplianceReportRequestMod) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
related := o.f.NewComplianceReportRequestWithContext(ctx, mods...)
m.AddComplianceReportRequests(number, related).Apply(ctx, o)
})
}
func (m leadMods) AddExistingComplianceReportRequests(existingModels ...*models.ComplianceReportRequest) LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
for _, em := range existingModels {
o.r.ComplianceReportRequests = append(o.r.ComplianceReportRequests, &leadRComplianceReportRequestsR{
o: o.f.FromExistingComplianceReportRequest(em),
})
}
})
}
func (m leadMods) WithoutComplianceReportRequests() LeadMod {
return LeadModFunc(func(ctx context.Context, o *LeadTemplate) {
o.r.ComplianceReportRequests = nil
})
}