api/app/trading_test.go

116 lines
3.1 KiB
Go
Raw Permalink Normal View History

2018-06-11 12:59:14 +02:00
package app
import (
"fmt"
"testing"
"github.com/kortschak/ct"
)
type testcase struct {
Data powerData
Result resultType
}
type resultType uint8
func (r resultType) String() string {
return []string{``, `positive`, `negative`, ` neutral`}[r]
}
const (
resultPositive = iota + 1
resultNegative
resultNeutral
)
func resultTypeFromScore(s float64) resultType {
switch {
case s < 1 && s > -1:
return resultNeutral
case s >= 1:
return resultPositive
case s <= -1:
return resultNegative
}
panic(`Unreachable`)
}
var (
passColor = ct.Fg(ct.BoldGreen).Paint
infoColor = ct.Fg(ct.Yellow).Paint
)
func TestProductionScore(t *testing.T) {
cases := []testcase{
{powerData{Voltage: 240, MaxAmpere: 10, CosPhi: 0.8}, resultNegative},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 0.4}, resultNegative},
{powerData{Voltage: 230, MaxAmpere: 200, CosPhi: 0.8}, resultNegative},
{powerData{Voltage: 220, MaxAmpere: 10, CosPhi: 0.8}, resultPositive},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 1}, resultPositive},
{powerData{Voltage: 230, MaxAmpere: 0, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 234, MaxAmpere: 10, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 226, MaxAmpere: 10, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 0.71}, resultNeutral},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 0.89}, resultNeutral},
{powerData{Voltage: 230, MaxAmpere: 120, CosPhi: 0.8}, resultNeutral},
}
testScore(t, cases, productionScore)
}
func TestUsageScore(t *testing.T) {
cases := []testcase{
{powerData{Voltage: 240, MaxAmpere: 10, CosPhi: 0.8}, resultPositive},
{powerData{Voltage: 230, MaxAmpere: 200, CosPhi: 0.8}, resultNegative},
{powerData{Voltage: 220, MaxAmpere: 10, CosPhi: 0.8}, resultNegative},
{powerData{Voltage: 230, MaxAmpere: 0, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 234, MaxAmpere: 10, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 226, MaxAmpere: 10, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 230, MaxAmpere: 120, CosPhi: 0.8}, resultNeutral},
}
testScore(t, cases, usageScore)
}
func TestStorageScore(t *testing.T) {
cases := []testcase{
{powerData{Voltage: 240, MaxAmpere: 10, CosPhi: 0.8}, resultPositive},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 0.4}, resultPositive},
{powerData{Voltage: 220, MaxAmpere: 10, CosPhi: 0.8}, resultNegative},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 1}, resultNegative},
{powerData{Voltage: 234, MaxAmpere: 10, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 226, MaxAmpere: 10, CosPhi: 0.8}, resultNeutral},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 0.71}, resultNeutral},
{powerData{Voltage: 230, MaxAmpere: 10, CosPhi: 0.89}, resultNeutral},
}
testScore(t, cases, storageScore)
}
func testScore(t *testing.T, cases []testcase, f func(powerData) float64) {
t.Helper()
for _, v := range cases {
s := f(v.Data)
r := resultTypeFromScore(s)
if r != v.Result {
t.Error(`Expected a`, v.Result, `score, but got`, s)
continue
}
score := fmt.Sprintf(`%6.2f`, s)
t.Log(passColor(`PASS`), infoColor(v.Data), `->`, infoColor(r, `(`+score+`)`))
}
}