auth
auth/scope_test.go
Fix tests for scopeStore. Update all our tests to use the PG_TEST_DB environment variable if set, and use that to control whether or not the postgres tests get run. testing.Short() just wasn't working. Update ErrScopeNotFound and ErrScopeAlreadyExists to be variables instead of types, because PostgreSQL (annoyingly) offers no way to determine which specific row insertion caused the problem, and I anticipate this being a problem that is ongoing. So it's really just not worth it. Stop getScopes from returning an ErrScopeNotFound. Let's return what we find, and let the absence of what we didn't find speak for itself. Fix an error with generating the SQL for the postgres.createScopes call. We used to generate it in a way that was invalid (not joining values with ",") when more than one set of values was supplied. Hooray, testing! Update the postgres scopeStore to return ErrScopeNotFound and ErrScopeAlreadyExists errors, as appropriate. Update our tests to reflect that ErrScopeNotFound and ErrScopeAlreadyExists are now variables, not types.
1.1 --- a/scope_test.go Tue Mar 24 20:39:03 2015 -0400 1.2 +++ b/scope_test.go Tue Mar 24 21:50:42 2015 -0400 1.3 @@ -1,7 +1,18 @@ 1.4 package auth 1.5 1.6 +import "os" 1.7 import "testing" 1.8 1.9 +func init() { 1.10 + if os.Getenv("PG_TEST_DB") != "" { 1.11 + p, err := NewPostgres(os.Getenv("PG_TEST_DB")) 1.12 + if err != nil { 1.13 + panic(err) 1.14 + } 1.15 + scopeStores = append(scopeStores, &p) 1.16 + } 1.17 +} 1.18 + 1.19 var scopeStores = []scopeStore{NewMemstore()} 1.20 1.21 func compareScopes(scope1, scope2 Scope) (success bool, field string, val1, val2 interface{}) { 1.22 @@ -52,30 +63,13 @@ 1.23 t.Logf("%+v", retrieved) 1.24 t.Errorf("Expected %d results, got %d from %T", 0, len(retrieved), store) 1.25 } 1.26 - if e, ok := err.(ErrScopeNotFound); !ok { 1.27 - t.Errorf("Expected ErrScopeNotFound, got %+v instead for %T", err, store) 1.28 - } else { 1.29 - if e.Pos != 0 { 1.30 - t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store) 1.31 - } 1.32 - if e.ID != scope.ID { 1.33 - t.Errorf("Expected the error to be with scope %s, got %s from %T", scope.ID, e.ID, store) 1.34 - } 1.35 - } 1.36 err = context.CreateScopes([]Scope{scope}) 1.37 if err != nil { 1.38 t.Errorf("Error saving scope to %T: %s", store, err) 1.39 } 1.40 err = context.CreateScopes([]Scope{scope}) 1.41 - if e, ok := err.(ErrScopeAlreadyExists); !ok { 1.42 + if err != ErrScopeAlreadyExists { 1.43 t.Errorf("Expected ErrScopeAlreadyExists, got %s instead for %T", err, store) 1.44 - } else { 1.45 - if e.Pos != 0 { 1.46 - t.Errorf("Expected the error to be in position %d, got position %d from %T", 0, e.Pos, store) 1.47 - } 1.48 - if e.ID != scope.ID { 1.49 - t.Errorf("Expected the error to be for ID %s, got %s from %T", scope.ID, e.ID, store) 1.50 - } 1.51 } 1.52 retrieved, err = context.GetScopes([]string{scope.ID}) 1.53 if err != nil { 1.54 @@ -152,41 +146,12 @@ 1.55 if err == nil { 1.56 t.Errorf("No error returned removing non-existent scopes from %T", store) 1.57 } 1.58 - if e, ok := err.(ErrScopeNotFound); !ok { 1.59 + if err != ErrScopeNotFound { 1.60 t.Errorf("Unexpected error removing non-existent scopes from %T: %+v", store, err) 1.61 - } else { 1.62 - if e.Pos != 0 { 1.63 - t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store) 1.64 - } 1.65 - if e.ID != scope.ID { 1.66 - t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store) 1.67 - } 1.68 } 1.69 err = context.UpdateScope(scope.ID, update) 1.70 - if err == nil { 1.71 - t.Errorf("No error returned updating non-existent scopes from %T", store) 1.72 - } 1.73 - if e, ok := err.(ErrScopeNotFound); !ok { 1.74 + if err != ErrScopeNotFound { 1.75 t.Errorf("Unexpected error updating non-existent scopes from %T: %+v", store, err) 1.76 - } else { 1.77 - if e.Pos != 0 { 1.78 - t.Errorf("Expected error to be for scope ID in pos %d, but got %d from %T", 0, e.Pos, store) 1.79 - } 1.80 - if e.ID != scope.ID { 1.81 - t.Errorf("Expected error to be for scope ID %s, but got %s from %T", scope.ID, e.ID, store) 1.82 - } 1.83 } 1.84 } 1.85 } 1.86 - 1.87 -func TestScopeErrors(t *testing.T) { 1.88 - errors := map[string]error{ 1.89 - "scope test couldn't be found": ErrScopeNotFound{ID: "test", Pos: 0}, 1.90 - "scope test already exists": ErrScopeAlreadyExists{ID: "test", Pos: 0}, 1.91 - } 1.92 - for expectation, e := range errors { 1.93 - if e.Error() != expectation { 1.94 - t.Errorf("Expected %+v to produce '%s', produced '%s'", e, expectation, e.Error()) 1.95 - } 1.96 - } 1.97 -}