forked from patrickrand/udocs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock_dao.go
46 lines (39 loc) · 867 Bytes
/
mock_dao.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
package storage
import (
"errors"
"path/filepath"
"strings"
)
// MockDao should only be used for testing purposes
type MockDao struct {
Dao
root string
pages map[string][]byte
}
func NewMockDao(root string) *MockDao {
return &MockDao{root: root, pages: make(map[string][]byte)}
}
func (m *MockDao) Insert(id string, data []byte) error {
m.pages[filepath.Join(m.root, id)] = data
return nil
}
func (m *MockDao) Index(id, title string, data []byte) error {
return nil
}
func (m *MockDao) Fetch(id string) ([]byte, error) {
data, ok := m.pages[filepath.Join(m.root, id)]
if !ok {
return nil, errors.New(id + " not found")
}
return data, nil
}
func (m *MockDao) FetchGlob(pattern string) []string {
var pages []string
for id := range m.pages {
if strings.HasPrefix(id, m.
root+pattern) {
pages = append(pages, id)
}
}
return pages
}