github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_z_expr_test.go (about) 1 package builder_test 2 3 import ( 4 "context" 5 "testing" 6 7 g "github.com/onsi/gomega" 8 9 . "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 10 . "github.com/machinefi/w3bstream/pkg/depends/testutil/buildertestutil" 11 ) 12 13 func TestResolveExpr(t *testing.T) { 14 t.Run("Empty", func(t *testing.T) { 15 g.NewWithT(t).Expect(ResolveExpr(nil)).To(g.BeNil()) 16 }) 17 } 18 19 type Byte uint8 20 21 func TestEx(t *testing.T) { 22 t.Run("EmptyQuery", func(t *testing.T) { 23 g.NewWithT(t).Expect( 24 Expr(""), 25 ).To(BeExpr("")) 26 }) 27 28 t.Run("FlattenSlice", func(t *testing.T) { 29 g.NewWithT(t).Expect( 30 Expr(`#ID IN (?)`, []int{28, 29, 30}), 31 ).To(BeExpr("#ID IN (?,?,?)", 28, 29, 30)) 32 }) 33 34 t.Run("FlattenSliceWithNamedByte", func(t *testing.T) { 35 g.NewWithT(t).Expect( 36 And( 37 And(nil, Col("f_id").In([]int{28})), 38 Col("f_id").In([]Byte{28}), 39 ), 40 ).To(BeExpr("((f_id IN (?))) AND (f_id IN (?))", 28, Byte(28))) 41 }) 42 43 t.Run("FlattenSliceShouldSkipBytes", func(t *testing.T) { 44 g.NewWithT(t).Expect( 45 Expr(`#ID = (?)`, []byte("")), 46 ).To(BeExpr("#ID = (?)", []byte(""))) 47 }) 48 49 t.Run("FlattenWithSubExpr ", func(t *testing.T) { 50 g.NewWithT(t).Expect( 51 Expr(`#ID = ?`, Expr("#ID + ?", 1)), 52 ).To(BeExpr("#ID = #ID + ?", 1)) 53 }) 54 55 t.Run("FlattenWithValuerExpr", func(t *testing.T) { 56 g.NewWithT(t).Expect( 57 Expr(`#Point = ?`, Point{X: 1, Y: 1}), 58 ).To(BeExpr("#Point = ST_GeomFromText(?)", Point{X: 1, Y: 1})) 59 }) 60 } 61 62 func BenchmarkEx(b *testing.B) { 63 b.Run("EmptyQuery", func(b *testing.B) { 64 for i := 0; i < b.N; i++ { 65 _ = Expr("").Ex(context.Background()) 66 } 67 }) 68 69 b.Run("FlattenSlice", func(b *testing.B) { 70 for i := 0; i < b.N; i++ { 71 Expr(`#ID IN (?)`, []int{28, 29, 30}).Ex(context.Background()) 72 } 73 }) 74 75 b.Run("FlattenWithSubExpr", func(b *testing.B) { 76 b.Run("Raw", func(b *testing.B) { 77 eb := Expr("") 78 eb.Grow(2) 79 80 eb.WriteQuery("#ID > ?") 81 eb.WriteQuery(" AND ") 82 eb.WriteQuery("#ID < ?") 83 84 eb.AppendArgs(1, 10) 85 86 rawBuild := func() *Ex { 87 return eb.Ex(context.Background()) 88 } 89 90 clone := func(ex *Ex) *Ex { 91 return Expr(ex.Query(), ex.Args()...).Ex(context.Background()) 92 } 93 94 b.Run("clone", func(b *testing.B) { 95 ex := rawBuild() 96 97 for i := 0; i < b.N; i++ { 98 _ = clone(ex) 99 } 100 }) 101 }) 102 103 b.Run("IsNilExpr", func(b *testing.B) { 104 for i := 0; i < b.N; i++ { 105 IsNilExpr(Expr(`#ID > ?`, 1)) 106 } 107 }) 108 109 b.Run("ByChain", func(b *testing.B) { 110 for i := 0; i < b.N; i++ { 111 e := AsCond(Expr(`#ID > ?`, 1)).And(AsCond(Expr(`#ID < ?`, 10))) 112 e.Ex(context.Background()) 113 } 114 }) 115 116 b.Run("ByExpr", func(b *testing.B) { 117 for i := 0; i < b.N; i++ { 118 e := And( 119 Col("f_id").Lt(0), 120 Col("f_id").In([]int{1, 2, 3}), 121 ) 122 e.Ex(context.Background()) 123 } 124 }) 125 126 b.Run("ByExprWithoutRecreated", func(b *testing.B) { 127 l := Col("f_id").Lt(0) 128 r := Col("f_id").In([]int{1, 2, 3}) 129 130 b.Run("Single", func(b *testing.B) { 131 for i := 0; i < b.N; i++ { 132 l.Ex(context.Background()) 133 } 134 }) 135 136 b.Run("Composed", func(b *testing.B) { 137 e := And(l, l, r, r) 138 139 // b.Log(e.Ex(context.Background()).Query()) 140 141 for i := 0; i < b.N; i++ { 142 e.Ex(context.Background()) 143 } 144 }) 145 }) 146 }) 147 }