pgweb/pkg/client/dump_test.go

47 lines
1.0 KiB
Go

package client
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func testDumpExport(t *testing.T) {
url := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, serverDatabase)
savePath := "/tmp/dump.sql.gz"
os.Remove(savePath)
saveFile, err := os.Create(savePath)
if err != nil {
t.Fatal(err.Error())
}
defer func() {
saveFile.Close()
os.Remove(savePath)
}()
dump := Dump{}
// Test for pg_dump presence
assert.True(t, dump.CanExport())
// Test full db dump
err = dump.Export(url, saveFile)
assert.NoError(t, err)
// Test nonexistent database
invalidUrl := fmt.Sprintf("postgres://%s@%s:%s/%s?sslmode=disable", serverUser, serverHost, serverPort, "foobar")
err = dump.Export(invalidUrl, saveFile)
assert.Contains(t, err.Error(), `database "foobar" does not exist`)
// Test dump of non existent db
dump = Dump{Table: "foobar"}
err = dump.Export(url, saveFile)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "pg_dump: no matching tables were found")
}