github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/middleware/querylog/querylog_test.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package querylog
    16  
    17  import (
    18  	"context"
    19  	"database/sql"
    20  	"errors"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/ecodeclub/eorm"
    26  	_ "github.com/mattn/go-sqlite3"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestMiddlewareBuilder_Build(t *testing.T) {
    31  	testCases := []struct {
    32  		name    string
    33  		mdls    []eorm.Middleware
    34  		builder *testMiddlewareBuilder
    35  		wantVal string
    36  		wantErr error
    37  	}{
    38  		{
    39  			name: "default",
    40  			builder: &testMiddlewareBuilder{
    41  				MiddlewareBuilder: NewBuilder(),
    42  				printVal:          strings.Builder{},
    43  			},
    44  			mdls: []eorm.Middleware{},
    45  		},
    46  		{
    47  			name: "output args",
    48  			builder: func() *testMiddlewareBuilder {
    49  				b := &testMiddlewareBuilder{
    50  					MiddlewareBuilder: NewBuilder(),
    51  					printVal:          strings.Builder{},
    52  				}
    53  				logfunc := func(sql string, args ...any) {
    54  					fmt.Println(sql, args)
    55  					b.printVal.WriteString(sql)
    56  				}
    57  				b.LogFunc(logfunc)
    58  				return b
    59  			}(),
    60  			mdls:    []eorm.Middleware{},
    61  			wantVal: "SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` LIMIT ?;",
    62  		},
    63  		{
    64  			name: "not args",
    65  			builder: &testMiddlewareBuilder{
    66  				printVal: strings.Builder{},
    67  				MiddlewareBuilder: NewBuilder().LogFunc(func(sql string, args ...any) {
    68  					fmt.Println(sql)
    69  				}),
    70  			},
    71  			mdls:    []eorm.Middleware{},
    72  			wantVal: "SELECT `id`,`first_name`,`age`,`last_name` FROM `test_model` LIMIT ?;",
    73  		},
    74  		{
    75  			name: "interrupt err",
    76  			builder: &testMiddlewareBuilder{
    77  				printVal: strings.Builder{},
    78  				MiddlewareBuilder: NewBuilder().LogFunc(func(sql string, args ...any) {
    79  					fmt.Println(sql)
    80  				}),
    81  			},
    82  			mdls: func() []eorm.Middleware {
    83  				var interrupt eorm.Middleware = func(next eorm.HandleFunc) eorm.HandleFunc {
    84  					return func(ctx context.Context, qc *eorm.QueryContext) *eorm.QueryResult {
    85  						return &eorm.QueryResult{
    86  							Err: errors.New("interrupt execution"),
    87  						}
    88  					}
    89  				}
    90  				return []eorm.Middleware{interrupt}
    91  			}(),
    92  			wantErr: errors.New("interrupt execution"),
    93  		},
    94  	}
    95  
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			mdls := tc.mdls
    99  			mdls = append(mdls, tc.builder.Build())
   100  			orm, err := eorm.Open("sqlite3",
   101  				"file:test.db?cache=shared&mode=memory",
   102  				eorm.DBWithMiddlewares(mdls...))
   103  			if err != nil {
   104  				t.Fatal(err)
   105  			}
   106  			defer func() {
   107  				_ = orm.Close()
   108  			}()
   109  			_, err = eorm.NewSelector[TestModel](orm).Get(context.Background())
   110  			if err.Error() == "no such table: test_model" {
   111  				return
   112  			}
   113  			if err != nil {
   114  				assert.Equal(t, tc.wantErr, err)
   115  				return
   116  			}
   117  			assert.Equal(t, tc.wantVal, tc.builder.printVal.String())
   118  		})
   119  	}
   120  
   121  }
   122  
   123  type testMiddlewareBuilder struct {
   124  	*MiddlewareBuilder
   125  	printVal strings.Builder
   126  }
   127  
   128  type TestModel struct {
   129  	Id        int64 `eorm:"primary_key"`
   130  	FirstName string
   131  	Age       int8
   132  	LastName  *sql.NullString
   133  }