-
Notifications
You must be signed in to change notification settings - Fork 14
/
pg_restore_test.go
94 lines (88 loc) · 2.72 KB
/
pg_restore_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package pgcommands_test
import (
"fmt"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
pg "github.com/habx/pg-commands"
"github.com/habx/pg-commands/tests/fixtures"
)
func TestNewRestoreWrongCommand(t *testing.T) {
savePGRestoreCmd := pg.PGRestoreCmd
pg.PGRestoreCmd = "xxxx"
Convey("Create new dump with wrong command", t, func() {
restore, err := pg.NewRestore(fixtures.Setup())
So(err, ShouldNotBeNil)
So(restore, ShouldBeNil)
})
pg.PGRestoreCmd = savePGRestoreCmd
}
func TestNewRestore(t *testing.T) {
restore, err := pg.NewRestore(fixtures.Setup())
Convey("Create new restore", t, func() {
So(err, ShouldBeNil)
restore.SetPath("./")
restore.SetSchemas([]string{"public"})
So(restore.Options, ShouldNotBeEmpty)
So(restore.Verbose, ShouldBeFalse)
Convey("Public funcs", func() {
restore.ResetOptions()
So(restore.Options, ShouldBeEmpty)
restore.EnableVerbose()
So(restore.Verbose, ShouldBeTrue)
})
})
Convey("Create without binary", t, func() {
savePGRestoreCmd := pg.PGRestoreCmd
pg.PGRestoreCmd = ""
restore.ResetOptions()
restoreBad := restore.Exec("test", pg.ExecOptions{StreamPrint: false})
So(restoreBad.Error, ShouldNotBeNil)
pg.PGRestoreCmd = savePGRestoreCmd
})
}
func TestRestore(t *testing.T) {
pgSetup := fixtures.Setup()
dump, _ := pg.NewDump(pgSetup)
result := dump.Exec(pg.ExecOptions{StreamPrint: false})
Convey("Create standard restore", t, func() {
restore, err := pg.NewRestore(fixtures.Setup())
So(err, ShouldBeNil)
x := restore.Exec(result.File, pg.ExecOptions{StreamPrint: true, StreamDestination: os.Stdout})
So(x.Error, ShouldBeNil)
So(x.FullCommand, ShouldNotBeEmpty)
fmt.Println(x.FullCommand)
So(x.FullCommand, ShouldEqual, fmt.Sprintf(
"--no-owner --no-acl --clean --exit-on-error --dbname=%s --host=%s --port=%d --username=%s %s--schema=public %s",
pgSetup.DB,
pgSetup.Host,
pgSetup.Port,
pgSetup.Username,
func() string {
if restore.Role != "" {
return fmt.Sprintf("--role=%s ", restore.Role)
}
return ""
}(),
result.File))
restore.EnableVerbose()
restore.Role = "dev_example"
x = restore.Exec(result.File, pg.ExecOptions{StreamPrint: false})
So(x.Error, ShouldBeNil)
So(x.FullCommand, ShouldNotBeEmpty)
So(x.FullCommand, ShouldEqual, fmt.Sprintf(
"--no-owner --no-acl --clean --exit-on-error --dbname=%s --host=%s --port=%d --username=%s --role=%s -v --schema=public %s",
pgSetup.DB,
pgSetup.Host,
pgSetup.Port,
pgSetup.Username,
restore.Role,
result.File))
})
Convey("Create failed restore", t, func() {
restore, err := pg.NewRestore(&pg.Postgres{})
So(err, ShouldBeNil)
result := restore.Exec("ok", pg.ExecOptions{StreamPrint: false})
So(result.Error, ShouldNotBeNil)
})
}