github.com/kaydxh/golang@v0.0.131/pkg/database/mysql/mysql_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package mysql_test 23 24 import ( 25 "context" 26 "testing" 27 "time" 28 29 mysql_ "github.com/kaydxh/golang/pkg/database/mysql" 30 viper_ "github.com/kaydxh/golang/pkg/viper" 31 "github.com/stretchr/testify/assert" 32 ) 33 34 func TestGetDataBase(t *testing.T) { 35 testCases := []struct { 36 Address string 37 DataName string 38 UserName string 39 Password string 40 InterpolateParams bool 41 }{ 42 { 43 Address: "127.0.0.1:3306", 44 DataName: "sealet", 45 UserName: "root", 46 Password: "123456", 47 InterpolateParams: true, 48 }, 49 } 50 51 for _, testCase := range testCases { 52 t.Run(testCase.Address+testCase.DataName, func(t *testing.T) { 53 db := mysql_.NewDB(mysql_.DBConfig{ 54 Address: testCase.Address, 55 DataName: testCase.DataName, 56 UserName: testCase.UserName, 57 Password: testCase.Password, 58 }, 59 mysql_.WithInterpolateParams(testCase.InterpolateParams), 60 ) 61 sqlDB, err := db.GetDatabase() 62 if err != nil { 63 t.Fatalf("failed to get database: %v, got : %s", testCase.DataName, err) 64 } 65 assert.NotNil(t, sqlDB) 66 }) 67 } 68 69 } 70 71 func TestGetDatabaseUntil(t *testing.T) { 72 testCases := []struct { 73 Address string 74 DataName string 75 UserName string 76 Password string 77 DailTimeout time.Duration 78 }{ 79 { 80 Address: "127.0.0.1:3306", 81 DataName: "test", 82 UserName: "root", 83 Password: "123456", 84 DailTimeout: 3 * time.Second, 85 }, 86 } 87 88 for _, testCase := range testCases { 89 t.Run(testCase.Address+testCase.DataName, func(t *testing.T) { 90 db := mysql_.NewDB(mysql_.DBConfig{ 91 Address: testCase.Address, 92 DataName: testCase.DataName, 93 UserName: testCase.UserName, 94 Password: testCase.Password, 95 }, mysql_.WithDialTimeout(testCase.DailTimeout)) 96 sqlDB, err := db.GetDatabaseUntil(context.Background(), 5*time.Second, 20*time.Second) 97 if err != nil { 98 t.Fatalf("failed to get database: %v, got : %s", testCase.DataName, err) 99 } 100 assert.NotNil(t, sqlDB) 101 }) 102 } 103 104 } 105 106 func TestNew(t *testing.T) { 107 108 cfgFile := "./mysql.yaml" 109 config := mysql_.NewConfig(mysql_.WithViper(viper_.GetViper(cfgFile, "database.mysql"))) 110 111 db, err := config.Complete().New(context.Background()) 112 if err != nil { 113 t.Errorf("failed to new config err: %v", err) 114 } 115 116 t.Logf("db: %#v", db) 117 } 118 119 func TestGetTheDBAndClose(t *testing.T) { 120 testCases := []struct { 121 Address string 122 DataName string 123 UserName string 124 Password string 125 }{ 126 { 127 Address: "127.0.0.1:3306", 128 DataName: "test", 129 UserName: "root", 130 Password: "123456", 131 }, 132 } 133 134 for _, testCase := range testCases { 135 t.Run(testCase.Address+testCase.DataName, func(t *testing.T) { 136 conf := mysql_.DBConfig{ 137 Address: testCase.Address, 138 DataName: testCase.DataName, 139 UserName: testCase.UserName, 140 Password: testCase.Password, 141 } 142 db := mysql_.NewDB(conf) 143 sqlDB, err := db.GetDatabaseUntil(context.Background(), 5*time.Second, 20*time.Second) 144 if err != nil { 145 t.Fatalf("failed to get database: %v, got : %s", testCase.DataName, err) 146 } 147 assert.NotNil(t, sqlDB) 148 149 theDB, err := mysql_.GetTheDB(conf) 150 assert.Nil(t, err) 151 152 assert.Equal(t, sqlDB, mysql_.GetDB()) 153 t.Logf("GetDB got sqlDB: %v, expect %v", sqlDB, mysql_.GetDB()) 154 t.Logf("GetTheDB got sqlDB: %v, expect %v", sqlDB, theDB) 155 err = mysql_.CloseTheDB(conf) 156 assert.Nil(t, err) 157 err = mysql_.CloseDB() 158 assert.Nil(t, err) 159 }) 160 } 161 162 }