Skip to content

Commit

Permalink
Merge pull request #23 from make-software/hotfix/day_ttl
Browse files Browse the repository at this point in the history
Fix 1day TTL for deploy header
  • Loading branch information
koltsov-iv committed Jun 19, 2023
2 parents 7ea0454 + a72f96a commit b2bcc7a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
9 changes: 9 additions & 0 deletions tests/data/deploy/deploy_header_ttl_1day.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"account": "01226a34b8cbcc24b8a3b758d710010dff63bb6f3b278222f33c294c8e1d6a9e25",
"timestamp": "2021-05-11T12:10:58.754Z",
"ttl": "1day",
"gas_price": 1,
"body_hash": "efcbe01ef3a2d9825d99b5a3c5c3dcc574a7c60c5152de39e03e3aeb29aa4400",
"dependencies": [],
"chain_name": "casper-test"
}
10 changes: 7 additions & 3 deletions tests/types/deploy_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/types"
)
Expand All @@ -18,19 +19,22 @@ func Test_DeployHeader_MarshalUnmarshal_ShouldBeSameResult(t *testing.T) {
{
"deploy with StoredContractByName",
"../data/deploy/deploy_header_with_deps.json",
}, {
"deploy with StoredContractByName",
"../data/deploy/deploy_header_ttl_1day.json",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
data, err := os.ReadFile(test.fixturePath)
assert.NoError(t, err)
require.NoError(t, err)

var deploy types.DeployHeader
err = json.Unmarshal(data, &deploy)
assert.NoError(t, err)
require.NoError(t, err)

result, err := json.Marshal(deploy)
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, string(data), string(result), test.name)
})
}
Expand Down
21 changes: 21 additions & 0 deletions tests/types/deploy_ttl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package types

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/types"
)

func Test_DurationUnmarshal_withSpace_shouldBeParsed(t *testing.T) {
value := `"2h 46m 40s"`
var result types.Duration
err := json.Unmarshal([]byte(value), &result)
require.NoError(t, err)
data, err := result.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `"2h46m40s"`, string(data))
}
8 changes: 7 additions & 1 deletion types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Duration time.Duration

func (d Duration) MarshalJSON() ([]byte, error) {
s := time.Duration(d).String()
if s == "24h0m0s" {
s = "1day"
}
if strings.HasSuffix(s, "h0m0s") {
s = strings.TrimSuffix(s, "0m0s")
}
Expand All @@ -51,7 +54,10 @@ func (d *Duration) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &dataString); err != nil {
return err
}

dataString = strings.ReplaceAll(dataString, " ", "")
if dataString == "1day" {
dataString = "24h"
}
duration, err := time.ParseDuration(dataString)
if err != nil {
return err
Expand Down

0 comments on commit b2bcc7a

Please sign in to comment.