ooni-probe-cli/internal/engine/saver_test.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

81 lines
1.8 KiB
Go

package engine
import (
"errors"
"testing"
"github.com/apex/log"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/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")
}
}