fix(test/logfile): valid temperatures

This commit is contained in:
Markus Pesch 2019-06-21 11:48:44 +02:00
parent a3a8cc6e51
commit 87475225bc
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
18 changed files with 185 additions and 60 deletions

View File

@ -26,7 +26,7 @@ test: bindata
go test -v ./pkg/...
bindata:
go-bindata -pkg logfile -o ./pkg/logfile/bindata_test.go ./pkg/logfile/test
go-bindata -pkg logfile_test -o ./pkg/logfile/bindata_test.go ./pkg/logfile/test/***
container-build:
$(MAKE) container-run COMMAND=build

1
go.mod
View File

@ -10,5 +10,6 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stianeikeland/go-rpio v4.2.0+incompatible
github.com/stretchr/testify v1.3.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)

7
go.sum
View File

@ -1,3 +1,5 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-flucky/go-dht v0.1.1 h1:dPz9F5D7oUaTd0GUGTvT4lBH2R043h1bkmhTlpQax1Y=
github.com/go-flucky/go-dht v0.1.1/go.mod h1:Yk/cct+/u+eCS7pB/kc0tc7MrVXdFI4W15MJCj5FRUc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@ -7,6 +9,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
@ -15,6 +19,9 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stianeikeland/go-rpio v4.2.0+incompatible h1:CUOlIxdJdT+H1obJPsmg8byu7jMSECLfAN9zynm5QGo=
github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
periph.io/x/periph v3.4.0+incompatible h1:5gzxE4ryPq52cdqSw0mErR6pyJK8cBF2qdUAcOWh0bo=

View File

@ -21,21 +21,21 @@ func (cl *csvLogfile) GetLogfile() string {
func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, cl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
}
humidities := make([]*types.Humidity, 0)
f, err := os.Open(cl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, cl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
}
defer f.Close()
jsonDecoder := json.NewDecoder(f)
err = jsonDecoder.Decode(&humidities)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
return humidities, nil
@ -43,21 +43,21 @@ func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, cl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
}
temperatures := make([]*types.Temperature, 0)
f, err := os.Open(cl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, cl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
}
defer f.Close()
r := csv.NewReader(f)
records, err := r.ReadAll()
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, cl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, cl.logfile)
}
for _, record := range records {
@ -65,14 +65,14 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
for _, j := range []int{2, 3} {
time, err := time.Parse(timeFormat, record[j])
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrParseTime, record[j])
return nil, fmt.Errorf("%v: %v", errorParseTime, record[j])
}
times = append(times, time)
}
temperatureValue, err := strconv.ParseFloat(record[1], 64)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrParseFloat, record[1])
return nil, fmt.Errorf("%v: %v", errorParseFloat, record[1])
}
temperature := &types.Temperature{
@ -86,7 +86,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if len(record) == 6 && record[5] != "" {
temperatureCreationDate, err := time.Parse(timeFormat, record[5])
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrParseTime, record[5])
return nil, fmt.Errorf("%v: %v", errorParseTime, record[5])
}
temperature.CreationDate = &temperatureCreationDate
@ -95,7 +95,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if len(record) == 7 && record[6] != "" {
temperatureUpdateDate, err := time.Parse(timeFormat, record[6])
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrParseTime, record[6])
return nil, fmt.Errorf("%v: %v", errorParseTime, record[6])
}
temperature.UpdateDate = &temperatureUpdateDate
@ -111,7 +111,7 @@ func (cl *csvLogfile) WriteHumidities(humidities []*types.Humidity) error {
f, err := os.Create(cl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogileCreate, cl.logfile)
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
}
defer f.Close()
@ -137,7 +137,7 @@ func (cl *csvLogfile) WriteHumidities(humidities []*types.Humidity) error {
func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
f, err := os.Create(cl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogileCreate, cl.logfile)
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
}
defer f.Close()

View File

@ -2,10 +2,18 @@ package logfile
import "errors"
var ErrLogfileNotFound = errors.New("Can not find logfile")
var ErrLogileCreate = errors.New("Can not create logfile")
var ErrLogfileDecode = errors.New("Can not decode from reader")
var ErrLogfileEncode = errors.New("Cano not encode from writer")
var ErrLogfileOpen = errors.New("Can not open logfile")
var ErrParseFloat = errors.New("Can not parse float")
var ErrParseTime = errors.New("Can not parse time")
var errorLogfileNotFound = errors.New("Can not find logfile")
var errorLogfileCreate = errors.New("Can not create logfile")
var errorLogfileDecode = errors.New("Can not decode from reader")
var errorLogfileEncode = errors.New("Can not encode from writer")
var errorLogfileOpen = errors.New("Can not open logfile")
var errorParseFloat = errors.New("Can not parse float")
var errorParseTime = errors.New("Can not parse time")
var errorNoValidHumidityID = errors.New("No valid humidity id detected or available")
var errorNoValidMesuredValue = errors.New("No mesured value detected or available")
var errorNoValidSensorID = errors.New("No sensor id detected or available")
var errorNoValidTemperatureID = errors.New("No valid temperature id detected or available")
var errorNoValidTime = errors.New("No time detected or available")
var errorNoValidTimePeriods = errors.New("No valid time periods")

View File

@ -18,21 +18,21 @@ func (jl *jsonLogfile) GetLogfile() string {
func (jl *jsonLogfile) ReadHumidities() ([]*types.Humidity, error) {
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, jl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.logfile)
}
humidities := make([]*types.Humidity, 0)
f, err := os.Open(jl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, jl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.logfile)
}
defer f.Close()
jsonDecoder := json.NewDecoder(f)
err = jsonDecoder.Decode(&humidities)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
return humidities, nil
@ -40,21 +40,21 @@ func (jl *jsonLogfile) ReadHumidities() ([]*types.Humidity, error) {
func (jl *jsonLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, jl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.logfile)
}
temperatures := make([]*types.Temperature, 0)
f, err := os.Open(jl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, jl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.logfile)
}
defer f.Close()
jsonDecoder := json.NewDecoder(f)
err = jsonDecoder.Decode(&temperatures)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
return temperatures, nil
@ -64,7 +64,7 @@ func (jl *jsonLogfile) WriteHumidities(humidities []*types.Humidity) error {
f, err := os.Create(jl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogileCreate, jl.logfile)
return fmt.Errorf("%v: %v", errorLogfileCreate, jl.logfile)
}
defer f.Close()
@ -72,7 +72,7 @@ func (jl *jsonLogfile) WriteHumidities(humidities []*types.Humidity) error {
jsonEncoder.SetIndent("", " ")
err = jsonEncoder.Encode(humidities)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
}
return nil
}
@ -80,7 +80,7 @@ func (jl *jsonLogfile) WriteHumidities(humidities []*types.Humidity) error {
func (jl *jsonLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
f, err := os.Create(jl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogileCreate, jl.logfile)
return fmt.Errorf("%v: %v", errorLogfileCreate, jl.logfile)
}
defer f.Close()
@ -90,7 +90,7 @@ func (jl *jsonLogfile) WriteTemperatures(temperatures []*types.Temperature) erro
jsonEncoder.SetIndent("", " ")
err = jsonEncoder.Encode(temperatures)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
}
return nil
}

90
pkg/logfile/json_test.go Normal file
View File

@ -0,0 +1,90 @@
package logfile_test
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/go-flucky/flucky/pkg/logfile"
"github.com/go-flucky/flucky/pkg/types"
uuid "github.com/satori/go.uuid"
"github.com/stretchr/testify/require"
)
var tmpDir string
func TestJSON(t *testing.T) {
require := require.New(t)
tmpDir = filepath.Join(os.TempDir(), uuid.NewV4().String())
err := os.MkdirAll(tmpDir, 0775)
require.Nil(err)
testCases := []*testCase{
&testCase{
name: "validateTemperatures",
f: testValidateTemperatures,
},
}
for _, testCase := range testCases {
err := t.Run(testCase.name, testCase.f)
require.True(err)
}
}
// func testCompressTemperatures(t *testing.T) {
// require := require.New(t)
// assets := []string{
// "pkg/logfile/test/json/faultyTemperatures_01.json",
// "pkg/logfile/test/json/faultyTemperatures_02.json",
// "pkg/logfile/test/json/faultyTemperatures_03.json",
// "pkg/logfile/test/json/faultyTemperatures_04.json",
// "pkg/logfile/test/json/faultyTemperatures_05.json",
// }
// for i, asset := range assets {
// l := filepath.Join(tmpDir, fmt.Sprintf("%v.json", i))
// temperatures, err := saveAndReadTemperatureAsset(t, logfile.New(l), asset)
// require.Nil(err, "%v", err)
// }
// }
func testValidateTemperatures(t *testing.T) {
require := require.New(t)
assets := []string{
"pkg/logfile/test/json/faultyTemperatures_01.json",
"pkg/logfile/test/json/faultyTemperatures_02.json",
"pkg/logfile/test/json/faultyTemperatures_03.json",
"pkg/logfile/test/json/faultyTemperatures_04.json",
"pkg/logfile/test/json/faultyTemperatures_05.json",
}
for i, asset := range assets {
l := filepath.Join(tmpDir, fmt.Sprintf("%v.json", i))
temperatures, err := saveAndReadTemperatureAsset(t, logfile.New(l), asset)
require.Nil(err, "%v", err)
err = logfile.ValidateTemperatures(temperatures)
require.Errorf(err, "Asset %v", asset)
}
}
func saveAndReadTemperatureAsset(t *testing.T, logfile logfile.Logfile, a string) ([]*types.Temperature, error) {
require := require.New(t)
f, err := os.Create(logfile.GetLogfile())
require.Nil(err)
f.Write(MustAsset(a))
f.Close()
temperatures, err := logfile.ReadTemperatures()
require.Nil(err, "%v", err)
return temperatures, nil
}

View File

@ -3,12 +3,16 @@ package logfile
import (
"os"
"path/filepath"
"regexp"
"sort"
"time"
"github.com/go-flucky/flucky/pkg/types"
)
var validUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
var timeFormat = "2006-01-02 15:04:05.999999999 -0700"
// AppendTemperatures with temperature values from a logfile. As additional option it's possible to compress the temperature data.
func AppendTemperatures(logfile Logfile, compression bool, temperatures []*types.Temperature) error {
@ -124,3 +128,30 @@ func SortTemperatures(temperatures []*types.Temperature) {
return temperatures[i].TemperatureFromDate.Before(temperatures[j].TemperatureFromDate)
})
}
func ValidateTemperatures(temperatures []*types.Temperature) error {
for _, temperature := range temperatures {
if !validUUID.MatchString(temperature.TemperatureID) {
return errorNoValidTemperatureID
} else if temperature.TemperatureValue == 0 {
return errorNoValidMesuredValue
} else if temperature.TemperatureFromDate.After(temperature.TemperatureTillDate) {
return errorNoValidTimePeriods
} else if !validUUID.MatchString(temperature.SensorID) {
return errorNoValidSensorID
} else if temperature.CreationDate.After(*temperature.UpdateDate) && temperature.UpdateDate != nil {
return errorNoValidTimePeriods
}
}
return nil
}
func writeCreationDate(temperatures []*types.Temperature) {
now := time.Now()
for _, temperature := range temperatures {
if temperature.CreationDate == nil {
temperature.CreationDate = &now
}
}
}

View File

@ -0,0 +1,8 @@
package logfile_test
import "testing"
type testCase struct {
name string
f func(t *testing.T)
}

View File

@ -1,7 +1,6 @@
[
{
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "",
"temperature_from_date": "2019-05-01T00:00:00.000000000+02:00",
"temperature_till_date": "2019-10-01T00:00:00.000000000+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",

View File

@ -1,19 +0,0 @@
package logfile
import (
"time"
"github.com/go-flucky/flucky/pkg/types"
)
//.999999999 -0700 MST
var timeFormat = "2006-01-02 15:04:05.999999999 -0700"
func writeCreationDate(temperatures []*types.Temperature) {
now := time.Now()
for _, temperature := range temperatures {
if temperature.CreationDate == nil {
temperature.CreationDate = &now
}
}
}

View File

@ -18,21 +18,21 @@ func (xl *xmlLogfile) GetLogfile() string {
func (xl *xmlLogfile) ReadHumidities() ([]*types.Humidity, error) {
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, xl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
}
humidities := make([]*types.Humidity, 0)
f, err := os.Open(xl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, xl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
}
defer f.Close()
xmlDecoder := xml.NewDecoder(f)
err = xmlDecoder.Decode(&humidities)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
return humidities, nil
@ -40,21 +40,21 @@ func (xl *xmlLogfile) ReadHumidities() ([]*types.Humidity, error) {
func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
return nil, fmt.Errorf("%v: %v", ErrLogfileNotFound, xl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
}
temperatures := make([]*types.Temperature, 0)
f, err := os.Open(xl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileOpen, xl.logfile)
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
}
defer f.Close()
xmlDecoder := xml.NewDecoder(f)
err = xmlDecoder.Decode(&temperatures)
if err != nil {
return nil, fmt.Errorf("%v: %v", ErrLogfileDecode, err)
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
}
return temperatures, nil
@ -64,7 +64,7 @@ func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
f, err := os.Create(xl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogileCreate, xl.logfile)
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
}
defer f.Close()
@ -72,7 +72,7 @@ func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
xmlEncoder.Indent("", " ")
err = xmlEncoder.Encode(humidities)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
}
return nil
}
@ -80,7 +80,7 @@ func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
f, err := os.Create(xl.logfile)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogileCreate, xl.logfile)
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
}
defer f.Close()
@ -90,7 +90,7 @@ func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error
xmlEncoder.Indent("", " ")
err = xmlEncoder.Encode(temperatures)
if err != nil {
return fmt.Errorf("%v: %v", ErrLogfileEncode, err)
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
}
return nil
}