Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Oct 11, 2024
1 parent 3e87125 commit c6a8b16
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions connector/api/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Decimal struct {
func NewDecimal[T comparable](value T) (Decimal, error) {
result := Decimal{}
if err := result.FromValue(value); err != nil {
return Decimal{}, nil
return Decimal{}, err
}
return result, nil
}
Expand Down Expand Up @@ -50,7 +50,7 @@ func (bi Decimal) MarshalJSON() ([]byte, error) {
func (bi *Decimal) UnmarshalJSON(b []byte) error {
var value any
if err := json.Unmarshal(b, &value); err != nil {
return err
return fmt.Errorf("invalid decimal value: %s", err)
}

return bi.FromValue(value)
Expand Down
30 changes: 30 additions & 0 deletions connector/api/decimal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

import (
"encoding/json"
"testing"

"github.com/hasura/ndc-sdk-go/utils"
"gotest.tools/v3/assert"
)

func TestDecimal(t *testing.T) {
assert.Assert(t, Decimal{}.IsNil())
assert.Equal(t, Decimal{}.String(), "Inf")
assert.Equal(t, Decimal{value: utils.ToPtr(1.2)}.String(), "1.2")

_, err := NewDecimal("foo")
assert.ErrorContains(t, err, "failed to convert Float, got: foo")

dec, err := NewDecimal("1.1")
assert.NilError(t, err)
assert.Equal(t, dec.ScalarName(), "Decimal")
assert.Equal(t, dec.String(), "1.1")

assert.ErrorContains(t, json.Unmarshal([]byte("foo"), &dec), "invalid character")
assert.NilError(t, json.Unmarshal([]byte("2.2"), &dec))

bs, err := json.Marshal(dec)
assert.NilError(t, err)
assert.Equal(t, string(bs), `"2.2"`)
}

0 comments on commit c6a8b16

Please sign in to comment.