Skip to content

Commit ca4bd0a

Browse files
author
Daniel Perez
committed
Test against all supported databases.
1 parent 795095b commit ca4bd0a

File tree

7 files changed

+54
-9
lines changed

7 files changed

+54
-9
lines changed

.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/dbpopulate
22

33
*.db
4+
.env

.travis.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
language: go
22

3+
services:
4+
- postgresql
5+
- mysql
6+
7+
before_script:
8+
- sh -c "if [ '$DB' = 'postgres' ]; then psql -c 'create database dbpopulate_test;' -U postgres; fi"
9+
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database dbpopulate_test;'; fi"
10+
- go get -t -v ./...
11+
312
go:
413
- 1.4
5-
- tip
14+
- 1.5
15+
- 1.6
16+
17+
env:
18+
- DB=sqlite3 DATABASE_URL="sqlite3://:memory:"
19+
- DB=postgres DATABASE_URL="postgres://postgres@localhost/dbpopulate_test?sslmode=disable"
20+
- DB=mysql DATABASE_URL="mysql://root@tcp(localhost:3306)/dbpopulate_test"

db/tables.mysql.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
DROP TABLE IF EXISTS countries;
12
CREATE TABLE countries(
23
id integer primary key auto_increment,
34
name varchar(255)
45
);
56

7+
DROP TABLE IF EXISTS regions;
68
CREATE TABLE regions(
79
id integer primary key auto_increment,
810
name varchar(255),
@@ -11,6 +13,7 @@ CREATE TABLE regions(
1113

1214
CREATE INDEX regions_country_id ON regions (country_id);
1315

16+
DROP TABLE IF EXISTS prefectures;
1417
CREATE TABLE prefectures(
1518
id integer primary key auto_increment,
1619
name varchar(255),

db/tables.pg.sql renamed to db/tables.postgres.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
DROP TABLE IF EXISTS countries;
12
CREATE TABLE countries(
23
id serial primary key,
34
name varchar(255)
45
);
56

7+
DROP TABLE IF EXISTS regions;
68
CREATE TABLE regions(
79
id serial primary key,
810
name varchar(255),
@@ -11,6 +13,7 @@ CREATE TABLE regions(
1113

1214
CREATE INDEX regions_country_id ON regions (country_id);
1315

16+
DROP TABLE IF EXISTS prefectures;
1417
CREATE TABLE prefectures(
1518
id serial primary key,
1619
name varchar(255),

db/tables.sqlite3.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
DROP TABLE IF EXISTS countries;
12
CREATE TABLE countries(
23
id integer primary key autoincrement,
34
name varchar(255)
45
);
56

7+
DROP TABLE IF EXISTS regions;
68
CREATE TABLE regions(
79
id integer primary key autoincrement,
810
name varchar(255),
@@ -11,6 +13,7 @@ CREATE TABLE regions(
1113

1214
CREATE INDEX regions_country_id ON regions (country_id);
1315

16+
DROP TABLE IF EXISTS prefectures;
1417
CREATE TABLE prefectures(
1518
id integer primary key autoincrement,
1619
name varchar(255),

populator_test.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package main
33
import (
44
"fmt"
55
"io/ioutil"
6+
"net/url"
67
"os"
8+
"strings"
79
"testing"
810

911
"github.com/stretchr/testify/assert"
1012
)
1113

1214
const (
13-
dbFile = "db/data.sqlite.db"
14-
dbUrl = "sqlite3://" + dbFile
15-
schemaFile = "./db/tables.sqlite3.sql"
15+
dbURL = "sqlite3://:memory:"
16+
schemaFileTemplate = "./db/tables.%s.sql"
1617
)
1718

1819
var populator *Populator
@@ -34,18 +35,38 @@ func showErrAndExitTest(err error) {
3435
}
3536

3637
func TestMain(m *testing.M) {
37-
os.Remove(dbFile)
3838
var err error
39-
if populator, err = NewPopulator(dbUrl); err != nil {
39+
40+
connectionURL := dbURL
41+
if envURL := os.Getenv("DATABASE_URL"); envURL != "" {
42+
connectionURL = envURL
43+
}
44+
45+
if populator, err = NewPopulator(connectionURL); err != nil {
4046
showErrAndExitTest(err)
4147
}
42-
createTablesStmt, err := ioutil.ReadFile(schemaFile)
48+
49+
uri, err := url.Parse(connectionURL)
4350
if err != nil {
4451
showErrAndExitTest(err)
4552
}
46-
if _, err = populator.DB.Exec(string(createTablesStmt)); err != nil {
53+
54+
schemaFile := fmt.Sprintf(schemaFileTemplate, uri.Scheme)
55+
createTablesStmt, err := ioutil.ReadFile(schemaFile)
56+
if err != nil {
4757
showErrAndExitTest(err)
4858
}
59+
60+
statements := strings.Split(string(createTablesStmt), ";")
61+
for _, statement := range statements {
62+
if strings.TrimSpace(statement) == "" {
63+
continue
64+
}
65+
if _, err = populator.DB.Exec(statement); err != nil {
66+
showErrAndExitTest(err)
67+
}
68+
}
69+
4970
os.Exit(m.Run())
5071
}
5172

0 commit comments

Comments
 (0)