modifiers_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2018 Huan Du. All rights reserved.
  2. // Licensed under the MIT license that can be found in the LICENSE file.
  3. package sqlbuilder
  4. import (
  5. "reflect"
  6. "testing"
  7. )
  8. func TestEscape(t *testing.T) {
  9. cases := map[string]string{
  10. "foo": "foo",
  11. "$foo": "$$foo",
  12. "$$$": "$$$$$$",
  13. }
  14. var inputs, expects []string
  15. for s, expected := range cases {
  16. inputs = append(inputs, s)
  17. expects = append(expects, expected)
  18. if actual := Escape(s); actual != expected {
  19. t.Fatalf("invalid escape result. [expected:%v] [actual:%v]", expected, actual)
  20. }
  21. }
  22. actuals := EscapeAll(inputs...)
  23. if !reflect.DeepEqual(expects, actuals) {
  24. t.Fatalf("invalid escape result. [expected:%v] [actual:%v]", expects, actuals)
  25. }
  26. }
  27. func TestFlatten(t *testing.T) {
  28. cases := [][2]interface{}{
  29. {
  30. "foo",
  31. []interface{}{"foo"},
  32. },
  33. {
  34. []int{1, 2, 3},
  35. []interface{}{1, 2, 3},
  36. },
  37. {
  38. []interface{}{"abc", []int{1, 2, 3}, [3]string{"def", "ghi"}},
  39. []interface{}{"abc", 1, 2, 3, "def", "ghi", ""},
  40. },
  41. }
  42. for _, c := range cases {
  43. input, expected := c[0], c[1]
  44. actual := Flatten(input)
  45. if !reflect.DeepEqual(expected, actual) {
  46. t.Fatalf("invalid flatten result. [expected:%v] [actual:%v]", expected, actual)
  47. }
  48. }
  49. }