Fix my tests.
This commit is contained in:
@@ -34,6 +34,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type muninData struct {
|
type muninData struct {
|
||||||
@@ -51,7 +52,7 @@ func (w *WebGui) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||||||
calcMuninAverages(&muninDat)
|
calcMuninAverages(&muninDat)
|
||||||
|
|
||||||
statusP := &muninDat.statusP
|
statusP := &muninDat.statusP
|
||||||
tmpInput := buildTemplateInput(statusP)
|
tmpInput := buildTemplateInput(statusP, time.Now())
|
||||||
outputBuf := &bytes.Buffer{}
|
outputBuf := &bytes.Buffer{}
|
||||||
fmt.Fprintf(outputBuf, "multigraph in_batvolt\n")
|
fmt.Fprintf(outputBuf, "multigraph in_batvolt\n")
|
||||||
fmt.Fprintf(outputBuf, "volt.value %s\n", tmpInput.BatVoltage)
|
fmt.Fprintf(outputBuf, "volt.value %s\n", tmpInput.BatVoltage)
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ type templateInput struct {
|
|||||||
func (w *WebGui) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func (w *WebGui) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
statusErr := <-w.respChan
|
statusErr := <-w.respChan
|
||||||
|
|
||||||
tmpInput := buildTemplateInput(&statusErr)
|
tmpInput := buildTemplateInput(&statusErr, time.Now())
|
||||||
|
|
||||||
err := w.template.Execute(rw, tmpInput)
|
err := w.template.Execute(rw, tmpInput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -127,14 +127,14 @@ func (w *WebGui) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildTemplateInput(statusErr *statusProcessed) *templateInput {
|
func buildTemplateInput(statusErr *statusProcessed, now time.Time) *templateInput {
|
||||||
status := statusErr.status
|
status := statusErr.status
|
||||||
outPower := status.OutVoltage * status.OutCurrent
|
outPower := status.OutVoltage * status.OutCurrent
|
||||||
inPower := status.InCurrent * status.InVoltage
|
inPower := status.InCurrent * status.InVoltage
|
||||||
|
|
||||||
tmpInput := &templateInput{
|
tmpInput := &templateInput{
|
||||||
Error: statusErr.err,
|
Error: statusErr.err,
|
||||||
Date: time.Now().Format(time.RFC1123Z),
|
Date: now.Format(time.RFC1123Z),
|
||||||
OutCurrent: fmt.Sprintf("%.3f", status.OutCurrent),
|
OutCurrent: fmt.Sprintf("%.3f", status.OutCurrent),
|
||||||
OutVoltage: fmt.Sprintf("%.3f", status.OutVoltage),
|
OutVoltage: fmt.Sprintf("%.3f", status.OutVoltage),
|
||||||
OutPower: fmt.Sprintf("%.3f", outPower),
|
OutPower: fmt.Sprintf("%.3f", outPower),
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ import (
|
|||||||
"github.com/hpdvanwyk/invertergui/datasource"
|
"github.com/hpdvanwyk/invertergui/datasource"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockSource struct {
|
type mockSource struct {
|
||||||
@@ -63,9 +64,10 @@ func TestWebGui(t *testing.T) {
|
|||||||
|
|
||||||
type templateTest struct {
|
type templateTest struct {
|
||||||
input *statusProcessed
|
input *statusProcessed
|
||||||
output *TemplateInput
|
output *templateInput
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fakenow = time.Date(2017, 1, 2, 3, 4, 5, 6, time.UTC)
|
||||||
var templateInputTests = []templateTest{
|
var templateInputTests = []templateTest{
|
||||||
{
|
{
|
||||||
input: &statusProcessed{
|
input: &statusProcessed{
|
||||||
@@ -81,8 +83,9 @@ var templateInputTests = []templateTest{
|
|||||||
Leds: []int{0, 0, 0, 0, 1, 0, 0, 1}},
|
Leds: []int{0, 0, 0, 0, 1, 0, 0, 1}},
|
||||||
err: nil,
|
err: nil,
|
||||||
},
|
},
|
||||||
output: &TemplateInput{
|
output: &templateInput{
|
||||||
Error: nil,
|
Error: nil,
|
||||||
|
Date: fakenow.Format(time.RFC1123Z),
|
||||||
OutCurrent: "2.000",
|
OutCurrent: "2.000",
|
||||||
OutVoltage: "230.000",
|
OutVoltage: "230.000",
|
||||||
OutPower: "460.000",
|
OutPower: "460.000",
|
||||||
@@ -101,7 +104,7 @@ var templateInputTests = []templateTest{
|
|||||||
|
|
||||||
func TestTemplateInput(t *testing.T) {
|
func TestTemplateInput(t *testing.T) {
|
||||||
for i := range templateInputTests {
|
for i := range templateInputTests {
|
||||||
templateInput := buildTemplateInput(templateInputTests[i].input)
|
templateInput := buildTemplateInput(templateInputTests[i].input, fakenow)
|
||||||
if !reflect.DeepEqual(templateInput, templateInputTests[i].output) {
|
if !reflect.DeepEqual(templateInput, templateInputTests[i].output) {
|
||||||
t.Errorf("buildTemplateInput not producing expected results")
|
t.Errorf("buildTemplateInput not producing expected results")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user