|
1 | 1 | package jsonpatch
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "log" |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
5 | 6 | "testing"
|
6 | 7 |
|
7 | 8 | "github.com/stretchr/testify/assert"
|
| 9 | + "github.com/stretchr/testify/require" |
8 | 10 | )
|
9 | 11 |
|
10 |
| -var subArray1_current = `{"created":1739943104628936621,"updated":0,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3}]}}` |
11 |
| -var subArray1_target = `{"created":1739943104628936621,"updated":1739942662496282458,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3}]}}` |
| 12 | +var subArray1_current = `{"created":1739943104628936621,"updated":0,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3}]}}` |
| 13 | +var subArray1_target = `{"created":1739943104628936621,"updated":1739942662496282458,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3}]}}` |
12 | 14 |
|
13 |
| -var subArray2_current = `{"created":1739943104628936621,"updated":1739942662496282458,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3}]}}` |
14 |
| -var subArray2_target = `{"created":1739943104628936621,"updated":1739942662496282459,"index":"test","path":"test","data":{"subFields":[{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3},{"one":"one","two":"two","three":3}]}}` |
| 15 | +var subArray2_current = `{"created":1739943104628936621,"updated":1739942662496282458,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3}]}}` |
| 16 | +var subArray2_target = `{"created":1739943104628936621,"updated":1739942662496282459,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3},{"one":"P","two":"two","three":3}]}}` |
| 17 | + |
| 18 | +var subArray3_current = `{"created":1739943104628936621,"updated":1739942662496282459,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3},{"one":"P","two":"two","three":3}]}}` |
| 19 | +var subArray3_target = `{"created":1739943104628936621,"updated":1739942662496282460,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3},{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3}]}}` |
| 20 | + |
| 21 | +var subArray4_current = `{"created":1739943104628936621,"updated":1739942662496282460,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3},{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3}]}}` |
| 22 | +var subArray4_target = `{"created":1739943104628936621,"updated":1739942662496282461,"index":"test","path":"test","data":{"subFields":[{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3},{"one":"P","two":"two","three":3},{"one":"B","two":"two","three":3},{"one":"P","two":"two","three":3}]}}` |
| 23 | + |
| 24 | +var subArray5_current = `{"created":1739954651885800613,"updated":1739954651890302236,"index":"game","path":"","data":{"id":"18258f8986f08d85","deck":1739954651885767932,"started":1739954651885767932,"ended":0,"burn":false,"burning":0,"overriden":0,"overrider":"","voided":0,"voider":"","edited":0,"editor":"","status":"ongoing","cards":[{"suit":1,"value":10,"owner":"P","show":false},{"suit":1,"value":10,"owner":"B","show":false},{"suit":1,"value":10,"owner":"P","show":false},{"suit":1,"value":10,"owner":"B","show":false}],"tally":{"player":0,"banker":0},"previousCards":null,"result":{"status":"","playerPair":false,"bankerPair":false,"natural8":false,"natural9":false,"lucky6_2":false,"lucky6_3":false},"fakeCards":false}}` |
| 25 | +var subArray5__target = `{"created":1739954651885800613,"updated":1739954651891192258,"index":"game","path":"","data":{"id":"18258f8986f08d85","deck":1739954651885767932,"started":1739954651885767932,"ended":0,"burn":false,"burning":0,"overriden":0,"overrider":"","voided":0,"voider":"","edited":0,"editor":"","status":"ongoing","cards":[{"suit":1,"value":10,"owner":"P","show":true},{"suit":1,"value":10,"owner":"B","show":true},{"suit":1,"value":10,"owner":"P","show":true},{"suit":1,"value":10,"owner":"B","show":true},{"suit":1,"value":10,"owner":"P","show":false}],"tally":{"player":0,"banker":0},"previousCards":null,"result":{"status":"","playerPair":false,"bankerPair":false,"natural8":false,"natural9":false,"lucky6_2":false,"lucky6_3":false},"fakeCards":false}}` |
| 26 | + |
| 27 | +type Card struct { |
| 28 | + Suit int `json:"suit"` |
| 29 | + Value int `json:"value"` |
| 30 | + Owner string `json:"owner"` |
| 31 | + Show bool `json:"show"` |
| 32 | +} |
| 33 | + |
| 34 | +type GameData struct { |
| 35 | + Cards []Card `json:"cards"` |
| 36 | +} |
| 37 | + |
| 38 | +type Game struct { |
| 39 | + Data GameData `json:"data"` |
| 40 | +} |
15 | 41 |
|
16 | 42 | func TestSubfieldArray(t *testing.T) {
|
17 | 43 | patch, e := CreatePatch([]byte(subArray1_current), []byte(subArray1_target))
|
18 | 44 | assert.NoError(t, e)
|
19 |
| - assert.Equal(t, len(patch), 2, "the patch should update 2 fields") |
| 45 | + assert.Equal(t, 2, len(patch), "the patch should update 2 fields") |
20 | 46 |
|
21 | 47 | patch, e = CreatePatch([]byte(subArray2_current), []byte(subArray2_target))
|
22 | 48 | assert.NoError(t, e)
|
23 |
| - log.Println("OP", patch) |
24 |
| - assert.Equal(t, len(patch), 2, "the patch should update 2 fields") |
| 49 | + assert.Equal(t, 2, len(patch), "the patch should update 2 fields") |
| 50 | + |
| 51 | + patch, e = CreatePatch([]byte(subArray3_current), []byte(subArray3_target)) |
| 52 | + assert.NoError(t, e) |
| 53 | + assert.Equal(t, 2, len(patch), "the patch should update 2 fields") |
| 54 | + |
| 55 | + patch, e = CreatePatch([]byte(subArray4_current), []byte(subArray4_target)) |
| 56 | + assert.NoError(t, e) |
| 57 | + assert.Equal(t, 2, len(patch), "the patch should update 2 fields") |
| 58 | + |
| 59 | + patch, e = CreatePatch([]byte(subArray5_current), []byte(subArray5__target)) |
| 60 | + assert.NoError(t, e) |
| 61 | + // log.Println("OP", patch) |
| 62 | + assert.Equal(t, 8, len(patch), "the patch should update 2 fields") |
| 63 | + |
| 64 | + _patch, err := json.Marshal(patch) |
| 65 | + require.NoError(t, err) |
| 66 | + obj, err := DecodePatch([]byte(_patch)) |
| 67 | + require.NoError(t, err) |
| 68 | + patched, err := obj.Apply([]byte(subArray5_current)) |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + var _target = Game{} |
| 72 | + json.Unmarshal([]byte(subArray5__target), &_target) |
| 73 | + var _patched = Game{} |
| 74 | + json.Unmarshal([]byte(patched), &_patched) |
| 75 | + |
| 76 | + require.True(t, reflect.DeepEqual(_target, _patched)) |
25 | 77 | }
|
0 commit comments