-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasource_writer_test.go
105 lines (74 loc) · 2.29 KB
/
datasource_writer_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package inj
import "testing"
///////////////////////////////////////////////////////////////
// A mock DatasourceWriter implementation
///////////////////////////////////////////////////////////////
type MockDatasourceWriter struct {
stack map[string]interface{}
}
func NewMockDatasourceWriter(data ...map[string]interface{}) *MockDatasourceWriter {
d := &MockDatasourceWriter{}
d.stack = make(map[string]interface{})
for _, datum := range data {
for k, v := range datum {
d.stack[k] = v
}
}
return d
}
func (d *MockDatasourceWriter) Write(key string, value interface{}) error {
d.stack[key] = value
return nil
}
func (d *MockDatasourceWriter) Assert(t *testing.T, key string, value interface{}) {
v, exists := d.stack[key]
if !exists {
t.Fatalf("MockDatasourceWriter.Assert: Key '%s' doesn't exist", key)
}
if v != value {
t.Fatalf("MockDatasourceWriter.Assert: key %s doesn't match (%v, %v)", key, v, value)
}
}
func (d *MockDatasourceWriter) AssertMap(t *testing.T, data map[string]interface{}) {
for k, v := range data {
d.Assert(t, k, v)
}
}
///////////////////////////////////////////////////////////////
// Unit tests for graph implementation
///////////////////////////////////////////////////////////////
type datasourceWriterDep struct {
IntVal int `inj:"datasource.writer.int"`
StringVal string `inj:"datasource.writer.string"`
}
func Test_DatasourceReaderWriterLoopInGraph(t *testing.T) {
expected_values := map[string]interface{}{
"datasource.writer.int": 10,
"datasource.writer.string": DEFAULT_STRING,
}
reader := NewMockDatasourceReader(expected_values)
writer := NewMockDatasourceWriter()
dep := datasourceWriterDep{}
g := NewGraph()
g.AddDatasource(reader, writer)
g.Provide(&dep)
assertNoGraphErrors(t, g.(*graph))
writer.AssertMap(t, expected_values)
}
func Test_DatasourceWriterWritesWithoutAReader(t *testing.T) {
expected_values := map[string]interface{}{
"datasource.writer.int": 10,
"datasource.writer.string": DEFAULT_STRING,
}
writer := NewMockDatasourceWriter()
dep := datasourceWriterDep{}
g := NewGraph()
g.AddDatasource(writer)
g.Provide(
expected_values["datasource.writer.int"],
expected_values["datasource.writer.string"],
&dep,
)
assertNoGraphErrors(t, g.(*graph))
writer.AssertMap(t, expected_values)
}