-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
generators.go
82 lines (66 loc) · 1.58 KB
/
generators.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
package mock
import (
"testing"
"time"
platform "github.com/influxdata/influxdb"
)
// IDGenerator is mock implementation of platform.IDGenerator.
type IDGenerator struct {
IDFn func() platform.ID
}
// ID generates a new platform.ID from a mock function.
func (g IDGenerator) ID() platform.ID {
return g.IDFn()
}
// NewIDGenerator is a simple way to create immutable id generator
func NewIDGenerator(s string, t *testing.T) IDGenerator {
return IDGenerator{
IDFn: func() platform.ID {
id, err := platform.IDFromString(s)
if err != nil {
t.Fatal(err)
}
return *id
},
}
}
type MockIDGenerator struct {
Last *platform.ID
Count int
}
const FirstMockID int = 65536
func NewMockIDGenerator() *MockIDGenerator {
return &MockIDGenerator{
Count: FirstMockID,
}
}
func (g *MockIDGenerator) ID() platform.ID {
id := platform.ID(g.Count)
g.Count++
g.Last = &id
return id
}
// NewTokenGenerator is a simple way to create immutable token generator.
func NewTokenGenerator(s string, err error) TokenGenerator {
return TokenGenerator{
TokenFn: func() (string, error) {
return s, err
},
}
}
// TokenGenerator is mock implementation of platform.TokenGenerator.
type TokenGenerator struct {
TokenFn func() (string, error)
}
// Token generates a new platform.Token from a mock function.
func (g TokenGenerator) Token() (string, error) {
return g.TokenFn()
}
// TimeGenerator stores a fake value of time.
type TimeGenerator struct {
FakeValue time.Time
}
// Now will return the FakeValue stored in the struct.
func (g TimeGenerator) Now() time.Time {
return g.FakeValue
}