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

253 lines
6.9 KiB
Go

5 years ago
package client
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNodeIsValue(t *testing.T) {
a := assert.New(t)
tt := []struct {
node *Node
expected bool
}{
{&Node{}, false},
{&Node{Children: []Node{{}}}, false},
{&Node{Children: []Node{}}, true},
}
for i, tc := range tt {
msg := fmt.Sprintf("test case %d", i)
a.Equal(tc.expected, tc.node.IsValue(), msg)
}
}
func TestNodeIsRoot(t *testing.T) {
a := assert.New(t)
tt := []struct {
node *Node
expected bool
}{
{&Node{}, true},
{&Node{ID: 123}, false},
}
for i, tc := range tt {
msg := fmt.Sprintf("test case %d", i)
a.Equal(tc.expected, tc.node.IsRoot(), msg)
}
}
func TestNodeSensor(t *testing.T) {
a := assert.New(t)
tt := []struct {
node *Node
sensor Sensor
str string
isSensor bool
}{
{&Node{}, UnknownSensor, "Unknown", false},
{&Node{Text: "any unknown sensor"}, UnknownSensor, "Unknown", false},
{&Node{ImageURL: "x"}, UnknownSensor, "Unknown", false},
{&Node{Text: "Example"}, UnknownSensor, "Unknown", false},
{&Node{ImageURL: "x", Text: "Example"}, UnknownSensor, "Unknown", false},
{&Node{ImageURL: "x", Text: "Clocks"}, Clock, "Clock", true},
{&Node{ImageURL: "x", Text: "Controls"}, Control, "Control", true},
{&Node{ImageURL: "x", Text: "Data"}, Data, "Data", true},
{&Node{ImageURL: "x", Text: "Data", Children: []Node{{Value: "123.4 MB"}}}, SmallData, "SmallData", true},
{&Node{ImageURL: "x", Text: "Factors"}, Factor, "Factor", true},
{&Node{ImageURL: "x", Text: "Fans"}, Fan, "Fan", true},
{&Node{ImageURL: "x", Text: "Flows"}, Flow, "Flow", true},
{&Node{ImageURL: "x", Text: "Levels"}, Level, "Level", true},
{&Node{ImageURL: "x", Text: "Load"}, Load, "Load", true},
{&Node{ImageURL: "x", Text: "Powers"}, Power, "Power", true},
{&Node{ImageURL: "x", Text: "Temperatures"}, Temperature, "Temperature", true},
{&Node{ImageURL: "x", Text: "Voltages"}, Voltage, "Voltage", true},
}
for _, tc := range tt {
a.Equal(tc.isSensor, tc.node.IsSensor())
a.Equal(tc.sensor, tc.node.SensorType())
a.Equal(tc.str, tc.node.SensorType().String())
}
}
func TestNodeHardware(t *testing.T) {
a := assert.New(t)
tt := []struct {
node *Node
kind HardwareType
str string
isHW bool
}{
{&Node{}, UnknownHardware, "Unknown", false},
{&Node{Text: "unknown hardware"}, UnknownHardware, "Unknown", false},
{&Node{ImageURL: "x"}, UnknownHardware, "Unknown", false},
{&Node{ImageURL: "x/ati.png", Text: "x"}, GpuAti, "GpuAti", true},
{&Node{ImageURL: "x/bigng.png", Text: "x"}, Controller, "Controller", true},
{&Node{ImageURL: "x/chip.png", Text: "x"}, SuperIO, "SuperIO", true},
{&Node{ImageURL: "x/cpu.png", Text: "x"}, CPU, "CPU", true},
{&Node{ImageURL: "x/hdd.png", Text: "x"}, HDD, "HDD", true},
{&Node{ImageURL: "x/mainboard.png", Text: "x"}, Mainboard, "Mainboard", true},
{&Node{ImageURL: "x/nvidia.png", Text: "x"}, GpuNvidia, "GpuNvidia", true},
{&Node{ImageURL: "x/ram.png", Text: "x"}, RAM, "RAM", true},
{&Node{ImageURL: "x/computer.png", Text: "x"}, Computer, "Computer", true},
}
for _, tc := range tt {
a.Equal(tc.isHW, tc.node.IsHardware())
a.Equal(tc.kind, tc.node.HardwareType())
a.Equal(tc.str, tc.node.HardwareType().String())
}
a.Equal("TBalancer", TBalancer.String())
a.Equal("Heatmaster", Heatmaster.String())
a.Equal("Unknown", HardwareType(1234).String())
}
var exampleNode = &Node{
ID: 0, Text: "Sensor", Min: "Min", Value: "Value", Max: "Max",
Children: []Node{{
ID: 1, Text: "hostname", ImageURL: "images_icon/computer.png",
Children: []Node{{
ID: 2, Text: "board model", ImageURL: "images_icon/mainboard.png",
Children: []Node{{
ID: 3, Text: "chipset model", ImageURL: "images_icon/chip.png",
Children: []Node{
{
ID: 4, Text: "Voltages", ImageURL: "images_icon/voltage.png",
Children: []Node{{
ID: 5,
Text: "Voltage #1",
Min: "0.540 V",
Value: "1.332 V",
Max: "1.392 V",
ImageURL: "images/transparent.png",
}},
},
{
ID: 6, Text: "Temperatures", ImageURL: "images_icon/temperature.png",
Children: []Node{{
ID: 7,
Text: "Temperature #1",
Min: "20.0 °C",
Value: "36.0 °C",
Max: "51.0 °C",
ImageURL: "images/transparent.png",
}},
},
},
}, {
ID: 8, Text: "CPU Model", ImageURL: "images_icon/cpu.png",
Children: []Node{{
ID: 9, Text: "Clocks", ImageURL: "images_icon/clock.png",
Children: []Node{{
ID: 10,
Text: "Bus Speed",
Min: "100 MHz",
Value: "100 MHz",
Max: "100 MHz",
ImageURL: "images/transparent.png",
}, {
ID: 11,
Text: "CPU Core #1",
Min: "802 MHz",
Value: "4008 MHz",
Max: "4208 MHz",
ImageURL: "images/transparent.png",
}},
}},
}},
}},
}},
}
func TestNodeValues(t *testing.T) {
a := assert.New(t)
tt := []struct {
node *Node
values []Value
}{
{&Node{}, []Value{}},
{
node: exampleNode,
values: []Value{{
Unit: Voltage,
Label: "Voltage #1",
Value: "1.332 V",
Hardware: []Hardware{
Hardware{Type: Computer, Value: "hostname", TypeCount: 1},
Hardware{Type: Mainboard, Value: "board model", TypeCount: 1},
Hardware{Type: SuperIO, Value: "chipset model", TypeCount: 1},
},
}, {
Unit: Temperature,
Label: "Temperature #1",
Value: "36.0 °C",
Hardware: []Hardware{
{Type: Computer, Value: "hostname", TypeCount: 1},
{Type: Mainboard, Value: "board model", TypeCount: 1},
{Type: SuperIO, Value: "chipset model", TypeCount: 1},
},
}, {
Unit: Clock,
Label: "Bus Speed",
Value: "100 MHz",
Hardware: []Hardware{
{Type: Computer, Value: "hostname", TypeCount: 1},
{Type: Mainboard, Value: "board model", TypeCount: 1},
{Type: CPU, Value: "CPU Model", TypeCount: 1},
},
}, {
Unit: Clock,
Label: "CPU Core #1",
Value: "4008 MHz",
Hardware: []Hardware{
{Type: Computer, Value: "hostname", TypeCount: 1},
{Type: Mainboard, Value: "board model", TypeCount: 1},
{Type: CPU, Value: "CPU Model", TypeCount: 1},
},
}},
},
}
for _, tc := range tt {
actual, err := tc.node.Values()
a.Nil(err)
a.Equal(actual, tc.values)
}
}
func TestNodeStringify(t *testing.T) {
a := assert.New(t)
tt := []struct {
node *Node
expected string
}{
{&Node{}, "\n"},
{
node: exampleNode,
expected: `Sensor: Value (Min - Max)
hostname
board model
chipset model
Voltages
Voltage #1: 1.332 V (0.540 V - 1.392 V)
Temperatures
Temperature #1: 36.0 °C (20.0 °C - 51.0 °C)
CPU Model
Clocks
Bus Speed: 100 MHz (100 MHz - 100 MHz)
CPU Core #1: 4008 MHz (802 MHz - 4208 MHz)
`,
},
}
for _, tc := range tt {
a.Equal(tc.expected, tc.node.Stringify())
}
}