cond_if_test.go 942 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestCond_If(t *testing.T) {
  10. cond1 := If(1 > 0, Eq{"a": 1}, Eq{"b": 1})
  11. sql, err := ToBoundSQL(cond1)
  12. assert.NoError(t, err)
  13. assert.EqualValues(t, "a=1", sql)
  14. cond2 := If(1 < 0, Eq{"a": 1}, Eq{"b": 1})
  15. sql, err = ToBoundSQL(cond2)
  16. assert.NoError(t, err)
  17. assert.EqualValues(t, "b=1", sql)
  18. cond3 := If(1 > 0, cond2, Eq{"c": 1})
  19. sql, err = ToBoundSQL(cond3)
  20. assert.NoError(t, err)
  21. assert.EqualValues(t, "b=1", sql)
  22. cond4 := If(2 < 0, Eq{"d": "a"})
  23. sql, err = ToBoundSQL(cond4)
  24. assert.NoError(t, err)
  25. assert.EqualValues(t, "", sql)
  26. cond5 := And(cond1, cond2, cond3, cond4)
  27. sql, err = ToBoundSQL(cond5)
  28. assert.NoError(t, err)
  29. assert.EqualValues(t, "a=1 AND b=1 AND b=1", sql)
  30. }