Skip to content

Commit

Permalink
Merge pull request #5 from magicalbanana/enhancements
Browse files Browse the repository at this point in the history
Enhancements
  • Loading branch information
magicalbanana authored Jul 30, 2020
2 parents 2247db0 + 5d1d4ce commit 062b2d5
Show file tree
Hide file tree
Showing 73 changed files with 482 additions and 12,174 deletions.
72 changes: 72 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
kind: pipeline
name: build-test

services:
- name: postgres
image: postgres:11
environment:
POSTGRES_DB: dbal_development
POSTGRES_USER: dbal
POSTGRES_PASSWORD: dbal

volumes:
- name: deps
temp: {}

steps:
- name: build-modules
image: golang:1.14.2-alpine3.11
volumes:
- name: deps
path: /go
commands:
- apk add --no-cache git
- go mod tidy
- go build -v ./...
- go mod vendor

- name: wait-for-it
image: svajone/wait-for-it:latest
pull: always
commands:
- apk add --update --no-cache bash
- wait-for-it.sh -h 'postgres' -p 5432 -t 180

- name: postgres-migrations
image: svajone/pgmngr:v0.1.0
depends_on:
- wait-for-it
pull: always
commands:
- pgmngr -c .pgmngr.json migration forward

- name: test
# the reason we're using this image here is because of:
# https://github.com/golang/go/issues/14481
image: golang:1.14.6-stretch
environment:
DBAL_PG_DB: postgres://dbal:dbal@postgres:5432/dbal_development?sslmode=disable
volumes:
- name: deps
path: /go
depends_on:
- build-modules
- postgres-migrations
commands:
- tests/scripts/testcov.sh

- name: codecov
image: plugins/codecov
depends_on:
- test
volumes:
- name: deps
path: /go
settings:
required: true
paths:
# see tests/scripts/testcov.sh $workdir
- .cover
token:
from_secret: codecov-token
12 changes: 0 additions & 12 deletions .pgmgr.json

This file was deleted.

34 changes: 34 additions & 0 deletions .pgmngr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"connection": {
"admin": {
"ping_intervals": 5,
"username": "dbal",
"password": "dbal",
"template_database": "postgres",
"host": "postgres",
"port": 5432,
"query_params": {
"sslmode": "disable"
}
},
"migration": {
"ping_intervals": 5,
"username": "dbal",
"password": "dbal",
"database": "dbal_development",
"host": "postgres",
"port": 5432,
"query_params": {
"sslmode": "disable"
}
}
},
"migration": {
"directory": "./tests/migrations/",
"table": {
"schema": "public",
"table": "schema_migrations"
}
}
}

14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2016 Kareem Gan
Copyright (c) 2020 Kareem Hepburn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
[Build Status]: https://travis-ci.org/magicalbanana/dbal
[Build Status Badge]: https://travis-ci.org/magicalbanana/dbal.svg?branch=master

[Coverage Status]: https://coveralls.io/github/magicalbanana/dbal?branch=master
[Coverage Status Badge]: https://coveralls.io/repos/github/magicalbanana/dbal/badge.svg?branch=master

[Doc]: https://godoc.org/github.com/magicalbanana/dbal
[Doc Badge]: https://godoc.org/github.com/magicalbanana/dbal?status.svg

[Go Report Card]: https://goreportcard.com/report/github.com/magicalbanana/dbal
[Go Report Card Badge]: https://goreportcard.com/badge/github.com/magicalbanana/dbal

[![Build Status][Build Status Badge]][Build Status]
[![Coverage Status][Coverage Status Badge]][Coverage Status]
[![Doc][Doc Badge]][Doc]
[![Go Report Card][Go Report Card Badge]][Go Report Card]
[codecov]: https://codecov.io/gh/magicalbanana/dbal
[codecov badge master]: https://codecov.io/gh/magicalbanana/dbal/branch/master/graph/badge.svg
[drone]: https://cloud.drone.io
[drone repo]: https://cloud.drone.io/magicalbanana/dbal
[drone badge master]: https://cloud.drone.io/api/badges/magicalbanana/dbal/status.svg
[doc]: https://godoc.org/github.com/magicalbanana/dbal
[doc badge]: https://godoc.org/github.com/magicalbanana/dbal?status.svg
[go report card]: https://goreportcard.com/report/github.com/magicalbanana/dbal
[go report card badge]: https://goreportcard.com/badge/github.com/magicalbanana/dbal

| master |
| - |
| [![drone repo][drone badge master]][drone repo] |
| [![codecov][codecov badge master]][codecov] |
| [![doc][doc badge]][doc] |
| [![go report card][go report card badge]][go report card] |

# dbal - DB Access Layer

## Description

This package wraps the `database/sql` package.

## Development

This package is still under active development. It is wise to vendor this package because although not planned some breaking API changes may be introduced.

## Required Interfaces
This package wraps the `database/sql` package by allowing you to specify
parameters in a map and converting the SQL parameters to positional parameters
to leverage the SQL santizaton provided by the `database/sql` package.

## Usage

TBD ...

## TODO

- [ ] add support for transactions
- [ ] benchmark SQL parsing
File renamed without changes.
112 changes: 107 additions & 5 deletions dbal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,109 @@
package dbal

var badSQLStmnt = `SSSSSS`
var insertCustomer = `insert into customers(first_name, last_name, address) values(:first_name, :last_name, CAST(NULLIF(:address, '') as jsonb));`
var selectCustomer = `select first_name, last_name, address from customers where first_name = :first_name AND last_name = :last_name;`
var selectAllCustomers = `select first_name, last_name, address from customers;`
var deleteCustomer = `delete from customers where first_name = :first_name and last_name = :last_name;`
import (
"database/sql"
"log"
"testing"

"github.com/c2fo/testify/require"
)

func TestNew(t *testing.T) {
t.Parallel()
db, openErr := sql.Open("postgres", testPgDbCreds(t).String())
require.NoError(t, openErr)

dal := New(db)
require.NotNil(t, dal)
defer db.Close()
}

func TestOpen(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
driver string
dbcreds func() string
assertion func(t *testing.T, e error, m string)
}{
{
// db connection fails because of the invalid driver being passed
// to sqlx.Open
desc: "database fails to open",
driver: "manbearpig",
dbcreds: func() string {
return "dbname=piggly_wiggly"
},
assertion: func(t *testing.T, e error, m string) {
require.Error(t, e, m)
},
},
{
desc: "database loads successfully",
driver: "postgres",
dbcreds: func() string {
return "dbname=piggly_wiggly"
},
assertion: func(t *testing.T, e error, m string) {
require.NoError(t, e, m)
},
},
}

for _, test := range tests {
db, err := Open(test.driver, test.dbcreds())
defer func() {
if db != nil {
db.Close()
}
}()
test.assertion(t, err, test.desc)
}
}

func TestPingDataBase(t *testing.T) {
t.Parallel()
tests := []struct {
desc string
driver string
db func() DB
assertion func(t *testing.T, e error, m string)
}{
{
// DB fails to ping because the database does not exist
desc: "database ping reaches maximum ping time",
driver: "postgres",
db: func() DB {
db, _ := Open("postgres", "dbname=piggly_wiggly")
return db
},
assertion: func(t *testing.T, e error, m string) {
require.Error(t, e, m)
},
},
{
desc: "database loads successfully",
driver: "postgres",
db: func() DB {
db, _ := Open("postgres", testPgDbCreds(t).String())
return db
},
assertion: func(t *testing.T, e error, m string) {
require.NoError(t, e, m)
},
},
}

for _, test := range tests {
lgr := func(msg string) {
log.Println(msg)
}
pingErr := PingDatabase(test.db(), 2, lgr)
defer func() {
if db := test.db(); db != nil {
db.Close()
}
}()
test.assertion(t, pingErr, test.desc)
}
}
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
version: '3.7'

services:
postgres:
image: postgres:11
environment:
POSTGRES_DB: dbal_development
POSTGRES_USER: dbal
POSTGRES_PASSWORD: dbal
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data

postgres-migration:
build:
context: .
dockerfile: tests/postgres-migration.Dockerfile
volumes:
- ./tests/migrations:/app/resources/postgres/migrations

volumes:
postgres: {}
21 changes: 9 additions & 12 deletions exec_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package dbal

import (
"database/sql"
"testing"

"github.com/magicalbanana/dbal/mocks"
"github.com/stretchr/testify/assert"
"github.com/c2fo/testify/require"
)

func TestExec(t *testing.T) {
Expand All @@ -19,36 +17,35 @@ func TestExec(t *testing.T) {
desc: "db.Prepare returned an error",
assertion: func(t *testing.T, desc string) {
// arrangement
d := &dbal{db: &mocks.Db{PrepareOk: false}}
d := &dbal{db: &mockDb{PrepareOk: false}}

// act
_, err := d.Exec(badSQLStmnt, nil)

// assertion
assert.Error(t, err)
require.Error(t, err, desc)
},
},
{
desc: "db.Prepare did not return an error",
assertion: func(t *testing.T, desc string) {
db, openErr := sql.Open("postgres", "dbname=dbal_test sslmode=disable")
assert.NoError(t, openErr)
db := testLoadDBAL(t)
defer db.Close()
params := map[string]interface{}{
"first_name": "bearpig",
"last_name": "man",
"address": []byte(`{ "test": "foo" }`),
}
d := &dbal{db: db}

// act
_, qryErr := d.Exec(insertCustomer, params)
_, qryErr := db.Exec(insertCustomer, params)

// assertion
assert.NoError(t, qryErr)
require.NoError(t, qryErr, desc)

// clean up
_, qryErr = d.Query(deleteCustomer, params)
assert.NoError(t, qryErr)
_, qryErr = db.Query(deleteCustomer, params)
require.NoError(t, qryErr, desc)
},
},
}
Expand Down
Loading

0 comments on commit 062b2d5

Please sign in to comment.