MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestBasicAttachmentRemoval

Function TestBasicAttachmentRemoval

rest/attachment_test.go:1081–2054  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1079}
1080
1081func TestBasicAttachmentRemoval(t *testing.T) {
1082 rt := NewRestTester(t, &RestTesterConfig{GuestEnabled: true})
1083 defer rt.Close()
1084
1085 retrieveAttachment := func(t *testing.T, docID, attName string) (attBody string) {
1086 resource := fmt.Sprintf("/{{.keyspace}}/%s/%s", docID, attName)
1087 response := rt.SendRequest(http.MethodGet, resource, "")
1088 RequireStatus(t, response, http.StatusOK)
1089 return response.Body.String()
1090 }
1091
1092 retrieveAttachmentKey := func(t *testing.T, docID, attName string) string {
1093 resource := fmt.Sprintf("/{{.keyspace}}/%s/%s?meta=true", docID, attName)
1094 response := rt.SendRequest(http.MethodGet, resource, "")
1095 RequireStatus(t, response, http.StatusOK)
1096 return getAttachmentMetaFromREST(t, response.BodyBytes()).Key
1097 }
1098
1099 requireAttachmentNotFound := func(t *testing.T, docID, attName string) {
1100 resource := fmt.Sprintf("/{{.keyspace}}/%s/%s", docID, attName)
1101 response := rt.SendRequest(http.MethodGet, resource, "")
1102 RequireStatus(t, response, http.StatusNotFound)
1103 }
1104
1105 retrieveAttachmentMeta := func(t *testing.T, docID string) db.AttachmentMap {
1106 response := rt.SendRequest(http.MethodGet, fmt.Sprintf("/{{.keyspace}}/%s", docID), "")
1107 RequireStatus(t, response, http.StatusOK)
1108 return db.GetAttachmentsFromInlineBody(t, response.BodyBytes())
1109 }
1110
1111 dataStore := rt.GetSingleDataStore()
1112 requireAttachmentFound := func(attKey string, attBodyExpected []byte) {
1113 var attBodyActual []byte
1114 _, err := dataStore.Get(attKey, &attBodyActual)
1115 require.NoError(t, err)
1116 assert.Equal(t, attBodyExpected, attBodyActual)
1117 }
1118
1119 rt.Run("single attachment removal upon document update", func(t *testing.T) {
1120 // Create a document.
1121 docID := "foo"
1122 version := rt.CreateTestDoc(docID)
1123 // Add an attachment to the document.
1124 attName := "foo.txt"
1125 attBody := "this is the body of attachment foo.txt"
1126 version = rt.storeAttachment(docID, version, attName, attBody)
1127
1128 // Retrieve the attachment added to the document.
1129 actualAttBody := retrieveAttachment(t, docID, attName)
1130 require.Equal(t, attBody, actualAttBody)
1131
1132 // Get the document and check the attachment metadata.
1133 attachments := retrieveAttachmentMeta(t, docID)
1134 require.Equal(t, db.AttachmentMap{
1135 attName: {
1136 Stub: true,
1137 ContentType: "content/type",
1138 Digest: "sha1-CTJaowVFZ4ozgmvBageTH9w+OKU=",

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
SendRequestMethod · 0.95
GetSingleDataStoreMethod · 0.95
RunMethod · 0.95
CreateTestDocMethod · 0.95
storeAttachmentMethod · 0.95
UpdateDocMethod · 0.95
RequireDocNotFoundMethod · 0.95
PurgeDocMethod · 0.95
DeleteDocMethod · 0.95
PutDocMethod · 0.95
SendAdminRequestMethod · 0.95

Tested by

no test coverage detected