github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/providers/mysql/mysql_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     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 mysql
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  )
    21  
    22  func Test_config_url(t *testing.T) {
    23  	type fields struct {
    24  		MySQLURL          string
    25  		MySQLHost         string
    26  		MySQLPort         string
    27  		MySQLUsername     string
    28  		MySQLPassword     string
    29  		MySQLDatabase     string
    30  		MySQLMaxIdleConns uint64
    31  		MySQLMaxOpenConns uint64
    32  		MySQLMaxLifeTime  time.Duration
    33  		MySQLDebug        bool
    34  		MySQLCharset      string
    35  		MySQLTLS          string
    36  	}
    37  	tests := []struct {
    38  		name   string
    39  		fields fields
    40  		want   string
    41  	}{
    42  		{
    43  			name: "test_not_have_tls",
    44  			fields: fields{
    45  				MySQLUsername: "erda",
    46  				MySQLHost:     "erda",
    47  				MySQLCharset:  "erda",
    48  				MySQLDatabase: "erda",
    49  				MySQLPort:     "3306",
    50  				MySQLPassword: "erda",
    51  			},
    52  			want: "erda:erda@tcp(erda:3306)/erda?charset=erda&parseTime=True&loc=Local",
    53  		},
    54  		{
    55  			name: "test_not_have_tls",
    56  			fields: fields{
    57  				MySQLUsername: "erda",
    58  				MySQLHost:     "erda",
    59  				MySQLCharset:  "erda",
    60  				MySQLDatabase: "erda",
    61  				MySQLPort:     "3306",
    62  				MySQLPassword: "erda",
    63  				MySQLTLS:      "custom",
    64  			},
    65  			want: "erda:erda@tcp(erda:3306)/erda?charset=erda&parseTime=True&loc=Local&tls=custom",
    66  		},
    67  	}
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			c := &config{
    71  				MySQLURL:          tt.fields.MySQLURL,
    72  				MySQLHost:         tt.fields.MySQLHost,
    73  				MySQLPort:         tt.fields.MySQLPort,
    74  				MySQLUsername:     tt.fields.MySQLUsername,
    75  				MySQLPassword:     tt.fields.MySQLPassword,
    76  				MySQLDatabase:     tt.fields.MySQLDatabase,
    77  				MySQLMaxIdleConns: tt.fields.MySQLMaxIdleConns,
    78  				MySQLMaxOpenConns: tt.fields.MySQLMaxOpenConns,
    79  				MySQLMaxLifeTime:  tt.fields.MySQLMaxLifeTime,
    80  				MySQLDebug:        tt.fields.MySQLDebug,
    81  				MySQLCharset:      tt.fields.MySQLCharset,
    82  				MySQLTLS:          tt.fields.MySQLTLS,
    83  			}
    84  			if got := c.url(); got != tt.want {
    85  				t.Errorf("url() = %v, want %v", got, tt.want)
    86  			}
    87  		})
    88  	}
    89  }