forked from ooni/probe-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saver_test.go
80 lines (72 loc) · 1.74 KB
/
saver_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
package engine
import (
"errors"
"testing"
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-engine/model"
)
func TestNewSaverDisabled(t *testing.T) {
saver, err := NewSaver(SaverConfig{
Enabled: false,
})
if err != nil {
t.Fatal(err)
}
if _, ok := saver.(fakeSaver); !ok {
t.Fatal("not the type of Saver we expected")
}
m := new(model.Measurement)
if err := saver.SaveMeasurement(m); err != nil {
t.Fatal(err)
}
}
func TestNewSaverWithEmptyFilePath(t *testing.T) {
saver, err := NewSaver(SaverConfig{
Enabled: true,
FilePath: "",
})
if err == nil || err.Error() != "saver: passed an empty filepath" {
t.Fatalf("not the error we expected: %+v", err)
}
if saver != nil {
t.Fatal("saver should be nil here")
}
}
type FakeSaverExperiment struct {
M *model.Measurement
Error error
FilePath string
}
func (fse *FakeSaverExperiment) SaveMeasurement(m *model.Measurement, filepath string) error {
fse.M = m
fse.FilePath = filepath
return fse.Error
}
var _ SaverExperiment = &FakeSaverExperiment{}
func TestNewSaverWithFailureWhenSaving(t *testing.T) {
expected := errors.New("mocked error")
fse := &FakeSaverExperiment{Error: expected}
saver, err := NewSaver(SaverConfig{
Enabled: true,
FilePath: "report.jsonl",
Experiment: fse,
Logger: log.Log,
})
if err != nil {
t.Fatal(err)
}
if _, ok := saver.(realSaver); !ok {
t.Fatal("not the type of saver we expected")
}
m := &model.Measurement{Input: "www.kernel.org"}
if err := saver.SaveMeasurement(m); !errors.Is(err, expected) {
t.Fatalf("not the error we expected: %+v", err)
}
if diff := cmp.Diff(fse.M, m); diff != "" {
t.Fatal(diff)
}
if fse.FilePath != "report.jsonl" {
t.Fatal("passed invalid filepath")
}
}