Skip to content

Commit 1a86243

Browse files
committed
Add RestTester test to fetch by old RevTree ID
1 parent 42b6698 commit 1a86243

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

rest/revision_old_revtree_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2025-Present Couchbase, Inc.
2+
//
3+
// Use of this software is governed by the Business Source License included
4+
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
5+
// in that file, in accordance with the Business Source License, use of this
6+
// software will be governed by the Apache License, Version 2.0, included in
7+
// the file licenses/APL2.txt.
8+
9+
package rest
10+
11+
import (
12+
"fmt"
13+
"net/http"
14+
"testing"
15+
16+
"github.com/couchbase/sync_gateway/base"
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
// TestGetOldRevisionBodyByRevTreeID ensures that fetching an older revision body using a legacy RevTree ID after flushing the revision cache works with store_legacy_revtree_data set.
21+
func TestGetOldRevisionBodyByRevTreeID(t *testing.T) {
22+
base.SetUpTestLogging(t, base.LevelDebug, base.KeyCRUD)
23+
24+
tests := []struct {
25+
name string
26+
storeLegacyRevTreeData bool
27+
deltaSyncEnabled bool
28+
expectedToFindOldRev bool
29+
}{
30+
{
31+
name: "store_legacy_revtree_data=true, delta_sync=true",
32+
storeLegacyRevTreeData: true,
33+
deltaSyncEnabled: true,
34+
expectedToFindOldRev: true,
35+
},
36+
{
37+
name: "store_legacy_revtree_data=true, delta_sync=false",
38+
storeLegacyRevTreeData: true,
39+
deltaSyncEnabled: false,
40+
expectedToFindOldRev: false, // TODO: CBG-4840 - Should be true - Requires restoration of non-delta sync rev storage
41+
},
42+
{
43+
name: "store_legacy_revtree_data=false, delta_sync=true",
44+
storeLegacyRevTreeData: false,
45+
deltaSyncEnabled: true,
46+
expectedToFindOldRev: false,
47+
},
48+
{
49+
name: "store_legacy_revtree_data=false, delta_sync=false",
50+
storeLegacyRevTreeData: false,
51+
deltaSyncEnabled: false,
52+
expectedToFindOldRev: false,
53+
},
54+
}
55+
56+
for _, tc := range tests {
57+
t.Run(tc.name, func(t *testing.T) {
58+
rt := NewRestTester(t, &RestTesterConfig{
59+
DatabaseConfig: &DatabaseConfig{DbConfig: DbConfig{
60+
DeltaSync: &DeltaSyncConfig{Enabled: base.Ptr(tc.deltaSyncEnabled)},
61+
StoreLegacyRevTreeData: base.Ptr(tc.storeLegacyRevTreeData),
62+
}},
63+
})
64+
defer rt.Close()
65+
66+
// Create and update a document to generate an old revision.
67+
const docID = "doc1"
68+
v1 := rt.PutDoc(docID, `{"v":1}`)
69+
v2 := rt.UpdateDoc(docID, v1, `{"v":2}`)
70+
_ = v2
71+
72+
// Flush the revision cache to force retrieval from bucket-stored old revision docs.
73+
rt.GetDatabase().FlushRevisionCacheForTest()
74+
75+
resp := rt.SendAdminRequest(http.MethodGet, fmt.Sprintf("/{{.keyspace}}/%s?rev=%s", docID, v1.RevTreeID), "")
76+
// Attempt to fetch the old revision body using its RevTree ID.
77+
if tc.expectedToFindOldRev {
78+
RequireStatus(t, resp, http.StatusOK)
79+
assert.Contains(t, resp.BodyString(), `"v":1`)
80+
} else {
81+
RequireStatus(t, resp, http.StatusNotFound)
82+
}
83+
})
84+
}
85+
}

rest/utilities_testing_resttester.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (rt *RestTester) GetDocVersion(docID string, version DocVersion) db.Body {
9595

9696
// GetDocByRev returns the doc body for the given docID and Rev. If the document is not found, t.Fail will be called.
9797
func (rt *RestTester) GetDocByRev(docID, revTreeID string) db.Body {
98-
rawResponse := rt.SendAdminRequest(http.MethodGet, fmt.Sprintf("/%s/%s?rev=%s", rt.GetSingleKeyspace(), docID, revTreeID), "")
98+
rawResponse := rt.SendAdminRequest(http.MethodGet, fmt.Sprintf("/{{.keyspace}}/%s?rev=%s", docID, revTreeID), "")
9999
RequireStatus(rt.TB(), rawResponse, http.StatusOK)
100100
var body db.Body
101101
require.NoError(rt.TB(), base.JSONUnmarshal(rawResponse.Body.Bytes(), &body))
@@ -104,14 +104,14 @@ func (rt *RestTester) GetDocByRev(docID, revTreeID string) db.Body {
104104

105105
// CreateTestDoc creates a document with an arbitrary body.
106106
func (rt *RestTester) CreateTestDoc(docid string) DocVersion {
107-
response := rt.SendAdminRequest(http.MethodPut, fmt.Sprintf("/%s/%s", rt.GetSingleKeyspace(), docid), `{"prop":true}`)
107+
response := rt.SendAdminRequest(http.MethodPut, fmt.Sprintf("/{{.keyspace}}/%s", docid), `{"prop":true}`)
108108
RequireStatus(rt.TB(), response, 201)
109109
return DocVersionFromPutResponse(rt.TB(), response)
110110
}
111111

112112
// PutDoc will upsert the document with a given contents.
113113
func (rt *RestTester) PutDoc(docID, body string) DocVersion {
114-
rawResponse := rt.SendAdminRequest(http.MethodPut, fmt.Sprintf("/%s/%s", rt.GetSingleKeyspace(), docID), body)
114+
rawResponse := rt.SendAdminRequest(http.MethodPut, fmt.Sprintf("/{{.keyspace}}/%s", docID), body)
115115
RequireStatus(rt.TB(), rawResponse, 201)
116116
return DocVersionFromPutResponse(rt.TB(), rawResponse)
117117
}

0 commit comments

Comments
 (0)