mirror of
https://github.com/kyverno/kyverno.git
synced 2025-04-09 10:42:22 +00:00
chore: add unit tests for pkg/utils/json (#4516)
Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
parent
103ba4b947
commit
a278123464
1 changed files with 210 additions and 0 deletions
210
pkg/utils/json/patch_test.go
Normal file
210
pkg/utils/json/patch_test.go
Normal file
|
@ -0,0 +1,210 @@
|
|||
package json
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewPatchOperation(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
op string
|
||||
value interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want PatchOperation
|
||||
}{{
|
||||
name: "test",
|
||||
args: args{"path", "op", 123},
|
||||
want: PatchOperation{
|
||||
Path: "path",
|
||||
Op: "op",
|
||||
Value: 123,
|
||||
},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert.Equal(t, tt.want, NewPatchOperation(tt.args.path, tt.args.op, tt.args.value))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchOperation_Marshal(t *testing.T) {
|
||||
type fields struct {
|
||||
Path string
|
||||
Op string
|
||||
Value interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{{
|
||||
name: "test",
|
||||
fields: fields{
|
||||
Path: "path",
|
||||
Op: "op",
|
||||
Value: 123,
|
||||
},
|
||||
want: []byte(`{"path":"path","op":"op","value":123}`),
|
||||
wantErr: false,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := &PatchOperation{
|
||||
Path: tt.fields.Path,
|
||||
Op: tt.fields.Op,
|
||||
Value: tt.fields.Value,
|
||||
}
|
||||
got, err := p.Marshal()
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchOperation_ToPatchBytes(t *testing.T) {
|
||||
type fields struct {
|
||||
Path string
|
||||
Op string
|
||||
Value interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{{
|
||||
name: "test",
|
||||
fields: fields{
|
||||
Path: "path",
|
||||
Op: "op",
|
||||
Value: 123,
|
||||
},
|
||||
want: []byte(`[{"path":"path","op":"op","value":123}]`),
|
||||
wantErr: false,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := &PatchOperation{
|
||||
Path: tt.fields.Path,
|
||||
Op: tt.fields.Op,
|
||||
Value: tt.fields.Value,
|
||||
}
|
||||
got, err := p.ToPatchBytes()
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalPatchOperation(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
op string
|
||||
value interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{{
|
||||
name: "test",
|
||||
args: args{
|
||||
path: "path",
|
||||
op: "op",
|
||||
value: 123,
|
||||
},
|
||||
want: []byte(`{"path":"path","op":"op","value":123}`),
|
||||
wantErr: false,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := MarshalPatchOperation(tt.args.path, tt.args.op, tt.args.value)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckPatch(t *testing.T) {
|
||||
type args struct {
|
||||
patch []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantErr bool
|
||||
}{{
|
||||
name: "test",
|
||||
args: args{[]byte(`{"path":"path","op":"op","value":123}`)},
|
||||
wantErr: false,
|
||||
}, {
|
||||
name: "error",
|
||||
args: args{[]byte(`"foo":"bar"`)},
|
||||
wantErr: true,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := CheckPatch(tt.args.patch)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalPatchOperation(t *testing.T) {
|
||||
type args struct {
|
||||
patch []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *PatchOperation
|
||||
wantErr bool
|
||||
}{{
|
||||
name: "test",
|
||||
args: args{[]byte(`{"path":"path","op":"op","value":123}`)},
|
||||
want: &PatchOperation{
|
||||
Path: "path",
|
||||
Op: "op",
|
||||
Value: float64(123),
|
||||
},
|
||||
wantErr: false,
|
||||
}, {
|
||||
name: "error",
|
||||
args: args{[]byte(`"foo":"bar"`)},
|
||||
wantErr: true,
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := UnmarshalPatchOperation(tt.args.patch)
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue