Skip to content

Commit

Permalink
postgres test fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Onur CEVIK committed Mar 30, 2021
1 parent 9f4578b commit 2cea9c8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
22 changes: 16 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ func main() {
flag.StringVar(&binaryPath, "binaryPath", "", "Binary path")
flag.Parse()

password, err := util.GetPassword()

var err error
binaryPath,err = internal.CheckBinary(binaryPath, config.SourceType(sourceType), importArg, exportArg)
if err != nil {
log.Fatalln(err)
panic(err)
}

binaryPath = internal.CheckBinary(binaryPath, config.SourceType(sourceType), importArg, exportArg)

dumpConfig := config.Config{
Source: config.SourceType(sourceType),
Import: importArg,
Expand All @@ -46,7 +45,7 @@ func main() {
Path: path,
DB: db,
BinaryPath: binaryPath,
Password: password,
Password: "",
}

var dump internal.Dump
Expand All @@ -59,12 +58,23 @@ func main() {
dump = internal.Postgres{}

case "mysql":
password, err := util.GetPassword()
if err != nil {
log.Fatalln(err)
}
dumpConfig.Password=password
if err := dumpConfig.CheckConfigMySQL(); err != nil {
panic(err)
}
dump = internal.MySQL{}

case "mssql":
password, err := util.GetPassword()

if err != nil {
log.Fatalln(err)
}
dumpConfig.Password=password
if err := dumpConfig.CheckConfigMsSQL(); err != nil {
panic(err)
}
Expand All @@ -75,7 +85,7 @@ func main() {
return
}

if err = dump.Check(); err != nil {
if err := dump.Check(); err != nil {
panic(err)
}

Expand Down
30 changes: 17 additions & 13 deletions internal/detect_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,34 @@ package internal
import (
"bytes"
"github.com/sadihakan/dummy-dump/config"
"log"
"os"
"os/exec"
"strings"
)

// CheckBinary ...
func CheckBinary(binaryPath string, sourceType config.SourceType, importArg bool, exportArg bool) string {
func CheckBinary(binaryPath string, sourceType config.SourceType, importArg bool, exportArg bool) (string,error) {
var err error
if binaryPath == "" {

if importArg {
binaryPath = checkImport(sourceType)
binaryPath,err= checkImport(sourceType)
if err != nil {
return "", err
}
}

if exportArg {
binaryPath = checkExport(sourceType)

binaryPath,err= checkExport(sourceType)
if err != nil {
return "", err
}
}

}
return binaryPath
return binaryPath,nil
}

func checkImport(sourceType config.SourceType) string {
func checkImport(sourceType config.SourceType) (string,error) {
var out bytes.Buffer
var cmd *exec.Cmd

Expand All @@ -38,13 +42,13 @@ func checkImport(sourceType config.SourceType) string {
err := cmd.Run()

if err != nil {
log.Fatal(err)
return "",err
}
lines := strings.Split(out.String(), "\n")
return strings.TrimSpace(lines[0])
return strings.TrimSpace(lines[0]),nil
}

func checkExport(sourceType config.SourceType) string {
func checkExport(sourceType config.SourceType) (string,error) {
var out bytes.Buffer
var cmd *exec.Cmd

Expand All @@ -56,9 +60,9 @@ func checkExport(sourceType config.SourceType) string {
err := cmd.Run()

if err != nil {
log.Fatal(err)
return "",err
}

lines := strings.Split(out.String(), "\n")
return strings.TrimSpace(lines[0])
return strings.TrimSpace(lines[0]),nil
}
2 changes: 1 addition & 1 deletion internal/detect_directories.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func mysqlBinaryDirectory() string {
connstr := fmt.Sprintf("%s:%s@/%s", "root", "deneme332", "deneme")
db, err := sql.Open("mysql", connstr)
if err != nil {
log.Fatalln(err)
log.Println(err)
}
err = db.Ping()
if err != nil {
Expand Down
64 changes: 46 additions & 18 deletions internal/postgres_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"github.com/sadihakan/dummy-dump/config"
"github.com/sadihakan/dummy-dump/util"
"path/filepath"
"testing"
Expand Down Expand Up @@ -29,51 +30,78 @@ func TestExport(t *testing.T) {
var dump Dump
dump = Postgres{}

user := "hakankosanoglu"
db := "test"

binaryPath := "pg_restore"
config := config.Config{
Source: "",
Import: false,
Export: true,
User: "hakankosanoglu",
Password: "",
Path: "",
DB: "test",
BinaryPath: "pg_dump",
}

dump.Export(binaryPath, user, db)
dump.Export(config)
}

func TestExportWithError(t *testing.T) {

var dump Dump
dump = Postgres{}

user := "none"
db := "test"

binaryPath := "pg_restore"
config := config.Config{
Source: "",
Import: false,
Export: true,
User: "none",
Password: "",
Path: "",
DB: "test",
BinaryPath: "pg_dump",
}

dump.Export(binaryPath, user, db)
dump.Export(config)
}

func TestImport(t *testing.T) {

var dump Dump
dump = Postgres{}

user := "hakankosanoglu"
file := filepath.Join(util.GetDirectory(), "test.backup")
db := "" //

binaryPath := "pg_dump"

dump.Import(binaryPath, user, db, file)
config := config.Config{
Source: "",
Import: true,
Export: false,
User: "hakankosanoglu",
Password: "",
Path: file,
DB: "",
BinaryPath: "pg_restore",
}

dump.Import(config)
}

func TestImportWithError(t *testing.T) {

var dump Dump
dump = Postgres{}

user := "hakankosanoglu"
file := "test"

binaryPath := "pg_dump"
config := config.Config{
Source: "",
Import: true,
Export: false,
User: "hakankosanoglu",
Password: "",
Path: file,
DB: "",
BinaryPath: "pg_restore",
}

db := "" //
dump.Import(binaryPath, user, db, file)
dump.Import(config)
}

0 comments on commit 2cea9c8

Please sign in to comment.