github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/sqlcon/connector_test.go (about) 1 /* 2 * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * @author Aeneas Rekkas <aeneas+oss@aeneas.io> 17 * @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> 18 * @license Apache-2.0 19 */ 20 21 package sqlcon 22 23 import ( 24 "fmt" 25 "net/url" 26 "strings" 27 "sync" 28 "testing" 29 30 _ "github.com/go-sql-driver/mysql" 31 _ "github.com/jackc/pgx/v4/stdlib" 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 35 "github.com/ory/dockertest/v3" 36 ) 37 38 var ( 39 mysqlURL string 40 postgresURL string 41 cockroachURL string 42 resources []*dockertest.Resource 43 lock sync.RWMutex 44 ) 45 46 func merge(u string, query url.Values) string { 47 if strings.Contains(u, "?") { 48 return u + "&" + query.Encode() 49 } 50 return u + "?" + query.Encode() 51 } 52 53 func TestClassifyDSN(t *testing.T) { 54 for k, tc := range [][]string{ 55 {"mysql://foo:bar@tcp(baz:1234)/db?foo=bar", "mysql://*:*@tcp(baz:1234)/db?foo=bar"}, 56 {"mysql://foo@email.com:bar@tcp(baz:1234)/db?foo=bar", "mysql://*:*@tcp(baz:1234)/db?foo=bar"}, 57 {"postgres://foo:bar@baz:1234/db?foo=bar", "postgres://*:*@baz:1234/db?foo=bar"}, 58 {"postgres://foo@email.com:bar@baz:1234/db?foo=bar", "postgres://*:*@baz:1234/db?foo=bar"}, 59 } { 60 t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { 61 assert.Equal(t, tc[1], classifyDSN(tc[0])) 62 }) 63 } 64 } 65 66 func TestCleanQueryURL(t *testing.T) { 67 a, err := url.ParseQuery("max_conn_lifetime=1h&max_idle_conn_time=1h&max_idle_conns=10&max_conns=10") 68 require.NoError(t, err) 69 70 b := cleanURLQuery(a) 71 assert.NotEqual(t, a, b) 72 assert.NotEqual(t, a.Encode(), b.Encode()) 73 assert.Equal(t, true, strings.Contains(a.Encode(), "max_idle_conn_time")) 74 assert.Equal(t, false, strings.Contains(b.Encode(), "max_idle_conn_time")) 75 assert.Equal(t, true, strings.Contains(a.Encode(), "max_conn_lifetime")) 76 assert.Equal(t, false, strings.Contains(b.Encode(), "max_conn_lifetime")) 77 }