Permalink
Browse files

more tests

  • Loading branch information...
mattbaird committed Oct 12, 2015
1 parent 42d8857 commit d982248cb184a402efd6e6897f9afe4abaa21a60
Showing with 711 additions and 24 deletions.
  1. +24 −24 jsonpatch_complex_test.go
  2. +181 −0 jsonpatch_hypercomplex_test.go
  3. +506 −0 jsonpatch_supercomplex_test.go
View
@@ -16,73 +16,73 @@ var complexE = `{"a":100, "b":[{"c1":"hello", "d1":"foo"},{"c2":"hello2", "d2":"
func TestComplexSame(t *testing.T) {
patch, e := CreatePatch([]byte(complexBase), []byte(complexBase))
assert.NoError(t, e)
assert.Equal(t, len(patch), 0, "they should be equal")
assert.Equal(t, 0, len(patch), "they should be equal")
}
func TestComplexOneStringReplaceInArray(t *testing.T) {
patch, e := CreatePatch([]byte(complexBase), []byte(complexA))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "replace", "they should be equal")
assert.Equal(t, change.Path, "/b/0/c1", "they should be equal")
assert.Equal(t, change.Value, "goodbye", "they should be equal")
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/b/0/c1", change.Path, "they should be equal")
assert.Equal(t, "goodbye", change.Value, "they should be equal")
}
func TestComplexOneIntReplace(t *testing.T) {
patch, e := CreatePatch([]byte(complexBase), []byte(complexB))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "replace", "they should be equal")
assert.Equal(t, change.Path, "/e/f", "they should be equal")
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/e/f", change.Path, "they should be equal")
var expected float64 = 100
assert.Equal(t, change.Value, expected, "they should be equal")
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestComplexOneAdd(t *testing.T) {
patch, e := CreatePatch([]byte(complexBase), []byte(complexC))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "add", "they should be equal")
assert.Equal(t, change.Path, "/k", "they should be equal")
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/k", change.Path, "they should be equal")
a := make(map[string]interface{})
b := make(map[string]interface{})
a["l"] = "m"
b["l"] = "o"
expected := []interface{}{a, b}
assert.Equal(t, change.Value, expected, "they should be equal")
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestComplexOneAddToArray(t *testing.T) {
patch, e := CreatePatch([]byte(complexBase), []byte(complexC))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "add", "they should be equal")
assert.Equal(t, change.Path, "/k", "they should be equal")
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/k", change.Path, "they should be equal")
a := make(map[string]interface{})
b := make(map[string]interface{})
a["l"] = "m"
b["l"] = "o"
expected := []interface{}{a, b}
assert.Equal(t, change.Value, expected, "they should be equal")
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestComplexVsEmpty(t *testing.T) {
patch, e := CreatePatch([]byte(complexBase), []byte(empty))
assert.NoError(t, e)
assert.Equal(t, len(patch), 3, "they should be equal")
assert.Equal(t, 3, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/a", "they should be equal")
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/a", change.Path, "they should be equal")
change = patch[1]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/b", "they should be equal")
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b", change.Path, "they should be equal")
change = patch[2]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/e", "they should be equal")
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/e", change.Path, "they should be equal")
}
@@ -0,0 +1,181 @@
package jsonpatch
import (
"github.com/stretchr/testify/assert"
"sort"
"testing"
)
var hyperComplexBase = `
{
"goods": [
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
]
}`
var hyperComplexA = `
{
"goods": [
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Strawberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Vanilla" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5004", "type": "Maple" }
]
}
]
}`
func TestHyperComplexSame(t *testing.T) {
patch, e := CreatePatch([]byte(hyperComplexBase), []byte(hyperComplexBase))
assert.NoError(t, e)
assert.Equal(t, len(patch), 0, "they should be equal")
}
func TestHyperComplexBoolReplace(t *testing.T) {
patch, e := CreatePatch([]byte(hyperComplexBase), []byte(hyperComplexA))
assert.NoError(t, e)
assert.Equal(t, 3, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/goods/0/batters/batter/2/type", change.Path, "they should be equal")
assert.Equal(t, "Strawberry", change.Value, "they should be equal")
change = patch[1]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/goods/2/batters/batter/2", change.Path, "they should be equal")
assert.Equal(t, map[string]interface{}{"id": "1003", "type": "Vanilla"}, change.Value, "they should be equal")
change = patch[2]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/goods/2/topping/2", "they should be equal")
assert.Equal(t, map[string]interface{}{"id": "5003", "type": "Chocolate"}, change.Value, "they should be equal")
}
Oops, something went wrong.

0 comments on commit d982248

Please sign in to comment.