Skip to content

go-rel/fixtures

Repository files navigation

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)
	}
}