Skip to content

Commit 7ded498

Browse files
authored
CBG-4280 Improve test assertions for attachments (#7637)
1 parent e405ca5 commit 7ded498

13 files changed

+899
-937
lines changed

base/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ const (
203203
ContentTypeJSON = "application/json"
204204
// ContentTypeFormEncoded is the content type for form-encoded requests.
205205
ContentTypeFormEncoded = "application/x-www-form-urlencoded"
206+
// ContentTypeOctetStream is for binary data
207+
ContentTypeOctetStream = "application/octet-stream"
206208
)
207209

208210
// UnitTestUrl returns the configured test URL.

base/util_testing.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import (
1919
"io"
2020
"io/fs"
2121
"log"
22+
"maps"
2223
"math/rand"
2324
"os"
2425
"path/filepath"
2526
"runtime"
2627
"runtime/pprof"
28+
"slices"
2729
"sort"
2830
"strconv"
2931
"strings"
@@ -1003,3 +1005,15 @@ func GetNonDefaultDatastoreNames(t testing.TB, bucket Bucket) []sgbucket.DataSto
10031005
func TestClusterSpec(t *testing.T) CouchbaseClusterSpec {
10041006
return GTestBucketPool.clusterSpec
10051007
}
1008+
1009+
// RequireKeysEqual asserts that a map has the expected keys.
1010+
func RequireKeysEqual[T any](t testing.TB, expectedKeys []string, actual map[string]T, msgAndArgs ...any) {
1011+
require.ElementsMatch(t, expectedKeys, slices.Collect(maps.Keys(actual)), msgAndArgs...)
1012+
}
1013+
1014+
// RequireXattrNotFound asserts that the given xattr is not found on the document.
1015+
func RequireXattrNotFound(t testing.TB, dataStore sgbucket.DataStore, docID string, xattrName string) {
1016+
xattrs, _, err := dataStore.GetXattrs(TestCtx(t), docID, []string{xattrName})
1017+
require.Error(t, err, fmt.Sprintf("Expected xattr %q to not be found on document %q but has contents %s", xattrName, docID, xattrs[xattrName]))
1018+
RequireXattrNotFoundError(t, err)
1019+
}

db/attachment.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,25 @@ type updatedAttachment struct {
4646
// updatedAttachments holds the user facing attachment name and raw contents
4747
type updatedAttachments map[string]updatedAttachment
4848

49-
// A map of keys -> DocAttachments.
50-
type AttachmentMap map[string]*DocAttachment
51-
52-
// A struct which models an attachment. Currently only used by test code, however
53-
// new code or refactoring in the main codebase should try to use where appropriate.
49+
// AttachmentMap can represent:
50+
// - _sync.attachments
51+
// - _globalSync.attachments_meta
52+
// - inline body `_attachments` from blip rev messages
53+
// - inline body `_attachments`from pre Sync Gateway 2.5
54+
type AttachmentMap map[string]DocAttachment
55+
56+
// DocAttachment models an attachment. This could be attachment metadata or the full body of an attachment.
57+
// - `_sync.attachments` : Data field should not be present
58+
// - `_globalSync.attachments_meta` : Data field should not be present
59+
// - `_attachments` body from blip or REST input/output: Data field could be present if Stub: false
5460
type DocAttachment struct {
5561
ContentType string `json:"content_type,omitempty"`
5662
Digest string `json:"digest,omitempty"`
5763
Length int `json:"length,omitempty"`
5864
Revpos int `json:"revpos,omitempty"`
5965
Stub bool `json:"stub,omitempty"`
6066
Version int `json:"ver,omitempty"`
61-
Data []byte `json:"-"` // tell json marshal/unmarshal to ignore this field
67+
Data []byte `json:"data,omitempty"` // this field is present for unmarshalling data but typically should not be marshalled for output.
6268
}
6369

6470
// ErrAttachmentTooLarge is returned when an attempt to attach an oversize attachment is made.

0 commit comments

Comments
 (0)