WIP: stuff
This commit is contained in:
36
server/internal/models/factory/bobfactory_context.bob.go
Normal file
36
server/internal/models/factory/bobfactory_context.bob.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
var (
|
||||
fileCtx = newContextual[*models.File]("file")
|
||||
itemCtx = newContextual[*models.Item]("item")
|
||||
userCtx = newContextual[*models.User]("user")
|
||||
)
|
||||
|
||||
// Contextual is a convienience wrapper around context.WithValue and context.Value
|
||||
type contextual[V any] struct {
|
||||
key contextKey
|
||||
}
|
||||
|
||||
func newContextual[V any](key string) contextual[V] {
|
||||
return contextual[V]{key: contextKey(key)}
|
||||
}
|
||||
|
||||
func (k contextual[V]) WithValue(ctx context.Context, val V) context.Context {
|
||||
return context.WithValue(ctx, k.key, val)
|
||||
}
|
||||
|
||||
func (k contextual[V]) Value(ctx context.Context) (V, bool) {
|
||||
v, ok := ctx.Value(k.key).(V)
|
||||
return v, ok
|
||||
}
|
74
server/internal/models/factory/bobfactory_main.bob.go
Normal file
74
server/internal/models/factory/bobfactory_main.bob.go
Normal file
@ -0,0 +1,74 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
type Factory struct {
|
||||
baseFileMods FileModSlice
|
||||
baseItemMods ItemModSlice
|
||||
baseUserMods UserModSlice
|
||||
}
|
||||
|
||||
func New() *Factory {
|
||||
return &Factory{}
|
||||
}
|
||||
|
||||
func (f *Factory) NewFile(mods ...FileMod) *FileTemplate {
|
||||
o := &FileTemplate{f: f}
|
||||
|
||||
if f != nil {
|
||||
f.baseFileMods.Apply(o)
|
||||
}
|
||||
|
||||
FileModSlice(mods).Apply(o)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (f *Factory) NewItem(mods ...ItemMod) *ItemTemplate {
|
||||
o := &ItemTemplate{f: f}
|
||||
|
||||
if f != nil {
|
||||
f.baseItemMods.Apply(o)
|
||||
}
|
||||
|
||||
ItemModSlice(mods).Apply(o)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (f *Factory) NewUser(mods ...UserMod) *UserTemplate {
|
||||
o := &UserTemplate{f: f}
|
||||
|
||||
if f != nil {
|
||||
f.baseUserMods.Apply(o)
|
||||
}
|
||||
|
||||
UserModSlice(mods).Apply(o)
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (f *Factory) ClearBaseFileMods() {
|
||||
f.baseFileMods = nil
|
||||
}
|
||||
|
||||
func (f *Factory) AddBaseFileMod(mods ...FileMod) {
|
||||
f.baseFileMods = append(f.baseFileMods, mods...)
|
||||
}
|
||||
|
||||
func (f *Factory) ClearBaseItemMods() {
|
||||
f.baseItemMods = nil
|
||||
}
|
||||
|
||||
func (f *Factory) AddBaseItemMod(mods ...ItemMod) {
|
||||
f.baseItemMods = append(f.baseItemMods, mods...)
|
||||
}
|
||||
|
||||
func (f *Factory) ClearBaseUserMods() {
|
||||
f.baseUserMods = nil
|
||||
}
|
||||
|
||||
func (f *Factory) AddBaseUserMod(mods ...UserMod) {
|
||||
f.baseUserMods = append(f.baseUserMods, mods...)
|
||||
}
|
56
server/internal/models/factory/bobfactory_random.bob.go
Normal file
56
server/internal/models/factory/bobfactory_random.bob.go
Normal file
@ -0,0 +1,56 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jaswdr/faker/v2"
|
||||
)
|
||||
|
||||
var defaultFaker = faker.New()
|
||||
|
||||
func random___byte(f *faker.Faker) []byte {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return []byte(random_string(f))
|
||||
}
|
||||
|
||||
func random_float32(f *faker.Faker) float32 {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return f.Float32(10, -1_000_000, 1_000_000)
|
||||
}
|
||||
|
||||
func random_int32(f *faker.Faker) int32 {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return f.Int32()
|
||||
}
|
||||
|
||||
func random_string(f *faker.Faker) string {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
return strings.Join(f.Lorem().Words(f.IntBetween(1, 5)), " ")
|
||||
}
|
||||
|
||||
func random_time_Time(f *faker.Faker) time.Time {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
year := time.Hour * 24 * 365
|
||||
min := time.Now().Add(-year)
|
||||
max := time.Now().Add(year)
|
||||
return f.Time().TimeBetween(min, max)
|
||||
}
|
64
server/internal/models/factory/bobfactory_random_test.bob.go
Normal file
64
server/internal/models/factory/bobfactory_random_test.bob.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Code generated by BobGen sqlite (devel). DO NOT EDIT.
|
||||
// This file is meant to be re-generated in place and/or deleted at any time.
|
||||
|
||||
package factory
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRandom_int32(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_int32(nil)
|
||||
val2 := random_int32(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_int32() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_string(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_string(nil)
|
||||
val2 := random_string(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_string() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom___byte(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random___byte(nil)
|
||||
val2 := random___byte(nil)
|
||||
|
||||
if bytes.Equal(val1, val2) {
|
||||
t.Fatalf("random___byte() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_float32(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_float32(nil)
|
||||
val2 := random_float32(nil)
|
||||
|
||||
if val1 == val2 {
|
||||
t.Fatalf("random_float32() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom_time_Time(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
val1 := random_time_Time(nil)
|
||||
val2 := random_time_Time(nil)
|
||||
|
||||
if val1.Equal(val2) {
|
||||
t.Fatalf("random_time_Time() returned the same value twice: %v", val1)
|
||||
}
|
||||
}
|
466
server/internal/models/factory/files.bob.go
Normal file
466
server/internal/models/factory/files.bob.go
Normal file
@ -0,0 +1,466 @@
|
||||
// Code generated by BobGen sqlite (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"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/jaswdr/faker/v2"
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
"github.com/stephenafamo/bob"
|
||||
)
|
||||
|
||||
type FileMod interface {
|
||||
Apply(*FileTemplate)
|
||||
}
|
||||
|
||||
type FileModFunc func(*FileTemplate)
|
||||
|
||||
func (f FileModFunc) Apply(n *FileTemplate) {
|
||||
f(n)
|
||||
}
|
||||
|
||||
type FileModSlice []FileMod
|
||||
|
||||
func (mods FileModSlice) Apply(n *FileTemplate) {
|
||||
for _, f := range mods {
|
||||
f.Apply(n)
|
||||
}
|
||||
}
|
||||
|
||||
// FileTemplate is an object representing the database table.
|
||||
// all columns are optional and should be set by mods
|
||||
type FileTemplate struct {
|
||||
ID func() int32
|
||||
Name func() null.Val[string]
|
||||
Data func() null.Val[[]byte]
|
||||
UserID func() null.Val[int32]
|
||||
|
||||
r fileR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type fileR struct {
|
||||
User *fileRUserR
|
||||
}
|
||||
|
||||
type fileRUserR struct {
|
||||
o *UserTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the FileTemplate
|
||||
func (o *FileTemplate) Apply(mods ...FileMod) {
|
||||
for _, mod := range mods {
|
||||
mod.Apply(o)
|
||||
}
|
||||
}
|
||||
|
||||
// toModel returns an *models.File
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) toModel() *models.File {
|
||||
m := &models.File{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = o.ID()
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = o.Name()
|
||||
}
|
||||
if o.Data != nil {
|
||||
m.Data = o.Data()
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = o.UserID()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// toModels returns an models.FileSlice
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) toModels(number int) models.FileSlice {
|
||||
m := make(models.FileSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.toModel()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// setModelRels creates and sets the relationships on *models.File
|
||||
// according to the relationships in the template. Nothing is inserted into the db
|
||||
func (t FileTemplate) setModelRels(o *models.File) {
|
||||
if t.r.User != nil {
|
||||
rel := t.r.User.o.toModel()
|
||||
rel.R.Files = append(rel.R.Files, o)
|
||||
o.UserID = null.From(rel.ID)
|
||||
o.R.User = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.FileSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) BuildSetter() *models.FileSetter {
|
||||
m := &models.FileSetter{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Name != nil {
|
||||
m.Name = omitnull.FromNull(o.Name())
|
||||
}
|
||||
if o.Data != nil {
|
||||
m.Data = omitnull.FromNull(o.Data())
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = omitnull.FromNull(o.UserID())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildManySetter returns an []*models.FileSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o FileTemplate) BuildManySetter(number int) []*models.FileSetter {
|
||||
m := make([]*models.FileSetter, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.BuildSetter()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Build returns an *models.File
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use FileTemplate.Create
|
||||
func (o FileTemplate) Build() *models.File {
|
||||
m := o.toModel()
|
||||
o.setModelRels(m)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildMany returns an models.FileSlice
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use FileTemplate.CreateMany
|
||||
func (o FileTemplate) BuildMany(number int) models.FileSlice {
|
||||
m := make(models.FileSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.Build()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func ensureCreatableFile(m *models.FileSetter) {
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.File
|
||||
// according to the relationships in the template.
|
||||
// any required relationship should have already exist on the model
|
||||
func (o *FileTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.File) (context.Context, error) {
|
||||
var err error
|
||||
|
||||
if o.r.User != nil {
|
||||
var rel0 *models.User
|
||||
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
err = m.AttachUser(ctx, exec, rel0)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
// Create builds a file and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o *FileTemplate) Create(ctx context.Context, exec bob.Executor) (*models.File, error) {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreate builds a file and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o *FileTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.File {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateOrFail builds a file 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 *FileTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.File {
|
||||
tb.Helper()
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// create builds a file 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 *FileTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.File, error) {
|
||||
var err error
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableFile(opt)
|
||||
|
||||
m, err := models.Files.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = fileCtx.WithValue(ctx, m)
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
|
||||
// CreateMany builds multiple files and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o FileTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.FileSlice, error) {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreateMany builds multiple files and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o FileTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.FileSlice {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateManyOrFail builds multiple files 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 FileTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.FileSlice {
|
||||
tb.Helper()
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// createMany builds multiple files 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 FileTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.FileSlice, error) {
|
||||
var err error
|
||||
m := make(models.FileSlice, number)
|
||||
|
||||
for i := range m {
|
||||
ctx, m[i], err = o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, m, nil
|
||||
}
|
||||
|
||||
// File has methods that act as mods for the FileTemplate
|
||||
var FileMods fileMods
|
||||
|
||||
type fileMods struct{}
|
||||
|
||||
func (m fileMods) RandomizeAllColumns(f *faker.Faker) FileMod {
|
||||
return FileModSlice{
|
||||
FileMods.RandomID(f),
|
||||
FileMods.RandomName(f),
|
||||
FileMods.RandomData(f),
|
||||
FileMods.RandomUserID(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) ID(val int32) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) IDFunc(f func() int32) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetID() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
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 fileMods) RandomID(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.ID = func() int32 {
|
||||
return random_int32(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) Name(val null.Val[string]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) NameFunc(f func() null.Val[string]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetName() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
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 fileMods) RandomName(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Name = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) Data(val null.Val[[]byte]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = func() null.Val[[]byte] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) DataFunc(f func() null.Val[[]byte]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetData() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m fileMods) RandomData(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.Data = func() null.Val[[]byte] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[[]byte](nil)
|
||||
}
|
||||
|
||||
return null.From(random___byte(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m fileMods) UserID(val null.Val[int32]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m fileMods) UserIDFunc(f func() null.Val[int32]) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m fileMods) UnsetUserID() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
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 fileMods) RandomUserID(f *faker.Faker) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.UserID = func() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithUser(rel *UserTemplate) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.User = &fileRUserR{
|
||||
o: rel,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithNewUser(mods ...UserMod) FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
related := o.f.NewUser(mods...)
|
||||
|
||||
m.WithUser(related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m fileMods) WithoutUser() FileMod {
|
||||
return FileModFunc(func(o *FileTemplate) {
|
||||
o.r.User = nil
|
||||
})
|
||||
}
|
608
server/internal/models/factory/items.bob.go
Normal file
608
server/internal/models/factory/items.bob.go
Normal file
@ -0,0 +1,608 @@
|
||||
// Code generated by BobGen sqlite (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/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"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() int32
|
||||
Name func() null.Val[string]
|
||||
Description func() null.Val[string]
|
||||
Price func() null.Val[float32]
|
||||
Quantity func() null.Val[int32]
|
||||
Added func() null.Val[time.Time]
|
||||
UserID func() null.Val[int32]
|
||||
|
||||
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.Description != nil {
|
||||
m.Description = o.Description()
|
||||
}
|
||||
if o.Price != nil {
|
||||
m.Price = o.Price()
|
||||
}
|
||||
if o.Quantity != nil {
|
||||
m.Quantity = o.Quantity()
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = o.Added()
|
||||
}
|
||||
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 = null.From(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 = omitnull.FromNull(o.Name())
|
||||
}
|
||||
if o.Description != nil {
|
||||
m.Description = omitnull.FromNull(o.Description())
|
||||
}
|
||||
if o.Price != nil {
|
||||
m.Price = omitnull.FromNull(o.Price())
|
||||
}
|
||||
if o.Quantity != nil {
|
||||
m.Quantity = omitnull.FromNull(o.Quantity())
|
||||
}
|
||||
if o.Added != nil {
|
||||
m.Added = omitnull.FromNull(o.Added())
|
||||
}
|
||||
if o.UserID != nil {
|
||||
m.UserID = omitnull.FromNull(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) {
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
if o.r.User != nil {
|
||||
var rel0 *models.User
|
||||
ctx, rel0, err = o.r.User.o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
err = m.AttachUser(ctx, exec, rel0)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
m, err := models.Items.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = itemCtx.WithValue(ctx, m)
|
||||
|
||||
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.RandomDescription(f),
|
||||
ItemMods.RandomPrice(f),
|
||||
ItemMods.RandomQuantity(f),
|
||||
ItemMods.RandomAdded(f),
|
||||
ItemMods.RandomUserID(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) ID(val int32) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) IDFunc(f func() int32) 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() int32 {
|
||||
return random_int32(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Name(val null.Val[string]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Name = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) NameFunc(f func() null.Val[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() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Description(val null.Val[string]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Description = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) DescriptionFunc(f func() null.Val[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() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Price(val null.Val[float32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Price = func() null.Val[float32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) PriceFunc(f func() null.Val[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() null.Val[float32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[float32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_float32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Quantity(val null.Val[int32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Quantity = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) QuantityFunc(f func() null.Val[int32]) 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() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) Added(val null.Val[time.Time]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.Added = func() null.Val[time.Time] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) AddedFunc(f func() null.Val[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() null.Val[time.Time] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[time.Time](nil)
|
||||
}
|
||||
|
||||
return null.From(random_time_Time(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m itemMods) UserID(val null.Val[int32]) ItemMod {
|
||||
return ItemModFunc(func(o *ItemTemplate) {
|
||||
o.UserID = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m itemMods) UserIDFunc(f func() null.Val[int32]) 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() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(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
|
||||
})
|
||||
}
|
610
server/internal/models/factory/users.bob.go
Normal file
610
server/internal/models/factory/users.bob.go
Normal file
@ -0,0 +1,610 @@
|
||||
// Code generated by BobGen sqlite (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"
|
||||
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/jaswdr/faker/v2"
|
||||
models "github.com/spotdemo4/trevstack/server/internal/models"
|
||||
"github.com/stephenafamo/bob"
|
||||
)
|
||||
|
||||
type UserMod interface {
|
||||
Apply(*UserTemplate)
|
||||
}
|
||||
|
||||
type UserModFunc func(*UserTemplate)
|
||||
|
||||
func (f UserModFunc) Apply(n *UserTemplate) {
|
||||
f(n)
|
||||
}
|
||||
|
||||
type UserModSlice []UserMod
|
||||
|
||||
func (mods UserModSlice) Apply(n *UserTemplate) {
|
||||
for _, f := range mods {
|
||||
f.Apply(n)
|
||||
}
|
||||
}
|
||||
|
||||
// UserTemplate is an object representing the database table.
|
||||
// all columns are optional and should be set by mods
|
||||
type UserTemplate struct {
|
||||
ID func() int32
|
||||
Username func() null.Val[string]
|
||||
Password func() null.Val[string]
|
||||
ProfilePictureID func() null.Val[int32]
|
||||
Challenge func() null.Val[string]
|
||||
|
||||
r userR
|
||||
f *Factory
|
||||
}
|
||||
|
||||
type userR struct {
|
||||
Files []*userRFilesR
|
||||
Items []*userRItemsR
|
||||
}
|
||||
|
||||
type userRFilesR struct {
|
||||
number int
|
||||
o *FileTemplate
|
||||
}
|
||||
type userRItemsR struct {
|
||||
number int
|
||||
o *ItemTemplate
|
||||
}
|
||||
|
||||
// Apply mods to the UserTemplate
|
||||
func (o *UserTemplate) Apply(mods ...UserMod) {
|
||||
for _, mod := range mods {
|
||||
mod.Apply(o)
|
||||
}
|
||||
}
|
||||
|
||||
// toModel returns an *models.User
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) toModel() *models.User {
|
||||
m := &models.User{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = o.ID()
|
||||
}
|
||||
if o.Username != nil {
|
||||
m.Username = o.Username()
|
||||
}
|
||||
if o.Password != nil {
|
||||
m.Password = o.Password()
|
||||
}
|
||||
if o.ProfilePictureID != nil {
|
||||
m.ProfilePictureID = o.ProfilePictureID()
|
||||
}
|
||||
if o.Challenge != nil {
|
||||
m.Challenge = o.Challenge()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// toModels returns an models.UserSlice
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) toModels(number int) models.UserSlice {
|
||||
m := make(models.UserSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.toModel()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// setModelRels creates and sets the relationships on *models.User
|
||||
// according to the relationships in the template. Nothing is inserted into the db
|
||||
func (t UserTemplate) setModelRels(o *models.User) {
|
||||
if t.r.Files != nil {
|
||||
rel := models.FileSlice{}
|
||||
for _, r := range t.r.Files {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.UserID = null.From(o.ID)
|
||||
rel.R.User = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
}
|
||||
o.R.Files = rel
|
||||
}
|
||||
|
||||
if t.r.Items != nil {
|
||||
rel := models.ItemSlice{}
|
||||
for _, r := range t.r.Items {
|
||||
related := r.o.toModels(r.number)
|
||||
for _, rel := range related {
|
||||
rel.UserID = null.From(o.ID)
|
||||
rel.R.User = o
|
||||
}
|
||||
rel = append(rel, related...)
|
||||
}
|
||||
o.R.Items = rel
|
||||
}
|
||||
}
|
||||
|
||||
// BuildSetter returns an *models.UserSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) BuildSetter() *models.UserSetter {
|
||||
m := &models.UserSetter{}
|
||||
|
||||
if o.ID != nil {
|
||||
m.ID = omit.From(o.ID())
|
||||
}
|
||||
if o.Username != nil {
|
||||
m.Username = omitnull.FromNull(o.Username())
|
||||
}
|
||||
if o.Password != nil {
|
||||
m.Password = omitnull.FromNull(o.Password())
|
||||
}
|
||||
if o.ProfilePictureID != nil {
|
||||
m.ProfilePictureID = omitnull.FromNull(o.ProfilePictureID())
|
||||
}
|
||||
if o.Challenge != nil {
|
||||
m.Challenge = omitnull.FromNull(o.Challenge())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildManySetter returns an []*models.UserSetter
|
||||
// this does nothing with the relationship templates
|
||||
func (o UserTemplate) BuildManySetter(number int) []*models.UserSetter {
|
||||
m := make([]*models.UserSetter, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.BuildSetter()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// Build returns an *models.User
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use UserTemplate.Create
|
||||
func (o UserTemplate) Build() *models.User {
|
||||
m := o.toModel()
|
||||
o.setModelRels(m)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// BuildMany returns an models.UserSlice
|
||||
// Related objects are also created and placed in the .R field
|
||||
// NOTE: Objects are not inserted into the database. Use UserTemplate.CreateMany
|
||||
func (o UserTemplate) BuildMany(number int) models.UserSlice {
|
||||
m := make(models.UserSlice, number)
|
||||
|
||||
for i := range m {
|
||||
m[i] = o.Build()
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func ensureCreatableUser(m *models.UserSetter) {
|
||||
}
|
||||
|
||||
// insertOptRels creates and inserts any optional the relationships on *models.User
|
||||
// according to the relationships in the template.
|
||||
// any required relationship should have already exist on the model
|
||||
func (o *UserTemplate) insertOptRels(ctx context.Context, exec bob.Executor, m *models.User) (context.Context, error) {
|
||||
var err error
|
||||
|
||||
if o.r.Files != nil {
|
||||
for _, r := range o.r.Files {
|
||||
var rel0 models.FileSlice
|
||||
ctx, rel0, err = r.o.createMany(ctx, exec, r.number)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
err = m.AttachFiles(ctx, exec, rel0...)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if o.r.Items != nil {
|
||||
for _, r := range o.r.Items {
|
||||
var rel1 models.ItemSlice
|
||||
ctx, rel1, err = r.o.createMany(ctx, exec, r.number)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
err = m.AttachItems(ctx, exec, rel1...)
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
|
||||
// Create builds a user and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o *UserTemplate) Create(ctx context.Context, exec bob.Executor) (*models.User, error) {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreate builds a user and inserts it into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o *UserTemplate) MustCreate(ctx context.Context, exec bob.Executor) *models.User {
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateOrFail builds a user 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 *UserTemplate) CreateOrFail(ctx context.Context, tb testing.TB, exec bob.Executor) *models.User {
|
||||
tb.Helper()
|
||||
_, m, err := o.create(ctx, exec)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// create builds a user 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 *UserTemplate) create(ctx context.Context, exec bob.Executor) (context.Context, *models.User, error) {
|
||||
var err error
|
||||
opt := o.BuildSetter()
|
||||
ensureCreatableUser(opt)
|
||||
|
||||
m, err := models.Users.Insert(opt).One(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
ctx = userCtx.WithValue(ctx, m)
|
||||
|
||||
ctx, err = o.insertOptRels(ctx, exec, m)
|
||||
return ctx, m, err
|
||||
}
|
||||
|
||||
// CreateMany builds multiple users and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
func (o UserTemplate) CreateMany(ctx context.Context, exec bob.Executor, number int) (models.UserSlice, error) {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
return m, err
|
||||
}
|
||||
|
||||
// MustCreateMany builds multiple users and inserts them into the database
|
||||
// Relations objects are also inserted and placed in the .R field
|
||||
// panics if an error occurs
|
||||
func (o UserTemplate) MustCreateMany(ctx context.Context, exec bob.Executor, number int) models.UserSlice {
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// CreateManyOrFail builds multiple users 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 UserTemplate) CreateManyOrFail(ctx context.Context, tb testing.TB, exec bob.Executor, number int) models.UserSlice {
|
||||
tb.Helper()
|
||||
_, m, err := o.createMany(ctx, exec, number)
|
||||
if err != nil {
|
||||
tb.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// createMany builds multiple users 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 UserTemplate) createMany(ctx context.Context, exec bob.Executor, number int) (context.Context, models.UserSlice, error) {
|
||||
var err error
|
||||
m := make(models.UserSlice, number)
|
||||
|
||||
for i := range m {
|
||||
ctx, m[i], err = o.create(ctx, exec)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, m, nil
|
||||
}
|
||||
|
||||
// User has methods that act as mods for the UserTemplate
|
||||
var UserMods userMods
|
||||
|
||||
type userMods struct{}
|
||||
|
||||
func (m userMods) RandomizeAllColumns(f *faker.Faker) UserMod {
|
||||
return UserModSlice{
|
||||
UserMods.RandomID(f),
|
||||
UserMods.RandomUsername(f),
|
||||
UserMods.RandomPassword(f),
|
||||
UserMods.RandomProfilePictureID(f),
|
||||
UserMods.RandomChallenge(f),
|
||||
}
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ID(val int32) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = func() int32 { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) IDFunc(f func() int32) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetID() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
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 userMods) RandomID(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ID = func() int32 {
|
||||
return random_int32(f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Username(val null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) UsernameFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetUsername() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomUsername(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Username = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Password(val null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) PasswordFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetPassword() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomPassword(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Password = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) ProfilePictureID(val null.Val[int32]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = func() null.Val[int32] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ProfilePictureIDFunc(f func() null.Val[int32]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetProfilePictureID() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomProfilePictureID(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.ProfilePictureID = func() null.Val[int32] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[int32](nil)
|
||||
}
|
||||
|
||||
return null.From(random_int32(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m userMods) Challenge(val null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m userMods) ChallengeFunc(f func() null.Val[string]) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m userMods) UnsetChallenge() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
func (m userMods) RandomChallenge(f *faker.Faker) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.Challenge = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
if f.Bool() {
|
||||
return null.FromPtr[string](nil)
|
||||
}
|
||||
|
||||
return null.From(random_string(f))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithFiles(number int, related *FileTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = []*userRFilesR{{
|
||||
number: number,
|
||||
o: related,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithNewFiles(number int, mods ...FileMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewFile(mods...)
|
||||
m.WithFiles(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddFiles(number int, related *FileTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = append(o.r.Files, &userRFilesR{
|
||||
number: number,
|
||||
o: related,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddNewFiles(number int, mods ...FileMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewFile(mods...)
|
||||
m.AddFiles(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithoutFiles() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Files = nil
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithItems(number int, related *ItemTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Items = []*userRItemsR{{
|
||||
number: number,
|
||||
o: related,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithNewItems(number int, mods ...ItemMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewItem(mods...)
|
||||
m.WithItems(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddItems(number int, related *ItemTemplate) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Items = append(o.r.Items, &userRItemsR{
|
||||
number: number,
|
||||
o: related,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) AddNewItems(number int, mods ...ItemMod) UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
related := o.f.NewItem(mods...)
|
||||
m.AddItems(number, related).Apply(o)
|
||||
})
|
||||
}
|
||||
|
||||
func (m userMods) WithoutItems() UserMod {
|
||||
return UserModFunc(func(o *UserTemplate) {
|
||||
o.r.Items = nil
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user