-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from danvergara/open-db-conn
feat(pkg/command): add a options struct to store the values provided …
- Loading branch information
Showing
10 changed files
with
338 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
name: Run Integration Tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
integration-test: | ||
name: Test | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
goVer: [1.13, 1.14, 1.15, 1.16] | ||
# Defines the platform for each test run. | ||
runs-on: ${{ matrix.os }} | ||
# define the container services used to run integration tests | ||
services: | ||
postgres: | ||
image: postgres:12 | ||
env: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: password | ||
POSTGRES_DB: users | ||
ports: | ||
- 5432:5432 | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
env: | ||
DB_DATABASE: users | ||
DB_USER: user | ||
DB_PASSWORD: password | ||
|
||
steps: | ||
- name: Set up MySQL | ||
run: | | ||
sudo /etc/init.d/mysql start | ||
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }} | ||
# The steps that will be run through for each version and platform combination. | ||
- name: Set up Go ${{ matrix.goVer }} | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.goVer }} | ||
# Checks out our code locally so we can work with the files. | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
# Caches downloaded modules | ||
- uses: actions/cache@v2 | ||
with: | ||
path: ~/go/pkg/mod | ||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go- | ||
# Runs go test ./... against postgres container | ||
- name: Test Postgres | ||
run: go test -v ./... | ||
env: | ||
DB_USER: postgres | ||
DB_PASSWORD: password | ||
DB_HOST: postgres | ||
DB_PORT: 5432 | ||
DB_NAME: users | ||
DB_DRIVER: postgres | ||
|
||
# Runs go test ./... against mysql container | ||
- name: Test MySQL | ||
run: go test -v ./... | ||
env: | ||
DB_USER: user | ||
DB_PASSWORD: password | ||
DB_HOST: localhost | ||
DB_PORT: 3306 | ||
DB_NAME: users | ||
DB_DRIVER: mysql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package client | ||
|
||
import ( | ||
// mysql driver. | ||
_ "github.com/go-sql-driver/mysql" | ||
// postgres driver. | ||
_ "github.com/lib/pq" | ||
|
||
"github.com/danvergara/dblab/pkg/command" | ||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
// Client is used to store the pool of db connection. | ||
type Client struct { | ||
db *sqlx.DB | ||
} | ||
|
||
// New return an instance of the client. | ||
func New(opts command.Options) (*Client, error) { | ||
db, err := sqlx.Open(opts.Driver, opts.URL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c := Client{ | ||
db: db, | ||
} | ||
|
||
return &c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/danvergara/dblab/pkg/command" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
driver string | ||
user string | ||
password string | ||
host string | ||
port string | ||
name string | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
driver = os.Getenv("DB_DRIVER") | ||
user = os.Getenv("DB_USER") | ||
password = os.Getenv("DB_PASSWORD") | ||
host = os.Getenv("DB_HOST") | ||
port = os.Getenv("DB_PORT") | ||
name = os.Getenv("DB_NAME") | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func generateURL(driver string) string { | ||
switch driver { | ||
case "postgres": | ||
return fmt.Sprintf("%s://%s:%s@%s:%s/%s?sslmode=mode", driver, user, password, host, port, name) | ||
case "mysql": | ||
return fmt.Sprintf("%s://%s:%s@tcp(%s:%s)/%s?sslmode=mode", driver, user, password, host, port, name) | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
func TestNewClientByURL(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping short mode") | ||
} | ||
|
||
url := generateURL(driver) | ||
|
||
opts := command.Options{ | ||
Driver: driver, | ||
URL: url, | ||
} | ||
|
||
c, err := New(opts) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
assert.NotNil(t, c) | ||
} |
Oops, something went wrong.