Skip to content

Commit

Permalink
Add tests for DateTime type
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo <[email protected]>
  • Loading branch information
lorenzodonini committed Oct 30, 2023
1 parent 73e165b commit bc0ceee
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
64 changes: 63 additions & 1 deletion ocpp1.6_test/common_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions ocpp1.6_test/ocpp16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -708,6 +709,7 @@ func (suite *OcppV16TestSuite) SetupTest() {
return defaultMessageId
}}
ocppj.SetMessageIdGenerator(suite.messageIdGenerator.generateId)
types.DateTimeFormat = time.RFC3339
}

func (suite *OcppV16TestSuite) TestIsConnected() {
Expand Down
60 changes: 60 additions & 0 deletions ocpp2.0.1_test/common_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions ocpp2.0.1_test/ocpp2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"reflect"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1141,6 +1142,7 @@ func (suite *OcppV2TestSuite) SetupTest() {
return defaultMessageId
}}
ocppj.SetMessageIdGenerator(suite.messageIdGenerator.generateId)
types.DateTimeFormat = time.RFC3339
}

func (suite *OcppV2TestSuite) TestIsConnected() {
Expand Down

0 comments on commit bc0ceee

Please sign in to comment.