116 lines
2.7 KiB
Go
116 lines
2.7 KiB
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"modelRT/common"
|
||
|
|
"modelRT/orm"
|
||
|
|
|
||
|
|
"github.com/stretchr/testify/assert"
|
||
|
|
"github.com/stretchr/testify/require"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestBuildMeasurementAttributeValue(t *testing.T) {
|
||
|
|
dataSource := orm.JSONMap{
|
||
|
|
"type": float64(1),
|
||
|
|
"io_address": map[string]any{
|
||
|
|
"channel": "tm1p",
|
||
|
|
},
|
||
|
|
}
|
||
|
|
eventPlan := orm.JSONMap{"enabled": true}
|
||
|
|
binding := orm.JSONMap{"ct": map[string]any{"ratio": float64(2)}}
|
||
|
|
measurement := &orm.Measurement{
|
||
|
|
Tag: "IA_rms",
|
||
|
|
Name: "A相电流",
|
||
|
|
Mode: 1,
|
||
|
|
Size: 10,
|
||
|
|
DataSource: dataSource,
|
||
|
|
EventPlan: eventPlan,
|
||
|
|
Binding: binding,
|
||
|
|
}
|
||
|
|
component := &orm.Component{
|
||
|
|
GridName: "grid000",
|
||
|
|
ZoneName: "zone000",
|
||
|
|
StationName: "station000",
|
||
|
|
NSPath: "110kV_TV",
|
||
|
|
Tag: "cable_22",
|
||
|
|
}
|
||
|
|
|
||
|
|
loader := func(_ context.Context, source orm.JSONMap) (any, error) {
|
||
|
|
assert.Equal(t, dataSource, source)
|
||
|
|
return float64(220), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
tests := []struct {
|
||
|
|
field string
|
||
|
|
expected any
|
||
|
|
}{
|
||
|
|
{field: "value", expected: float64(220)},
|
||
|
|
{field: "mode", expected: true},
|
||
|
|
{field: "meta", expected: "MEASUREMENT"},
|
||
|
|
{field: "type", expected: "TM"},
|
||
|
|
{field: "name", expected: "110kV_TV.IA_rms"},
|
||
|
|
{field: "description", expected: "A相电流"},
|
||
|
|
{field: "id", expected: "grid000.zone000.station000.110kV_TV.cable_22.bay.IA_rms"},
|
||
|
|
{field: "size", expected: 10},
|
||
|
|
{field: "data_source", expected: dataSource},
|
||
|
|
{field: "event_plan", expected: eventPlan},
|
||
|
|
{field: "binding", expected: binding},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
t.Run(tt.field, func(t *testing.T) {
|
||
|
|
actual, err := buildMeasurementAttributeValue(context.Background(), tt.field, measurement, component, loader)
|
||
|
|
require.NoError(t, err)
|
||
|
|
assert.Equal(t, tt.expected, actual)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestBuildMeasurementAttributeValueRejectsUnsupportedField(t *testing.T) {
|
||
|
|
_, err := buildMeasurementAttributeValue(
|
||
|
|
context.Background(),
|
||
|
|
"unknown",
|
||
|
|
&orm.Measurement{},
|
||
|
|
&orm.Component{},
|
||
|
|
nil,
|
||
|
|
)
|
||
|
|
require.Error(t, err)
|
||
|
|
assert.ErrorIs(t, err, common.ErrUnsupportedMeasurementField)
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestBuildMeasurementAttributeValueMode(t *testing.T) {
|
||
|
|
component := &orm.Component{}
|
||
|
|
|
||
|
|
manualMode, err := buildMeasurementAttributeValue(
|
||
|
|
context.Background(),
|
||
|
|
"mode",
|
||
|
|
&orm.Measurement{Mode: 0},
|
||
|
|
component,
|
||
|
|
nil,
|
||
|
|
)
|
||
|
|
require.NoError(t, err)
|
||
|
|
assert.Equal(t, false, manualMode)
|
||
|
|
|
||
|
|
automaticMode, err := buildMeasurementAttributeValue(
|
||
|
|
context.Background(),
|
||
|
|
"mode",
|
||
|
|
&orm.Measurement{Mode: 1},
|
||
|
|
component,
|
||
|
|
nil,
|
||
|
|
)
|
||
|
|
require.NoError(t, err)
|
||
|
|
assert.Equal(t, true, automaticMode)
|
||
|
|
|
||
|
|
_, err = buildMeasurementAttributeValue(
|
||
|
|
context.Background(),
|
||
|
|
"mode",
|
||
|
|
&orm.Measurement{Mode: 2},
|
||
|
|
component,
|
||
|
|
nil,
|
||
|
|
)
|
||
|
|
require.Error(t, err)
|
||
|
|
assert.Contains(t, err.Error(), "expected 0 or 1")
|
||
|
|
}
|