fixture

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2023 License: BSD-3-Clause Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WriteAsync = 1
	WriteSync  = 2
)

Variables

View Source
var ErrDatabaseNotFound = errors.New("database not found")
View Source
var ErrFieldNotFound = errors.New("field not found")
View Source
var ErrPrimaryKeyUndefined = errors.New("primary key undefined")
View Source
var ErrRecordNotFound = errors.New("record not found")
View Source
var ErrTableNotFound = errors.New("table not found")

Functions

func AddFuncMap

func AddFuncMap(fm template.FuncMap)

func ULID

func ULID(v any, err error) ulid.ULID

ULID is meant to be used with (*Fixture).GetField, and will panic if the incoming err is not nil. E.g.:

ULID(f.GetField("users", "1", "id"))

Types

type CommandDependency added in v0.4.0

type CommandDependency struct {
	Label    [2]string
	Callback func() (any, error)
}

type CommandFunc

type CommandFunc func(in *CommandInput) (*CommandOutput, error)

type CommandInput

type CommandInput struct {
	Fixture *Fixture
	Table   string
	Key     string
	Field   string

	Line string
}

func (*CommandInput) ScanLine

func (in *CommandInput) ScanLine() ([]string, map[string]string, error)

type CommandOutput

type CommandOutput struct {
	Dependencies []*CommandDependency
	IsUpdate     bool
	Value        any
}

type Config

type Config struct {
	// The default name for the primary key field.
	// This value can be overwritten by TableOptions.
	// Default: "id"
	PrimaryKeyName string

	// The default references for all tables.
	// Can be overwritten by TableOptions.
	References map[string]string

	// Whether to write records asynchronously or synchronously.
	// Default: WriteAsync
	WriteMode int

	// TableOptions can be used to set table specific options or
	// create multiple profiles for the same table. E.g.:
	//
	// 	tableOptions := map[string]*fixture.TableOptions{
	// 		"foo": {
	// 			DefaultValues: fixture.Record{
	// 				"bar": "default value",
	// 			},
	// 		},
	// 		"foo#profile2": {
	// 			TableName:     "foo",
	// 			DefaultValues: fixture.Record{
	// 				"bar": "profile2 value",
	// 			},
	// 		},
	// 	}
	TableOptions map[string]*TableOptions
	// contains filtered or unexported fields
}

func (*Config) GetPrimaryKeyName

func (c *Config) GetPrimaryKeyName(table string) (string, error)

func (*Config) GetReference

func (c *Config) GetReference(table, field string) (string, string, error)

GetReference checks if the given field has a reference and returns its table and field names. If no reference is found, both values are empty and no error is returned.

func (*Config) TableAlias

func (c *Config) TableAlias(table string) string

type Database

type Database map[string]Table

type Edge

type Edge struct {
	// contains filtered or unexported fields
}

func (*Edge) From

func (e *Edge) From() graph.Node

func (*Edge) ReversedEdge

func (e *Edge) ReversedEdge() graph.Edge

func (*Edge) To

func (e *Edge) To() graph.Node

type Fixture

type Fixture struct {
	Context context.Context
	Logger  *zerolog.Logger

	// Config are a set of parameters that can be reused across fixtures,
	// and should only be set once.
	Config *Config

	// The writer used for this runner.
	Writer Writer

	// The directory where fixture files are located.
	// If non-empty, will be prepended to File.
	Dir string

	// TODO: Should check for one of?
	File       string
	Body       io.Reader
	BodyFormat string

	// Database can be used to set an initial database state.
	// Any records defined in the File/Body will be merged with
	// the ones defined here.
	Database Database

	// If defined, the fixture body will be parsed as a Go text/template string
	// and executed with TemplateData as its data.
	TemplateData map[string]any

	PrintJSON               bool
	DoNotCreateDependencies bool
	// contains filtered or unexported fields
}

func (*Fixture) Applied

func (f *Fixture) Applied() bool

func (*Fixture) Apply

func (f *Fixture) Apply() error

func (*Fixture) Edge

func (f *Fixture) Edge(uid, vid int64) graph.Edge

func (*Fixture) From

func (f *Fixture) From(id int64) graph.Nodes

func (*Fixture) GetField

func (f *Fixture) GetField(table, key, field string) (any, error)

func (*Fixture) GetNode

func (f *Fixture) GetNode(label [2]string) *Node

func (*Fixture) HasEdgeBetween

func (f *Fixture) HasEdgeBetween(xid, yid int64) bool

func (*Fixture) HasEdgeFromTo

func (f *Fixture) HasEdgeFromTo(uid, vid int64) bool

func (*Fixture) Node

func (f *Fixture) Node(id int64) graph.Node

func (*Fixture) Nodes

func (f *Fixture) Nodes() graph.Nodes

func (*Fixture) ParseTemplate added in v0.5.0

func (f *Fixture) ParseTemplate(body []byte) ([]byte, error)

func (*Fixture) SetField

func (f *Fixture) SetField(table, key, field string, value any) error

func (*Fixture) To

func (f *Fixture) To(id int64) graph.Nodes

type Node

type Node struct {
	// contains filtered or unexported fields
}

func (*Node) AppendFrom

func (r *Node) AppendFrom(node *Node)

func (*Node) AppendTo

func (r *Node) AppendTo(node *Node)

func (*Node) ID

func (r *Node) ID() int64

func (*Node) Label

func (r *Node) Label() [2]string

func (*Node) LenFrom

func (r *Node) LenFrom() int

func (*Node) LenTo

func (r *Node) LenTo() int

type Nodes

type Nodes struct {
	// contains filtered or unexported fields
}

func (*Nodes) Len

func (r *Nodes) Len() int

func (*Nodes) Next

func (r *Nodes) Next() bool

func (*Nodes) Node

func (r *Nodes) Node() graph.Node

func (*Nodes) Reset

func (r *Nodes) Reset()

type PostgresWriter

type PostgresWriter struct {
	Conn   *pgxpool.Pool
	Tx     pgx.Tx
	GormDB *gorm.DB
}

func (*PostgresWriter) Insert

func (w *PostgresWriter) Insert(f *Fixture, table string, key string, record Record) error

func (*PostgresWriter) Update

func (w *PostgresWriter) Update(f *Fixture, table string, key string, record Record) error

type Record

type Record map[string]any

type RecordError

type RecordError struct {
	Table string
	Key   string
	Field string
	Err   error
}

func (*RecordError) Error

func (e *RecordError) Error() string

type RedisWriter

type RedisWriter struct {
	Client *redis.Client
}

func (*RedisWriter) Insert

func (w *RedisWriter) Insert(f *Fixture, table string, key string, record Record) error

func (*RedisWriter) Update

func (w *RedisWriter) Update(f *Fixture, table string, key string, record Record) error

type Table

type Table map[string]Record

func GetDefaultValues

func GetDefaultValues(file string) (Table, error)

type TableOptions

type TableOptions struct {
	TableName      string
	PrimaryKeyName string
	References     map[string]string
	WriteMode      int
	DefaultValues  Record
	BeforeWrite    func(ctx context.Context, record Record) error
}

type Writer

type Writer interface {
	Insert(f *Fixture, table, key string, record Record) error
	Update(f *Fixture, table, key string, record Record) error
}

Writer is an interface that handles inserting or updating database records.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL