Update
This commit is contained in:
@@ -1,83 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestJSONPatcher_Add(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
patch PatchOperation
|
||||
expected map[string]interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "add simple field",
|
||||
doc: map[string]interface{}{},
|
||||
patches: []PatchOperation{
|
||||
{Op: "add", Path: "/name", Value: "John"},
|
||||
doc: map[string]interface{}{"id": "test"},
|
||||
patch: PatchOperation{
|
||||
Op: "add",
|
||||
Path: "/content",
|
||||
Value: "test content",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "add nested field",
|
||||
doc: map[string]interface{}{
|
||||
"user": map[string]interface{}{},
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "add", Path: "/user/name", Value: "Jane"},
|
||||
doc: map[string]interface{}{"id": "test", "meta": map[string]interface{}{}},
|
||||
patch: PatchOperation{
|
||||
Op: "add",
|
||||
Path: "/meta/priority",
|
||||
Value: "high",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"user": map[string]interface{}{
|
||||
"name": "Jane",
|
||||
},
|
||||
"id": "test",
|
||||
"meta": map[string]interface{}{"priority": "high"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "add to existing field (overwrite)",
|
||||
doc: map[string]interface{}{
|
||||
"name": "Old Name",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "add", Path: "/name", Value: "New Name"},
|
||||
doc: map[string]interface{}{"id": "test", "content": "old"},
|
||||
patch: PatchOperation{
|
||||
Op: "add",
|
||||
Path: "/content",
|
||||
Value: "new",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"name": "New Name",
|
||||
"id": "test",
|
||||
"content": "new",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "add array field",
|
||||
doc: map[string]interface{}{},
|
||||
patches: []PatchOperation{
|
||||
{Op: "add", Path: "/tags", Value: []interface{}{"tag1", "tag2"}},
|
||||
doc: map[string]interface{}{"id": "test"},
|
||||
patch: PatchOperation{
|
||||
Op: "add",
|
||||
Path: "/tags",
|
||||
Value: []interface{}{"tag1", "tag2"},
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
"tags": []interface{}{"tag1", "tag2"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "add complex object",
|
||||
doc: map[string]interface{}{},
|
||||
patches: []PatchOperation{
|
||||
{Op: "add", Path: "/metadata", Value: map[string]interface{}{
|
||||
"version": "1.0",
|
||||
"active": true,
|
||||
}},
|
||||
doc: map[string]interface{}{"id": "test"},
|
||||
patch: PatchOperation{
|
||||
Op: "add",
|
||||
Path: "/metadata",
|
||||
Value: map[string]interface{}{
|
||||
"created": "2025-01-01",
|
||||
"version": 1,
|
||||
},
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
"metadata": map[string]interface{}{
|
||||
"version": "1.0",
|
||||
"active": true,
|
||||
"created": "2025-01-01",
|
||||
"version": 1,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
@@ -86,321 +95,340 @@ func TestJSONPatcher_Add(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
patcher := &JSONPatcher{}
|
||||
result, err := patcher.ApplyPatches(tt.doc, []PatchOperation{tt.patch})
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_Remove(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
patch PatchOperation
|
||||
expected map[string]interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "remove simple field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "remove", Path: "/name"},
|
||||
patch: PatchOperation{
|
||||
Op: "remove",
|
||||
Path: "/content",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"age": 30,
|
||||
"id": "test",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "remove nested field",
|
||||
doc: map[string]interface{}{
|
||||
"user": map[string]interface{}{
|
||||
"name": "Jane",
|
||||
"age": 25,
|
||||
},
|
||||
"id": "test",
|
||||
"meta": map[string]interface{}{"priority": "high", "tags": []string{"tag1"}},
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "remove", Path: "/user/name"},
|
||||
patch: PatchOperation{
|
||||
Op: "remove",
|
||||
Path: "/meta/priority",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"user": map[string]interface{}{
|
||||
"age": 25,
|
||||
},
|
||||
"id": "test",
|
||||
"meta": map[string]interface{}{"tags": []string{"tag1"}},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "remove non-existent field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "remove", Path: "/age"},
|
||||
patch: PatchOperation{
|
||||
Op: "remove",
|
||||
Path: "/nonexistent",
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
patcher := &JSONPatcher{}
|
||||
result, err := patcher.ApplyPatches(tt.doc, []PatchOperation{tt.patch})
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_Replace(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
patch PatchOperation
|
||||
expected map[string]interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "replace simple field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
"content": "old content",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "replace", Path: "/name", Value: "Jane"},
|
||||
patch: PatchOperation{
|
||||
Op: "replace",
|
||||
Path: "/content",
|
||||
Value: "new content",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"name": "Jane",
|
||||
"id": "test",
|
||||
"content": "new content",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "replace nested field",
|
||||
doc: map[string]interface{}{
|
||||
"user": map[string]interface{}{
|
||||
"name": "John",
|
||||
},
|
||||
"id": "test",
|
||||
"meta": map[string]interface{}{"priority": "low"},
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "replace", Path: "/user/name", Value: "Jane"},
|
||||
patch: PatchOperation{
|
||||
Op: "replace",
|
||||
Path: "/meta/priority",
|
||||
Value: "high",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"user": map[string]interface{}{
|
||||
"name": "Jane",
|
||||
},
|
||||
"id": "test",
|
||||
"meta": map[string]interface{}{"priority": "high"},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "replace non-existent field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "replace", Path: "/age", Value: 30},
|
||||
patch: PatchOperation{
|
||||
Op: "replace",
|
||||
Path: "/nonexistent",
|
||||
Value: "value",
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
patcher := &JSONPatcher{}
|
||||
result, err := patcher.ApplyPatches(tt.doc, []PatchOperation{tt.patch})
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_Test(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
expected map[string]interface{}
|
||||
wantErr bool
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patch PatchOperation
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test field success",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "test", Path: "/name", Value: "John"},
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"name": "John",
|
||||
patch: PatchOperation{
|
||||
Op: "test",
|
||||
Path: "/content",
|
||||
Value: "test content",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test field failure",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "test", Path: "/name", Value: "Jane"},
|
||||
patch: PatchOperation{
|
||||
Op: "test",
|
||||
Path: "/content",
|
||||
Value: "different content",
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "test non-existent field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "test", Path: "/age", Value: 30},
|
||||
patch: PatchOperation{
|
||||
Op: "test",
|
||||
Path: "/nonexistent",
|
||||
Value: "value",
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
patcher := &JSONPatcher{}
|
||||
_, err := patcher.ApplyPatches(tt.doc, []PatchOperation{tt.patch})
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_Move(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
patch PatchOperation
|
||||
expected map[string]interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "move field",
|
||||
doc: map[string]interface{}{
|
||||
"firstName": "John",
|
||||
"lastName": "Doe",
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "move", From: "/firstName", Path: "/name"},
|
||||
patch: PatchOperation{
|
||||
Op: "move",
|
||||
From: "/content",
|
||||
Path: "/title",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"lastName": "Doe",
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
"title": "test content",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "move non-existent field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "move", From: "/age", Path: "/userAge"},
|
||||
patch: PatchOperation{
|
||||
Op: "move",
|
||||
From: "/nonexistent",
|
||||
Path: "/title",
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
patcher := &JSONPatcher{}
|
||||
result, err := patcher.ApplyPatches(tt.doc, []PatchOperation{tt.patch})
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_Copy(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
patch PatchOperation
|
||||
expected map[string]interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "copy field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "copy", From: "/name", Path: "/displayName"},
|
||||
patch: PatchOperation{
|
||||
Op: "copy",
|
||||
From: "/content",
|
||||
Path: "/title",
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"name": "John",
|
||||
"displayName": "John",
|
||||
"id": "test",
|
||||
"content": "test content",
|
||||
"title": "test content",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "copy non-existent field",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"id": "test",
|
||||
},
|
||||
patches: []PatchOperation{
|
||||
{Op: "copy", From: "/age", Path: "/userAge"},
|
||||
patch: PatchOperation{
|
||||
Op: "copy",
|
||||
From: "/nonexistent",
|
||||
Path: "/title",
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
patcher := &JSONPatcher{}
|
||||
result, err := patcher.ApplyPatches(tt.doc, []PatchOperation{tt.patch})
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_Complex(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
@@ -410,71 +438,63 @@ func TestJSONPatcher_Complex(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "multiple operations",
|
||||
doc: map[string]interface{}{
|
||||
"name": "John",
|
||||
"age": 30,
|
||||
},
|
||||
doc: map[string]interface{}{"id": "test"},
|
||||
patches: []PatchOperation{
|
||||
{Op: "replace", Path: "/name", Value: "Jane"},
|
||||
{Op: "add", Path: "/email", Value: "jane@example.com"},
|
||||
{Op: "remove", Path: "/age"},
|
||||
{Op: "add", Path: "/content", Value: "test"},
|
||||
{Op: "add", Path: "/priority", Value: "high"},
|
||||
{Op: "replace", Path: "/content", Value: "updated test"},
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"name": "Jane",
|
||||
"email": "jane@example.com",
|
||||
"id": "test",
|
||||
"content": "updated test",
|
||||
"priority": "high",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test then modify",
|
||||
doc: map[string]interface{}{
|
||||
"version": "1.0",
|
||||
"name": "App",
|
||||
},
|
||||
doc: map[string]interface{}{"id": "test", "version": 1},
|
||||
patches: []PatchOperation{
|
||||
{Op: "test", Path: "/version", Value: "1.0"},
|
||||
{Op: "replace", Path: "/version", Value: "1.1"},
|
||||
{Op: "add", Path: "/updated", Value: true},
|
||||
{Op: "test", Path: "/version", Value: 1},
|
||||
{Op: "replace", Path: "/version", Value: 2},
|
||||
},
|
||||
expected: map[string]interface{}{
|
||||
"version": "1.1",
|
||||
"name": "App",
|
||||
"updated": true,
|
||||
"id": "test",
|
||||
"version": 2,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "failed test stops execution",
|
||||
doc: map[string]interface{}{
|
||||
"version": "1.0",
|
||||
"name": "App",
|
||||
},
|
||||
doc: map[string]interface{}{"id": "test", "version": 1},
|
||||
patches: []PatchOperation{
|
||||
{Op: "test", Path: "/version", Value: "2.0"}, // This will fail
|
||||
{Op: "replace", Path: "/version", Value: "1.1"},
|
||||
{Op: "test", Path: "/version", Value: 2}, // This should fail
|
||||
{Op: "replace", Path: "/version", Value: 3},
|
||||
},
|
||||
expected: nil,
|
||||
wantErr: true,
|
||||
expected: map[string]interface{}{
|
||||
"id": "test",
|
||||
"version": 1,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
result, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !tt.wantErr && !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("ApplyPatches() = %v, want %v", result, tt.expected)
|
||||
|
||||
if tt.wantErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.expected, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_ParsePath(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
@@ -492,69 +512,62 @@ func TestJSONPatcher_ParsePath(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "simple path",
|
||||
path: "/name",
|
||||
expected: []string{"name"},
|
||||
path: "/content",
|
||||
expected: []string{"content"},
|
||||
},
|
||||
{
|
||||
name: "nested path",
|
||||
path: "/user/name",
|
||||
expected: []string{"user", "name"},
|
||||
path: "/meta/priority",
|
||||
expected: []string{"meta", "priority"},
|
||||
},
|
||||
{
|
||||
name: "path with escaped characters",
|
||||
path: "/user/first~0name",
|
||||
expected: []string{"user", "first~name"},
|
||||
path: "/content~1title",
|
||||
expected: []string{"content/title"},
|
||||
},
|
||||
{
|
||||
name: "path with escaped slash",
|
||||
path: "/user/first~1name",
|
||||
expected: []string{"user", "first/name"},
|
||||
path: "/content~0title",
|
||||
expected: []string{"content~title"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
result := patcher.parsePath(tt.path)
|
||||
if !reflect.DeepEqual(result, tt.expected) {
|
||||
t.Errorf("parsePath() = %v, want %v", result, tt.expected)
|
||||
}
|
||||
assert.Equal(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONPatcher_InvalidOperations(t *testing.T) {
|
||||
patcher := &JSONPatcher{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
doc map[string]interface{}
|
||||
patches []PatchOperation
|
||||
wantErr bool
|
||||
name string
|
||||
patch PatchOperation
|
||||
}{
|
||||
{
|
||||
name: "invalid operation",
|
||||
doc: map[string]interface{}{},
|
||||
patches: []PatchOperation{
|
||||
{Op: "invalid", Path: "/name", Value: "John"},
|
||||
patch: PatchOperation{
|
||||
Op: "invalid",
|
||||
Path: "/content",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid path format",
|
||||
doc: map[string]interface{}{},
|
||||
patches: []PatchOperation{
|
||||
{Op: "add", Path: "name", Value: "John"}, // Missing leading slash
|
||||
patch: PatchOperation{
|
||||
Op: "add",
|
||||
Path: "content", // missing leading slash
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := patcher.ApplyPatches(tt.doc, tt.patches)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ApplyPatches() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
patcher := &JSONPatcher{}
|
||||
doc := map[string]interface{}{"id": "test"}
|
||||
_, err := patcher.ApplyPatches(doc, []PatchOperation{tt.patch})
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user