383 lines
10 KiB
Go
383 lines
10 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"
|
|
|
|
"github.com/Gleipnir-Technology/bob"
|
|
models "github.com/Gleipnir-Technology/nidus-sync/db/models"
|
|
"github.com/aarondl/opt/omit"
|
|
"github.com/jaswdr/faker/v2"
|
|
)
|
|
|
|
type ParcelMod interface {
|
|
Apply(context.Context, *ParcelTemplate)
|
|
}
|
|
|
|
type ParcelModFunc func(context.Context, *ParcelTemplate)
|
|
|
|
func (f ParcelModFunc) Apply(ctx context.Context, n *ParcelTemplate) {
|
|
f(ctx, n)
|
|
}
|
|
|
|
type ParcelModSlice []ParcelMod
|
|
|
|
func (mods ParcelModSlice) Apply(ctx context.Context, n *ParcelTemplate) {
|
|
for _, f := range mods {
|
|
f.Apply(ctx, n)
|
|
}
|
|
}
|
|
|
|
// ParcelTemplate is an object representing the database table.
|
|
// all columns are optional and should be set by mods
|
|
type ParcelTemplate struct {
|
|
Apn func() string
|
|
Description func() string
|
|
ID func() int32
|
|
Geometry func() string
|
|
|
|
f *Factory
|
|
|
|
alreadyPersisted bool
|
|
}
|
|
|
|
// Apply mods to the ParcelTemplate
|
|
func (o *ParcelTemplate) Apply(ctx context.Context, mods ...ParcelMod) {
|
|
for _, mod := range mods {
|
|
mod.Apply(ctx, o)
|
|
}
|
|
}
|
|
|
|
// setModelRels creates and sets the relationships on *models.Parcel
|
|
// according to the relationships in the template. Nothing is inserted into the db
|
|
func (t ParcelTemplate) setModelRels(o *models.Parcel) {}
|
|
|
|
// BuildSetter returns an *models.ParcelSetter
|
|
// this does nothing with the relationship templates
|
|
func (o ParcelTemplate) BuildSetter() *models.ParcelSetter {
|
|
m := &models.ParcelSetter{}
|
|
|
|
if o.Apn != nil {
|
|
val := o.Apn()
|
|
m.Apn = omit.From(val)
|
|
}
|
|
if o.Description != nil {
|
|
val := o.Description()
|
|
m.Description = omit.From(val)
|
|
}
|
|
if o.ID != nil {
|
|
val := o.ID()
|
|
m.ID = omit.From(val)
|
|
}
|
|
if o.Geometry != nil {
|
|
val := o.Geometry()
|
|
m.Geometry = omit.From(val)
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildManySetter returns an []*models.ParcelSetter
|
|
// this does nothing with the relationship templates
|
|
func (o ParcelTemplate) BuildManySetter(number int) []*models.ParcelSetter {
|
|
m := make([]*models.ParcelSetter, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.BuildSetter()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// Build returns an *models.Parcel
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use ParcelTemplate.Create
|
|
func (o ParcelTemplate) Build() *models.Parcel {
|
|
m := &models.Parcel{}
|
|
|
|
if o.Apn != nil {
|
|
m.Apn = o.Apn()
|
|
}
|
|
if o.Description != nil {
|
|
m.Description = o.Description()
|
|
}
|
|
if o.ID != nil {
|
|
m.ID = o.ID()
|
|
}
|
|
if o.Geometry != nil {
|
|
m.Geometry = o.Geometry()
|
|
}
|
|
|
|
o.setModelRels(m)
|
|
|
|
return m
|
|
}
|
|
|
|
// BuildMany returns an models.ParcelSlice
|
|
// Related objects are also created and placed in the .R field
|
|
// NOTE: Objects are not inserted into the database. Use ParcelTemplate.CreateMany
|
|
func (o ParcelTemplate) BuildMany(number int) models.ParcelSlice {
|
|
m := make(models.ParcelSlice, number)
|
|
|
|
for i := range m {
|
|
m[i] = o.Build()
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
func ensureCreatableParcel(m *models.ParcelSetter) {
|
|
if !(m.Apn.IsValue()) {
|
|
val := random_string(nil)
|
|
m.Apn = omit.From(val)
|
|
}
|
|
if !(m.Description.IsValue()) {
|
|
val := random_string(nil)
|
|
m.Description = omit.From(val)
|
|
}
|
|
if !(m.Geometry.IsValue()) {
|
|
val := random_string(nil)
|
|
m.Geometry = omit.From(val)
|
|
}
|
|
}
|
|
|
|
// insertOptRels creates and inserts any optional the relationships on *models.Parcel
|
|
// according to the relationships in the template.
|
|
// any required relationship should have already exist on the model
|
|
func (o *ParcelTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.Parcel) error {
|
|
var err error
|
|
|
|
return err
|
|
}
|
|
|
|
// Create builds a parcel and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o *ParcelTemplate) Create(ctx context.Context, exec bob.Executor) (*models.Parcel, error) {
|
|
var err error
|
|
opt := o.BuildSetter()
|
|
ensureCreatableParcel(opt)
|
|
|
|
m, err := models.Parcels.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 parcel and inserts it into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o *ParcelTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.Parcel {
|
|
m, err := o.Create(ctx, exec)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateOrFail builds a parcel 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 *ParcelTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.Parcel {
|
|
tb.Helper()
|
|
m, err := o.Create(ctx, exec)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateMany builds multiple parcels and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
func (o ParcelTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.ParcelSlice, error) {
|
|
var err error
|
|
m := make(models.ParcelSlice, number)
|
|
|
|
for i := range m {
|
|
m[i], err = o.Create(ctx, exec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return m, nil
|
|
}
|
|
|
|
// MustCreateMany builds multiple parcels and inserts them into the database
|
|
// Relations objects are also inserted and placed in the .R field
|
|
// panics if an error occurs
|
|
func (o ParcelTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.ParcelSlice {
|
|
m, err := o.CreateMany(ctx, exec, number)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// CreateManyOrFail builds multiple parcels 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 ParcelTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.ParcelSlice {
|
|
tb.Helper()
|
|
m, err := o.CreateMany(ctx, exec, number)
|
|
if err != nil {
|
|
tb.Fatal(err)
|
|
return nil
|
|
}
|
|
return m
|
|
}
|
|
|
|
// Parcel has methods that act as mods for the ParcelTemplate
|
|
var ParcelMods parcelMods
|
|
|
|
type parcelMods struct{}
|
|
|
|
func (m parcelMods) RandomizeAllColumns(f *faker.Faker) ParcelMod {
|
|
return ParcelModSlice{
|
|
ParcelMods.RandomApn(f),
|
|
ParcelMods.RandomDescription(f),
|
|
ParcelMods.RandomID(f),
|
|
ParcelMods.RandomGeometry(f),
|
|
}
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m parcelMods) Apn(val string) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Apn = func() string { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m parcelMods) ApnFunc(f func() string) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Apn = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m parcelMods) UnsetApn() ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Apn = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m parcelMods) RandomApn(f *faker.Faker) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Apn = func() string {
|
|
return random_string(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m parcelMods) Description(val string) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Description = func() string { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m parcelMods) DescriptionFunc(f func() string) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Description = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m parcelMods) UnsetDescription() ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
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 parcelMods) RandomDescription(f *faker.Faker) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Description = func() string {
|
|
return random_string(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m parcelMods) ID(val int32) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.ID = func() int32 { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m parcelMods) IDFunc(f func() int32) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.ID = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m parcelMods) UnsetID() ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
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 parcelMods) RandomID(f *faker.Faker) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.ID = func() int32 {
|
|
return random_int32(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Set the model columns to this value
|
|
func (m parcelMods) Geometry(val string) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Geometry = func() string { return val }
|
|
})
|
|
}
|
|
|
|
// Set the Column from the function
|
|
func (m parcelMods) GeometryFunc(f func() string) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Geometry = f
|
|
})
|
|
}
|
|
|
|
// Clear any values for the column
|
|
func (m parcelMods) UnsetGeometry() ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Geometry = nil
|
|
})
|
|
}
|
|
|
|
// Generates a random value for the column using the given faker
|
|
// if faker is nil, a default faker is used
|
|
func (m parcelMods) RandomGeometry(f *faker.Faker) ParcelMod {
|
|
return ParcelModFunc(func(_ context.Context, o *ParcelTemplate) {
|
|
o.Geometry = func() string {
|
|
return random_string(f)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (m parcelMods) WithParentsCascading() ParcelMod {
|
|
return ParcelModFunc(func(ctx context.Context, o *ParcelTemplate) {
|
|
if isDone, _ := parcelWithParentsCascadingCtx.Value(ctx); isDone {
|
|
return
|
|
}
|
|
ctx = parcelWithParentsCascadingCtx.WithValue(ctx, true)
|
|
})
|
|
}
|