Add ability to run tests on postgresql 9.1-9.5

This commit is contained in:
Dan Sosedoff 2016-01-15 12:33:30 -06:00
parent 3af20f9327
commit 9766bb2ed2
4 changed files with 96 additions and 13 deletions

View File

@ -1,16 +1,21 @@
sudo: required
language: go language: go
sudo: false
services:
- docker
addons: addons:
postgresql: "9.4" postgresql: "9.4"
go: go:
- 1.4.2 - 1.4.2
- 1.5.2 - 1.5.2
install: install:
- make setup - make setup
script: script:
- make build - make build
- make test - make test
- PGHOST=127.0.0.1 ./scripts/test_all.sh

View File

@ -907,6 +907,8 @@ CREATE FUNCTION "check_book_addition" () RETURNS opaque AS '
CREATE VIEW "stock_view" as SELECT stock.isbn, stock.retail, stock.stock FROM stock; CREATE VIEW "stock_view" as SELECT stock.isbn, stock.retail, stock.stock FROM stock;
CREATE MATERIALIZED VIEW "m_stock_view" as SELECT stock.isbn, stock.retail, stock.stock FROM stock;
-- --
-- TOC Entry ID 30 (OID 3628247) -- TOC Entry ID 30 (OID 3628247)
-- --

View File

@ -11,8 +11,13 @@ import (
) )
var ( var (
testClient *Client testClient *Client
testCommands map[string]string testCommands map[string]string
serverHost string
serverPort string
serverUser string
serverPassword string
serverDatabase string
) )
func mapKeys(data map[string]*Objects) []string { func mapKeys(data map[string]*Objects) []string {
@ -23,6 +28,28 @@ func mapKeys(data map[string]*Objects) []string {
return result return result
} }
func pgVersion() (int, int) {
var major, minor int
fmt.Sscanf(os.Getenv("PGVERSION"), "%d.%d", &major, &minor)
return major, minor
}
func getVar(name, def string) string {
val := os.Getenv(name)
if val == "" {
return def
}
return val
}
func initVars() {
serverHost = getVar("PGHOST", "localhost")
serverPort = getVar("PGPORT", "5432")
serverUser = getVar("PGUSER", "postgres")
serverPassword = getVar("PGPASSWORD", "postgres")
serverDatabase = getVar("PGDATABASE", "booktown")
}
func setupCommands() { func setupCommands() {
testCommands = map[string]string{ testCommands = map[string]string{
"createdb": "createdb", "createdb": "createdb",
@ -42,7 +69,13 @@ func onWindows() bool {
} }
func setup() { func setup() {
out, err := exec.Command(testCommands["createdb"], "-U", "postgres", "-h", "localhost", "booktown").CombinedOutput() out, err := exec.Command(
testCommands["createdb"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
serverDatabase,
).CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Database creation failed:", string(out)) fmt.Println("Database creation failed:", string(out))
@ -50,7 +83,14 @@ func setup() {
os.Exit(1) os.Exit(1)
} }
out, err = exec.Command(testCommands["psql"], "-U", "postgres", "-h", "localhost", "-f", "../../data/booktown.sql", "booktown").CombinedOutput() out, err = exec.Command(
testCommands["psql"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
"-f", "../../data/booktown.sql",
serverDatabase,
).CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Database import failed:", string(out)) fmt.Println("Database import failed:", string(out))
@ -60,7 +100,8 @@ func setup() {
} }
func setupClient() { func setupClient() {
testClient, _ = NewFromUrl("postgres://postgres@localhost/booktown?sslmode=disable") url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
testClient, _ = NewFromUrl(url)
} }
func teardownClient() { func teardownClient() {
@ -70,7 +111,13 @@ func teardownClient() {
} }
func teardown() { func teardown() {
_, err := exec.Command(testCommands["dropdb"], "-U", "postgres", "-h", "localhost", "booktown").CombinedOutput() _, err := exec.Command(
testCommands["dropdb"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
serverDatabase,
).CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Teardown error:", err) fmt.Println("Teardown error:", err)
@ -78,7 +125,7 @@ func teardown() {
} }
func test_NewClientFromUrl(t *testing.T) { func test_NewClientFromUrl(t *testing.T) {
url := "postgres://postgres@localhost/booktown?sslmode=disable" url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, err := NewFromUrl(url) client, err := NewFromUrl(url)
if err != nil { if err != nil {
@ -90,7 +137,7 @@ func test_NewClientFromUrl(t *testing.T) {
} }
func test_NewClientFromUrl2(t *testing.T) { func test_NewClientFromUrl2(t *testing.T) {
url := "postgresql://postgres@localhost/booktown?sslmode=disable" url := fmt.Sprintf("postgresql://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, err := NewFromUrl(url) client, err := NewFromUrl(url)
if err != nil { if err != nil {
@ -156,6 +203,13 @@ func test_Objects(t *testing.T) {
assert.Equal(t, tables, objects["public"].Tables) assert.Equal(t, tables, objects["public"].Tables)
assert.Equal(t, []string{"recent_shipments", "stock_view"}, objects["public"].Views) assert.Equal(t, []string{"recent_shipments", "stock_view"}, objects["public"].Views)
assert.Equal(t, []string{"author_ids", "book_ids", "shipments_ship_id_seq", "subject_ids"}, objects["public"].Sequences) assert.Equal(t, []string{"author_ids", "book_ids", "shipments_ship_id_seq", "subject_ids"}, objects["public"].Sequences)
major, minor := pgVersion()
if minor == 0 || minor >= 3 {
assert.Equal(t, []string{"m_stock_view"}, objects["public"].MaterializedViews)
} else {
t.Logf("Skipping materialized view on %d.%d\n", major, minor)
}
} }
func test_Table(t *testing.T) { func test_Table(t *testing.T) {
@ -257,7 +311,8 @@ func test_HistoryError(t *testing.T) {
} }
func test_HistoryUniqueness(t *testing.T) { func test_HistoryUniqueness(t *testing.T) {
client, _ := NewFromUrl("postgres://postgres@localhost/booktown?sslmode=disable") url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, _ := NewFromUrl(url)
client.Query("SELECT * FROM books WHERE id = 1") client.Query("SELECT * FROM books WHERE id = 1")
client.Query("SELECT * FROM books WHERE id = 1") client.Query("SELECT * FROM books WHERE id = 1")
@ -272,6 +327,7 @@ func TestAll(t *testing.T) {
return return
} }
initVars()
setupCommands() setupCommands()
teardown() teardown()
setup() setup()

20
script/test_all.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/bash
set -e
export PGHOST=${PGHOST:-192.168.99.100}
export PGUSER="postgres"
export PGPASSWORD=""
export PGDATABASE="booktown"
export PGPORT="5432"
for i in {1..5}
do
export PGVERSION="9.$i"
echo "Running tests against PostgreSQL v$PGVERSION"
docker rm -f postgres || true
docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=$PGPASSWORD -d postgres:$PGVERSION
sleep 5
make test
echo "----------"
done