forked from yourheropaul/inj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_test.go
69 lines (48 loc) · 1.58 KB
/
datasource_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
60
61
62
63
64
65
66
67
68
69
package inj
import (
"fmt"
"testing"
)
///////////////////////////////////////////////////////////////
// A mock DatasourceReader and DatasourceWriter implementation
///////////////////////////////////////////////////////////////
type MockDatasource struct {
stack map[string]interface{}
}
func NewMockDatasource() Datasource {
d := &MockDatasource{}
d.stack = make(map[string]interface{})
return d
}
func (d *MockDatasource) Read(key string) (interface{}, error) {
if value, exists := d.stack[key]; exists {
return value, nil
}
return nil, fmt.Errorf("No stack entry for '%s'", key)
}
func (d *MockDatasource) Write(key string, value interface{}) error {
d.stack[key] = value
return nil
}
///////////////////////////////////////////////////////////////
// A mock specific dependency implementation for testing
///////////////////////////////////////////////////////////////
type dataSourceDep struct {
StringValue string `inj:"datasource.string"`
FuncValue FuncType `inj:"datasource.func"`
IntValue int `inj:"datasource.int"`
}
func newMockDataSourceWithValues(t *testing.T) Datasource {
d := NewMockDatasource()
if e := d.Write("datasource.string", DEFAULT_STRING); e != nil {
t.Fatalf("newMockDataSourceWithValues: Datasource.Write: %s", e)
}
if e := d.Write("datasource.func", funcInstance); e != nil {
t.Fatalf("newMockDataSourceWithValues: Datasource.Write: %s", e)
}
// Try and integer value expressed as a float
if e := d.Write("datasource.int", 16.01); e != nil {
t.Fatalf("newMockDataSourceWithValues: Datasource.Write: %s", e)
}
return d
}