Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions pkg/types/json_values_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package types_test

import (
"encoding/json"
"errors"
"strconv"
"testing"

"github.com/devsy-org/devsy/pkg/types"
"gotest.tools/assert"
)

type decodeCase[T any] struct {
name string
input string
expect T
err error
}

func runDecodeCases[T any](t *testing.T, cases []decodeCase[T], decode func([]byte) (T, error)) {
t.Helper()
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
got, err := decode([]byte(testCase.input))
if testCase.err != nil {
assert.Assert(t, errors.Is(err, testCase.err), "got: %v", err)
return
}
assert.NilError(t, err)
assert.DeepEqual(t, testCase.expect, got)
})
}
}

func TestStrIntArrayUnmarshalJSON(t *testing.T) {
runDecodeCases(t, []decodeCase[[]string]{
{name: "string scalar", input: `{"input": "name"}`, expect: []string{"name"}},
{name: "integer scalar", input: `{"input": 42}`, expect: []string{"42"}},
{name: "string list", input: `{"input": ["a", "b"]}`, expect: []string{"a", "b"}},
{name: "mixed list", input: `{"input": ["a", 2]}`, expect: []string{"a", "2"}},
{name: "empty string list", input: `{"input": []}`, expect: []string{}},
{name: "boolean scalar", input: `{"input": true}`, err: types.ErrUnsupportedType},
{name: "nested list", input: `{"input": [["a"]]}`, err: types.ErrUnsupportedType},
}, func(input []byte) ([]string, error) {
var data struct {
Input types.StrIntArray `json:"input"`
}
err := json.Unmarshal(input, &data)
return []string(data.Input), err
})
}

func TestStrArrayUnmarshalJSON(t *testing.T) {
runDecodeCases(t, []decodeCase[[]string]{
{name: "string scalar", input: `{"input": "make build"}`, expect: []string{"make build"}},
{
name: "string list",
input: `{"input": ["npm", "install"]}`,
expect: []string{"npm", "install"},
},
{name: "empty list", input: `{"input": []}`, expect: []string{}},
{name: "integer scalar", input: `{"input": 42}`, err: types.ErrUnsupportedType},
{name: "integer element", input: `{"input": ["npm", 1]}`, err: types.ErrUnsupportedType},
{name: "null element", input: `{"input": [null]}`, err: types.ErrUnsupportedType},
}, func(input []byte) ([]string, error) {
var data struct {
Input types.StrArray `json:"input"`
}
err := json.Unmarshal(input, &data)
return []string(data.Input), err
})
}

func parseStrBool(input []byte) (bool, error) {
var data struct {
Local types.StrBool `json:"local"`
}
if err := json.Unmarshal(input, &data); err != nil {
return false, err
}
return data.Local.Bool()
}

func TestStrBoolJSONValueParsesToBool(t *testing.T) {
runDecodeCases(t, []decodeCase[bool]{
{name: "boolean true", input: `{"local": true}`, expect: true},
{name: "boolean false", input: `{"local": false}`, expect: false},
{name: "string true", input: `{"local": "true"}`, expect: true},
{name: "string one", input: `{"local": "1"}`, expect: true},
{name: "empty string is unset", input: `{"local": ""}`, expect: false},
{name: "absent field is unset", input: `{}`, expect: false},
{name: "string yes", input: `{"local": "yes"}`, err: strconv.ErrSyntax},
{name: "number", input: `{"local": 1}`, err: types.ErrUnsupportedType},
}, parseStrBool)
}

func TestStrBoolBoolNilReceiverIsFalse(t *testing.T) {
var unset *types.StrBool

got, err := unset.Bool()

assert.NilError(t, err)
assert.Equal(t, got, false)
}

func TestOptionEnumArrayUnmarshalJSON(t *testing.T) {
runDecodeCases(t, []decodeCase[[]types.OptionEnum]{
{
name: "plain values",
input: `{"enum": ["dev", "prod"]}`,
expect: []types.OptionEnum{{Value: "dev"}, {Value: "prod"}},
},
{
name: "objects with display names",
input: `{"enum": [{"value": "dev", "displayName": "Development"}]}`,
expect: []types.OptionEnum{{Value: "dev", DisplayName: "Development"}},
},
{name: "empty enum list", input: `{"enum": []}`, expect: []types.OptionEnum{}},
{
name: "object instead of list",
input: `{"enum": {"value": "dev"}}`,
err: types.ErrUnsupportedType,
},
{name: "integer elements", input: `{"enum": [1, 2]}`, err: types.ErrUnsupportedType},
}, func(input []byte) ([]types.OptionEnum, error) {
var data struct {
Enum types.OptionEnumArray `json:"enum"`
}
err := json.Unmarshal(input, &data)
return []types.OptionEnum(data.Enum), err
})
}
155 changes: 155 additions & 0 deletions pkg/types/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package types_test

import (
"encoding/json"
"testing"
"time"

"github.com/devsy-org/devsy/pkg/types"
"gotest.tools/assert"
)

const (
utcStamp = "2026-10-19T08:30:00Z"
jsonUTCStamp = `"` + utcStamp + `"`
)

var (
utcInstant = time.Date(2026, 10, 19, 8, 30, 0, 0, time.UTC)
plusTwoZone = time.FixedZone("UTC+2", 2*60*60)
plusTwoInstant = time.Date(2026, 10, 19, 10, 30, 0, 0, plusTwoZone)
subSecond = time.Date(2026, 10, 19, 8, 30, 0, 123456789, time.UTC)
)

func TestTimeMarshalJSON(t *testing.T) {
testCases := []struct {
name string
input types.Time
expect string
}{
{name: "zero value is null", input: types.Time{}, expect: "null"},
{name: "UTC is formatted with Z", input: types.NewTime(utcInstant), expect: jsonUTCStamp},
{
name: "offset is converted to UTC",
input: types.NewTime(plusTwoInstant),
expect: jsonUTCStamp,
},
{name: "sub-second is dropped", input: types.NewTime(subSecond), expect: jsonUTCStamp},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
out, err := json.Marshal(testCase.input)
assert.NilError(t, err)
assert.Equal(t, string(out), testCase.expect)
})
}
}

func TestTimeUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
input string
expect time.Time
wantErr bool
}{
{name: "null is zero", input: `{"at": null}`, expect: time.Time{}},
{name: "UTC instant", input: `{"at": "` + utcStamp + `"}`, expect: utcInstant},
{name: "offset instant", input: `{"at": "2026-10-19T10:30:00+02:00"}`, expect: utcInstant},
{name: "date only is rejected", input: `{"at": "2026-10-19"}`, wantErr: true},
{name: "missing zone is rejected", input: `{"at": "2026-10-19T08:30:00"}`, wantErr: true},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
var data struct {
At types.Time `json:"at"`
}
err := json.Unmarshal([]byte(testCase.input), &data)
if testCase.wantErr {
assert.Assert(t, err != nil)
return
}
assert.NilError(t, err)
assert.Assert(t, data.At.Time.Equal(testCase.expect), "got %v", data.At.Time)
})
}
}

func TestTimeQueryParameterRoundTrip(t *testing.T) {
testCases := []struct {
name string
input string
expect time.Time
wantErr bool
}{
{name: "empty is zero", input: "", expect: time.Time{}},
{name: "null is zero", input: "null", expect: time.Time{}},
{name: "UTC instant", input: utcStamp, expect: utcInstant},
{name: "offset instant", input: "2026-10-19T10:30:00+02:00", expect: utcInstant},
{name: "missing zone is rejected", input: "2026-10-19T08:30:00", wantErr: true},
{name: "garbage is rejected", input: "yesterday", wantErr: true},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
var got types.Time
err := got.UnmarshalQueryParameter(testCase.input)
if testCase.wantErr {
assert.Assert(t, err != nil)
return
}
assert.NilError(t, err)
assert.Assert(t, got.Time.Equal(testCase.expect), "got %v", got.Time)
})
}
}

func TestTimeMarshalQueryParameter(t *testing.T) {
testCases := []struct {
name string
input types.Time
expect string
}{
{name: "zero value is empty", input: types.Time{}, expect: ""},
{name: "offset is encoded as UTC", input: types.NewTime(plusTwoInstant), expect: utcStamp},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
encoded, err := testCase.input.MarshalQueryParameter()
assert.NilError(t, err)
assert.Equal(t, encoded, testCase.expect)
})
}
}

func TestTimeNilAndOrderingHelpers(t *testing.T) {
earlier := types.NewTime(utcInstant)
later := types.NewTime(utcInstant.Add(time.Hour))
sameInstant := types.NewTime(utcInstant.In(plusTwoZone))
var nilTime *types.Time
var zero types.Time

testCases := []struct {
name string
got bool
expect bool
}{
{name: "Before earlier than later", got: earlier.Before(&later), expect: true},
{name: "Before later than earlier", got: later.Before(&earlier), expect: false},
{name: "Before with nil argument", got: earlier.Before(nil), expect: false},
{name: "Equal nil and nil", got: nilTime.Equal(nil), expect: true},
{name: "Equal value and nil", got: earlier.Equal(nil), expect: false},
{name: "Equal across zones", got: earlier.Equal(&sameInstant), expect: true},
{name: "IsZero nil receiver", got: nilTime.IsZero(), expect: true},
{name: "IsZero zero value", got: zero.IsZero(), expect: true},
{name: "IsZero set value", got: later.IsZero(), expect: false},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.got, testCase.expect)
})
}
}
Loading