scopes
2015-12-13
scopes/scope_test.go
Port over Postgres storage and tests. Do the minimum possible port of the code from auth to get Postgres working and make the tests run and pass again. This leaves a bug where the ErrScopeAlreadyExists type, when populatd from Postgres, contains the entire error string returned from the database, instead of parsing the ID itself out. Which is a thing we should do and add a test for.
| paddy@2 | 1 package scopes |
| paddy@2 | 2 |
| paddy@2 | 3 import ( |
| paddy@2 | 4 "os" |
| paddy@2 | 5 |
| paddy@2 | 6 "golang.org/x/net/context" |
| paddy@2 | 7 |
| paddy@2 | 8 "code.secondbit.org/scopes.hg/types" |
| paddy@2 | 9 ) |
| paddy@2 | 10 import "testing" |
| paddy@2 | 11 |
| paddy@2 | 12 func init() { |
| paddy@2 | 13 if os.Getenv("PG_TEST_DB") != "" { |
| paddy@2 | 14 p, err := NewPostgres(os.Getenv("PG_TEST_DB")) |
| paddy@2 | 15 if err != nil { |
| paddy@2 | 16 panic(err) |
| paddy@2 | 17 } |
| paddy@2 | 18 storers = append(storers, &p) |
| paddy@2 | 19 } |
| paddy@2 | 20 } |
| paddy@2 | 21 |
| paddy@2 | 22 var storers = []Storer{NewMemstore()} |
| paddy@2 | 23 |
| paddy@2 | 24 func compareScopes(scope1, scope2 scopeTypes.Scope) (success bool, field string, val1, val2 interface{}) { |
| paddy@2 | 25 if scope1.ID != scope2.ID { |
| paddy@2 | 26 return false, "ID", scope1.ID, scope2.ID |
| paddy@2 | 27 } |
| paddy@2 | 28 if scope1.Name != scope2.Name { |
| paddy@2 | 29 return false, "Name", scope1.Name, scope2.Name |
| paddy@2 | 30 } |
| paddy@2 | 31 if scope1.Description != scope2.Description { |
| paddy@2 | 32 return false, "Description", scope1.Description, scope2.Description |
| paddy@2 | 33 } |
| paddy@2 | 34 return true, "", nil, nil |
| paddy@2 | 35 } |
| paddy@2 | 36 |
| paddy@2 | 37 func TestScopeInScopeStore(t *testing.T) { |
| paddy@2 | 38 scope := scopeTypes.Scope{ |
| paddy@2 | 39 ID: "testscope", |
| paddy@2 | 40 Name: "Test Scope", |
| paddy@2 | 41 Description: "Access to testing data.", |
| paddy@2 | 42 } |
| paddy@2 | 43 scope2 := scopeTypes.Scope{ |
| paddy@2 | 44 ID: "testscope2", |
| paddy@2 | 45 Name: "Test Scope 2", |
| paddy@2 | 46 Description: "Access to minions.", |
| paddy@2 | 47 } |
| paddy@2 | 48 scope3 := scopeTypes.Scope{ |
| paddy@2 | 49 ID: "testscope3", |
| paddy@2 | 50 Name: "Test Scope 3", |
| paddy@2 | 51 Description: "Access to bananas.", |
| paddy@2 | 52 } |
| paddy@2 | 53 updatedName := "Updated Scope" |
| paddy@2 | 54 updatedDescription := "An updated scope." |
| paddy@2 | 55 update := scopeTypes.ScopeChange{ |
| paddy@2 | 56 ScopeID: scope.ID, |
| paddy@2 | 57 Name: &updatedName, |
| paddy@2 | 58 } |
| paddy@2 | 59 update2 := scopeTypes.ScopeChange{ |
| paddy@2 | 60 ScopeID: scope2.ID, |
| paddy@2 | 61 Description: &updatedDescription, |
| paddy@2 | 62 } |
| paddy@2 | 63 update3 := scopeTypes.ScopeChange{ |
| paddy@2 | 64 ScopeID: scope3.ID, |
| paddy@2 | 65 Name: &updatedName, |
| paddy@2 | 66 Description: &updatedDescription, |
| paddy@2 | 67 } |
| paddy@2 | 68 for _, store := range storers { |
| paddy@2 | 69 ctx := context.Background() |
| paddy@2 | 70 retrieved, err := store.GetScopes([]string{scope.ID}, ctx) |
| paddy@2 | 71 if len(retrieved) != 0 { |
| paddy@2 | 72 t.Logf("%+v", retrieved) |
| paddy@2 | 73 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store) |
| paddy@2 | 74 } |
| paddy@2 | 75 err = store.CreateScopes([]scopeTypes.Scope{scope}, ctx) |
| paddy@2 | 76 if err != nil { |
| paddy@2 | 77 t.Errorf("Error saving scope to %T: %s", store, err) |
| paddy@2 | 78 } |
| paddy@2 | 79 err = store.CreateScopes([]scopeTypes.Scope{scope}, ctx) |
| paddy@2 | 80 if _, ok := err.(ErrScopeAlreadyExists); !ok { |
| paddy@2 | 81 t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store) |
| paddy@2 | 82 } |
| paddy@2 | 83 retrieved, err = store.GetScopes([]string{scope.ID}, ctx) |
| paddy@2 | 84 if err != nil { |
| paddy@2 | 85 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) |
| paddy@2 | 86 } |
| paddy@2 | 87 if len(retrieved) != 1 { |
| paddy@2 | 88 t.Logf("%+v", retrieved) |
| paddy@2 | 89 t.Errorf("Expected %d results, got %d from %T", 1, len(retrieved), store) |
| paddy@2 | 90 } |
| paddy@2 | 91 if _, ok := retrieved[scope.ID]; !ok { |
| paddy@2 | 92 t.Logf("%+v", retrieved) |
| paddy@2 | 93 t.Errorf("Expected %s to be in results from %T, wasn't", scope.ID, store) |
| paddy@2 | 94 } |
| paddy@2 | 95 success, field, val1, val2 := compareScopes(scope, retrieved[scope.ID]) |
| paddy@2 | 96 if !success { |
| paddy@2 | 97 t.Errorf("Expected %s to be %+v, got %+v from %T", field, val1, val2, store) |
| paddy@2 | 98 } |
| paddy@2 | 99 err = store.CreateScopes([]scopeTypes.Scope{scope2, scope3}, ctx) |
| paddy@2 | 100 if err != nil { |
| paddy@2 | 101 t.Errorf("Unexpected error trying to create scope2 and scope3 in %T: %+v", store, err) |
| paddy@2 | 102 } |
| paddy@2 | 103 retrieved, err = store.GetScopes([]string{scope.ID, scope2.ID, scope3.ID}, ctx) |
| paddy@2 | 104 if err != nil { |
| paddy@2 | 105 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) |
| paddy@2 | 106 } |
| paddy@2 | 107 if len(retrieved) != 3 { |
| paddy@2 | 108 t.Logf("%+v", retrieved) |
| paddy@2 | 109 t.Errorf("Expected %d results, got %d from %T", 3, len(retrieved), store) |
| paddy@2 | 110 } |
| paddy@2 | 111 for _, s := range []scopeTypes.Scope{scope, scope2, scope3} { |
| paddy@2 | 112 if _, ok := retrieved[s.ID]; !ok { |
| paddy@2 | 113 t.Logf("%+v", retrieved) |
| paddy@2 | 114 t.Errorf("Expected %s to be in results from %T, wasn't", s.ID, store) |
| paddy@2 | 115 } |
| paddy@2 | 116 success, field, val1, val2 = compareScopes(s, retrieved[s.ID]) |
| paddy@2 | 117 if !success { |
| paddy@2 | 118 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store) |
| paddy@2 | 119 } |
| paddy@2 | 120 } |
| paddy@2 | 121 err = store.UpdateScope(update, ctx) |
| paddy@2 | 122 if err != nil { |
| paddy@2 | 123 t.Errorf("Unexpected error updating scope in %T: %+v", store, err) |
| paddy@2 | 124 } |
| paddy@2 | 125 scope = scopeTypes.ApplyChange(update, scope) |
| paddy@2 | 126 err = store.UpdateScope(update2, ctx) |
| paddy@2 | 127 if err != nil { |
| paddy@2 | 128 t.Errorf("Unexpected error updating scope in %T: %+v", store, err) |
| paddy@2 | 129 } |
| paddy@2 | 130 scope2 = scopeTypes.ApplyChange(update2, scope2) |
| paddy@2 | 131 err = store.UpdateScope(update3, ctx) |
| paddy@2 | 132 if err != nil { |
| paddy@2 | 133 t.Errorf("Unexpected error updating scope in %T: %+v", store, err) |
| paddy@2 | 134 } |
| paddy@2 | 135 scope3 = scopeTypes.ApplyChange(update3, scope3) |
| paddy@2 | 136 results, err := store.ListScopes(ctx) |
| paddy@2 | 137 if err != nil { |
| paddy@2 | 138 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) |
| paddy@2 | 139 } |
| paddy@2 | 140 if len(results) != 3 { |
| paddy@2 | 141 t.Logf("%+v", results) |
| paddy@2 | 142 t.Errorf("Expected %d results, got %d from %T", 3, len(results), store) |
| paddy@2 | 143 } |
| paddy@2 | 144 for pos, s := range []scopeTypes.Scope{scope, scope2, scope3} { |
| paddy@2 | 145 success, field, val1, val2 = compareScopes(s, results[pos]) |
| paddy@2 | 146 if !success { |
| paddy@2 | 147 t.Errorf("Expected %s to be %+v for scope %s, got %+v from %T", field, val1, s.ID, val2, store) |
| paddy@2 | 148 } |
| paddy@2 | 149 } |
| paddy@2 | 150 err = store.RemoveScopes([]string{scope.ID, scope2.ID, scope3.ID}, ctx) |
| paddy@2 | 151 if err != nil { |
| paddy@2 | 152 t.Errorf("Unexpected error removing scopes from %T: %+v", store, err) |
| paddy@2 | 153 } |
| paddy@2 | 154 results, err = store.ListScopes(ctx) |
| paddy@2 | 155 if err != nil { |
| paddy@2 | 156 t.Errorf("Unexpected error retrieving scopes from %T: %+v", store, err) |
| paddy@2 | 157 } |
| paddy@2 | 158 if len(results) != 0 { |
| paddy@2 | 159 t.Logf("%+v", results) |
| paddy@2 | 160 t.Errorf("Expected %d results, got %d from %T", 0, len(results), store) |
| paddy@2 | 161 } |
| paddy@2 | 162 err = store.UpdateScope(update, ctx) |
| paddy@2 | 163 if err != ErrScopeNotFound { |
| paddy@2 | 164 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err) |
| paddy@2 | 165 } |
| paddy@2 | 166 } |
| paddy@2 | 167 } |