github.com/Accefy/pop@v0.0.0-20230428174248-e9f677eab5b9/dialect_cockroach_test.go (about)

     1  package pop
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_Cockroach_ConnectionDetails_URL_Finalize(t *testing.T) {
    11  	r := require.New(t)
    12  
    13  	cd := &ConnectionDetails{
    14  		Dialect: "cockroach",
    15  		URL:     "cockroach://user:pass%23@host:1234/database",
    16  	}
    17  	err := cd.Finalize()
    18  	r.NoError(err)
    19  
    20  	r.Equal("database", cd.Database)
    21  	r.Equal("cockroach", cd.Dialect)
    22  	r.Equal("host", cd.Host)
    23  	r.Equal("pass#", cd.Password)
    24  	r.Equal("1234", cd.Port)
    25  	r.Equal("user", cd.User)
    26  }
    27  
    28  func Test_Cockroach_ConnectionDetails_Values_Finalize(t *testing.T) {
    29  	r := require.New(t)
    30  
    31  	cd := &ConnectionDetails{
    32  		Dialect:  "cockroach",
    33  		Database: "database",
    34  		Host:     "host",
    35  		Port:     "1234",
    36  		User:     "user",
    37  		Password: "pass#",
    38  		Options:  map[string]string{"application_name": "testing"},
    39  	}
    40  	err := cd.Finalize()
    41  	r.NoError(err)
    42  
    43  	p := &cockroach{commonDialect: commonDialect{ConnectionDetails: cd}}
    44  
    45  	r.Equal("postgres://user:pass%23@host:1234/database?application_name=testing", p.URL())
    46  }
    47  
    48  func Test_Cockroach_URL_Raw(t *testing.T) {
    49  	r := require.New(t)
    50  	cd := &ConnectionDetails{
    51  		Dialect: "cockroach",
    52  		URL:     "cockroach://user:pass@host:1234/database?option1=value1",
    53  	}
    54  	err := cd.Finalize()
    55  	r.NoError(err)
    56  	m := &cockroach{commonDialect: commonDialect{ConnectionDetails: cd}}
    57  	r.Equal("postgres://user:pass@host:1234/database?option1=value1", m.URL())
    58  	r.Equal("postgres://user:pass@host:1234/?option1=value1", m.urlWithoutDb())
    59  }
    60  
    61  func Test_Cockroach_URL_Build(t *testing.T) {
    62  	r := require.New(t)
    63  
    64  	cd := &ConnectionDetails{
    65  		Dialect:  "cockroach",
    66  		Database: "database",
    67  		Host:     "host",
    68  		Port:     "port",
    69  		User:     "user",
    70  		Password: "pass#",
    71  		Options: map[string]string{
    72  			"option1": "value1",
    73  		},
    74  	}
    75  	err := cd.Finalize()
    76  	r.NoError(err)
    77  
    78  	m := &cockroach{commonDialect: commonDialect{ConnectionDetails: cd}}
    79  	r.True(strings.HasPrefix(m.URL(), "postgres://user:pass%23@host:port/database?"), "URL() returns %v", m.URL())
    80  	r.Contains(m.URL(), "option1=value1")
    81  	r.Contains(m.URL(), "application_name=pop.test")
    82  
    83  	r.True(strings.HasPrefix(m.urlWithoutDb(), "postgres://user:pass%23@host:port/?"), "urlWithoutDb() returns %v", m.urlWithoutDb())
    84  	r.Contains(m.urlWithoutDb(), "option1=value1")
    85  	r.Contains(m.urlWithoutDb(), "application_name=pop.test")
    86  
    87  	r.True(strings.HasPrefix(m.MigrationURL(), "postgres://user:pass%23@host:port/database?"), "MigrationURL() returns %v", m.MigrationURL())
    88  }
    89  
    90  func Test_Cockroach_URL_UserDefinedAppName(t *testing.T) {
    91  	r := require.New(t)
    92  
    93  	cd := &ConnectionDetails{
    94  		Dialect:  "cockroach",
    95  		Database: "database",
    96  		Options: map[string]string{
    97  			"application_name": "myapp",
    98  		},
    99  	}
   100  	err := cd.Finalize()
   101  	r.NoError(err)
   102  	m := &cockroach{commonDialect: commonDialect{ConnectionDetails: cd}}
   103  	r.Contains(m.URL(), "database?application_name=myapp")
   104  	r.Contains(m.urlWithoutDb(), "/?application_name=myapp")
   105  }
   106  
   107  func Test_Cockroach_tableQuery(t *testing.T) {
   108  	r := require.New(t)
   109  	cr := cockroach{}
   110  
   111  	cr.info.version = "v1.0.7"
   112  	r.Equal(selectTablesQueryCockroachV1, cr.tablesQuery())
   113  
   114  	cr.info.version = "v1.1.9"
   115  	r.Equal(selectTablesQueryCockroachV1, cr.tablesQuery())
   116  
   117  	cr.info.version = "v2.0.7"
   118  	r.Equal(selectTablesQueryCockroach, cr.tablesQuery())
   119  
   120  	cr.info.version = "v2.1.7"
   121  	r.Equal(selectTablesQueryCockroach, cr.tablesQuery())
   122  
   123  	cr.info.version = "v19.1.1"
   124  	r.Equal(selectTablesQueryCockroach, cr.tablesQuery())
   125  
   126  	cr.info.version = "v20.1.1"
   127  	r.Equal(selectTablesQueryCockroach, cr.tablesQuery())
   128  }
   129  
   130  func Test_Cockroach_URL_Only(t *testing.T) {
   131  	r := require.New(t)
   132  	cd := &ConnectionDetails{
   133  		URL: "cockroach://user:pass@host:1337/database?option1=value1",
   134  	}
   135  	err := cd.Finalize()
   136  	r.NoError(err)
   137  	m := &cockroach{commonDialect: commonDialect{ConnectionDetails: cd}}
   138  	r.Equal("postgres://user:pass@host:1337/database?option1=value1", m.URL())
   139  	r.Equal("postgres://user:pass@host:1337/?option1=value1", m.urlWithoutDb())
   140  }