github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mysql/query_test.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package mysql 21 22 import ( 23 "context" 24 "encoding/json" 25 "testing" 26 "time" 27 28 "github.com/go-logr/zapr" 29 "github.com/spf13/viper" 30 "go.uber.org/zap" 31 32 "github.com/DATA-DOG/go-sqlmock" 33 "github.com/stretchr/testify/assert" 34 ) 35 36 func TestQuery(t *testing.T) { 37 manager, mock, _ := mockDatabase(t) 38 39 t.Run("no dbType provided", func(t *testing.T) { 40 rows := sqlmock.NewRows([]string{"id", "value", "timestamp"}). 41 AddRow(1, "value-1", time.Now()). 42 AddRow(2, "value-2", time.Now().Add(1000)). 43 AddRow(3, "value-3", time.Now().Add(2000)) 44 45 mock.ExpectQuery("SELECT \\* FROM foo WHERE id < 4").WillReturnRows(rows) 46 ret, err := manager.Query(context.Background(), `SELECT * FROM foo WHERE id < 4`) 47 assert.Nil(t, err) 48 t.Logf("query result: %s", ret) 49 assert.Contains(t, string(ret), "\"id\":\"1") 50 var result []interface{} 51 err = json.Unmarshal(ret, &result) 52 assert.Nil(t, err) 53 assert.Equal(t, 3, len(result)) 54 }) 55 56 t.Run("dbType provided", func(t *testing.T) { 57 col1 := sqlmock.NewColumn("id").OfType("BIGINT", 1) 58 col2 := sqlmock.NewColumn("value").OfType("FLOAT", 1.0) 59 col3 := sqlmock.NewColumn("timestamp").OfType("TIME", time.Now()) 60 rows := sqlmock.NewRowsWithColumnDefinition(col1, col2, col3). 61 AddRow(1, 1.1, time.Now()). 62 AddRow(2, 2.2, time.Now().Add(1000)). 63 AddRow(3, 3.3, time.Now().Add(2000)) 64 mock.ExpectQuery("SELECT \\* FROM foo WHERE id < 4").WillReturnRows(rows) 65 ret, err := manager.Query(context.Background(), "SELECT * FROM foo WHERE id < 4") 66 assert.Nil(t, err) 67 t.Logf("query result: %s", ret) 68 69 // verify number 70 assert.Contains(t, string(ret), "\"id\":1") 71 assert.Contains(t, string(ret), "\"value\":2.2") 72 73 var result []interface{} 74 err = json.Unmarshal(ret, &result) 75 assert.Nil(t, err) 76 assert.Equal(t, 3, len(result)) 77 78 // verify timestamp 79 ts, ok := result[0].(map[string]interface{})["timestamp"].(string) 80 assert.True(t, ok) 81 var tt time.Time 82 tt, err = time.Parse(time.RFC3339, ts) 83 assert.Nil(t, err) 84 t.Logf("time stamp is: %v", tt) 85 }) 86 } 87 88 func TestExec(t *testing.T) { 89 manager, mock, _ := mockDatabase(t) 90 mock.ExpectExec("INSERT INTO foo \\(id, v1, ts\\) VALUES \\(.*\\)").WillReturnResult(sqlmock.NewResult(1, 1)) 91 i, err := manager.Exec(context.Background(), "INSERT INTO foo (id, v1, ts) VALUES (1, 'test-1', '2021-01-22')") 92 assert.Equal(t, int64(1), i) 93 assert.Nil(t, err) 94 } 95 96 func mockDatabase(t *testing.T) (*Manager, sqlmock.Sqlmock, error) { 97 viper.SetDefault("KB_SERVICE_ROLES", "{\"follower\":\"Readonly\",\"leader\":\"ReadWrite\"}") 98 viper.Set("KB_POD_NAME", "test-pod-0") 99 db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true)) 100 if err != nil { 101 t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) 102 } 103 104 manager := &Manager{} 105 development, _ := zap.NewDevelopment() 106 manager.Logger = zapr.NewLogger(development) 107 manager.DB = db 108 109 return manager, mock, err 110 }