Skip to content

Commit

Permalink
Add connectionString fn
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Mar 23, 2023
1 parent 9c55af3 commit 0cdb2ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
19 changes: 11 additions & 8 deletions modules/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,19 @@ func StartContainer(ctx context.Context, opts ...MySQLContainerOption) (*MySQLCo
return &MySQLContainer{container, username, password, database}, nil
}

func (c *MySQLContainer) Username() string {
return c.username
}
func (c *MySQLContainer) ConnectionString(ctx context.Context) (string, error) {
containerPort, err := c.MappedPort(ctx, "3306/tcp")
if err != nil {
return "", err
}

func (c *MySQLContainer) Password() string {
return c.password
}
host, err := c.Host(ctx)
if err != nil {
return "", err
}

func (c *MySQLContainer) Database() string {
return c.database
connectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", c.username, c.password, host, containerPort.Port(), c.database)
return connectionString, nil
}

// WithImage sets the image to be used for the mysql container
Expand Down
37 changes: 4 additions & 33 deletions modules/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mysql
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"testing"

Expand All @@ -27,14 +26,7 @@ func TestMySQL(t *testing.T) {
})

// perform assertions

host, _ := container.Host(ctx)

p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
container.Username(), container.Password(), host, port, container.Database())
connectionString, _ := container.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
Expand Down Expand Up @@ -88,14 +80,7 @@ func TestMySQLWithRootUserAndEmptyPassword(t *testing.T) {
})

// perform assertions

host, _ := container.Host(ctx)

p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
container.Username(), container.Password(), host, port, container.Database())
connectionString, _ := container.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
Expand Down Expand Up @@ -135,14 +120,7 @@ func TestMySQLWithConfigFile(t *testing.T) {
})

// perform assertions

host, _ := container.Host(ctx)

p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s",
container.Username(), container.Password(), host, port, container.Database())
connectionString, _ := container.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
Expand Down Expand Up @@ -188,14 +166,7 @@ func TestMySQLWithScripts(t *testing.T) {
})

// perform assertions

host, _ := container.Host(ctx)

p, _ := container.MappedPort(ctx, "3306/tcp")
port := p.Int()

connectionString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=skip-verify",
container.Username(), container.Password(), host, port, container.Database())
connectionString, _ := container.ConnectionString(ctx)

db, err := sql.Open("mysql", connectionString)
if err != nil {
Expand Down

0 comments on commit 0cdb2ed

Please sign in to comment.