This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
forked from derekdowling/jsh-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_storage.go
81 lines (62 loc) · 2.04 KB
/
mock_storage.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
package jshapi
import (
"log"
"strconv"
"github.com/derekdowling/go-json-spec-handler"
"golang.org/x/net/context"
)
// MockStorage allows you to mock out APIs really easily, and is also used internally
// for testing the API layer.
type MockStorage struct {
// ResourceType is the name of the resource you are mocking i.e. "user", "comment"
ResourceType string
// ResourceAttributes a sample set of attributes a resource object should have
// used by GET /resources and GET /resources/:id
ResourceAttributes interface{}
// ListCount is the number of sample objects to return in a GET /resources request
ListCount int
}
// Save assigns a URL of 1 to the object
func (m *MockStorage) Save(ctx context.Context, object *jsh.Object) (*jsh.Object, jsh.ErrorType) {
var err *jsh.Error
object.ID = "1"
return object, err
}
// Get returns a resource with ID as specified by the request
func (m *MockStorage) Get(ctx context.Context, id string) (*jsh.Object, jsh.ErrorType) {
var err *jsh.Error
return m.SampleObject(id), err
}
// List returns a sample list
func (m *MockStorage) List(ctx context.Context) (jsh.List, jsh.ErrorType) {
var err *jsh.Error
return m.SampleList(m.ListCount), err
}
// Update does nothing
func (m *MockStorage) Update(ctx context.Context, object *jsh.Object) (*jsh.Object, jsh.ErrorType) {
var err jsh.ErrorList
err = nil
return object, err
}
// Delete does nothing
func (m *MockStorage) Delete(ctx context.Context, id string) jsh.ErrorType {
var err *jsh.Error
return err
}
// SampleObject builds an object based on provided resource specifications
func (m *MockStorage) SampleObject(id string) *jsh.Object {
object, err := jsh.NewObject(id, m.ResourceType, m.ResourceAttributes)
if err != nil {
log.Fatal(err.Error())
}
return object
}
// SampleList generates a sample list of resources that can be used for/against the
// mock API
func (m *MockStorage) SampleList(length int) jsh.List {
list := jsh.List{}
for id := 1; id <= length; id++ {
list = append(list, m.SampleObject(strconv.Itoa(id)))
}
return list
}