github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/builder/stmt_common_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestWhere(t *testing.T) {
    10  	tt := assert.New(t)
    11  	w := (*where)(nil)
    12  	tt.Nil(w.Expr())
    13  }
    14  
    15  func TestStmt(t *testing.T) {
    16  	s := Stmt{}
    17  	if s.Type() != STMT_RAW {
    18  		panic("Stmt should be STMT_RAW")
    19  	}
    20  }
    21  
    22  func TestStmtCommon_Order(t *testing.T) {
    23  	o := order{}
    24  	table := T(DB("db"), "t")
    25  
    26  	o2 := order{
    27  		expr: Col(table, "F_id").In(),
    28  	}
    29  
    30  	exprCases{
    31  		Case(
    32  			"empty groupBy",
    33  			o.Expr(),
    34  			nil,
    35  		),
    36  		Case(
    37  			"empty groupBy",
    38  			o2.Expr(),
    39  			nil,
    40  		),
    41  	}.Run(t, "Stmt common order")
    42  }
    43  
    44  func TestStmtCommon_GroupBy(t *testing.T) {
    45  	o := groupBy{}
    46  	table := T(DB("db"), "t")
    47  
    48  	exprCases{
    49  		Case(
    50  			"empty groupBy",
    51  			o.Expr(),
    52  			nil,
    53  		),
    54  		Case(
    55  			"simple groupBy",
    56  			o.setBy(Col(table, "F_id"), "").Expr(),
    57  			Expr("GROUP BY (`F_id`)"),
    58  		),
    59  		Case(
    60  			"simple groupBy with order",
    61  			o.setBy(Col(table, "F_id"), ORDER_DESC).Expr(),
    62  			Expr("GROUP BY (`F_id`) DESC"),
    63  		),
    64  		Case(
    65  			"simple groupBy with rollup",
    66  			o.setBy(Col(table, "F_id"), ORDER_DESC).rollup().Expr(),
    67  			Expr("GROUP BY (`F_id`) DESC WITH ROLLUP"),
    68  		),
    69  		Case(
    70  			"multi groupBy",
    71  			o.addBy(Col(table, "F_id"), "").
    72  				addBy(Col(table, "F_b"), "").Expr(),
    73  			Expr("GROUP BY (`F_id`), (`F_b`)"),
    74  		),
    75  		Case(
    76  			"multi groupBy with order",
    77  			o.addBy(Col(table, "F_id"), ORDER_DESC).
    78  				addBy(Col(table, "F_b"), ORDER_ASC).Expr(),
    79  			Expr("GROUP BY (`F_id`) DESC, (`F_b`) ASC"),
    80  		),
    81  		Case(
    82  			"multi groupBy with having",
    83  			o.addBy(Col(table, "F_id"), "").
    84  				having(Col(table, "F_id").Eq(1)).Expr(),
    85  			Expr(
    86  				"GROUP BY (`F_id`) HAVING `F_id` = ?",
    87  				1,
    88  			),
    89  		),
    90  	}.Run(t, "Stmt common group by")
    91  }
    92  
    93  func TestStmtCommon_OrderBy(t *testing.T) {
    94  	o := orderBy{}
    95  	table := T(DB("db"), "t")
    96  
    97  	exprCases{
    98  		Case(
    99  			"empty orderBy",
   100  			o.Expr(),
   101  			nil,
   102  		),
   103  		Case(
   104  			"simple order by",
   105  			o.setBy(Col(table, "F_id"), ORDER_DESC).Expr(),
   106  			Expr("ORDER BY (`F_id`) DESC"),
   107  		),
   108  		Case(
   109  			"multi order by",
   110  			o.addBy(Col(table, "F_id"), ORDER_DESC).
   111  				addBy(Col(table, "F_b"), ORDER_ASC).Expr(),
   112  			Expr("ORDER BY (`F_id`) DESC, (`F_b`) ASC"),
   113  		),
   114  	}.Run(t, "Stmt common order by")
   115  }
   116  
   117  func TestStmtCommon_Limit(t *testing.T) {
   118  	limit := limit{}
   119  
   120  	exprCases{
   121  		Case(
   122  			"empty limit",
   123  			limit.Expr(),
   124  			nil,
   125  		),
   126  		Case(
   127  			"limit only with offset",
   128  			limit.offset(1).Expr(),
   129  			nil,
   130  		),
   131  		Case(
   132  			"limit with size",
   133  			limit.limit(1).Expr(),
   134  			Expr(
   135  				"LIMIT 1",
   136  			),
   137  		),
   138  		Case(
   139  			"limit with size and offset",
   140  			limit.limit(1).offset(1).Expr(),
   141  			Expr(
   142  				"LIMIT 1 OFFSET 1",
   143  			),
   144  		),
   145  	}.Run(t, "Stmt common limit")
   146  }