1
2
3
4
5
6
7
8
9
10
11
12
13 package cdb
14
15 import (
16 "errors"
17 "net/http"
18 "testing"
19
20 "gitlab.com/flimzy/testy"
21
22 "github.com/go-kivik/kivik/v4"
23 internal "github.com/go-kivik/kivik/v4/int/errors"
24 "github.com/go-kivik/kivik/v4/x/fsdb/filesystem"
25 )
26
27 func TestFSOpenDocID(t *testing.T) {
28 type tt struct {
29 fs filesystem.Filesystem
30 root string
31 docID string
32 options kivik.Option
33 status int
34 err string
35 }
36 tests := testy.NewTable()
37 tests.Add("not found", tt{
38 root: "notfound",
39 status: http.StatusNotFound,
40 err: "missing",
41 })
42 tests.Add("main rev only", tt{
43 root: "testdata/open",
44 docID: "foo",
45 })
46 tests.Add("main rev only, yaml", tt{
47 root: "testdata/open",
48 docID: "bar",
49 })
50 tests.Add("no id in doc", tt{
51 root: "testdata/open",
52 docID: "noid",
53 })
54 tests.Add("forbidden", tt{
55 fs: &filesystem.MockFS{
56 OpenFunc: func(_ string) (filesystem.File, error) {
57 return nil, statusError{status: http.StatusForbidden, error: errors.New("permission denied")}
58 },
59 },
60 root: "doesntmatter",
61 docID: "foo",
62 status: http.StatusForbidden,
63 err: "permission denied",
64 })
65 tests.Add("attachment", tt{
66 root: "testdata/open",
67 docID: "att",
68 })
69 tests.Add("attachments from multiple revs", tt{
70 root: "testdata/open",
71 docID: "splitatts",
72 })
73 tests.Add("no rev", tt{
74 root: "testdata/open",
75 docID: "norev",
76 })
77 tests.Add("no main rev", tt{
78 root: "testdata/open",
79 docID: "nomain",
80 })
81 tests.Add("json auto rev number", tt{
82 root: "testdata/open",
83 docID: "jsonautorevnum",
84 })
85 tests.Add("yaml auto rev number", tt{
86 root: "testdata/open",
87 docID: "yamlautorevnum",
88 })
89 tests.Add("json auto rev string", tt{
90 root: "testdata/open",
91 docID: "jsonautorevstr",
92 })
93 tests.Add("yaml auto rev string", tt{
94 root: "testdata/open",
95 docID: "yamlautorevstr",
96 })
97 tests.Add("multiple revs, winner selected", tt{
98 root: "testdata/open",
99 docID: "multiplerevs",
100 })
101
102 tests.Run(t, func(t *testing.T, tt tt) {
103 fs := New(tt.root, tt.fs)
104 opts := tt.options
105 if opts == nil {
106 opts = kivik.Params(nil)
107 }
108 result, err := fs.OpenDocID(tt.docID, opts)
109 if d := internal.StatusErrorDiff(tt.err, tt.status, err); d != "" {
110 t.Error(d)
111 }
112 if err != nil {
113 return
114 }
115 result.Options = map[string]interface{}{
116 "revs": true,
117 "attachments": true,
118 "header:accept": "application/json",
119 }
120 if d := testy.DiffAsJSON(testy.Snapshot(t), result); d != nil {
121 t.Error(d)
122 }
123 })
124 }
125
126 func TestRestoreAttachments(t *testing.T) {
127 type tt struct {
128 r *Revision
129 status int
130 err string
131 }
132 tests := testy.NewTable()
133 tests.Add("missing attachment", tt{
134 r: &Revision{
135 options: map[string]interface{}{
136 "attachments": true,
137 },
138 RevMeta: RevMeta{
139 fs: filesystem.Default(),
140 RevHistory: &RevHistory{},
141 Attachments: map[string]*Attachment{
142 "notfound.txt": {
143 fs: filesystem.Default(),
144 path: "/somewhere/notfound.txt",
145 },
146 },
147 },
148 },
149 status: http.StatusInternalServerError,
150 err: "attachment 'notfound.txt': missing",
151 })
152
153 tests.Run(t, func(t *testing.T, tt tt) {
154 err := tt.r.restoreAttachments()
155 if d := internal.StatusErrorDiff(tt.err, tt.status, err); d != "" {
156 t.Error(d)
157 }
158 })
159 }
160
View as plain text