github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/testutil/buildertestutil/builder.go (about) 1 package buildertestutil 2 3 import ( 4 "context" 5 "database/sql/driver" 6 "fmt" 7 "strings" 8 9 "github.com/onsi/gomega" 10 "github.com/onsi/gomega/types" 11 12 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 13 ) 14 15 func BeExpr(q string, args ...interface{}) types.GomegaMatcher { 16 return &SqlExprMatcher{ 17 QueryMatcher: gomega.Equal(strings.TrimSpace(q)), 18 ArgsMatcher: gomega.Equal(args), 19 } 20 } 21 22 type SqlExprMatcher struct { 23 QueryMatcher types.GomegaMatcher 24 ArgsMatcher types.GomegaMatcher 25 } 26 27 func (m *SqlExprMatcher) Match(actual interface{}) (success bool, err error) { 28 e, ok := actual.(builder.SqlExpr) 29 if !ok { 30 return false, fmt.Errorf("actual should be SqlExpr") 31 } 32 if builder.IsNilExpr(e) { 33 return m.QueryMatcher.Match("") 34 } 35 ex := e.Ex(context.Background()) 36 queryMatched, err := m.QueryMatcher.Match(ex.Query()) 37 if err != nil { 38 return false, err 39 } 40 argsMatched, err := m.ArgsMatcher.Match(ex.Args()) 41 if err != nil { 42 return false, err 43 } 44 45 return queryMatched && argsMatched, nil 46 } 47 48 func (m *SqlExprMatcher) FailureMessage(actual interface{}) (msg string) { 49 e := actual.(builder.SqlExpr).Ex(context.Background()) 50 return m.QueryMatcher.FailureMessage(e.Query()) + "\n" + 51 m.ArgsMatcher.FailureMessage(e.Args()) 52 } 53 54 func (m *SqlExprMatcher) NegatedFailureMessage(actual interface{}) (msg string) { 55 e := actual.(builder.SqlExpr).Ex(context.Background()) 56 return m.QueryMatcher.NegatedFailureMessage(e.Query()) + "\n" + 57 m.ArgsMatcher.NegatedFailureMessage(e.Args()) 58 } 59 60 func MustCols(cols *builder.Columns, err error) *builder.Columns { 61 return cols 62 } 63 64 type Point struct { 65 X float64 66 Y float64 67 } 68 69 func (Point) DataType(engine string) string { return "POINT" } 70 71 func (Point) ValueEx() string { return `ST_GeomFromText(?)` } 72 73 func (p Point) Value() (driver.Value, error) { return fmt.Sprintf("POINT(%v %v)", p.X, p.Y), nil }