github.com/eden-framework/sqlx@v0.0.2/builder/expr_test.go (about)

     1  package builder_test
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"fmt"
     6  	"testing"
     7  
     8  	. "github.com/eden-framework/sqlx/builder"
     9  	. "github.com/eden-framework/sqlx/builder/buidertestingutils"
    10  	"github.com/onsi/gomega"
    11  )
    12  
    13  func TestResolveExpr(t *testing.T) {
    14  	t.Run("empty", func(t *testing.T) {
    15  		gomega.NewWithT(t).Expect(ResolveExpr(nil)).To(gomega.BeNil())
    16  	})
    17  }
    18  
    19  func TestEx(t *testing.T) {
    20  	t.Run("empty query", func(t *testing.T) {
    21  		gomega.NewWithT(t).Expect(
    22  			Expr(""),
    23  		).To(BeExpr(""))
    24  	})
    25  	t.Run("flatten slice", func(t *testing.T) {
    26  		gomega.NewWithT(t).Expect(
    27  			Expr(`#ID IN (?)`, []int{28, 29, 30}),
    28  		).To(BeExpr("#ID IN (?,?,?)", 28, 29, 30))
    29  	})
    30  	t.Run("flatten should skip for bytes", func(t *testing.T) {
    31  		gomega.NewWithT(t).Expect(
    32  			Expr(`#ID = (?)`, []byte("")),
    33  		).To(BeExpr("#ID = (?)", []byte("")))
    34  	})
    35  	t.Run("flatten with sub expr ", func(t *testing.T) {
    36  		gomega.NewWithT(t).Expect(
    37  			Expr(`#ID = ?`, Expr("#ID + ?", 1)),
    38  		).To(BeExpr("#ID = #ID + ?", 1))
    39  	})
    40  	t.Run("flatten with ValuerExpr", func(t *testing.T) {
    41  		gomega.NewWithT(t).Expect(
    42  			Expr(`#Point = ?`, Point{1, 1}),
    43  		).To(BeExpr("#Point = ST_GeomFromText(?)", Point{1, 1}))
    44  	})
    45  }
    46  
    47  type Point struct {
    48  	X float64
    49  	Y float64
    50  }
    51  
    52  func (Point) DataType(engine string) string {
    53  	return "POINT"
    54  }
    55  
    56  func (Point) ValueEx() string {
    57  	return `ST_GeomFromText(?)`
    58  }
    59  
    60  func (p Point) Value() (driver.Value, error) {
    61  	return fmt.Sprintf("POINT(%v %v)", p.X, p.Y), nil
    62  }