github.com/yongjacky/phoenix-go-orm-builder@v0.3.5/cond_if_test.go (about)

     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  
     5  package builder
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCond_If(t *testing.T) {
    14  	cond1 := If(1 > 0, Eq{"a": 1}, Eq{"b": 1})
    15  	sql, err := ToBoundSQL(cond1)
    16  	assert.NoError(t, err)
    17  	assert.EqualValues(t, "a=1", sql)
    18  
    19  	cond2 := If(1 < 0, Eq{"a": 1}, Eq{"b": 1})
    20  	sql, err = ToBoundSQL(cond2)
    21  	assert.NoError(t, err)
    22  	assert.EqualValues(t, "b=1", sql)
    23  
    24  	cond3 := If(1 > 0, cond2, Eq{"c": 1})
    25  	sql, err = ToBoundSQL(cond3)
    26  	assert.NoError(t, err)
    27  	assert.EqualValues(t, "b=1", sql)
    28  
    29  	cond4 := If(2 < 0, Eq{"d": "a"})
    30  	sql, err = ToBoundSQL(cond4)
    31  	assert.NoError(t, err)
    32  	assert.EqualValues(t, "", sql)
    33  
    34  	cond5 := And(cond1, cond2, cond3, cond4)
    35  	sql, err = ToBoundSQL(cond5)
    36  	assert.NoError(t, err)
    37  	assert.EqualValues(t, "a=1 AND b=1 AND b=1", sql)
    38  }