-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
32 changed files
with
1,822 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
) | ||
|
||
// Experiment mocks model.Experiment | ||
type Experiment struct { | ||
MockKibiBytesReceived func() float64 | ||
|
||
MockKibiBytesSent func() float64 | ||
|
||
MockName func() string | ||
|
||
MockGetSummaryKeys func(m *model.Measurement) (any, error) | ||
|
||
MockReportID func() string | ||
|
||
MockMeasureAsync func(ctx context.Context, input string) (<-chan *model.Measurement, error) | ||
|
||
MockMeasureWithContext func( | ||
ctx context.Context, input string) (measurement *model.Measurement, err error) | ||
|
||
MockSaveMeasurement func(measurement *model.Measurement, filePath string) error | ||
|
||
MockSubmitAndUpdateMeasurementContext func( | ||
ctx context.Context, measurement *model.Measurement) error | ||
|
||
MockOpenReportContext func(ctx context.Context) error | ||
} | ||
|
||
func (e *Experiment) KibiBytesReceived() float64 { | ||
return e.MockKibiBytesReceived() | ||
} | ||
|
||
func (e *Experiment) KibiBytesSent() float64 { | ||
return e.MockKibiBytesSent() | ||
} | ||
|
||
func (e *Experiment) Name() string { | ||
return e.MockName() | ||
} | ||
|
||
func (e *Experiment) GetSummaryKeys(m *model.Measurement) (any, error) { | ||
return e.MockGetSummaryKeys(m) | ||
} | ||
|
||
func (e *Experiment) ReportID() string { | ||
return e.MockReportID() | ||
} | ||
|
||
func (e *Experiment) MeasureAsync( | ||
ctx context.Context, input string) (<-chan *model.Measurement, error) { | ||
return e.MockMeasureAsync(ctx, input) | ||
} | ||
|
||
func (e *Experiment) MeasureWithContext( | ||
ctx context.Context, input string) (measurement *model.Measurement, err error) { | ||
return e.MockMeasureWithContext(ctx, input) | ||
} | ||
|
||
func (e *Experiment) SaveMeasurement(measurement *model.Measurement, filePath string) error { | ||
return e.MockSaveMeasurement(measurement, filePath) | ||
} | ||
|
||
func (e *Experiment) SubmitAndUpdateMeasurementContext( | ||
ctx context.Context, measurement *model.Measurement) error { | ||
return e.MockSubmitAndUpdateMeasurementContext(ctx, measurement) | ||
} | ||
|
||
func (e *Experiment) OpenReportContext(ctx context.Context) error { | ||
return e.MockOpenReportContext(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
) | ||
|
||
func TestExperiment(t *testing.T) { | ||
t.Run("KibiBytesReceived", func(t *testing.T) { | ||
expected := 1.0 | ||
e := &Experiment{ | ||
MockKibiBytesReceived: func() float64 { | ||
return expected | ||
}, | ||
} | ||
if e.KibiBytesReceived() != expected { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("KibiBytesSent", func(t *testing.T) { | ||
expected := 1.0 | ||
e := &Experiment{ | ||
MockKibiBytesSent: func() float64 { | ||
return expected | ||
}, | ||
} | ||
if e.KibiBytesSent() != expected { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("Name", func(t *testing.T) { | ||
expected := "antani" | ||
e := &Experiment{ | ||
MockName: func() string { | ||
return expected | ||
}, | ||
} | ||
if e.Name() != expected { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("GetSummaryKeys", func(t *testing.T) { | ||
expected := errors.New("mocked err") | ||
e := &Experiment{ | ||
MockGetSummaryKeys: func(m *model.Measurement) (any, error) { | ||
return nil, expected | ||
}, | ||
} | ||
out, err := e.GetSummaryKeys(&model.Measurement{}) | ||
if !errors.Is(err, expected) { | ||
t.Fatal("unexpected err", err) | ||
} | ||
if out != nil { | ||
t.Fatal("invalid out") | ||
} | ||
}) | ||
|
||
t.Run("ReportID", func(t *testing.T) { | ||
expect := "xyz" | ||
e := &Experiment{ | ||
MockReportID: func() string { | ||
return expect | ||
}, | ||
} | ||
if e.ReportID() != expect { | ||
t.Fatal("invalid value") | ||
} | ||
}) | ||
|
||
t.Run("MeasureAsync", func(t *testing.T) { | ||
expected := errors.New("mocked err") | ||
e := &Experiment{ | ||
MockMeasureAsync: func(ctx context.Context, input string) (<-chan *model.Measurement, error) { | ||
return nil, expected | ||
}, | ||
} | ||
out, err := e.MeasureAsync(context.Background(), "xo") | ||
if !errors.Is(err, expected) { | ||
t.Fatal("unexpected err", err) | ||
} | ||
if out != nil { | ||
t.Fatal("expected nil") | ||
} | ||
}) | ||
|
||
t.Run("MeasureWithContext", func(t *testing.T) { | ||
expected := errors.New("mocked err") | ||
e := &Experiment{ | ||
MockMeasureWithContext: func(ctx context.Context, input string) (measurement *model.Measurement, err error) { | ||
return nil, expected | ||
}, | ||
} | ||
out, err := e.MeasureWithContext(context.Background(), "xo") | ||
if !errors.Is(err, expected) { | ||
t.Fatal("unexpected err", err) | ||
} | ||
if out != nil { | ||
t.Fatal("expected nil") | ||
} | ||
}) | ||
|
||
t.Run("SaveMeasurement", func(t *testing.T) { | ||
expected := errors.New("mocked err") | ||
e := &Experiment{ | ||
MockSaveMeasurement: func(measurement *model.Measurement, filePath string) error { | ||
return expected | ||
}, | ||
} | ||
err := e.SaveMeasurement(&model.Measurement{}, "x") | ||
if !errors.Is(err, expected) { | ||
t.Fatal("unexpected err", err) | ||
} | ||
}) | ||
|
||
t.Run("SubmitAndUpdateMeasurementContext", func(t *testing.T) { | ||
expected := errors.New("mocked err") | ||
e := &Experiment{ | ||
MockSubmitAndUpdateMeasurementContext: func(ctx context.Context, measurement *model.Measurement) error { | ||
return expected | ||
}, | ||
} | ||
err := e.SubmitAndUpdateMeasurementContext(context.Background(), &model.Measurement{}) | ||
if !errors.Is(err, expected) { | ||
t.Fatal("unexpected err", err) | ||
} | ||
}) | ||
|
||
t.Run("OpenReportContext", func(t *testing.T) { | ||
expected := errors.New("mocked err") | ||
e := &Experiment{ | ||
MockOpenReportContext: func(ctx context.Context) error { | ||
return expected | ||
}, | ||
} | ||
err := e.OpenReportContext(context.Background()) | ||
if !errors.Is(err, expected) { | ||
t.Fatal("unexpected err", err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mocks | ||
|
||
import "github.com/ooni/probe-cli/v3/internal/model" | ||
|
||
// ExperimentBuilder mocks model.ExperimentBuilder. | ||
type ExperimentBuilder struct { | ||
MockInterruptible func() bool | ||
|
||
MockInputPolicy func() model.InputPolicy | ||
|
||
MockOptions func() (map[string]model.ExperimentOptionInfo, error) | ||
|
||
MockSetOptionAny func(key string, value any) error | ||
|
||
MockSetOptionsAny func(options map[string]any) error | ||
|
||
MockSetCallbacks func(callbacks model.ExperimentCallbacks) | ||
|
||
MockNewExperiment func() model.Experiment | ||
} | ||
|
||
func (eb *ExperimentBuilder) Interruptible() bool { | ||
return eb.MockInterruptible() | ||
} | ||
|
||
func (eb *ExperimentBuilder) InputPolicy() model.InputPolicy { | ||
return eb.MockInputPolicy() | ||
} | ||
|
||
func (eb *ExperimentBuilder) Options() (map[string]model.ExperimentOptionInfo, error) { | ||
return eb.MockOptions() | ||
} | ||
|
||
func (eb *ExperimentBuilder) SetOptionAny(key string, value any) error { | ||
return eb.MockSetOptionAny(key, value) | ||
} | ||
|
||
func (eb *ExperimentBuilder) SetOptionsAny(options map[string]any) error { | ||
return eb.MockSetOptionsAny(options) | ||
} | ||
|
||
func (eb *ExperimentBuilder) SetCallbacks(callbacks model.ExperimentCallbacks) { | ||
eb.MockSetCallbacks(callbacks) | ||
} | ||
|
||
func (eb *ExperimentBuilder) NewExperiment() model.Experiment { | ||
return eb.MockNewExperiment() | ||
} |
Oops, something went wrong.