47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
|
|
package manualsync
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"modelRT/constants"
|
||
|
|
"modelRT/orm"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
)
|
||
|
|
|
||
|
|
type syncerFunc func(context.Context, orm.JSONMap, int16, *SyntheticData) error
|
||
|
|
|
||
|
|
func (syncer syncerFunc) Sync(ctx context.Context, dataSource orm.JSONMap, mode int16, sample *SyntheticData) error {
|
||
|
|
return syncer(ctx, dataSource, mode, sample)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSyncRequiresDefaultSyncer(t *testing.T) {
|
||
|
|
SetDefaultSyncer(nil)
|
||
|
|
t.Cleanup(func() { SetDefaultSyncer(nil) })
|
||
|
|
|
||
|
|
err := Sync(context.Background(), nil, constants.MeasurementModeManual, nil)
|
||
|
|
require.Error(t, err)
|
||
|
|
assert.Contains(t, err.Error(), "not initialized")
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSyncUsesDefaultSyncer(t *testing.T) {
|
||
|
|
SetDefaultSyncer(nil)
|
||
|
|
t.Cleanup(func() { SetDefaultSyncer(nil) })
|
||
|
|
|
||
|
|
dataSource := orm.JSONMap{"type": 2}
|
||
|
|
sample := &SyntheticData{Time: 123, Value: 4.5}
|
||
|
|
called := false
|
||
|
|
SetDefaultSyncer(syncerFunc(func(_ context.Context, gotDataSource orm.JSONMap, gotMode int16, gotSample *SyntheticData) error {
|
||
|
|
called = true
|
||
|
|
assert.Equal(t, dataSource, gotDataSource)
|
||
|
|
assert.Equal(t, constants.MeasurementModeManual, gotMode)
|
||
|
|
assert.Same(t, sample, gotSample)
|
||
|
|
return nil
|
||
|
|
}))
|
||
|
|
|
||
|
|
require.NoError(t, Sync(context.Background(), dataSource, constants.MeasurementModeManual, sample))
|
||
|
|
assert.True(t, called)
|
||
|
|
}
|