Skip to content

Commit

Permalink
UT for PD API struct
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc committed Dec 24, 2024
1 parent 96ede0b commit 222d7e5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/pdapi/pd/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewDuration(duration time.Duration) Duration {

// MarshalJSON returns the duration as a JSON string.
func (d *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%q"`, d.String())), nil
return []byte(fmt.Sprintf(`%q`, d.String())), nil
}

// UnmarshalJSON parses a JSON string into the duration.
Expand Down
42 changes: 42 additions & 0 deletions pkg/pdapi/pd/duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pd

import (
"testing"
"time"

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

func TestDuration(t *testing.T) {
du := NewDuration(72*time.Hour + 3*time.Minute + 500*time.Millisecond)
data, err := du.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `"72h3m0.5s"`, string(data))
data2, err := du.MarshalText()
require.NoError(t, err)
assert.Equal(t, "72h3m0.5s", string(data2))

var du2 Duration
err = du2.UnmarshalJSON(data)
require.NoError(t, err)
assert.Equal(t, du, du2)

err = du2.UnmarshalText(data2)
require.NoError(t, err)
assert.Equal(t, du, du2)
}
48 changes: 48 additions & 0 deletions pkg/pdapi/pd/size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pd

import (
"testing"

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

func TestParseMBFromText(t *testing.T) {
value := uint64(512)
result := ParseMBFromText("1.0 MiB", value)
assert.Equal(t, uint64(1), result)

result = ParseMBFromText("invalid", value)
assert.Equal(t, value, result)
}

func TestByteSize(t *testing.T) {
b := ByteSize(17 * 1024 * 1024) // 17 MiB
data, err := b.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, `"17MiB"`, string(data))

var b2 ByteSize
err = b2.UnmarshalJSON(data)
require.NoError(t, err)
assert.Equal(t, b, b2)

data2 := []byte("17MiB")
err = b2.UnmarshalText(data2)
require.NoError(t, err)
assert.Equal(t, b, b2)
}

0 comments on commit 222d7e5

Please sign in to comment.