-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.go
91 lines (72 loc) · 2.16 KB
/
fixtures.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package fixtures
import (
"errors"
"strings"
"github.com/elmagician/godog"
"go.uber.org/zap"
"github.com/elmagician/kactus/internal/picker"
)
const (
// Variables indicates fixtures to load data
// using store.
Variables Kind = iota + 1
// Postgres indicates fixtures to load SQL
// data for a picked postgres db instance.
Postgres
// Pubsub indicates fixtures to load PUBSUB
// events for a picked pubsub instance.
Pubsub
// API indicates fixtures to load API
// definitions background data.
API
manifestTag = "@manifest"
)
var (
// ErrInvalidInstance indicates that an object instance was expected but
// did not matched expected types.
ErrInvalidInstance = errors.New("invalid picked instance")
// ErrUnknown indicates that on object should be available but
// was not stored.
ErrUnknown = errors.New("unknown instance")
// ErrUnsupportedFixture indicates provided fixture kind
// is not supported.
ErrUnsupportedFixture = errors.New("unsupported fixture kind")
)
type (
// Fixtures manages fixtures files to
// help data initialisation
// for test.
//
// It relies on picker.Picker manager.
Fixtures struct {
store *picker.Store
basePath string
}
// Kind of the loaded fixtures.
Kind int
)
// New initiates a fixtures manager instance using provided store.
func New(store *picker.Store) *Fixtures {
return &Fixtures{store: store}
}
// WithBasePath add a path element as prefix for any fixture file path.
func (fix *Fixtures) WithBasePath(path string) *Fixtures {
fix.basePath = strings.TrimSuffix(path, "/") + "/"
return fix
}
// NewTagLoader initializes tag parsing process to load
// Manifest from `@manifest` tag.
func (fix Fixtures) NewTagLoader(s *godog.Scenario) {
log.Debug("parsing tags to find fixture manifest", zap.Reflect("tags", s.Tags))
if err := fix.LoadFromTags(s.Tags); err != nil {
log.Error("could not load fixtures from manifest tag", zap.Error(err))
}
}
// Reset resets fixture instance.
func (fix Fixtures) Reset() {
ResetLog()
}
// getPath construct a fixture file loading path using basePath and provided path.
func (fix Fixtures) getPath(path string) string {
return fix.basePath + strings.TrimPrefix(path, "/")
}