From bc0ceee1fcde13025edc2f494e0a8d39a3b01b3c Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Fri, 27 Oct 2023 09:14:15 +0200 Subject: [PATCH] Add tests for DateTime type Signed-off-by: Lorenzo --- ocpp1.6_test/common_test.go | 64 ++++++++++++++++++++++++++++++++++- ocpp1.6_test/ocpp16_test.go | 2 ++ ocpp2.0.1_test/common_test.go | 60 ++++++++++++++++++++++++++++++++ ocpp2.0.1_test/ocpp2_test.go | 2 ++ 4 files changed, 127 insertions(+), 1 deletion(-) diff --git a/ocpp1.6_test/common_test.go b/ocpp1.6_test/common_test.go index 7680d76b..dba35aa0 100644 --- a/ocpp1.6_test/common_test.go +++ b/ocpp1.6_test/common_test.go @@ -1,8 +1,13 @@ package ocpp16_test import ( - "github.com/lorenzodonini/ocpp-go/ocpp1.6/types" + "encoding/json" + "strings" "time" + + "github.com/relvacode/iso8601" + + "github.com/lorenzodonini/ocpp-go/ocpp1.6/types" ) // Utility functions @@ -114,3 +119,60 @@ func (suite *OcppV16TestSuite) TestMeterValueValidation() { } ExecuteGenericTestTable(suite.T(), testTable) } + +func (suite *OcppV16TestSuite) TestUnmarshalDateTime() { + testTable := []struct { + RawDateTime string + ExpectedValid bool + ExpectedTime time.Time + ExpectedError error + }{ + {"\"2019-03-01T10:00:00Z\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00+01:00\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000Z\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000+01:00\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00+01\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000+01\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01 10:00:00+00:00\"", false, time.Time{}, &iso8601.UnexpectedCharacterError{Character: ' '}}, + {"\"null\"", false, time.Time{}, &iso8601.UnexpectedCharacterError{Character: 110}}, + {"\"\"", false, time.Time{}, &iso8601.RangeError{Element: "month", Min: 1, Max: 12}}, + {"null", true, time.Time{}, nil}, + } + for _, dt := range testTable { + jsonStr := []byte(dt.RawDateTime) + var dateTime types.DateTime + err := json.Unmarshal(jsonStr, &dateTime) + if dt.ExpectedValid { + suite.NoError(err) + suite.NotNil(dateTime) + suite.True(dt.ExpectedTime.Equal(dateTime.Time)) + } else { + suite.Error(err) + suite.ErrorAs(err, &dt.ExpectedError) + } + } +} + +func (suite *OcppV16TestSuite) TestMarshalDateTime() { + testTable := []struct { + Time time.Time + Format string + ExpectedFormattedString string + }{ + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), "", "2019-03-01T10:00:00Z"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), time.RFC3339, "2019-03-01T10:00:00Z"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), time.RFC822, "01 Mar 19 10:00 UTC"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), time.RFC1123, "Fri, 01 Mar 2019 10:00:00 UTC"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), "invalidFormat", "invalidFormat"}, + } + for _, dt := range testTable { + dateTime := types.NewDateTime(dt.Time) + types.DateTimeFormat = dt.Format + rawJson, err := dateTime.MarshalJSON() + suite.NoError(err) + formatted := strings.Trim(string(rawJson), "\"") + suite.Equal(dt.ExpectedFormattedString, formatted) + } +} diff --git a/ocpp1.6_test/ocpp16_test.go b/ocpp1.6_test/ocpp16_test.go index 69cb7cd5..be00a706 100644 --- a/ocpp1.6_test/ocpp16_test.go +++ b/ocpp1.6_test/ocpp16_test.go @@ -7,6 +7,7 @@ import ( "net/http" "reflect" "testing" + "time" "github.com/stretchr/testify/require" @@ -708,6 +709,7 @@ func (suite *OcppV16TestSuite) SetupTest() { return defaultMessageId }} ocppj.SetMessageIdGenerator(suite.messageIdGenerator.generateId) + types.DateTimeFormat = time.RFC3339 } func (suite *OcppV16TestSuite) TestIsConnected() { diff --git a/ocpp2.0.1_test/common_test.go b/ocpp2.0.1_test/common_test.go index 728e25f1..b3e0fe64 100644 --- a/ocpp2.0.1_test/common_test.go +++ b/ocpp2.0.1_test/common_test.go @@ -1,9 +1,12 @@ package ocpp2_test import ( + "encoding/json" "strings" "time" + "github.com/relvacode/iso8601" + "github.com/lorenzodonini/ocpp-go/ocpp2.0.1/display" "github.com/lorenzodonini/ocpp-go/ocpp2.0.1/types" ) @@ -278,3 +281,60 @@ func (suite *OcppV2TestSuite) TestMessageInfoValidation() { } ExecuteGenericTestTable(suite.T(), testTable) } + +func (suite *OcppV2TestSuite) TestUnmarshalDateTime() { + testTable := []struct { + RawDateTime string + ExpectedValid bool + ExpectedTime time.Time + ExpectedError error + }{ + {"\"2019-03-01T10:00:00Z\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00+01:00\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000Z\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000+01:00\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00+01\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000\"", true, time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01T10:00:00.000+01\"", true, time.Date(2019, 3, 1, 9, 0, 0, 0, time.UTC), nil}, + {"\"2019-03-01 10:00:00+00:00\"", false, time.Time{}, &iso8601.UnexpectedCharacterError{Character: ' '}}, + {"\"null\"", false, time.Time{}, &iso8601.UnexpectedCharacterError{Character: 110}}, + {"\"\"", false, time.Time{}, &iso8601.RangeError{Element: "month", Min: 1, Max: 12}}, + {"null", true, time.Time{}, nil}, + } + for _, dt := range testTable { + jsonStr := []byte(dt.RawDateTime) + var dateTime types.DateTime + err := json.Unmarshal(jsonStr, &dateTime) + if dt.ExpectedValid { + suite.NoError(err) + suite.NotNil(dateTime) + suite.True(dt.ExpectedTime.Equal(dateTime.Time)) + } else { + suite.Error(err) + suite.ErrorAs(err, &dt.ExpectedError) + } + } +} + +func (suite *OcppV2TestSuite) TestMarshalDateTime() { + testTable := []struct { + Time time.Time + Format string + ExpectedFormattedString string + }{ + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), "", "2019-03-01T10:00:00Z"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), time.RFC3339, "2019-03-01T10:00:00Z"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), time.RFC822, "01 Mar 19 10:00 UTC"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), time.RFC1123, "Fri, 01 Mar 2019 10:00:00 UTC"}, + {time.Date(2019, 3, 1, 10, 0, 0, 0, time.UTC), "invalidFormat", "invalidFormat"}, + } + for _, dt := range testTable { + dateTime := types.NewDateTime(dt.Time) + types.DateTimeFormat = dt.Format + rawJson, err := dateTime.MarshalJSON() + suite.NoError(err) + formatted := strings.Trim(string(rawJson), "\"") + suite.Equal(dt.ExpectedFormattedString, formatted) + } +} diff --git a/ocpp2.0.1_test/ocpp2_test.go b/ocpp2.0.1_test/ocpp2_test.go index dc1cc53d..71702490 100644 --- a/ocpp2.0.1_test/ocpp2_test.go +++ b/ocpp2.0.1_test/ocpp2_test.go @@ -7,6 +7,7 @@ import ( "net/http" "reflect" "testing" + "time" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" @@ -1141,6 +1142,7 @@ func (suite *OcppV2TestSuite) SetupTest() { return defaultMessageId }} ocppj.SetMessageIdGenerator(suite.messageIdGenerator.generateId) + types.DateTimeFormat = time.RFC3339 } func (suite *OcppV2TestSuite) TestIsConnected() {