-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdatasourcer_test.go
59 lines (45 loc) · 1.05 KB
/
datasourcer_test.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
package core
import (
"context"
"errors"
"github.com/paulmach/osm"
)
var _ Datasourcer = &TestDS{}
var ErrNotFound = errors.New("not found")
// TestDS implements a datasource for testing.
type TestDS struct {
data map[osm.FeatureID]ChildList
}
// Get returns the history in ChildList form.
func (tds *TestDS) Get(ctx context.Context, id osm.FeatureID) (ChildList, error) {
if tds.data == nil {
return nil, ErrNotFound
}
v := tds.data[id]
if v == nil {
return nil, ErrNotFound
}
return v, nil
}
// MustGet is used by tests only to simplify some code.
func (tds *TestDS) MustGet(id osm.FeatureID) ChildList {
v, err := tds.Get(context.TODO(), id)
if err != nil {
panic(err)
}
return v
}
func (tds *TestDS) NotFound(err error) bool {
return err == ErrNotFound
}
// Set sets the element history into the map.
// The element is deleted if list is nil.
func (tds *TestDS) Set(id osm.FeatureID, list ChildList) {
if tds.data == nil {
tds.data = make(map[osm.FeatureID]ChildList)
}
if list == nil {
delete(tds.data, id)
}
tds.data[id] = list
}