github.com/polarismesh/polaris@v1.17.8/store/mysql/tool_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package sqldb
    19  
    20  import (
    21  	"errors"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/DATA-DOG/go-sqlmock"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func Test_toolStore_GetUnixSecond(t *testing.T) {
    30  	t.Run("正常场景", func(t *testing.T) {
    31  		db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    32  		if err != nil {
    33  			t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    34  		}
    35  		defer db.Close()
    36  
    37  		rows := sqlmock.NewRows([]string{"UNIX_TIMESTAMP(SYSDATE())"})
    38  		rows.AddRow(1)
    39  		mock.ExpectQuery(nowSql).WillDelayFor(2 * time.Second).WillReturnRows(rows)
    40  
    41  		tr := &toolStore{
    42  			db: &BaseDB{DB: db},
    43  		}
    44  		got, err := tr.GetUnixSecond(0)
    45  		assert.NoError(t, err)
    46  		assert.Equal(t, int64(1), int64(got))
    47  	})
    48  
    49  	t.Run("SQL执行时间超过MaxWait", func(t *testing.T) {
    50  		db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    51  		if err != nil {
    52  			t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    53  		}
    54  		defer db.Close()
    55  
    56  		rows := sqlmock.NewRows([]string{"UNIX_TIMESTAMP(SYSDATE())"})
    57  		rows.AddRow(100)
    58  		mock.ExpectQuery(nowSql).WillDelayFor(2 * time.Second).WillReturnRows(rows)
    59  
    60  		tr := &toolStore{
    61  			db: &BaseDB{DB: db},
    62  		}
    63  		got, err := tr.GetUnixSecond(time.Second)
    64  		assert.NoError(t, err)
    65  		assert.Equal(t, int64(0), int64(got))
    66  	})
    67  
    68  	t.Run("SQL查询出错", func(t *testing.T) {
    69  		db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    70  		if err != nil {
    71  			t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    72  		}
    73  		defer db.Close()
    74  
    75  		mock.ExpectQuery(nowSql).WillReturnError(errors.New("mock error"))
    76  
    77  		tr := &toolStore{
    78  			db: &BaseDB{DB: db},
    79  		}
    80  		got, err := tr.GetUnixSecond(time.Second)
    81  		assert.Error(t, err)
    82  		assert.Equal(t, int64(0), int64(got))
    83  	})
    84  
    85  	t.Run("SQL返回的不是int", func(t *testing.T) {
    86  		db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    87  		if err != nil {
    88  			t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    89  		}
    90  		defer db.Close()
    91  
    92  		rows := sqlmock.NewRows([]string{"UNIX_TIMESTAMP(SYSDATE())"})
    93  		rows.AddRow("100qer")
    94  		mock.ExpectQuery(nowSql).WillDelayFor(0).WillReturnRows(rows)
    95  
    96  		tr := &toolStore{
    97  			db: &BaseDB{DB: db},
    98  		}
    99  		got, err := tr.GetUnixSecond(time.Second)
   100  		assert.Error(t, err)
   101  		assert.Equal(t, int64(0), int64(got))
   102  	})
   103  }