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

     1  package buidertestingutils
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/eden-framework/sqlx/builder"
     9  	"github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/types"
    11  )
    12  
    13  func BeExpr(query string, args ...interface{}) types.GomegaMatcher {
    14  	return &SqlExprMatcher{
    15  		QueryMatcher: gomega.Equal(strings.TrimSpace(query)),
    16  		ArgsMatcher:  gomega.Equal(args),
    17  	}
    18  }
    19  
    20  type SqlExprMatcher struct {
    21  	QueryMatcher types.GomegaMatcher
    22  	ArgsMatcher  types.GomegaMatcher
    23  }
    24  
    25  func (matcher *SqlExprMatcher) Match(actual interface{}) (success bool, err error) {
    26  	sqlExpr, ok := actual.(builder.SqlExpr)
    27  	if !ok {
    28  		return false, fmt.Errorf("actual shoud be SqlExpr,  but got %#v", actual)
    29  	}
    30  
    31  	if builder.IsNilExpr(sqlExpr) {
    32  		return matcher.QueryMatcher.Match("")
    33  	}
    34  
    35  	expr := sqlExpr.Ex(context.Background())
    36  
    37  	queryMatched, err := matcher.QueryMatcher.Match(expr.Query())
    38  	if err != nil {
    39  		return false, err
    40  	}
    41  
    42  	argsMatched, err := matcher.ArgsMatcher.Match(expr.Args())
    43  	if err != nil {
    44  		return false, err
    45  	}
    46  
    47  	return queryMatched && argsMatched, nil
    48  }
    49  
    50  func (matcher *SqlExprMatcher) FailureMessage(actual interface{}) (message string) {
    51  	expr := actual.(builder.SqlExpr).Ex(context.Background())
    52  
    53  	return matcher.QueryMatcher.FailureMessage(expr.Query()) + "\n" + matcher.ArgsMatcher.FailureMessage(expr.Args())
    54  }
    55  
    56  func (matcher SqlExprMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    57  	expr := actual.(builder.SqlExpr).Ex(context.Background())
    58  
    59  	return matcher.QueryMatcher.NegatedFailureMessage(expr.Query()) + "\n" + matcher.ArgsMatcher.NegatedFailureMessage(expr.Args())
    60  }