This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
pg_helper_test.go
115 lines (109 loc) · 3 KB
/
pg_helper_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"reflect"
"testing"
)
func TestHpgOptNames(t *testing.T) {
expected := []string{"fork", "follow", "rollback"}
if !reflect.DeepEqual(hpgOptNames, expected) {
t.Fatalf("expected hpgOptNames to be %v, got %v", expected, hpgOptNames)
}
}
var hpgOptResolveTests = []struct {
opts *map[string]string
env map[string]string
result map[string]string
err error
}{
{
opts: &map[string]string{"fork": "yellow"},
env: map[string]string{"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com"},
result: map[string]string{"fork": "postgres://test.com"},
err: nil,
},
{
opts: &map[string]string{
"fork": "HEROKU_POSTGRESQL_ORANGE_URL",
"follow": "orange",
"rollback": "heroku-postgresql-yellow",
"ignore": "yes",
},
env: map[string]string{
"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com",
"HEROKU_POSTGRESQL_ORANGE_URL": "postgres://test.com/orange",
},
result: map[string]string{
"fork": "postgres://test.com/orange",
"follow": "postgres://test.com/orange",
"rollback": "postgres://test.com",
"ignore": "yes",
},
err: nil,
},
{
opts: &map[string]string{"fork": "nope"},
env: map[string]string{"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com"},
result: nil,
err: fmt.Errorf("could not resolve fork option \"nope\" to a heroku-postgresql addon"),
},
{
opts: &map[string]string{"fork": "postgres://test.com/1", "ignore": "yes"},
env: map[string]string{"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com"},
result: map[string]string{"fork": "postgres://test.com/1", "ignore": "yes"},
err: nil,
},
{
opts: nil,
env: nil,
result: nil,
err: nil,
},
}
func TestHpgAddonOptResolve(t *testing.T) {
for i, unit := range hpgOptResolveTests {
err := hpgAddonOptResolve(unit.opts, unit.env)
if unit.err != nil {
if !reflect.DeepEqual(unit.err, err) {
t.Fatalf("test %d: expected error %v on, got %v", i, unit.err, err)
}
} else if unit.opts != nil {
if !reflect.DeepEqual(*unit.opts, unit.result) {
t.Errorf("test %d: expected %v, got %v", i, unit.result, *unit.opts)
}
}
}
}
func TestPgEnvToDBName(t *testing.T) {
tests := []struct {
in string
out string
}{
{"HEROKU_POSTGRESQL_BLUE_URL", "heroku-postgresql-blue"},
{"HEROKU_POSTGRESQL_CRIMSON_URL", "heroku-postgresql-crimson"},
}
for _, ex := range tests {
result := pgEnvToDBName(ex.in)
if result != ex.out {
t.Errorf("expected %s, got %s", ex.out, result)
}
}
}
func TestDBNameToPgEnv(t *testing.T) {
tests := []struct {
in string
out string
}{
{"heroku-postgresql-blue", "HEROKU_POSTGRESQL_BLUE_URL"},
{"heroku-postgresql-crimson", "HEROKU_POSTGRESQL_CRIMSON_URL"},
{"rose", "HEROKU_POSTGRESQL_ROSE_URL"},
{"HEROKU_POSTGRESQL_ROSE", "HEROKU_POSTGRESQL_ROSE_URL"},
{"HEROKU_POSTGRESQL_CRIMSON_URL", "HEROKU_POSTGRESQL_CRIMSON_URL"},
}
for _, ex := range tests {
result := dbNameToPgEnv(ex.in)
if result != ex.out {
t.Errorf("expected %s, got %s", ex.out, result)
}
}
}