github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/alter/alter_test.go (about) 1 package alter_test 2 3 import ( 4 "context" 5 "encoding/json" 6 "testing" 7 8 "github.com/lmorg/murex/test/count" 9 "github.com/lmorg/murex/utils/alter" 10 ) 11 12 type plan struct { 13 original string 14 path string 15 change string 16 expected string 17 } 18 19 func alterTest(t *testing.T, test *plan) { 20 t.Helper() 21 22 count.Tests(t, 1) 23 24 pathS, err := alter.SplitPath(test.path) 25 if err != nil { 26 panic(err) 27 } 28 29 var expV interface{} 30 if err := json.Unmarshal([]byte(test.expected), &expV); err != nil { 31 panic(err) 32 } 33 b, err := json.Marshal(expV) 34 if err != nil { 35 panic(err) 36 } 37 test.expected = string(b) 38 39 var old interface{} 40 err = json.Unmarshal([]byte(test.original), &old) 41 if err != nil { 42 t.Error("Error unmarshalling original for alter.Alter()") 43 t.Logf(" original: %s", test.original) 44 t.Logf(" path: %s: %v", test.path, pathS) 45 t.Logf(" change: %s", test.change) 46 t.Logf(" expected: %s.(%T)", test.expected, expV) 47 t.Logf(" actual: %s", "n/a") 48 t.Logf(" error: %s", err) 49 return 50 } 51 52 new := alter.StrToInterface(test.change) 53 54 v, err := alter.Alter(context.TODO(), old, pathS, new) 55 if err != nil { 56 t.Error("Error received from alter.Alter()") 57 t.Logf(" original: %s", test.original) 58 t.Logf(" path: %s: %v", test.path, pathS) 59 t.Logf(" change: %s", test.change) 60 t.Logf(" expected: %s.(%T)", test.expected, expV) 61 t.Logf(" actual: %s", "n/a") 62 t.Logf(" error: %s", err) 63 return 64 } 65 66 actual, err := json.Marshal(v) 67 if err != nil { 68 t.Error("Error marshalling v from alter.Alter()") 69 t.Logf(" original: %s", test.original) 70 t.Logf(" path: %s: %v", test.path, pathS) 71 t.Logf(" change: %s", test.change) 72 t.Logf(" expected: %s.(%T)", test.expected, expV) 73 t.Logf(" actual: %s", "n/a") 74 t.Logf(" error: %s", err) 75 return 76 } 77 78 if string(actual) != test.expected { 79 t.Error("Expected does not match actual") 80 t.Logf(" original: %s.(%T)", test.original, old) 81 t.Logf(" path: %s: %v", test.path, pathS) 82 t.Logf(" change: %s", test.change) 83 t.Logf(" expected: %s.(%T)", test.expected, expV) 84 t.Logf(" actual: %s.(%T)", string(actual), v) 85 t.Logf(" error: %s", "nil") 86 } 87 } 88 89 func TestUpdateMap(t *testing.T) { 90 test := plan{ 91 original: `{"1": "foo", "2": "bar"}`, 92 path: "/2", 93 change: `test`, 94 expected: `{ 95 "1": "foo", 96 "2": "test" 97 }`, 98 } 99 100 alterTest(t, &test) 101 } 102 103 func TestUpdateNestedMap(t *testing.T) { 104 test := plan{ 105 original: ` 106 { 107 "1": "foo", 108 "2": "bar", 109 "3": { 110 "a": "aye", 111 "b": "bee", 112 "c": "cee" 113 } 114 }`, 115 path: "/3", 116 change: ` 117 { 118 "d": "dee", 119 "e": "ee", 120 "f": "eff" 121 }`, 122 expected: ` 123 { 124 "1": "foo", 125 "2": "bar", 126 "3": { 127 "d": "dee", 128 "e": "ee", 129 "f": "eff" 130 } 131 }`, 132 } 133 134 alterTest(t, &test) 135 } 136 137 func TestNewMap(t *testing.T) { 138 test := plan{ 139 original: `{"1": "foo", "2": "bar"}`, 140 path: "/3", 141 change: `{"3": "test"}`, 142 expected: `{ 143 "1": "foo", 144 "2": "bar", 145 "3": { 146 "3": "test" 147 } 148 }`, 149 } 150 151 alterTest(t, &test) 152 } 153 154 func TestUpdateArrayAlpha(t *testing.T) { 155 test := plan{ 156 original: `["foo", "bar"]`, 157 path: "/1", 158 change: `test`, 159 expected: `[ 160 "foo", 161 "test" 162 ]`, 163 } 164 165 alterTest(t, &test) 166 } 167 168 func TestUpdateArrayNumeric(t *testing.T) { 169 test := plan{ 170 original: `[1, 2]`, 171 path: "/1", 172 change: `3`, 173 expected: `[ 174 1, 175 3 176 ]`, 177 } 178 179 alterTest(t, &test) 180 } 181 182 func TestNewArray(t *testing.T) { 183 test := plan{ 184 original: `{"1": "foo", "2": "bar"}`, 185 path: "/3", 186 change: `[4, 5, 6]`, 187 expected: `{ 188 "1": "foo", 189 "2": "bar", 190 "3": [ 191 4, 192 5, 193 6 194 ] 195 }`, 196 } 197 198 alterTest(t, &test) 199 } 200 201 func TestUpdateNestedArrayAlpha(t *testing.T) { 202 test := plan{ 203 original: ` 204 { 205 "1": "foo", 206 "2": "bar", 207 "3": [ 208 "aye", 209 "bee", 210 "cee" 211 ] 212 }`, 213 path: "/3", 214 change: ` 215 [ 216 "dee", 217 "ee", 218 "eff" 219 ]`, 220 expected: ` 221 { 222 "1": "foo", 223 "2": "bar", 224 "3": [ 225 "dee", 226 "ee", 227 "eff" 228 ] 229 }`, 230 } 231 232 alterTest(t, &test) 233 } 234 235 func TestUpdateNestedArrayNumeric(t *testing.T) { 236 test := plan{ 237 original: ` 238 { 239 "1": "foo", 240 "2": "bar", 241 "3": [ 242 4, 243 5, 244 6 245 ] 246 }`, 247 path: "/3", 248 change: ` 249 [ 250 7, 251 8, 252 9 253 ]`, 254 expected: ` 255 { 256 "1": "foo", 257 "2": "bar", 258 "3": [ 259 7, 260 8, 261 9 262 ] 263 }`, 264 } 265 266 alterTest(t, &test) 267 } 268 269 func TestUpdateArrayDiffDataTypesInt(t *testing.T) { 270 test := plan{ 271 original: `[ 1, 2, 3 ]`, 272 path: "/1", 273 change: `10`, 274 expected: `[ 1, 10, 3 ]`, 275 } 276 277 alterTest(t, &test) 278 } 279 280 func TestUpdateArrayDiffDataTypesFloat(t *testing.T) { 281 test := plan{ 282 original: `[ 1.1, 2.2, 3.3 ]`, 283 path: "/1", 284 change: `10.01`, 285 expected: `[ 1.1, 10.01, 3.3 ]`, 286 } 287 288 alterTest(t, &test) 289 } 290 291 func TestUpdateArrayDiffDataTypesBoolTrue(t *testing.T) { 292 test := plan{ 293 original: `[ true, true, true ]`, 294 path: "/1", 295 change: `false`, 296 expected: `[ true, false, true ]`, 297 } 298 299 alterTest(t, &test) 300 } 301 302 func TestUpdateArrayDiffDataTypesBoolFalse(t *testing.T) { 303 test := plan{ 304 original: `[ false, false, false ]`, 305 path: "/1", 306 change: `true`, 307 expected: `[ false, true, false ]`, 308 } 309 310 alterTest(t, &test) 311 } 312 313 func TestUpdateArrayDiffDataTypesString(t *testing.T) { 314 test := plan{ 315 original: `[ "a", "b", "c" ]`, 316 path: "/1", 317 change: `z`, 318 expected: `[ "a", "z", "c" ]`, 319 } 320 321 alterTest(t, &test) 322 } 323 324 func TestUpdateArrayFloat(t *testing.T) { 325 test := plan{ 326 original: `[ 1.1, 2.2, 3.3 ]`, 327 path: "/1", 328 change: `4.4`, 329 expected: `[ 1.1, 4.4, 3.3 ]`, 330 } 331 332 alterTest(t, &test) 333 }