You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.9 KiB

package common_models
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func Test_GroupItem_GetDoubleParam(t *testing.T) {
groupItem := GroupItem{
StationId: 1,
ParamsValue: map[string]interface{}{
"intVal": 1,
"doubleVal": 5.0,
"stringVal": "1",
},
}
// 正确的 int,double,string 数字
intVal, err := groupItem.GetDoubleParam("intVal")
doubleVal, err := groupItem.GetDoubleParam("doubleVal")
stringVal, err := groupItem.GetDoubleParam("string")
assert.IsTypef(t, float64(1), intVal, "val should be of type float64")
assert.IsTypef(t, float64(1), doubleVal, "val should be of type float64")
assert.IsTypef(t, float64(1), stringVal, "val should be of type float64")
// 错误的数字字符串,返回 (0.0,err) 测试
groupItem.ParamsValue["stringVal"] = "dddd"
errVal, err := groupItem.GetDoubleParam("stringVal")
fmt.Printf("%v, %v \n", errVal, err)
assert.NotNil(t, errVal, "val should be of type float64")
// 不存在的 key, 返回 (0.0,err) 测试
noKeyVal, err := groupItem.GetDoubleParam("noKey")
fmt.Printf("%v, %v \n", noKeyVal, err)
assert.NotNil(t, noKeyVal, "val should be of type float64")
}
func Test_GroupItem_CorrItems(t *testing.T) {
// 假设有3个分组,分别为:group1,group2,group3, group1为基点分组, group2参考group1, group3参考group2
// group1: id=1, 组内测点编号 1~5, 1为基点
// group2: id=2, 组内测点编号 6~10, 6为基点
// group3: id=3, 组内测点编号 11~15, 11为基点
// group1: id=1, 组内测点编号 1~5; group1为基点分组
group1_item1 := GroupItem{
StationId: 1,
ParamsValue: map[string]interface{}{
"base": true,
},
}
group1_item5 := GroupItem{
StationId: 5,
ParamsValue: map[string]interface{}{
"base": false,
},
}
// group2: id=2, 组内测点编号 6~10; group2参考group1
group2_item6 := GroupItem{
StationId: 6,
ParamsValue: map[string]interface{}{
"base": true,
},
}
group2_item10 := GroupItem{
StationId: 10,
ParamsValue: map[string]interface{}{
"base": false,
},
}
// 只有 baseItem 才设置 SubItems
group2_item6.SubItems = map[string]GroupItem{
"ref_base": group1_item1,
"ref_point": group1_item5,
}
// group3: id=3, 组内测点编号 11~15; group3参考group2
group3_item11 := GroupItem{
StationId: 11,
ParamsValue: map[string]interface{}{
"base": true,
},
}
group3_item15 := GroupItem{
StationId: 15,
ParamsValue: map[string]interface{}{
"base": false,
},
}
// 只有 baseItem 才设置 SubItems
group3_item11.SubItems = map[string]GroupItem{
"ref_base": group2_item6,
"ref_point": group2_item10,
}
// ***************** 测试用例 ********************
// SubItems = nil
group1_corrItems := group1_item1.CorrItems()
printMSG(group1_corrItems)
assert.Equal(t, 1, len(group1_corrItems), "基点分组的基点,应该无级联分组")
assert.Nil(t, group1_corrItems[0].SubItems, "基点分组的基点,应该无级联分组")
// SubItems = group1_item1 + group1_item5 + group2_item6
group2_corrItems := group2_item6.CorrItems()
printMSG(group2_corrItems)
assert.Equal(t, 3, len(group2_corrItems), "group2的级联为group1, 应该返回3个分组项")
// SubItems = group1_item1 + group1_item5 + group2_item6 + group2_item10 + group3_item11
group3_corrItems := group3_item11.CorrItems()
printMSG(group3_corrItems)
assert.Equal(t, 5, len(group3_corrItems), "group3级联group2, group2的级联group1, 应该返回5个分组项")
group3_point_corrItems := group3_item15.CorrItems()
printMSG(group3_point_corrItems)
assert.Equal(t, 1, len(group3_point_corrItems), "非基点项,应该无级联分组")
assert.Nil(t, group3_point_corrItems[0].SubItems, "非基点项,应该无级联分组")
}
func Test_StationGroup_AllCorrItems(t *testing.T) {
params := map[string]interface{}{
"ref_base": 193,
"ref_point": 23,
}
items := []GroupItem{
GroupItem{
StationId: 26,
ParamsValue: map[string]interface{}{
"base": false,
},
},
GroupItem{
StationId: 43,
ParamsValue: map[string]interface{}{
"base": false,
},
},
GroupItem{
StationId: 192,
ParamsValue: map[string]interface{}{
"base": true,
},
},
}
items[2].SubItems = map[string]GroupItem{
"ref_base": GroupItem{
StationId: 193,
ParamsValue: map[string]interface{}{
"base": true,
},
},
"ref_point": GroupItem{
StationId: 23,
ParamsValue: map[string]interface{}{
"base": false,
},
},
}
group := StationGroup{
Id: 36,
Name: "上游沉降",
GroupType: "202",
Items: items,
Params: params,
}
corrItems := group.AllCorrItems()
printMSG(corrItems)
assert.Equal(t, 5, len(corrItems), "【上游沉降分组】有3个分组项(26,43,192*) + 有两个关联项(193,23), 应该返回5个分组项")
}
// 打印 []GroupItem
func printMSG(items []GroupItem) {
for _, item := range items {
fmt.Printf("%d,", item.StationId)
}
println("")
}