github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/optional/select/utils_test.go (about) 1 package sqlselect 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/lmorg/murex/lang" 8 "github.com/lmorg/murex/test/count" 9 ) 10 11 func inlineJson(v interface{}) string { 12 b, _ := json.Marshal(v) 13 return string(b) 14 } 15 16 type Str2IfaceT struct { 17 Input []string 18 Max int 19 Output []string 20 } 21 22 func TestStringToInterfaceTrim(t *testing.T) { 23 tests := []Str2IfaceT{ 24 { 25 Input: []string{"a", "b", "c", "d", "e"}, 26 Max: 0, 27 Output: []string{}, 28 }, 29 { 30 Input: []string{"a", "b", "c", "d", "e"}, 31 Max: 1, 32 Output: []string{"a"}, 33 }, 34 { 35 Input: []string{"a", "b", "c", "d", "e"}, 36 Max: 2, 37 Output: []string{"a", "b"}, 38 }, 39 { 40 Input: []string{"a", "b", "c", "d", "e"}, 41 Max: 3, 42 Output: []string{"a", "b", "c"}, 43 }, 44 { 45 Input: []string{"a", "b", "c", "d", "e"}, 46 Max: 4, 47 Output: []string{"a", "b", "c", "d"}, 48 }, 49 { 50 Input: []string{"a", "b", "c", "d", "e"}, 51 Max: 5, 52 Output: []string{"a", "b", "c", "d", "e"}, 53 }, 54 { 55 Input: []string{"a", "b", "c", "d", "e"}, 56 Max: 6, 57 Output: []string{"a", "b", "c", "d", "e", ""}, 58 }, 59 { 60 Input: []string{"a", "b", "c", "d", "e"}, 61 Max: 7, 62 Output: []string{"a", "b", "c", "d", "e", "", ""}, 63 }, 64 { 65 Input: []string{"a", "b", "c", "d", "e"}, 66 Max: 8, 67 Output: []string{"a", "b", "c", "d", "e", "", "", ""}, 68 }, 69 ///// 70 { 71 Input: []string{}, 72 Max: 5, 73 Output: []string{"", "", "", "", ""}, 74 }, 75 { 76 Input: []string{"a"}, 77 Max: 5, 78 Output: []string{"a", "", "", "", ""}, 79 }, 80 { 81 Input: []string{"a", "b"}, 82 Max: 5, 83 Output: []string{"a", "b", "", "", ""}, 84 }, 85 { 86 Input: []string{"a", "b", "c"}, 87 Max: 5, 88 Output: []string{"a", "b", "c", "", ""}, 89 }, 90 { 91 Input: []string{"a", "b", "c", "d"}, 92 Max: 5, 93 Output: []string{"a", "b", "c", "d", ""}, 94 }, 95 { 96 Input: []string{"a", "b", "c", "d", "e"}, 97 Max: 5, 98 Output: []string{"a", "b", "c", "d", "e"}, 99 }, 100 { 101 Input: []string{"a", "b", "c", "d", "e", "f"}, 102 Max: 5, 103 Output: []string{"a", "b", "c", "d", "e"}, 104 }, 105 } 106 107 count.Tests(t, len(tests)) 108 109 for i, test := range tests { 110 actual := stringToInterfaceTrim(test.Input, test.Max) 111 112 if len(test.Output) != len(actual) { 113 t.Errorf("Length mismatch in test %d", i) 114 t.Logf(" Input: %v", test.Input) 115 t.Logf(" Max: %d", test.Max) 116 t.Logf(" Expected: %v", test.Output) 117 t.Logf(" Actual: %v", actual) 118 } 119 120 for j := range test.Output { 121 if test.Output[j] != actual[j].(string) { 122 t.Errorf("Value mismatch in test %d[%d]", i, j) 123 t.Logf(" Input: %v", test.Input) 124 t.Logf(" Max: %d", test.Max) 125 t.Logf(" Expected: %v", test.Output) 126 t.Logf(" Actual: %v", actual) 127 t.Logf(" Expected: '%s'", test.Output[j]) 128 t.Logf(" Actual: '%s'", actual[j].(string)) 129 } 130 } 131 } 132 } 133 134 type DissectParametersT struct { 135 Input []string 136 IsMethod bool 137 Output string 138 Error bool 139 FileName string 140 NamedPipes []string 141 Variables []string 142 } 143 144 func TestDissectParameters(t *testing.T) { 145 tests := []DissectParametersT{ 146 147 // is a method 148 149 { 150 Input: []string{"FROM", "file.csv", "ORDER BY", "1"}, 151 IsMethod: true, 152 Output: "", 153 FileName: "", 154 Error: true, 155 }, 156 { 157 Input: []string{"*", "FROM", "file.csv", "ORDER BY", "1"}, 158 IsMethod: true, 159 Output: "", 160 FileName: "", 161 Error: true, 162 }, 163 { 164 Input: []string{"*", "FROM", "file.csv"}, 165 IsMethod: true, 166 Output: "", 167 FileName: "", 168 Error: true, 169 }, 170 { 171 Input: []string{"FROM", "file.csv"}, 172 IsMethod: true, 173 Output: "", 174 FileName: "", 175 Error: true, 176 }, 177 { 178 Input: []string{"FROM", "file name with space.csv"}, 179 IsMethod: true, 180 Output: "", 181 FileName: "", 182 Error: true, 183 }, 184 { 185 Input: []string{"a", "b", "c", "FROM", "file.csv"}, 186 IsMethod: true, 187 Output: "", 188 FileName: "", 189 Error: true, 190 }, 191 { 192 Input: []string{"FROM", "file.csv", "ORDER BY", "1", "2", "3"}, 193 IsMethod: true, 194 Output: "", 195 FileName: "", 196 Error: true, 197 }, 198 { 199 Input: []string{"a", "b", "c", "FROM", "file.csv", "ORDER BY", "1", "2", "3"}, 200 IsMethod: true, 201 Output: "", 202 FileName: "", 203 Error: true, 204 }, 205 { 206 Input: []string{"a", "b", "c", "FROM", "file name with space.csv", "ORDER BY", "1", "2", "3"}, 207 IsMethod: true, 208 Output: "", 209 FileName: "", 210 Error: true, 211 }, 212 213 { 214 Input: []string{"a", "b", "c", "ORDER BY", "1", "2", "3"}, 215 IsMethod: true, 216 Output: "a b c ORDER BY 1 2 3", 217 FileName: "", 218 }, 219 220 // not a method 221 222 { 223 Input: []string{"FROM", "file.csv", "ORDER BY", "1"}, 224 IsMethod: false, 225 Output: "* ORDER BY 1", 226 FileName: "file.csv", 227 }, 228 { 229 Input: []string{"*", "FROM", "file.csv", "ORDER BY", "1"}, 230 IsMethod: false, 231 Output: "* ORDER BY 1", 232 FileName: "file.csv", 233 }, 234 { 235 Input: []string{"*", "FROM", "file.csv"}, 236 IsMethod: false, 237 Output: "*", 238 FileName: "file.csv", 239 }, 240 { 241 Input: []string{"FROM", "file.csv"}, 242 IsMethod: false, 243 Output: "*", 244 FileName: "file.csv", 245 }, 246 { 247 Input: []string{"FROM", "file name with space.csv"}, 248 IsMethod: false, 249 Output: "*", 250 FileName: "file name with space.csv", 251 }, 252 { 253 Input: []string{"a", "b", "c", "FROM", "file.csv"}, 254 IsMethod: false, 255 Output: "a b c", 256 FileName: "file.csv", 257 }, 258 { 259 Input: []string{"FROM", "file.csv", "ORDER BY", "1", "2", "3"}, 260 IsMethod: false, 261 Output: "* ORDER BY 1 2 3", 262 FileName: "file.csv", 263 }, 264 { 265 Input: []string{"a", "b", "c", "FROM", "file.csv", "ORDER BY", "1", "2", "3"}, 266 IsMethod: false, 267 Output: "a b c ORDER BY 1 2 3", 268 FileName: "file.csv", 269 }, 270 { 271 Input: []string{"a", "b", "c", "FROM", "file name with space.csv", "ORDER BY", "1", "2", "3"}, 272 IsMethod: false, 273 Output: "a b c ORDER BY 1 2 3", 274 FileName: "file name with space.csv", 275 }, 276 277 { 278 Input: []string{"FROM", "<foo>,", "<bar>", "ORDER BY", "1"}, 279 IsMethod: false, 280 Output: "* ORDER BY 1", 281 NamedPipes: []string{"foo", "bar"}, 282 }, 283 { 284 Input: []string{"*", "FROM", "<foo>,", "<bar>", "ORDER BY", "1"}, 285 IsMethod: false, 286 Output: "* ORDER BY 1", 287 NamedPipes: []string{"foo", "bar"}, 288 }, 289 { 290 Input: []string{"*", "FROM", "<foo>,", "<bar>"}, 291 IsMethod: false, 292 Output: "*", 293 NamedPipes: []string{"foo", "bar"}, 294 }, 295 { 296 Input: []string{"FROM", "<foo>,", "<bar>"}, 297 IsMethod: false, 298 Output: "*", 299 NamedPipes: []string{"foo", "bar"}, 300 }, 301 { 302 Input: []string{"a", "b", "c", "FROM", "<foo>,", "<bar>"}, 303 IsMethod: false, 304 Output: "a b c", 305 NamedPipes: []string{"foo", "bar"}, 306 }, 307 { 308 Input: []string{"FROM", "<foo>,", "<bar>", "ORDER BY", "1", "2", "3"}, 309 IsMethod: false, 310 Output: "* ORDER BY 1 2 3", 311 NamedPipes: []string{"foo", "bar"}, 312 }, 313 { 314 Input: []string{"a", "b", "c", "FROM", "<fee>", "ORDER BY", "1", "2", "3"}, 315 IsMethod: false, 316 Output: "a b c ORDER BY 1 2 3", 317 NamedPipes: []string{"fee"}, 318 }, 319 { 320 Input: []string{"a", "b", "c", "FROM", "<fee>,", "<fii>", "ORDER BY", "1", "2", "3"}, 321 IsMethod: false, 322 Output: "a b c ORDER BY 1 2 3", 323 NamedPipes: []string{"fee", "fii"}, 324 }, 325 { 326 Input: []string{"a", "b", "c", "FROM", "<fee>,", "<fii>,", "<fo>", "ORDER BY", "1", "2", "3"}, 327 IsMethod: false, 328 Output: "a b c ORDER BY 1 2 3", 329 NamedPipes: []string{"fee", "fii", "fo"}, 330 }, 331 { 332 Input: []string{"a", "b", "c", "FROM", "<fee>,", "<fii>,", "<fo>,", "<fum>", "ORDER BY", "1", "2", "3"}, 333 IsMethod: false, 334 Output: "a b c ORDER BY 1 2 3", 335 NamedPipes: []string{"fee", "fii", "fo", "fum"}, 336 }, 337 338 { 339 Input: []string{"FROM", "$foo,", "$bar", "ORDER BY", "1"}, 340 IsMethod: false, 341 Output: "* ORDER BY 1", 342 Variables: []string{"foo", "bar"}, 343 }, 344 { 345 Input: []string{"*", "FROM", "$foo,", "$bar", "ORDER BY", "1"}, 346 IsMethod: false, 347 Output: "* ORDER BY 1", 348 Variables: []string{"foo", "bar"}, 349 }, 350 { 351 Input: []string{"*", "FROM", "$foo,", "$bar"}, 352 IsMethod: false, 353 Output: "*", 354 Variables: []string{"foo", "bar"}, 355 }, 356 { 357 Input: []string{"FROM", "$foo,", "$bar"}, 358 IsMethod: false, 359 Output: "*", 360 Variables: []string{"foo", "bar"}, 361 }, 362 { 363 Input: []string{"a", "b", "c", "FROM", "$foo,", "$bar"}, 364 IsMethod: false, 365 Output: "a b c", 366 Variables: []string{"foo", "bar"}, 367 }, 368 { 369 Input: []string{"FROM", "$foo,", "$bar", "ORDER BY", "1", "2", "3"}, 370 IsMethod: false, 371 Output: "* ORDER BY 1 2 3", 372 Variables: []string{"foo", "bar"}, 373 }, 374 { 375 Input: []string{"a", "b", "c", "FROM", "$fee", "ORDER BY", "1", "2", "3"}, 376 IsMethod: false, 377 Output: "a b c ORDER BY 1 2 3", 378 Variables: []string{"fee"}, 379 }, 380 { 381 Input: []string{"a", "b", "c", "FROM", "$fee,", "$fii", "ORDER BY", "1", "2", "3"}, 382 IsMethod: false, 383 Output: "a b c ORDER BY 1 2 3", 384 Variables: []string{"fee", "fii"}, 385 }, 386 { 387 Input: []string{"a", "b", "c", "FROM", "$fee,", "$fii,", "$fo", "ORDER BY", "1", "2", "3"}, 388 IsMethod: false, 389 Output: "a b c ORDER BY 1 2 3", 390 Variables: []string{"fee", "fii", "fo"}, 391 }, 392 { 393 Input: []string{"a", "b", "c", "FROM", "$fee,", "$fii,", "$fo,", "$fum", "ORDER BY", "1", "2", "3"}, 394 IsMethod: false, 395 Output: "a b c ORDER BY 1 2 3", 396 Variables: []string{"fee", "fii", "fo", "fum"}, 397 }, 398 399 { 400 Input: []string{"a", "b", "c", "ORDER BY", "1", "2", "3"}, 401 IsMethod: false, 402 Output: "", 403 FileName: "", 404 Error: true, 405 }, 406 } 407 408 count.Tests(t, len(tests)) 409 410 for i, test := range tests { 411 p := lang.NewTestProcess() 412 p.IsMethod = test.IsMethod 413 p.Parameters.DefineParsed(test.Input) 414 actOutput, actFileName, actPipes, actVars, err := dissectParameters(p) 415 416 if actOutput != test.Output { 417 t.Errorf("Parameter output does not match expected in test %d", i) 418 t.Logf(" Input: %v", inlineJson(test.Input)) 419 t.Logf(" IsMethod: %v", test.IsMethod) 420 t.Logf(" exp param: '%s'", test.Output) 421 t.Logf(" act param: '%s'", actOutput) 422 t.Logf(" exp file: '%s'", test.FileName) 423 t.Logf(" act file: '%s'", actFileName) 424 t.Logf(" exp pipes: %s", inlineJson(test.NamedPipes)) 425 t.Logf(" act pipes: %s", inlineJson(actPipes)) 426 t.Logf(" exp vars: %s", inlineJson(test.Variables)) 427 t.Logf(" act vars: %s", inlineJson(actVars)) 428 t.Logf(" exp error: %v", test.Error) 429 t.Logf(" act error: %v", err) 430 } 431 432 if actFileName != test.FileName { 433 t.Errorf("FileName output does not match expected in test %d", i) 434 t.Logf(" Input: %v", inlineJson(test.Input)) 435 t.Logf(" IsMethod: %v", test.IsMethod) 436 t.Logf(" exp param: '%s'", test.Output) 437 t.Logf(" act param: '%s'", actOutput) 438 t.Logf(" exp file: '%s'", test.FileName) 439 t.Logf(" act file: '%s'", actFileName) 440 t.Logf(" exp pipes: %s", inlineJson(test.NamedPipes)) 441 t.Logf(" act pipes: %s", inlineJson(actPipes)) 442 t.Logf(" exp vars: %s", inlineJson(test.Variables)) 443 t.Logf(" act vars: %s", inlineJson(actVars)) 444 t.Logf(" exp error: %v", test.Error) 445 t.Logf(" act error: %v", err) 446 } 447 448 if (err != nil) != test.Error { 449 t.Errorf("Output does not match expected in test %d", i) 450 t.Logf(" Input: %v", inlineJson(test.Input)) 451 t.Logf(" IsMethod: %v", test.IsMethod) 452 t.Logf(" exp param: '%s'", test.Output) 453 t.Logf(" act param: '%s'", actOutput) 454 t.Logf(" exp file: '%s'", test.FileName) 455 t.Logf(" act file: '%s'", actFileName) 456 t.Logf(" exp pipes: %s", inlineJson(test.NamedPipes)) 457 t.Logf(" act pipes: %s", inlineJson(actPipes)) 458 t.Logf(" exp vars: %s", inlineJson(test.Variables)) 459 t.Logf(" act vars: %s", inlineJson(actVars)) 460 t.Logf(" exp error: %v", test.Error) 461 t.Logf(" act error: %v", err) 462 } 463 464 if inlineJson(actPipes) != inlineJson(test.NamedPipes) { 465 t.Errorf("Pipes do not match expected in test %d", i) 466 t.Logf(" Input: %v", inlineJson(test.Input)) 467 t.Logf(" IsMethod: %v", test.IsMethod) 468 t.Logf(" exp param: '%s'", test.Output) 469 t.Logf(" act param: '%s'", actOutput) 470 t.Logf(" exp file: '%s'", test.FileName) 471 t.Logf(" act file: '%s'", actFileName) 472 t.Logf(" exp pipes: %s", inlineJson(test.NamedPipes)) 473 t.Logf(" act pipes: %s", inlineJson(actPipes)) 474 t.Logf(" exp vars: %s", inlineJson(test.Variables)) 475 t.Logf(" act vars: %s", inlineJson(actVars)) 476 t.Logf(" exp error: %v", test.Error) 477 t.Logf(" act error: %v", err) 478 } 479 480 if inlineJson(actVars) != inlineJson(test.Variables) { 481 t.Errorf("Variables do not match expected in test %d", i) 482 t.Logf(" Input: %v", inlineJson(test.Input)) 483 t.Logf(" IsMethod: %v", test.IsMethod) 484 t.Logf(" exp param: '%s'", test.Output) 485 t.Logf(" act param: '%s'", actOutput) 486 t.Logf(" exp file: '%s'", test.FileName) 487 t.Logf(" act file: '%s'", actFileName) 488 t.Logf(" exp pipes: %s", inlineJson(test.NamedPipes)) 489 t.Logf(" act pipes: %s", inlineJson(actPipes)) 490 t.Logf(" exp vars: %s", inlineJson(test.Variables)) 491 t.Logf(" act vars: %s", inlineJson(actVars)) 492 t.Logf(" exp error: %v", test.Error) 493 t.Logf(" act error: %v", err) 494 } 495 } 496 }