scopes

Paddy 2015-12-13

2:a64a25ae2db1 Go to Latest

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.

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