forked from SkygearIO/skygear-server
/
skydbtest.go
468 lines (391 loc) · 12.6 KB
/
skydbtest.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package skydbtest
import (
"fmt"
"reflect"
"time"
"github.com/skygeario/skygear-server/pkg/server/skydb"
)
// MapConn is a naive memory implementation of skydb.Conn
type MapConn struct {
UserMap map[string]skydb.AuthInfo
AssetMap map[string]skydb.Asset
InternalPublicDB skydb.Database
recordAccessMap map[string]skydb.RecordACL
recordDefaultAccessMap map[string]skydb.RecordACL
fieldAccess skydb.FieldACL
skydb.Conn
}
// NewMapConn returns a new MapConn.
func NewMapConn() *MapConn {
return &MapConn{
UserMap: map[string]skydb.AuthInfo{},
recordAccessMap: map[string]skydb.RecordACL{},
recordDefaultAccessMap: map[string]skydb.RecordACL{},
fieldAccess: skydb.FieldACL{},
AssetMap: map[string]skydb.Asset{},
}
}
// CreateAuth creates a AuthInfo in UserMap.
func (conn *MapConn) CreateAuth(authinfo *skydb.AuthInfo) error {
if _, existed := conn.UserMap[authinfo.ID]; existed {
return skydb.ErrUserDuplicated
}
conn.UserMap[authinfo.ID] = *authinfo
return nil
}
// GetAuth returns a AuthInfo in UserMap.
func (conn *MapConn) GetAuth(id string, authinfo *skydb.AuthInfo) error {
u, ok := conn.UserMap[id]
if !ok {
return skydb.ErrUserNotFound
}
*authinfo = u
return nil
}
// GetAuthByPrincipalID returns a AuthInfo by its principalID.
func (conn *MapConn) GetAuthByPrincipalID(principalID string, authinfo *skydb.AuthInfo) error {
for _, u := range conn.UserMap {
if _, ok := u.ProviderInfo[principalID]; ok {
*authinfo = u
return nil
}
}
return skydb.ErrUserNotFound
}
// UpdateAuth updates an existing AuthInfo in UserMap.
func (conn *MapConn) UpdateAuth(authinfo *skydb.AuthInfo) error {
if _, ok := conn.UserMap[authinfo.ID]; !ok {
return skydb.ErrUserNotFound
}
conn.UserMap[authinfo.ID] = *authinfo
return nil
}
// DeleteAuth remove an existing in UserMap.
func (conn *MapConn) DeleteAuth(id string) error {
if _, ok := conn.UserMap[id]; !ok {
return skydb.ErrUserNotFound
}
delete(conn.UserMap, id)
return nil
}
// GetAdminRoles is not implemented.
func (conn *MapConn) GetAdminRoles() ([]string, error) {
return []string{
"admin",
}, nil
}
// SetAdminRoles is not implemented.
func (conn *MapConn) SetAdminRoles(roles []string) error {
panic("not implemented")
}
// GetDefaultRoles always return user for testing
func (conn *MapConn) GetDefaultRoles() ([]string, error) {
return []string{
"user",
}, nil
}
// SetDefaultRoles is not implemented.
func (conn *MapConn) SetDefaultRoles(roles []string) error {
panic("not implemented")
}
// SetRecordAccess sets record creation access
func (conn *MapConn) SetRecordAccess(recordType string, acl skydb.RecordACL) error {
conn.recordAccessMap[recordType] = acl
return nil
}
// SetRecordDefaultAccess sets record creation access
func (conn *MapConn) SetRecordDefaultAccess(recordType string, acl skydb.RecordACL) error {
conn.recordDefaultAccessMap[recordType] = acl
return nil
}
// GetRecordAccess returns record creation access of a specific type
func (conn *MapConn) GetRecordAccess(recordType string) (skydb.RecordACL, error) {
acl, gotIt := conn.recordAccessMap[recordType]
if !gotIt {
acl = skydb.NewRecordACL([]skydb.RecordACLEntry{})
}
return acl, nil
}
// GetRecordDefaultAccess returns record default access of a specific type
func (conn *MapConn) GetRecordDefaultAccess(recordType string) (skydb.RecordACL, error) {
acl, gotIt := conn.recordDefaultAccessMap[recordType]
if !gotIt {
return nil, nil
}
return acl, nil
}
// SetRecordFieldAccess sets record field access for all types
func (conn *MapConn) SetRecordFieldAccess(acl skydb.FieldACL) error {
conn.fieldAccess = acl
return nil
}
// GetRecordFieldAccess returns record field access for all types
func (conn *MapConn) GetRecordFieldAccess() (skydb.FieldACL, error) {
return conn.fieldAccess, nil
}
// GetAsset is not implemented.
func (conn *MapConn) GetAsset(name string, asset *skydb.Asset) error {
panic("not implemented")
}
// SaveAsset is not implemented.
func (conn *MapConn) SaveAsset(asset *skydb.Asset) error {
panic("not implemented")
}
// GetAssets always returns empty array.
func (conn *MapConn) GetAssets(names []string) ([]skydb.Asset, error) {
assets := []skydb.Asset{}
for _, v := range names {
asset, ok := conn.AssetMap[v]
if ok {
assets = append(assets, asset)
}
}
return assets, nil
}
// QueryRelation is not implemented.
func (conn *MapConn) QueryRelation(user string, name string, direction string, config skydb.QueryConfig) []skydb.AuthInfo {
panic("not implemented")
}
// QueryRelationCount is not implemented.
func (conn *MapConn) QueryRelationCount(user string, name string, direction string) (uint64, error) {
panic("not implemented")
}
// AddRelation is not implemented.
func (conn *MapConn) AddRelation(user string, name string, targetUser string) error {
panic("not implemented")
}
// RemoveRelation is not implemented.
func (conn *MapConn) RemoveRelation(user string, name string, targetUser string) error {
panic("not implemented")
}
// GetDevice is not implemented.
func (conn *MapConn) GetDevice(id string, device *skydb.Device) error {
panic("not implemented")
}
// QueryDevicesByUser is not implemented.
func (conn *MapConn) QueryDevicesByUser(user string) ([]skydb.Device, error) {
panic("not implemented")
}
// SaveDevice is not implemented.
func (conn *MapConn) SaveDevice(device *skydb.Device) error {
panic("not implemented")
}
// DeleteDevice is not implemented.
func (conn *MapConn) DeleteDevice(id string) error {
panic("not implemented")
}
// DeleteDevicesByToken is not implemented.
func (conn *MapConn) DeleteDevicesByToken(token string, t time.Time) error {
panic("not implemented")
}
// DeleteEmptyDevicesByTime is not implemented.
func (conn *MapConn) DeleteEmptyDevicesByTime(t time.Time) error {
panic("not implemented")
}
// PublicDB is not implemented.
func (conn *MapConn) PublicDB() skydb.Database {
return conn.InternalPublicDB
}
// PrivateDB is not implemented.
func (conn *MapConn) PrivateDB(userKey string) skydb.Database {
panic("not implemented")
}
// Subscribe is not implemented.
func (conn *MapConn) Subscribe(recordEventChan chan skydb.RecordEvent) error {
panic("not implemented")
}
func (conn *MapConn) EnsureAuthRecordKeysValid(authRecordKeys [][]string) error {
return nil
}
// Close does nothing.
func (conn *MapConn) Close() error {
// do nothing
return nil
}
// RecordMap is a string=>Record map
type RecordMap map[string]skydb.Record
// SubscriptionMap is a string=>Subscription map
type SubscriptionMap map[string]skydb.Subscription
// RecordSchemaMap is a string=>RecordSchema map
type RecordSchemaMap map[string]skydb.RecordSchema
//recordType string, acl RecordACL
// MapDB is a naive memory implementation of skydb.Database.
type MapDB struct {
RecordMap RecordMap
SubscriptionMap SubscriptionMap
RecordSchemaMap RecordSchemaMap
DBConn skydb.Conn
skydb.Database
}
// NewMapDB returns a new MapDB ready for use.
func NewMapDB() *MapDB {
return &MapDB{
RecordMap: RecordMap{},
SubscriptionMap: SubscriptionMap{},
RecordSchemaMap: RecordSchemaMap{},
DBConn: &MapConn{},
}
}
func (db *MapDB) IsReadOnly() bool { return false }
func (db *MapDB) DatabaseType() skydb.DatabaseType { return skydb.PublicDatabase }
// ID returns a mock Database ID.
func (db *MapDB) ID() string {
return ""
}
func (db *MapDB) UserRecordType() string {
return "user"
}
// Get returns a Record from RecordMap.
func (db *MapDB) Get(id skydb.RecordID, record *skydb.Record) error {
r, ok := db.RecordMap[id.String()]
if !ok {
return skydb.ErrRecordNotFound
}
*record = r
return nil
}
// Save assigns Record to RecordMap.
func (db *MapDB) Save(record *skydb.Record) error {
recordID := record.ID.String()
if origRecord, ok := db.RecordMap[recordID]; ok {
// keep the meta-data of record, only update record.Data
origRecordMergedCopy := origRecord.MergedCopy(record)
record.Apply(&origRecordMergedCopy)
}
db.RecordMap[recordID] = *record
return nil
}
// Delete remove the specified key from RecordMap.
func (db *MapDB) Delete(id skydb.RecordID) error {
_, ok := db.RecordMap[id.String()]
if !ok {
return skydb.ErrRecordNotFound
}
delete(db.RecordMap, id.String())
return nil
}
// Query is not implemented.
func (db *MapDB) Query(query *skydb.Query) (*skydb.Rows, error) {
panic("skydbtest: MapDB.Query not supported")
}
// Extend store the type of the field.
func (db *MapDB) Extend(recordType string, schema skydb.RecordSchema) (bool, error) {
if _, ok := db.RecordSchemaMap[recordType]; ok {
for fieldName, fieldType := range schema {
if _, ok := db.RecordSchemaMap[recordType][fieldName]; ok {
ft := db.RecordSchemaMap[recordType][fieldName]
if !reflect.DeepEqual(ft, fieldType) {
return false, fmt.Errorf("Wrong type")
}
}
db.RecordSchemaMap[recordType][fieldName] = fieldType
}
} else {
db.RecordSchemaMap[recordType] = schema
}
return true, nil
}
func (db *MapDB) RenameSchema(recordType, oldColumnName, newColumnName string) error {
if _, ok := db.RecordSchemaMap[recordType]; !ok {
return fmt.Errorf("record type %s does not exist", recordType)
}
if _, ok := db.RecordSchemaMap[recordType][oldColumnName]; !ok {
return fmt.Errorf("column %s does not exist", oldColumnName)
}
if _, ok := db.RecordSchemaMap[recordType][newColumnName]; ok {
if !reflect.DeepEqual(
db.RecordSchemaMap[recordType][oldColumnName],
db.RecordSchemaMap[recordType][newColumnName],
) {
return fmt.Errorf("column type conflict")
}
}
db.RecordSchemaMap[recordType][newColumnName] = db.RecordSchemaMap[recordType][oldColumnName]
delete(db.RecordSchemaMap[recordType], oldColumnName)
return nil
}
func (db *MapDB) DeleteSchema(recordType, columnName string) error {
if _, ok := db.RecordSchemaMap[recordType]; !ok {
return fmt.Errorf("record type %s does not exist", recordType)
}
if _, ok := db.RecordSchemaMap[recordType][columnName]; !ok {
return fmt.Errorf("column %s does not exist", columnName)
}
delete(db.RecordSchemaMap[recordType], columnName)
return nil
}
// GetSchema returns the record schema of a record type
func (db *MapDB) GetSchema(recordType string) (skydb.RecordSchema, error) {
if _, ok := db.RecordSchemaMap[recordType]; !ok {
return nil, fmt.Errorf("record type %s does not exist", recordType)
}
return db.RecordSchemaMap[recordType], nil
}
// GetRecordSchemas returns a list of all existing record type
func (db *MapDB) GetRecordSchemas() (map[string]skydb.RecordSchema, error) {
return db.RecordSchemaMap, nil
}
// GetSubscription return a Subscription from SubscriptionMap.
func (db *MapDB) GetSubscription(name string, deviceID string, subscription *skydb.Subscription) error {
s, ok := db.SubscriptionMap[deviceID+"/"+name]
if !ok {
return skydb.ErrSubscriptionNotFound
}
*subscription = s
return nil
}
// SaveSubscription assigns to SubscriptionMap.
func (db *MapDB) SaveSubscription(subscription *skydb.Subscription) error {
db.SubscriptionMap[subscription.DeviceID+"/"+subscription.ID] = *subscription
return nil
}
// DeleteSubscription deletes the specified key from SubscriptionMap.
func (db *MapDB) DeleteSubscription(name string, deviceID string) error {
key := deviceID + "/" + name
_, ok := db.SubscriptionMap[key]
if !ok {
return skydb.ErrSubscriptionNotFound
}
delete(db.SubscriptionMap, key)
return nil
}
// MockTxDatabase implements and records TxDatabase's methods and delegates other
// calls to underlying Database
type MockTxDatabase struct {
DidBegin, DidCommit, DidRollback bool
skydb.Database
}
func NewMockTxDatabase(backingDB skydb.Database) *MockTxDatabase {
return &MockTxDatabase{Database: backingDB}
}
func (db *MockTxDatabase) Begin() error {
db.DidBegin = true
return nil
}
func (db *MockTxDatabase) Commit() error {
db.DidCommit = true
return nil
}
func (db *MockTxDatabase) Rollback() error {
db.DidRollback = true
return nil
}
var _ skydb.TxDatabase = &MockTxDatabase{}
var (
_ skydb.Conn = NewMapConn()
_ skydb.Database = NewMapDB()
_ skydb.TxDatabase = &MockTxDatabase{}
)