github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/pg_helper_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 ) 8 9 func TestHpgOptNames(t *testing.T) { 10 expected := []string{"fork", "follow", "rollback"} 11 if !reflect.DeepEqual(hpgOptNames, expected) { 12 t.Fatalf("expected hpgOptNames to be %v, got %v", expected, hpgOptNames) 13 } 14 } 15 16 var hpgOptResolveTests = []struct { 17 opts *map[string]string 18 env map[string]string 19 result map[string]string 20 err error 21 }{ 22 { 23 opts: &map[string]string{"fork": "yellow"}, 24 env: map[string]string{"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com"}, 25 result: map[string]string{"fork": "postgres://test.com"}, 26 err: nil, 27 }, 28 { 29 opts: &map[string]string{ 30 "fork": "HEROKU_POSTGRESQL_ORANGE_URL", 31 "follow": "orange", 32 "rollback": "heroku-postgresql-yellow", 33 "ignore": "yes", 34 }, 35 env: map[string]string{ 36 "HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com", 37 "HEROKU_POSTGRESQL_ORANGE_URL": "postgres://test.com/orange", 38 }, 39 result: map[string]string{ 40 "fork": "postgres://test.com/orange", 41 "follow": "postgres://test.com/orange", 42 "rollback": "postgres://test.com", 43 "ignore": "yes", 44 }, 45 err: nil, 46 }, 47 { 48 opts: &map[string]string{"fork": "nope"}, 49 env: map[string]string{"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com"}, 50 result: nil, 51 err: fmt.Errorf("could not resolve fork option \"nope\" to a heroku-postgresql addon"), 52 }, 53 { 54 opts: &map[string]string{"fork": "postgres://test.com/1", "ignore": "yes"}, 55 env: map[string]string{"HEROKU_POSTGRESQL_YELLOW_URL": "postgres://test.com"}, 56 result: map[string]string{"fork": "postgres://test.com/1", "ignore": "yes"}, 57 err: nil, 58 }, 59 { 60 opts: nil, 61 env: nil, 62 result: nil, 63 err: nil, 64 }, 65 } 66 67 func TestHpgAddonOptResolve(t *testing.T) { 68 for i, unit := range hpgOptResolveTests { 69 err := hpgAddonOptResolve(unit.opts, unit.env) 70 if unit.err != nil { 71 if !reflect.DeepEqual(unit.err, err) { 72 t.Fatalf("test %d: expected error %v on, got %v", i, unit.err, err) 73 } 74 } else if unit.opts != nil { 75 if !reflect.DeepEqual(*unit.opts, unit.result) { 76 t.Errorf("test %d: expected %v, got %v", i, unit.result, *unit.opts) 77 } 78 } 79 } 80 } 81 82 func TestPgEnvToDBName(t *testing.T) { 83 tests := []struct { 84 in string 85 out string 86 }{ 87 {"HEROKU_POSTGRESQL_BLUE_URL", "heroku-postgresql-blue"}, 88 {"HEROKU_POSTGRESQL_CRIMSON_URL", "heroku-postgresql-crimson"}, 89 } 90 for _, ex := range tests { 91 result := pgEnvToDBName(ex.in) 92 if result != ex.out { 93 t.Errorf("expected %s, got %s", ex.out, result) 94 } 95 } 96 } 97 98 func TestDBNameToPgEnv(t *testing.T) { 99 tests := []struct { 100 in string 101 out string 102 }{ 103 {"heroku-postgresql-blue", "HEROKU_POSTGRESQL_BLUE_URL"}, 104 {"heroku-postgresql-crimson", "HEROKU_POSTGRESQL_CRIMSON_URL"}, 105 {"rose", "HEROKU_POSTGRESQL_ROSE_URL"}, 106 {"HEROKU_POSTGRESQL_ROSE", "HEROKU_POSTGRESQL_ROSE_URL"}, 107 {"HEROKU_POSTGRESQL_CRIMSON_URL", "HEROKU_POSTGRESQL_CRIMSON_URL"}, 108 } 109 for _, ex := range tests { 110 result := dbNameToPgEnv(ex.in) 111 if result != ex.out { 112 t.Errorf("expected %s, got %s", ex.out, result) 113 } 114 } 115 }