fixtures

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 16 Imported by: 0

README

Fixtures

Go Reference Tests Go Report Card codecov Gitter chat

Fixture importing in database for REL.

Example

Using single YAML file

YAML file must contain properties named after database table names with array of objects with fields as column names.

package main

import (
	"context"

	"github.com/go-rel/fixtures"
	"github.com/go-rel/rel"
)

func main() {
	repo := fixtures.New()
	// Register all needed types
	repo.Register(&User{})
	repo.Register(&Address{})
	repo.Register(&Transaction{})

	// TODO db := rel.New(adapter)

	if err := repo.Import(context.Background(), db,
		[]byte(`---
users:
- id: 1
  name: John Doe
  age: 20
  created_at: 2019-01-01T06:10:00Z
  address_id: 1
addresses:
- id: 1
  city: New York
`)); err != nil {
		panic(err)
	}
}
Using directory with YAML files

Directory must contain YAML files named as table names with extension .yaml containing just array of objects with fields as column names.

package main

import (
	"context"

	"github.com/go-rel/fixtures"
	"github.com/go-rel/rel"
)

func main() {
	repo := fixtures.New()
	// Register all needed types
	repo.Register(&User{})
	repo.Register(&Address{})
	repo.Register(&Transaction{})

	// TODO db := rel.New(adapter)

	if err := repo.ImportDir(context.Background(), db, "path/to/dir/")); err != nil {
		panic(err)
	}
}

Documentation

Overview

Package fixtures importing data from YAML files in database for REL.

Usage:

repo := fixtures.New()
// Register all needed types
repo.Register(&MyTableType{})

// Import data from YAML content
err := repo.Import(ctx, db, content)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BeforeSave added in v0.2.0

type BeforeSave interface {
	BeforeSave(context.Context) error
}

BeforeSave interface can be implemented by a type to allow changing type data before saving data to database.

type Logger

type Logger interface {
	Warn(msg string)
}

Logger to be used by the repository to notify about warnings.

type Repository

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

Repsitory of fixtures that can be loaded and imported.

func New added in v0.2.0

func New() *Repository

New creates a new fixtures repository.

func (Repository) DrawDependencies

func (r Repository) DrawDependencies(w io.Writer) error

DrawDependencies draws a graph of all tables and their relations in DOT format.

func (*Repository) Import added in v0.2.0

func (r *Repository) Import(ctx context.Context, db rel.Repository, source []byte) error

Import data from YAML file content.

func (*Repository) ImportDir added in v0.2.0

func (r *Repository) ImportDir(ctx context.Context, db rel.Repository, path string) error

ImportDir imports data from YAML files in a directory.

func (*Repository) Register

func (r *Repository) Register(v any)

Register a type that can be loaded as fixture.

func (*Repository) SetLogger

func (r *Repository) SetLogger(l Logger)

SetLogger sets the logger to be used by the repository to notify about warnings.

func (*Repository) SetSkipResolve added in v0.3.0

func (r *Repository) SetSkipResolve(skip bool)

SetSkipResolve sets whether the repository should skip resolving relations and use registration order.

Jump to

Keyboard shortcuts

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