cond_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. "testing"
  6. )
  7. func TestCond(t *testing.T) {
  8. cases := map[string]func() string{
  9. "$$a = $0": func() string { return newTestCond().Equal("$a", 123) },
  10. "$$b = $0": func() string { return newTestCond().E("$b", 123) },
  11. "$$a <> $0": func() string { return newTestCond().NotEqual("$a", 123) },
  12. "$$b <> $0": func() string { return newTestCond().NE("$b", 123) },
  13. "$$a > $0": func() string { return newTestCond().GreaterThan("$a", 123) },
  14. "$$b > $0": func() string { return newTestCond().G("$b", 123) },
  15. "$$a >= $0": func() string { return newTestCond().GreaterEqualThan("$a", 123) },
  16. "$$b >= $0": func() string { return newTestCond().GE("$b", 123) },
  17. "$$a < $0": func() string { return newTestCond().LessThan("$a", 123) },
  18. "$$b < $0": func() string { return newTestCond().L("$b", 123) },
  19. "$$a <= $0": func() string { return newTestCond().LessEqualThan("$a", 123) },
  20. "$$b <= $0": func() string { return newTestCond().LE("$b", 123) },
  21. "$$a IN ($0, $1, $2)": func() string { return newTestCond().In("$a", 1, 2, 3) },
  22. "$$a NOT IN ($0, $1, $2)": func() string { return newTestCond().NotIn("$a", 1, 2, 3) },
  23. "$$a LIKE $0": func() string { return newTestCond().Like("$a", "%Huan%") },
  24. "$$a NOT LIKE $0": func() string { return newTestCond().NotLike("$a", "%Huan%") },
  25. "$$a IS NULL": func() string { return newTestCond().IsNull("$a") },
  26. "$$a IS NOT NULL": func() string { return newTestCond().IsNotNull("$a") },
  27. "$$a BETWEEN $0 AND $1": func() string { return newTestCond().Between("$a", 123, 456) },
  28. "$$a NOT BETWEEN $0 AND $1": func() string { return newTestCond().NotBetween("$a", 123, 456) },
  29. "(1 = 1 OR 2 = 2 OR 3 = 3)": func() string { return newTestCond().Or("1 = 1", "2 = 2", "3 = 3") },
  30. "$0": func() string { return newTestCond().Var(123) },
  31. }
  32. for expected, f := range cases {
  33. if actual := f(); expected != actual {
  34. t.Fatalf("invalid result. [expected:%v] [actual:%v]", expected, actual)
  35. }
  36. }
  37. }
  38. func newTestCond() *Cond {
  39. return &Cond{
  40. Args: &Args{},
  41. }
  42. }