pgweb/pkg/client/client_test.go

394 lines
9.0 KiB
Go
Raw Normal View History

2015-04-30 19:47:07 +03:00
package client
2015-01-14 06:03:52 +03:00
import (
"fmt"
"os"
"os/exec"
2015-01-14 06:32:47 +03:00
"runtime"
2015-01-14 06:03:52 +03:00
"testing"
"github.com/stretchr/testify/assert"
)
2015-04-30 20:09:29 +03:00
var (
testClient *Client
testCommands map[string]string
serverHost string
serverPort string
serverUser string
serverPassword string
serverDatabase string
2015-04-30 20:09:29 +03:00
)
2015-01-14 06:32:47 +03:00
2016-01-13 06:33:44 +03:00
func mapKeys(data map[string]*Objects) []string {
result := []string{}
for k, _ := range data {
result = append(result, k)
}
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")
}
2015-01-14 06:32:47 +03:00
func setupCommands() {
testCommands = map[string]string{
"createdb": "createdb",
"psql": "psql",
"dropdb": "dropdb",
}
if onWindows() {
2015-01-14 06:32:47 +03:00
for k, v := range testCommands {
2015-01-14 06:39:22 +03:00
testCommands[k] = v + ".exe"
2015-01-14 06:32:47 +03:00
}
}
}
2015-01-14 06:03:52 +03:00
func onWindows() bool {
return runtime.GOOS == "windows"
}
2015-01-14 06:03:52 +03:00
func setup() {
out, err := exec.Command(
testCommands["createdb"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
serverDatabase,
).CombinedOutput()
2015-01-14 06:03:52 +03:00
if err != nil {
fmt.Println("Database creation failed:", string(out))
fmt.Println("Error:", err)
2015-01-14 06:03:52 +03:00
os.Exit(1)
}
out, err = exec.Command(
testCommands["psql"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
"-f", "../../data/booktown.sql",
serverDatabase,
).CombinedOutput()
2015-01-14 06:03:52 +03:00
if err != nil {
fmt.Println("Database import failed:", string(out))
fmt.Println("Error:", err)
2015-01-14 06:03:52 +03:00
os.Exit(1)
}
}
func setupClient() {
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
2016-01-18 00:22:33 +03:00
testClient, _ = NewFromUrl(url, nil)
2015-01-14 06:03:52 +03:00
}
func teardownClient() {
if testClient != nil {
testClient.db.Close()
}
}
func teardown() {
_, err := exec.Command(
testCommands["dropdb"],
"-U", serverUser,
"-h", serverHost,
"-p", serverPort,
serverDatabase,
).CombinedOutput()
2015-01-14 06:03:52 +03:00
if err != nil {
fmt.Println("Teardown error:", err)
2015-01-14 06:03:52 +03:00
}
}
func test_NewClientFromUrl(t *testing.T) {
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, err := NewFromUrl(url, nil)
2015-01-14 06:03:52 +03:00
if err != nil {
2015-04-30 20:09:29 +03:00
defer client.Close()
2015-01-14 06:03:52 +03:00
}
assert.Equal(t, nil, err)
2015-04-30 20:09:29 +03:00
assert.Equal(t, url, client.ConnectionString)
2015-01-14 06:03:52 +03:00
}
func test_NewClientFromUrl2(t *testing.T) {
url := fmt.Sprintf("postgresql://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, err := NewFromUrl(url, nil)
if err != nil {
defer client.Close()
}
assert.Equal(t, nil, err)
assert.Equal(t, url, client.ConnectionString)
}
2015-01-14 06:03:52 +03:00
func test_Test(t *testing.T) {
assert.Equal(t, nil, testClient.Test())
}
func test_Info(t *testing.T) {
res, err := testClient.Info()
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, res)
}
2017-03-11 07:24:30 +03:00
func test_Activity(t *testing.T) {
res, err := testClient.Activity()
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, res)
}
2015-01-14 06:03:52 +03:00
func test_Databases(t *testing.T) {
res, err := testClient.Databases()
assert.Equal(t, nil, err)
assert.Contains(t, res, "booktown")
assert.Contains(t, res, "postgres")
}
2016-01-13 06:33:44 +03:00
func test_Objects(t *testing.T) {
res, err := testClient.Objects()
objects := ObjectsFromResult(res)
2015-01-14 06:03:52 +03:00
2016-01-13 06:33:44 +03:00
tables := []string{
2015-01-14 06:03:52 +03:00
"alternate_stock",
"authors",
"book_backup",
"book_queue",
"books",
"customers",
"daily_inventory",
"distinguished_authors",
2016-11-04 03:56:55 +03:00
"dummies",
2015-01-14 06:03:52 +03:00
"editions",
"employees",
"favorite_authors",
"favorite_books",
"money_example",
"my_list",
"numeric_values",
"publishers",
"schedules",
"shipments",
"states",
"stock",
"stock_backup",
"subjects",
"text_sorting",
}
assert.Equal(t, nil, err)
assert.Equal(t, []string{"schema", "name", "type", "owner", "comment"}, res.Columns)
2016-01-13 06:33:44 +03:00
assert.Equal(t, []string{"public"}, mapKeys(objects))
assert.Equal(t, tables, objects["public"].Tables)
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)
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)
}
2015-01-14 06:03:52 +03:00
}
func test_Table(t *testing.T) {
res, err := testClient.Table("books")
columns := []string{
"column_name",
"data_type",
"is_nullable",
"character_maximum_length",
"character_set_catalog",
"column_default",
2017-08-05 00:16:59 +03:00
"comment",
2015-01-14 06:03:52 +03:00
}
assert.Equal(t, nil, err)
assert.Equal(t, columns, res.Columns)
assert.Equal(t, 4, len(res.Rows))
}
func test_TableRows(t *testing.T) {
res, err := testClient.TableRows("books", RowsOptions{})
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(res.Columns))
assert.Equal(t, 15, len(res.Rows))
}
func test_TableInfo(t *testing.T) {
res, err := testClient.TableInfo("books")
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(res.Columns))
assert.Equal(t, 1, len(res.Rows))
}
func test_TableIndexes(t *testing.T) {
res, err := testClient.TableIndexes("books")
assert.Equal(t, nil, err)
assert.Equal(t, 2, len(res.Columns))
assert.Equal(t, 2, len(res.Rows))
}
2015-12-05 03:14:03 +03:00
func test_TableConstraints(t *testing.T) {
res, err := testClient.TableConstraints("editions")
assert.Equal(t, nil, err)
assert.Equal(t, []string{"name", "definition"}, res.Columns)
assert.Equal(t, Row{"pkey", "PRIMARY KEY (isbn)"}, res.Rows[0])
assert.Equal(t, Row{"integrity", "CHECK (book_id IS NOT NULL AND edition IS NOT NULL)"}, res.Rows[1])
2015-12-05 03:14:03 +03:00
}
2015-01-14 06:03:52 +03:00
func test_Query(t *testing.T) {
res, err := testClient.Query("SELECT * FROM books")
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(res.Columns))
assert.Equal(t, 15, len(res.Rows))
}
func test_QueryError(t *testing.T) {
res, err := testClient.Query("SELCT * FROM books")
assert.NotEqual(t, nil, err)
assert.Equal(t, "pq: syntax error at or near \"SELCT\"", err.Error())
assert.Equal(t, true, res == nil)
}
func test_QueryInvalidTable(t *testing.T) {
res, err := testClient.Query("SELECT * FROM books2")
assert.NotEqual(t, nil, err)
assert.Equal(t, "pq: relation \"books2\" does not exist", err.Error())
assert.Equal(t, true, res == nil)
}
2016-11-04 03:56:55 +03:00
func test_TableRowsOrderEscape(t *testing.T) {
rows, err := testClient.TableRows("dummies", RowsOptions{SortColumn: "isDummy"})
assert.Equal(t, nil, err)
assert.Equal(t, 2, len(rows.Rows))
rows, err = testClient.TableRows("dummies", RowsOptions{SortColumn: "isdummy"})
assert.NotEqual(t, nil, err)
assert.Equal(t, `pq: column "isdummy" does not exist`, err.Error())
assert.Equal(t, true, rows == nil)
}
2015-01-14 06:03:52 +03:00
func test_ResultCsv(t *testing.T) {
res, _ := testClient.Query("SELECT * FROM books ORDER BY id ASC LIMIT 1")
csv := res.CSV()
expected := "id,title,author_id,subject_id\n156,The Tell-Tale Heart,115,9\n"
assert.Equal(t, expected, string(csv))
}
2015-01-14 06:21:56 +03:00
func test_History(t *testing.T) {
_, err := testClient.Query("SELECT * FROM books WHERE id = 12345")
2015-04-30 20:09:29 +03:00
query := testClient.History[len(testClient.History)-1].Query
2015-01-14 06:21:56 +03:00
assert.Equal(t, nil, err)
assert.Equal(t, "SELECT * FROM books WHERE id = 12345", query)
2015-01-14 06:21:56 +03:00
}
func test_HistoryError(t *testing.T) {
_, err := testClient.Query("SELECT * FROM books123")
2015-04-30 20:09:29 +03:00
query := testClient.History[len(testClient.History)-1].Query
2015-01-14 06:21:56 +03:00
assert.NotEqual(t, nil, err)
assert.NotEqual(t, "SELECT * FROM books123", query)
}
func test_HistoryUniqueness(t *testing.T) {
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
2016-01-18 00:22:33 +03:00
client, _ := NewFromUrl(url, nil)
client.Query("SELECT * FROM books WHERE id = 1")
client.Query("SELECT * FROM books WHERE id = 1")
assert.Equal(t, 1, len(client.History))
assert.Equal(t, "SELECT * FROM books WHERE id = 1", client.History[0].Query)
}
2016-11-06 01:51:34 +03:00
func test_ReadOnlyMode(t *testing.T) {
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
client, _ := NewFromUrl(url, nil)
err := client.SetReadOnlyMode()
assert.Equal(t, nil, err)
_, err = client.Query("CREATE TABLE foobar(id integer);")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "in a read-only transaction")
}
2015-01-14 06:03:52 +03:00
func TestAll(t *testing.T) {
2015-01-14 07:24:00 +03:00
if onWindows() {
2016-11-06 02:53:42 +03:00
t.Log("Unit testing on Windows platform is not supported.")
2015-01-14 07:24:00 +03:00
return
}
initVars()
2015-01-14 06:32:47 +03:00
setupCommands()
2015-01-14 06:03:52 +03:00
teardown()
setup()
setupClient()
test_NewClientFromUrl(t)
test_Test(t)
test_Info(t)
2017-03-11 07:24:30 +03:00
test_Activity(t)
2015-01-14 06:03:52 +03:00
test_Databases(t)
2016-01-13 06:33:44 +03:00
test_Objects(t)
2015-01-14 06:03:52 +03:00
test_Table(t)
test_TableRows(t)
test_TableInfo(t)
test_TableIndexes(t)
2015-12-05 03:14:03 +03:00
test_TableConstraints(t)
2015-01-14 06:03:52 +03:00
test_Query(t)
test_QueryError(t)
test_QueryInvalidTable(t)
2016-11-04 03:56:55 +03:00
test_TableRowsOrderEscape(t)
2015-01-14 06:03:52 +03:00
test_ResultCsv(t)
2015-01-14 06:21:56 +03:00
test_History(t)
2016-11-06 01:52:18 +03:00
test_HistoryUniqueness(t)
2015-01-14 06:21:56 +03:00
test_HistoryError(t)
2016-11-06 01:51:34 +03:00
test_ReadOnlyMode(t)
2017-09-23 06:06:36 +03:00
test_DumpExport(t)
2015-01-14 06:03:52 +03:00
teardownClient()
teardown()
}