forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg_test.go
More file actions
116 lines (89 loc) · 2.86 KB
/
pkg_test.go
File metadata and controls
116 lines (89 loc) · 2.86 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package pgpkg
import (
"errors"
"fmt"
"os"
"testing"
)
var dsn = os.Getenv("PGPKG_DSN")
func applyProject(dsn string, commit bool, pkgPath string) error {
Options.DryRun = !commit
p, err := NewProjectFrom(pkgPath)
if err != nil {
return fmt.Errorf("unable to open project %s: %w", pkgPath, err)
}
err = p.Migrate(dsn)
if err != nil {
return fmt.Errorf("unable to migrate project %s: %w", pkgPath, err)
}
return nil
}
// Install or migrate a pgpkg package. Some test packages are designed to test that pgpkg
// fails in some circumstances. Set `expectFailure` to return success when they do fail, and
// to return failure if they don't.
func testProject(t *testing.T, dsn string, commit bool, expectFailure bool, pkgPath string) {
err := applyProject(dsn, commit, pkgPath)
// applyProject returns ErrDryRun if commit is not set. So "no error" means
// either err is nil, or it's a dry run error - and we expected it.
if err == nil || (errors.Is(err, ErrDryRun) && !commit) {
if expectFailure {
t.Fatal("test should have produced an error, but did not")
}
return
}
if err != nil && expectFailure {
return
}
t.Fatal(err)
}
func TestCrossSchema(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/cross-schema")
}
func TestDependencies(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/dependencies")
}
func TestComplexProject(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/gl")
}
func TestSchemaName(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/good-schema-name")
}
func TestObjects(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/objects")
}
func TestPassingTests(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/passing-tests")
}
func TestQuotedSchema(t *testing.T) {
testProject(t, dsn, false, false, "tests/good/quoted-schema")
}
func TestBadSchemaName(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/bad-schema-name")
}
func TestDuplicateMigrationName(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/duplicate-migration-name")
}
func TestBadEntitySyntax(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/entity-syntax")
}
func TestBadFailingTests(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/bad-failing-tests")
}
func TestBadFuncDuplicates(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/func-duplicates")
}
func TestBadFuncSyntax(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/func-syntax")
}
func TestBadFuncArgs(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/function-args")
}
func TestBadSQLSyntax(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/sql-syntax")
}
func TestBadTableRef(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/table-ref")
}
func TestBadTextException(t *testing.T) {
testProject(t, dsn, false, true, "tests/bad/test-exception")
}