...

Source file src/github.com/ory/x/sqlcon/parse_opts_test.go

Documentation: github.com/ory/x/sqlcon

     1  package sqlcon
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/ory/x/logrusx"
    11  )
    12  
    13  func TestParseConnectionOptions(t *testing.T) {
    14  	defaultMaxConns, defaultMaxIdleConns, defaultMaxConnLifetime := maxParallelism()*2, maxParallelism(), time.Duration(0)
    15  	logger := logrusx.New("", "")
    16  	for i, tc := range []struct {
    17  		name, dsn, cleanedDSN  string
    18  		maxConns, maxIdleConns int
    19  		maxConnLifetime        time.Duration
    20  	}{
    21  		{
    22  			name:            "no parameters",
    23  			dsn:             "postgres://user:pwd@host:port",
    24  			cleanedDSN:      "postgres://user:pwd@host:port",
    25  			maxConns:        defaultMaxConns,
    26  			maxIdleConns:    defaultMaxIdleConns,
    27  			maxConnLifetime: defaultMaxConnLifetime,
    28  		},
    29  		{
    30  			name:            "only other parameters",
    31  			dsn:             "postgres://user:pwd@host:port?bar=value&foo=other_value",
    32  			cleanedDSN:      "postgres://user:pwd@host:port?bar=value&foo=other_value",
    33  			maxConns:        defaultMaxConns,
    34  			maxIdleConns:    defaultMaxIdleConns,
    35  			maxConnLifetime: defaultMaxConnLifetime,
    36  		},
    37  		{
    38  			name:            "only maxConns",
    39  			dsn:             "postgres://user:pwd@host:port?max_conns=5254",
    40  			cleanedDSN:      "postgres://user:pwd@host:port?",
    41  			maxConns:        5254,
    42  			maxIdleConns:    defaultMaxIdleConns,
    43  			maxConnLifetime: defaultMaxConnLifetime,
    44  		},
    45  		{
    46  			name:            "only maxIdleConns",
    47  			dsn:             "postgres://user:pwd@host:port?max_idle_conns=9342",
    48  			cleanedDSN:      "postgres://user:pwd@host:port?",
    49  			maxConns:        defaultMaxConns,
    50  			maxIdleConns:    9342,
    51  			maxConnLifetime: defaultMaxConnLifetime,
    52  		},
    53  		{
    54  			name:            "only maxConnLifetime",
    55  			dsn:             "postgres://user:pwd@host:port?max_conn_lifetime=112s",
    56  			cleanedDSN:      "postgres://user:pwd@host:port?",
    57  			maxConns:        defaultMaxConns,
    58  			maxIdleConns:    defaultMaxIdleConns,
    59  			maxConnLifetime: 112 * time.Second,
    60  		},
    61  		{
    62  			name:            "all parameters and others",
    63  			dsn:             "postgres://user:pwd@host:port?max_conns=5254&max_idle_conns=9342&max_conn_lifetime=112s&bar=value&foo=other_value",
    64  			cleanedDSN:      "postgres://user:pwd@host:port?bar=value&foo=other_value",
    65  			maxConns:        5254,
    66  			maxIdleConns:    9342,
    67  			maxConnLifetime: 112 * time.Second,
    68  		},
    69  	} {
    70  		t.Run(fmt.Sprintf("case=%d/name=%s", i, tc.name), func(t *testing.T) {
    71  			maxConns, maxIdleConns, maxConnLifetime, cleanedDSN := ParseConnectionOptions(logger, tc.dsn)
    72  			assert.Equal(t, tc.maxConns, maxConns)
    73  			assert.Equal(t, tc.maxIdleConns, maxIdleConns)
    74  			assert.Equal(t, tc.maxConnLifetime, maxConnLifetime)
    75  			assert.Equal(t, tc.cleanedDSN, cleanedDSN)
    76  		})
    77  	}
    78  }
    79  
    80  func TestFinalizeDSN(t *testing.T) {
    81  	for i, tc := range []struct {
    82  		dsn, expected string
    83  	}{
    84  		{
    85  			dsn:      "mysql://localhost",
    86  			expected: "mysql://localhost?multiStatements=true&parseTime=true",
    87  		},
    88  		{
    89  			dsn:      "mysql://localhost?multiStatements=true&parseTime=true",
    90  			expected: "mysql://localhost?multiStatements=true&parseTime=true",
    91  		},
    92  		{
    93  			dsn:      "postgres://localhost",
    94  			expected: "postgres://localhost",
    95  		},
    96  	} {
    97  		t.Run(fmt.Sprintf("case=%d", i), func(t *testing.T) {
    98  			assert.Equal(t, tc.expected, FinalizeDSN(logrusx.New("", ""), tc.dsn))
    99  		})
   100  	}
   101  }
   102  

View as plain text