Fix my tests.

This commit is contained in:
Hendrik van Wyk
2017-09-16 11:53:45 +02:00
parent ad170b556e
commit 74a3722b09
3 changed files with 11 additions and 7 deletions

View File

@@ -34,6 +34,7 @@ import (
"bytes"
"fmt"
"net/http"
"time"
)
type muninData struct {
@@ -51,7 +52,7 @@ func (w *WebGui) ServeMuninHTTP(rw http.ResponseWriter, r *http.Request) {
calcMuninAverages(&muninDat)
statusP := &muninDat.statusP
tmpInput := buildTemplateInput(statusP)
tmpInput := buildTemplateInput(statusP, time.Now())
outputBuf := &bytes.Buffer{}
fmt.Fprintf(outputBuf, "multigraph in_batvolt\n")
fmt.Fprintf(outputBuf, "volt.value %s\n", tmpInput.BatVoltage)

View File

@@ -119,7 +119,7 @@ type templateInput struct {
func (w *WebGui) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
statusErr := <-w.respChan
tmpInput := buildTemplateInput(&statusErr)
tmpInput := buildTemplateInput(&statusErr, time.Now())
err := w.template.Execute(rw, tmpInput)
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
outPower := status.OutVoltage * status.OutCurrent
inPower := status.InCurrent * status.InVoltage
tmpInput := &templateInput{
Error: statusErr.err,
Date: time.Now().Format(time.RFC1123Z),
Date: now.Format(time.RFC1123Z),
OutCurrent: fmt.Sprintf("%.3f", status.OutCurrent),
OutVoltage: fmt.Sprintf("%.3f", status.OutVoltage),
OutPower: fmt.Sprintf("%.3f", outPower),

View File

@@ -34,6 +34,7 @@ import (
"github.com/hpdvanwyk/invertergui/datasource"
"reflect"
"testing"
"time"
)
type mockSource struct {
@@ -63,9 +64,10 @@ func TestWebGui(t *testing.T) {
type templateTest struct {
input *statusProcessed
output *TemplateInput
output *templateInput
}
var fakenow = time.Date(2017, 1, 2, 3, 4, 5, 6, time.UTC)
var templateInputTests = []templateTest{
{
input: &statusProcessed{
@@ -81,8 +83,9 @@ var templateInputTests = []templateTest{
Leds: []int{0, 0, 0, 0, 1, 0, 0, 1}},
err: nil,
},
output: &TemplateInput{
output: &templateInput{
Error: nil,
Date: fakenow.Format(time.RFC1123Z),
OutCurrent: "2.000",
OutVoltage: "230.000",
OutPower: "460.000",
@@ -101,7 +104,7 @@ var templateInputTests = []templateTest{
func TestTemplateInput(t *testing.T) {
for i := range templateInputTests {
templateInput := buildTemplateInput(templateInputTests[i].input)
templateInput := buildTemplateInput(templateInputTests[i].input, fakenow)
if !reflect.DeepEqual(templateInput, templateInputTests[i].output) {
t.Errorf("buildTemplateInput not producing expected results")
}