github.com/go-kivik/kivik/v4@v4.3.2/couchdb/multiatt_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package couchdb 14 15 import ( 16 "testing" 17 18 "gitlab.com/flimzy/testy" 19 20 kivik "github.com/go-kivik/kivik/v4" 21 ) 22 23 type attStruct struct { 24 Attachments kivik.Attachments `json:"_attachments"` 25 } 26 27 type attPtrStruct struct { 28 Attachments *kivik.Attachments `json:"_attachments"` 29 } 30 31 type wrongTypeStruct struct { 32 Attachments string `json:"_attachments"` 33 } 34 35 type wrongTagStruct struct { 36 Attachments kivik.Attachments `json:"foo"` 37 } 38 39 func TestExtractAttachments(t *testing.T) { 40 tests := []struct { 41 name string 42 doc interface{} 43 44 expected *kivik.Attachments 45 ok bool 46 }{ 47 { 48 name: "no attachments", 49 doc: map[string]interface{}{"foo": "bar"}, 50 expected: nil, 51 ok: false, 52 }, 53 { 54 name: "in map", 55 doc: map[string]interface{}{"_attachments": kivik.Attachments{ 56 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 57 }}, 58 expected: &kivik.Attachments{ 59 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain"}, 60 }, 61 ok: true, 62 }, 63 { 64 name: "wrong type in map", 65 doc: map[string]interface{}{"_attachments": "oink"}, 66 expected: nil, 67 ok: false, 68 }, 69 { 70 name: "non standard map, non struct", 71 doc: map[string]string{"foo": "bar"}, 72 expected: nil, 73 ok: false, 74 }, 75 { 76 name: "attachments in struct", 77 doc: attStruct{ 78 Attachments: kivik.Attachments{ 79 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 80 }, 81 }, 82 expected: &kivik.Attachments{ 83 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain"}, 84 }, 85 ok: true, 86 }, 87 { 88 name: "pointer to attachments in struct", 89 doc: attPtrStruct{ 90 Attachments: &kivik.Attachments{ 91 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 92 }, 93 }, 94 expected: &kivik.Attachments{ 95 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain"}, 96 }, 97 ok: true, 98 }, 99 { 100 name: "wrong type of struct", 101 doc: wrongTypeStruct{ 102 Attachments: "foo", 103 }, 104 expected: nil, 105 ok: false, 106 }, 107 { 108 name: "wrong json tag", 109 doc: wrongTagStruct{ 110 Attachments: kivik.Attachments{ 111 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 112 }, 113 }, 114 expected: nil, 115 ok: false, 116 }, 117 { 118 name: "pointer to struct with attachments", 119 doc: &attStruct{ 120 Attachments: kivik.Attachments{ 121 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 122 }, 123 }, 124 expected: &kivik.Attachments{ 125 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain"}, 126 }, 127 ok: true, 128 }, 129 { 130 name: "pointer to map with attachments", 131 doc: &(map[string]interface{}{"_attachments": kivik.Attachments{ 132 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 133 }}), 134 expected: &kivik.Attachments{ 135 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain"}, 136 }, 137 ok: true, 138 }, 139 { 140 name: "pointer in map", 141 doc: map[string]interface{}{"_attachments": &kivik.Attachments{ 142 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain", Content: Body("test content")}, 143 }}, 144 expected: &kivik.Attachments{ 145 "foo.txt": &kivik.Attachment{Filename: "foo.txt", ContentType: "text/plain"}, 146 }, 147 ok: true, 148 }, 149 { 150 name: "nil doc", 151 doc: nil, 152 ok: false, 153 }, 154 } 155 for _, test := range tests { 156 t.Run(test.name, func(t *testing.T) { 157 result, ok := extractAttachments(test.doc) 158 if ok != test.ok { 159 t.Errorf("Unexpected OK: %v", ok) 160 } 161 if result != nil { 162 for _, att := range *result { 163 att.Content = nil 164 } 165 } 166 if d := testy.DiffInterface(test.expected, result); d != nil { 167 t.Error(d) 168 } 169 }) 170 } 171 }