Fix all tests
This commit is contained in:
parent
e9f2f8eb83
commit
50cdf99913
2
main.go
2
main.go
@ -7,8 +7,6 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
_ "github.com/lib/pq"
|
|
||||||
|
|
||||||
"github.com/sosedoff/pgweb/pkg/api"
|
"github.com/sosedoff/pgweb/pkg/api"
|
||||||
"github.com/sosedoff/pgweb/pkg/client"
|
"github.com/sosedoff/pgweb/pkg/client"
|
||||||
"github.com/sosedoff/pgweb/pkg/command"
|
"github.com/sosedoff/pgweb/pkg/command"
|
||||||
|
@ -10,17 +10,17 @@ func Test_Invalid_Bookmark_Files(t *testing.T) {
|
|||||||
_, err := readServerConfig("foobar")
|
_, err := readServerConfig("foobar")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
|
|
||||||
_, err = readServerConfig("./data/invalid.toml")
|
_, err = readServerConfig("../../data/invalid.toml")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, "Near line 1, key 'invalid encoding': Near line 2: Expected key separator '=', but got '\\n' instead.", err.Error())
|
assert.Equal(t, "Near line 1, key 'invalid encoding': Near line 2: Expected key separator '=', but got '\\n' instead.", err.Error())
|
||||||
|
|
||||||
_, err = readServerConfig("./data/invalid_port.toml")
|
_, err = readServerConfig("../../data/invalid_port.toml")
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, "Type mismatch for 'main.Bookmark.Port': Expected string but found 'int64'.", err.Error())
|
assert.Equal(t, "Type mismatch for 'bookmarks.Bookmark.Port': Expected string but found 'int64'.", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Bookmark(t *testing.T) {
|
func Test_Bookmark(t *testing.T) {
|
||||||
bookmark, err := readServerConfig("./data/bookmark.toml")
|
bookmark, err := readServerConfig("../../data/bookmark.toml")
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "localhost", bookmark.Host)
|
assert.Equal(t, "localhost", bookmark.Host)
|
||||||
@ -33,7 +33,7 @@ func Test_Bookmark(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Bookmark_URL(t *testing.T) {
|
func Test_Bookmark_URL(t *testing.T) {
|
||||||
bookmark, err := readServerConfig("./data/bookmark_url.toml")
|
bookmark, err := readServerConfig("../../data/bookmark_url.toml")
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://username:password@host:port/database?sslmode=disable", bookmark.Url)
|
assert.Equal(t, "postgres://username:password@host:port/database?sslmode=disable", bookmark.Url)
|
||||||
@ -46,7 +46,7 @@ func Test_Bookmark_URL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Bookmarks_Path(t *testing.T) {
|
func Test_Bookmarks_Path(t *testing.T) {
|
||||||
assert.NotEqual(t, "/.pgweb/bookmarks", bookmarksPath())
|
assert.NotEqual(t, "/.pgweb/bookmarks", Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Basename(t *testing.T) {
|
func Test_Basename(t *testing.T) {
|
||||||
@ -57,14 +57,14 @@ func Test_Basename(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_ReadBookmarks_Invalid(t *testing.T) {
|
func Test_ReadBookmarks_Invalid(t *testing.T) {
|
||||||
bookmarks, err := readAllBookmarks("foobar")
|
bookmarks, err := ReadAll("foobar")
|
||||||
|
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, 0, len(bookmarks))
|
assert.Equal(t, 0, len(bookmarks))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_ReadBookmarks(t *testing.T) {
|
func Test_ReadBookmarks(t *testing.T) {
|
||||||
bookmarks, err := readAllBookmarks("./data")
|
bookmarks, err := ReadAll("../../data")
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, 2, len(bookmarks))
|
assert.Equal(t, 2, len(bookmarks))
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/sosedoff/pgweb/pkg/command"
|
"github.com/sosedoff/pgweb/pkg/command"
|
||||||
"github.com/sosedoff/pgweb/pkg/connection"
|
"github.com/sosedoff/pgweb/pkg/connection"
|
||||||
@ -65,7 +67,6 @@ func NewFromUrl(url string) (*Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
db, err := sqlx.Open("postgres", url)
|
db, err := sqlx.Open("postgres", url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -237,7 +238,10 @@ func (res *Result) CSV() []byte {
|
|||||||
|
|
||||||
// Close database connection
|
// Close database connection
|
||||||
func (client *Client) Close() error {
|
func (client *Client) Close() error {
|
||||||
return client.db.Close()
|
if client.db != nil {
|
||||||
|
return client.db.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all rows as strings for a single column
|
// Fetch all rows as strings for a single column
|
||||||
|
@ -10,8 +10,10 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testClient *Client
|
var (
|
||||||
var testCommands map[string]string
|
testClient *Client
|
||||||
|
testCommands map[string]string
|
||||||
|
)
|
||||||
|
|
||||||
func setupCommands() {
|
func setupCommands() {
|
||||||
testCommands = map[string]string{
|
testCommands = map[string]string{
|
||||||
@ -40,7 +42,7 @@ 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", "postgres", "-h", "localhost", "-f", "../../data/booktown.sql", "booktown").CombinedOutput()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Database import failed:", string(out))
|
fmt.Println("Database import failed:", string(out))
|
||||||
@ -50,7 +52,7 @@ func setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setupClient() {
|
func setupClient() {
|
||||||
testClient, _ = NewClientFromUrl("postgres://postgres@localhost/booktown?sslmode=disable")
|
testClient, _ = NewFromUrl("postgres://postgres@localhost/booktown?sslmode=disable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func teardownClient() {
|
func teardownClient() {
|
||||||
@ -69,14 +71,14 @@ func teardown() {
|
|||||||
|
|
||||||
func test_NewClientFromUrl(t *testing.T) {
|
func test_NewClientFromUrl(t *testing.T) {
|
||||||
url := "postgres://postgres@localhost/booktown?sslmode=disable"
|
url := "postgres://postgres@localhost/booktown?sslmode=disable"
|
||||||
client, err := NewClientFromUrl(url)
|
client, err := NewFromUrl(url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
defer client.db.Close()
|
defer client.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, url, client.connectionString)
|
assert.Equal(t, url, client.ConnectionString)
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_Test(t *testing.T) {
|
func test_Test(t *testing.T) {
|
||||||
@ -209,7 +211,7 @@ func test_ResultCsv(t *testing.T) {
|
|||||||
|
|
||||||
func test_History(t *testing.T) {
|
func test_History(t *testing.T) {
|
||||||
_, err := testClient.Query("SELECT * FROM books")
|
_, err := testClient.Query("SELECT * FROM books")
|
||||||
query := testClient.history[len(testClient.history)-1].Query
|
query := testClient.History[len(testClient.History)-1].Query
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "SELECT * FROM books", query)
|
assert.Equal(t, "SELECT * FROM books", query)
|
||||||
@ -217,7 +219,7 @@ func test_History(t *testing.T) {
|
|||||||
|
|
||||||
func test_HistoryError(t *testing.T) {
|
func test_HistoryError(t *testing.T) {
|
||||||
_, err := testClient.Query("SELECT * FROM books123")
|
_, err := testClient.Query("SELECT * FROM books123")
|
||||||
query := testClient.history[len(testClient.history)-1].Query
|
query := testClient.History[len(testClient.History)-1].Query
|
||||||
|
|
||||||
assert.NotEqual(t, nil, err)
|
assert.NotEqual(t, nil, err)
|
||||||
assert.NotEqual(t, "SELECT * FROM books123", query)
|
assert.NotEqual(t, "SELECT * FROM books123", query)
|
||||||
|
@ -5,11 +5,12 @@ import (
|
|||||||
"os/user"
|
"os/user"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/sosedoff/pgweb/pkg/command"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Invalid_Url(t *testing.T) {
|
func Test_Invalid_Url(t *testing.T) {
|
||||||
opts := Options{}
|
opts := command.Options{}
|
||||||
examples := []string{
|
examples := []string{
|
||||||
"postgresql://foobar",
|
"postgresql://foobar",
|
||||||
"foobar",
|
"foobar",
|
||||||
@ -17,7 +18,7 @@ func Test_Invalid_Url(t *testing.T) {
|
|||||||
|
|
||||||
for _, val := range examples {
|
for _, val := range examples {
|
||||||
opts.Url = val
|
opts.Url = val
|
||||||
str, err := buildConnectionString(opts)
|
str, err := BuildString(opts)
|
||||||
|
|
||||||
assert.Equal(t, "", str)
|
assert.Equal(t, "", str)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
@ -27,14 +28,14 @@ func Test_Invalid_Url(t *testing.T) {
|
|||||||
|
|
||||||
func Test_Valid_Url(t *testing.T) {
|
func Test_Valid_Url(t *testing.T) {
|
||||||
url := "postgres://myhost/database"
|
url := "postgres://myhost/database"
|
||||||
str, err := buildConnectionString(Options{Url: url})
|
str, err := BuildString(command.Options{Url: url})
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, url, str)
|
assert.Equal(t, url, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Url_And_Ssl_Flag(t *testing.T) {
|
func Test_Url_And_Ssl_Flag(t *testing.T) {
|
||||||
str, err := buildConnectionString(Options{
|
str, err := BuildString(command.Options{
|
||||||
Url: "postgres://myhost/database",
|
Url: "postgres://myhost/database",
|
||||||
Ssl: "disable",
|
Ssl: "disable",
|
||||||
})
|
})
|
||||||
@ -44,14 +45,14 @@ func Test_Url_And_Ssl_Flag(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Localhost_Url_And_No_Ssl_Flag(t *testing.T) {
|
func Test_Localhost_Url_And_No_Ssl_Flag(t *testing.T) {
|
||||||
str, err := buildConnectionString(Options{
|
str, err := BuildString(command.Options{
|
||||||
Url: "postgres://localhost/database",
|
Url: "postgres://localhost/database",
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://localhost/database?sslmode=disable", str)
|
assert.Equal(t, "postgres://localhost/database?sslmode=disable", str)
|
||||||
|
|
||||||
str, err = buildConnectionString(Options{
|
str, err = BuildString(command.Options{
|
||||||
Url: "postgres://127.0.0.1/database",
|
Url: "postgres://127.0.0.1/database",
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ func Test_Localhost_Url_And_No_Ssl_Flag(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Localhost_Url_And_Ssl_Flag(t *testing.T) {
|
func Test_Localhost_Url_And_Ssl_Flag(t *testing.T) {
|
||||||
str, err := buildConnectionString(Options{
|
str, err := BuildString(command.Options{
|
||||||
Url: "postgres://localhost/database",
|
Url: "postgres://localhost/database",
|
||||||
Ssl: "require",
|
Ssl: "require",
|
||||||
})
|
})
|
||||||
@ -68,7 +69,7 @@ func Test_Localhost_Url_And_Ssl_Flag(t *testing.T) {
|
|||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://localhost/database?sslmode=require", str)
|
assert.Equal(t, "postgres://localhost/database?sslmode=require", str)
|
||||||
|
|
||||||
str, err = buildConnectionString(Options{
|
str, err = BuildString(command.Options{
|
||||||
Url: "postgres://127.0.0.1/database",
|
Url: "postgres://127.0.0.1/database",
|
||||||
Ssl: "require",
|
Ssl: "require",
|
||||||
})
|
})
|
||||||
@ -78,14 +79,14 @@ func Test_Localhost_Url_And_Ssl_Flag(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Localhost_Url_And_Ssl_Arg(t *testing.T) {
|
func Test_Localhost_Url_And_Ssl_Arg(t *testing.T) {
|
||||||
str, err := buildConnectionString(Options{
|
str, err := BuildString(command.Options{
|
||||||
Url: "postgres://localhost/database?sslmode=require",
|
Url: "postgres://localhost/database?sslmode=require",
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://localhost/database?sslmode=require", str)
|
assert.Equal(t, "postgres://localhost/database?sslmode=require", str)
|
||||||
|
|
||||||
str, err = buildConnectionString(Options{
|
str, err = BuildString(command.Options{
|
||||||
Url: "postgres://127.0.0.1/database?sslmode=require",
|
Url: "postgres://127.0.0.1/database?sslmode=require",
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ func Test_Localhost_Url_And_Ssl_Arg(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Flag_Args(t *testing.T) {
|
func Test_Flag_Args(t *testing.T) {
|
||||||
str, err := buildConnectionString(Options{
|
str, err := BuildString(command.Options{
|
||||||
Host: "host",
|
Host: "host",
|
||||||
Port: 5432,
|
Port: 5432,
|
||||||
User: "user",
|
User: "user",
|
||||||
@ -107,7 +108,7 @@ func Test_Flag_Args(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Localhost(t *testing.T) {
|
func Test_Localhost(t *testing.T) {
|
||||||
opts := Options{
|
opts := command.Options{
|
||||||
Host: "localhost",
|
Host: "localhost",
|
||||||
Port: 5432,
|
Port: 5432,
|
||||||
User: "user",
|
User: "user",
|
||||||
@ -115,18 +116,18 @@ func Test_Localhost(t *testing.T) {
|
|||||||
DbName: "db",
|
DbName: "db",
|
||||||
}
|
}
|
||||||
|
|
||||||
str, err := buildConnectionString(opts)
|
str, err := BuildString(opts)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=disable", str)
|
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=disable", str)
|
||||||
|
|
||||||
opts.Host = "127.0.0.1"
|
opts.Host = "127.0.0.1"
|
||||||
str, err = buildConnectionString(opts)
|
str, err = BuildString(opts)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://user:password@127.0.0.1:5432/db?sslmode=disable", str)
|
assert.Equal(t, "postgres://user:password@127.0.0.1:5432/db?sslmode=disable", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Localhost_And_Ssl(t *testing.T) {
|
func Test_Localhost_And_Ssl(t *testing.T) {
|
||||||
opts := Options{
|
opts := command.Options{
|
||||||
Host: "localhost",
|
Host: "localhost",
|
||||||
Port: 5432,
|
Port: 5432,
|
||||||
User: "user",
|
User: "user",
|
||||||
@ -135,31 +136,31 @@ func Test_Localhost_And_Ssl(t *testing.T) {
|
|||||||
Ssl: "require",
|
Ssl: "require",
|
||||||
}
|
}
|
||||||
|
|
||||||
str, err := buildConnectionString(opts)
|
str, err := BuildString(opts)
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=require", str)
|
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=require", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_No_User(t *testing.T) {
|
func Test_No_User(t *testing.T) {
|
||||||
opts := Options{Host: "host", Port: 5432, DbName: "db"}
|
opts := command.Options{Host: "host", Port: 5432, DbName: "db"}
|
||||||
u, _ := user.Current()
|
u, _ := user.Current()
|
||||||
str, err := buildConnectionString(opts)
|
str, err := BuildString(opts)
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, fmt.Sprintf("postgres://%s@host:5432/db", u.Username), str)
|
assert.Equal(t, fmt.Sprintf("postgres://%s@host:5432/db", u.Username), str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Port(t *testing.T) {
|
func Test_Port(t *testing.T) {
|
||||||
opts := Options{Host: "host", User: "user", Port: 5000, DbName: "db"}
|
opts := command.Options{Host: "host", User: "user", Port: 5000, DbName: "db"}
|
||||||
str, err := buildConnectionString(opts)
|
str, err := BuildString(opts)
|
||||||
|
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, "postgres://user@host:5000/db", str)
|
assert.Equal(t, "postgres://user@host:5000/db", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Blank(t *testing.T) {
|
func Test_Blank(t *testing.T) {
|
||||||
assert.Equal(t, true, connectionSettingsBlank(Options{}))
|
assert.Equal(t, true, IsBlank(command.Options{}))
|
||||||
assert.Equal(t, false, connectionSettingsBlank(Options{Host: "host", User: "user"}))
|
assert.Equal(t, false, IsBlank(command.Options{Host: "host", User: "user"}))
|
||||||
assert.Equal(t, false, connectionSettingsBlank(Options{Host: "host", User: "user", DbName: "db"}))
|
assert.Equal(t, false, IsBlank(command.Options{Host: "host", User: "user", DbName: "db"}))
|
||||||
assert.Equal(t, false, connectionSettingsBlank(Options{Url: "url"}))
|
assert.Equal(t, false, IsBlank(command.Options{Url: "url"}))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user