-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_writer.go
69 lines (61 loc) · 1.46 KB
/
mock_writer.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 peanut
import (
"reflect"
)
var _ Writer = &MockWriter{}
// MockWriter captures written data in memory, to provide easy mocking
// when testing code that uses peanut.
type MockWriter struct {
*base
Headers map[string][]string
Data map[string][]map[string]string
DisableDataCapture map[string]bool
CalledWrite int
CalledClose int
CalledCancel int
}
func (w *MockWriter) register(x interface{}) (reflect.Type, error) {
// Lazy init.
if w.base == nil {
w.base = &base{}
w.Headers = make(map[string][]string)
w.Data = make(map[string][]map[string]string)
}
// Register with base writer.
t, ok := w.base.register(x)
if !ok {
return t, nil
}
if err := allFieldsSupportedKinds(x); err != nil {
return nil, err
}
w.Headers[t.Name()] = w.headersByType[t]
return t, nil
}
// Write is called to persist records.
func (w *MockWriter) Write(x interface{}) error {
w.CalledWrite++
t, err := w.register(x)
if err != nil {
return err
}
n := t.Name()
if w.DisableDataCapture != nil && w.DisableDataCapture[n] {
return nil
}
// Capture the row of data.
data := w.Data[n]
data = append(data, stringValuesAsMap(x))
w.Data[n] = data
return nil
}
// Close should be called after successfully writing records.
func (w *MockWriter) Close() error {
w.CalledClose++
return nil
}
// Cancel should be called in the event of an error occurring.
func (w *MockWriter) Cancel() error {
w.CalledCancel++
return nil
}