2017-09-30 03:52:17 +08:00
|
|
|
import { readFileSync } from 'fs'
|
|
|
|
import test from 'ava'
|
2017-12-16 02:57:21 +08:00
|
|
|
import { META, jsonToEson, esonToJson } from '../src/eson'
|
|
|
|
import { patchEson } from '../src/patchEson'
|
|
|
|
import { assertDeepEqualEson } from './utils/assertDeepEqualEson'
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
test('jsonpatch add', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 2}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'add', path: '/obj/b', value: {foo: 'bar'}}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
assertDeepEqualEson(t, patchedData, jsonToEson({
|
2017-09-08 17:14:41 +08:00
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 2, b: {foo: 'bar'}}
|
2017-12-16 02:57:21 +08:00
|
|
|
}))
|
2017-09-08 17:14:41 +08:00
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op: 'remove', path: '/obj/b'}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
test('jsonpatch add: insert in matrix', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 2}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'add', path: '/arr/1', value: 4}
|
|
|
|
]
|
|
|
|
|
|
|
|
const data = jsonToEson(json)
|
|
|
|
const result = patchEson(data, patch)
|
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
|
|
|
|
|
|
|
assertDeepEqualEson(t, patchedData, jsonToEson({
|
|
|
|
arr: [1,4,2,3],
|
|
|
|
obj: {a : 2}
|
|
|
|
}))
|
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op: 'remove', path: '/arr/1'}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2017-09-08 17:14:41 +08:00
|
|
|
test('jsonpatch add: append to matrix', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 2}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'add', path: '/arr/-', value: 4}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
assertDeepEqualEson(t, patchedData, jsonToEson({
|
2017-09-08 17:14:41 +08:00
|
|
|
arr: [1,2,3,4],
|
|
|
|
obj: {a : 2}
|
2017-12-16 02:57:21 +08:00
|
|
|
}))
|
2017-09-08 17:14:41 +08:00
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op: 'remove', path: '/arr/3'}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch remove', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'remove', path: '/obj/a'},
|
|
|
|
{op: 'remove', path: '/arr/1'},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
assertDeepEqualEson(t, patchedData, jsonToEson({
|
2017-09-08 17:14:41 +08:00
|
|
|
arr: [1,3],
|
|
|
|
obj: {}
|
2017-12-16 02:57:21 +08:00
|
|
|
}))
|
2017-09-08 17:14:41 +08:00
|
|
|
t.deepEqual(revert, [
|
2017-12-17 20:53:06 +08:00
|
|
|
{op: 'add', path: '/arr/1', value: 2, meta: {type: 'value'}},
|
|
|
|
{op: 'add', path: '/obj/a', value: 4, meta: {type: 'value', before: null}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, patch)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch replace', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'replace', path: '/obj/a', value: 400},
|
|
|
|
{op: 'replace', path: '/arr/1', value: 200},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
assertDeepEqualEson(t, patchedData, jsonToEson({
|
2017-09-08 17:14:41 +08:00
|
|
|
arr: [1,200,3],
|
|
|
|
obj: {a: 400}
|
2017-12-16 02:57:21 +08:00
|
|
|
}))
|
2017-09-08 17:14:41 +08:00
|
|
|
t.deepEqual(revert, [
|
2017-12-17 20:53:06 +08:00
|
|
|
{op: 'replace', path: '/arr/1', value: 2, meta: {type: 'value'}},
|
|
|
|
{op: 'replace', path: '/obj/a', value: 4, meta: {type: 'value'}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, [
|
2017-12-17 20:53:06 +08:00
|
|
|
{op: 'replace', path: '/obj/a', value: 400, meta: {type: 'value'}},
|
|
|
|
{op: 'replace', path: '/arr/1', value: 200, meta: {type: 'value'}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
// // FIXME: keep ids intact
|
|
|
|
// test('jsonpatch replace (keep ids intact)', t => {
|
|
|
|
// const json = { value: 42 }
|
|
|
|
// const patch = [
|
|
|
|
// {op: 'replace', path: '/value', value: 100}
|
|
|
|
// ]
|
|
|
|
//
|
|
|
|
// const data = jsonToEson(json)
|
|
|
|
// const valueId = data.value[META].id
|
|
|
|
//
|
|
|
|
// const patchedData = patchEson(data, patch).data
|
|
|
|
// const patchedValueId = patchedData.value[META].id
|
|
|
|
//
|
|
|
|
// t.is(patchedValueId, valueId)
|
|
|
|
// })
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
test('jsonpatch copy', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'copy', from: '/obj', path: '/arr/2'},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: [1, 2, {a:4}, 3],
|
|
|
|
obj: {a: 4}
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op: 'remove', path: '/arr/2'}
|
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, [
|
2017-12-17 20:53:06 +08:00
|
|
|
{op: 'add', path: '/arr/2', value: {a: 4}, meta: {type: 'Object'}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch copy (keeps the same ids)', t => {
|
|
|
|
const json = { foo: { bar: 42 } }
|
|
|
|
const patch = [
|
|
|
|
{op: 'copy', from: '/foo', path: '/copied'}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
|
|
|
const fooId = data.foo[META].id
|
|
|
|
const barId = data.foo.bar[META].id
|
2017-09-08 17:14:41 +08:00
|
|
|
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedData = patchEson(data, patch).data
|
2017-12-16 02:57:21 +08:00
|
|
|
const patchedFooId = patchedData.foo[META].id
|
|
|
|
const patchedBarId = patchedData.foo.bar[META].id
|
|
|
|
const copiedId = patchedData.copied[META].id
|
|
|
|
const patchedCopiedBarId = patchedData.copied.bar[META].id
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.is(patchedFooId, fooId, 'same foo id')
|
|
|
|
t.is(patchedBarId, barId, 'same bar id')
|
|
|
|
|
|
|
|
t.not(copiedId, fooId, 'different id of property copied')
|
|
|
|
|
|
|
|
// The id's of the copied childs are the same, that's okish, they will not bite each other
|
|
|
|
t.is(patchedCopiedBarId, patchedBarId, 'same copied bar id')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch move', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'move', from: '/obj', path: '/arr/2'},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.is(result.error, null)
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: [1, 2, {a:4}, 3]
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op: 'move', from: '/arr/2', path: '/obj'}
|
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, patch)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch move before', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4},
|
|
|
|
zzz: 'zzz'
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'move', from: '/obj', path: '/arr/2'},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.is(result.error, null)
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: [1, 2, {a:4}, 3],
|
|
|
|
zzz: 'zzz'
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [
|
2017-12-17 20:53:06 +08:00
|
|
|
{op: 'move', from: '/arr/2', path: '/obj', meta: {before: 'zzz'}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, patch)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch move and replace', t => {
|
|
|
|
const json = { a: 2, b: 3 }
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'move', from: '/a', path: '/b'},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
// id of the replaced B must be kept intact
|
2017-12-16 02:57:21 +08:00
|
|
|
t.is(patchedData.b[META].id, data.b[META].id)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
assertDeepEqualEson(t, patchedData, jsonToEson({b: 2}))
|
2017-09-08 17:14:41 +08:00
|
|
|
t.deepEqual(patchedJson, { b : 2 })
|
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op:'move', from: '/b', path: '/a'},
|
2017-12-17 20:53:06 +08:00
|
|
|
{op:'add', path:'/b', value: 3, meta: {type: 'value', before: 'b'}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, [
|
2017-11-22 20:05:53 +08:00
|
|
|
{op: 'remove', path: '/b'},
|
2017-09-08 17:14:41 +08:00
|
|
|
{op: 'move', from: '/a', path: '/b'}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch move and replace (nested)', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'move', from: '/obj', path: '/arr'},
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: {a:4}
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op:'move', from: '/arr', path: '/obj'},
|
2017-12-17 20:53:06 +08:00
|
|
|
{op:'add', path:'/arr', value: [1,2,3], meta: {type: 'Array'}}
|
2017-09-08 17:14:41 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
// test revert
|
2017-12-16 02:57:21 +08:00
|
|
|
const data2 = jsonToEson(patchedJson)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result2 = patchEson(data2, revert)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData2 = result2.data
|
|
|
|
const revert2 = result2.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson2 = esonToJson(patchedData2)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson2, json)
|
|
|
|
t.deepEqual(revert2, [
|
2017-11-22 20:05:53 +08:00
|
|
|
{op: 'remove', path: '/arr'},
|
2017-09-08 17:14:41 +08:00
|
|
|
{op: 'move', from: '/obj', path: '/arr'}
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch move (keep id intact)', t => {
|
|
|
|
const json = { value: 42 }
|
|
|
|
const patch = [
|
|
|
|
{op: 'move', from: '/value', path: '/moved'}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
|
|
|
const valueId = data.value[META].id
|
2017-09-08 17:14:41 +08:00
|
|
|
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedData = patchEson(data, patch).data
|
2017-12-16 02:57:21 +08:00
|
|
|
const patchedValueId = patchedData.moved[META].id
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.is(patchedValueId, valueId)
|
|
|
|
})
|
|
|
|
|
2017-12-16 04:02:17 +08:00
|
|
|
test('jsonpatch move and replace (keep ids intact)', t => {
|
|
|
|
const json = { a: 2, b: 3 }
|
|
|
|
const patch = [
|
|
|
|
{op: 'move', from: '/a', path: '/b'}
|
|
|
|
]
|
|
|
|
|
|
|
|
const data = jsonToEson(json)
|
|
|
|
const bId = data.b[META].id
|
|
|
|
|
|
|
|
t.deepEqual(data[META].props, ['a', 'b'])
|
|
|
|
|
|
|
|
const patchedData = patchEson(data, patch).data
|
|
|
|
|
|
|
|
t.is(patchedData.b[META].id, bId)
|
|
|
|
t.deepEqual(patchedData[META].props, ['b'])
|
|
|
|
})
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
test('jsonpatch test (ok)', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'test', path: '/arr', value: [1,2,3]},
|
|
|
|
{op: 'add', path: '/added', value: 'ok'}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4},
|
|
|
|
added: 'ok'
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [
|
|
|
|
{op: 'remove', path: '/added'}
|
|
|
|
])
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch test (fail: path not found)', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'test', path: '/arr/5', value: [1,2,3]},
|
|
|
|
{op: 'add', path: '/added', value: 'ok'}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
// patch shouldn't be applied
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [])
|
|
|
|
t.is(result.error.toString(), 'Error: Test failed, path not found')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('jsonpatch test (fail: value not equal)', t => {
|
|
|
|
const json = {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
}
|
|
|
|
|
|
|
|
const patch = [
|
|
|
|
{op: 'test', path: '/obj', value: {a:4, b: 6}},
|
|
|
|
{op: 'add', path: '/added', value: 'ok'}
|
|
|
|
]
|
|
|
|
|
2017-12-16 02:57:21 +08:00
|
|
|
const data = jsonToEson(json)
|
2017-09-08 19:36:15 +08:00
|
|
|
const result = patchEson(data, patch)
|
2017-09-08 17:14:41 +08:00
|
|
|
const patchedData = result.data
|
|
|
|
const revert = result.revert
|
2017-09-08 19:36:15 +08:00
|
|
|
const patchedJson = esonToJson(patchedData)
|
2017-09-08 17:14:41 +08:00
|
|
|
|
|
|
|
// patch shouldn't be applied
|
|
|
|
t.deepEqual(patchedJson, {
|
|
|
|
arr: [1,2,3],
|
|
|
|
obj: {a : 4}
|
|
|
|
})
|
|
|
|
t.deepEqual(revert, [])
|
|
|
|
t.is(result.error.toString(), 'Error: Test failed, value differs')
|
|
|
|
})
|
|
|
|
|
2017-09-30 03:52:17 +08:00
|
|
|
// helper function to print JSON in the console
|
|
|
|
function printJSON (json, message = null) {
|
|
|
|
if (message) {
|
|
|
|
console.log(message)
|
|
|
|
}
|
|
|
|
console.log(JSON.stringify(json, null, 2))
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to load a JSON file
|
|
|
|
function loadJSON (filename) {
|
|
|
|
return JSON.parse(readFileSync(__dirname + '/' + filename, 'utf-8'))
|
|
|
|
}
|