Skip to content

Commit

Permalink
update test to account additional star channel
Browse files Browse the repository at this point in the history
  • Loading branch information
nirav24 committed Dec 27, 2022
1 parent d1fd380 commit ed9d48f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
5 changes: 3 additions & 2 deletions db/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,11 +1389,12 @@ func TestSyncFnOnPush(t *testing.T) {
// Check that the doc has the correct channel (test for issue #300)
doc, err := collection.GetDocument(ctx, "doc1", DocUnmarshalAll)
assert.Equal(t, channels.ChannelMap{
"*": nil,
"clibup": nil,
"public": &channels.ChannelRemoval{Seq: 2, RevID: "4-four"},
}, doc.Channels)

assert.Equal(t, base.SetOf("clibup"), doc.History["4-four"].Channels)
assert.Equal(t, base.SetOf("*", "clibup"), doc.History["4-four"].Channels)
}

func TestInvalidChannel(t *testing.T) {
Expand Down Expand Up @@ -2456,7 +2457,7 @@ func TestResyncUpdateAllDocChannels(t *testing.T) {
return state == DBOffline
})

_, err = collection.UpdateAllDocChannels(ctx, false, func(docsProcessed, docsChanged *int) {}, base.NewSafeTerminator(), false)
_, err = collection.UpdateAllDocChannels(ctx, false, func(docsProcessed, docsChanged *int) {}, base.NewSafeTerminator(), !db.AllDocsIndexExists)
assert.NoError(t, err)

syncFnCount := int(db.DbStats.Database().SyncFunctionCount.Value())
Expand Down
4 changes: 2 additions & 2 deletions rest/adminapitest/admin_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ func TestRawRedaction(t *testing.T) {
err := base.JSONUnmarshal(res.Body.Bytes(), &body)
assert.NoError(t, err)
syncData := body[base.SyncPropertyName]
assert.Equal(t, map[string]interface{}{"achannel": nil}, syncData.(map[string]interface{})["channels"])
assert.Equal(t, []interface{}([]interface{}{[]interface{}{"achannel"}}), syncData.(map[string]interface{})["history"].(map[string]interface{})["channels"])
assert.Equal(t, map[string]interface{}{"achannel": nil, "*": nil}, syncData.(map[string]interface{})["channels"])
assert.Equal(t, []interface{}([]interface{}{[]interface{}{"*", "achannel"}}), syncData.(map[string]interface{})["history"].(map[string]interface{})["channels"])

// Test redacted
body = map[string]interface{}{}
Expand Down
52 changes: 40 additions & 12 deletions rest/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2407,9 +2407,18 @@ func TestDocumentChannelHistory(t *testing.T) {
syncData, err := rt.GetDatabase().GetSingleDatabaseCollection().GetDocSyncData(base.TestCtx(t), "doc")
assert.NoError(t, err)

require.Len(t, syncData.ChannelSet, 1)
assert.Equal(t, syncData.ChannelSet[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 0})
assert.Len(t, syncData.ChannelSetHistory, 0)
shouldExpectStarChannel := !rt.GetDatabase().AllDocsIndexExists

if shouldExpectStarChannel {
require.Len(t, syncData.ChannelSet, 2)
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test", Start: 1, End: 0})
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "*", Start: 1, End: 0})
assert.Len(t, syncData.ChannelSetHistory, 0)
} else {
require.Len(t, syncData.ChannelSet, 1)
assert.Equal(t, syncData.ChannelSet[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 0})
assert.Len(t, syncData.ChannelSetHistory, 0)
}

// Update doc to remove from channel and ensure a single channel history entry with both start and end sequences
// and no old channel history entries
Expand All @@ -2420,9 +2429,16 @@ func TestDocumentChannelHistory(t *testing.T) {
syncData, err = rt.GetDatabase().GetSingleDatabaseCollection().GetDocSyncData(base.TestCtx(t), "doc")
assert.NoError(t, err)

require.Len(t, syncData.ChannelSet, 1)
assert.Equal(t, syncData.ChannelSet[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 2})
assert.Len(t, syncData.ChannelSetHistory, 0)
if shouldExpectStarChannel {
require.Len(t, syncData.ChannelSet, 2)
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test", Start: 1, End: 2})
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "*", Start: 1, End: 0})
assert.Len(t, syncData.ChannelSetHistory, 0)
} else {
require.Len(t, syncData.ChannelSet, 1)
assert.Equal(t, syncData.ChannelSet[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 2})
assert.Len(t, syncData.ChannelSetHistory, 0)
}

// Update doc to add to channels test and test2 and ensure a single channel history entry for both test and test2
// both with start sequences only and ensure old test entry was moved to old
Expand All @@ -2433,11 +2449,19 @@ func TestDocumentChannelHistory(t *testing.T) {
syncData, err = rt.GetDatabase().GetSingleDatabaseCollection().GetDocSyncData(base.TestCtx(t), "doc")
assert.NoError(t, err)

require.Len(t, syncData.ChannelSet, 2)
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test", Start: 3, End: 0})
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test2", Start: 3, End: 0})
require.Len(t, syncData.ChannelSetHistory, 1)
assert.Equal(t, syncData.ChannelSetHistory[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 2})
if shouldExpectStarChannel {
require.Len(t, syncData.ChannelSet, 3)
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test", Start: 3, End: 0})
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test2", Start: 3, End: 0})
require.Len(t, syncData.ChannelSetHistory, 1)
assert.Equal(t, syncData.ChannelSetHistory[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 2})
} else {
require.Len(t, syncData.ChannelSet, 2)
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test", Start: 3, End: 0})
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{Name: "test2", Start: 3, End: 0})
require.Len(t, syncData.ChannelSetHistory, 1)
assert.Equal(t, syncData.ChannelSetHistory[0], db.ChannelSetEntry{Name: "test", Start: 1, End: 2})
}
}

func TestChannelHistoryLegacyDoc(t *testing.T) {
Expand Down Expand Up @@ -2497,7 +2521,11 @@ func TestChannelHistoryLegacyDoc(t *testing.T) {
assert.NoError(t, err)
syncData, err := rt.GetDatabase().GetSingleDatabaseCollection().GetDocSyncData(base.TestCtx(t), "doc1")
assert.NoError(t, err)
require.Len(t, syncData.ChannelSet, 1)
if rt.GetDatabase().AllDocsIndexExists {
require.Len(t, syncData.ChannelSet, 1)
} else {
require.Len(t, syncData.ChannelSet, 2)
}
assert.Contains(t, syncData.ChannelSet, db.ChannelSetEntry{
Name: "test",
Start: 1,
Expand Down
8 changes: 7 additions & 1 deletion rest/doc_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func TestDocumentNumbers(t *testing.T) {

base.SetUpTestLogging(t, base.LevelDebug, base.KeyAll)

shouldExpectStarChannel := !rt.GetDatabase().AllDocsIndexExists

for _, test := range tests {
t.Run(test.name, func(ts *testing.T) {
// Create document
Expand All @@ -126,7 +128,11 @@ func TestDocumentNumbers(t *testing.T) {
var rawResponse RawResponse
require.NoError(ts, base.JSONUnmarshal(getRawResponse.Body.Bytes(), &rawResponse))
log.Printf("raw response: %s", getRawResponse.Body.Bytes())
assert.Equal(ts, 1, len(rawResponse.Sync.Channels))
if shouldExpectStarChannel {
assert.Equal(ts, 2, len(rawResponse.Sync.Channels))
} else {
assert.Equal(ts, 1, len(rawResponse.Sync.Channels))
}
assert.True(ts, HasActiveChannel(rawResponse.Sync.Channels, test.expectedFormatChannel), fmt.Sprintf("Expected channel %s was not found in document channels (%s)", test.expectedFormatChannel, test.name))

})
Expand Down
4 changes: 4 additions & 0 deletions rest/sync_fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestSyncFnBodyProperties(t *testing.T) {
testdataKey,
db.BodyId,
db.BodyRev,
"*",
}

// This sync function routes into channels based on top-level properties contained in doc
Expand Down Expand Up @@ -84,6 +85,7 @@ func TestSyncFnBodyPropertiesTombstone(t *testing.T) {
db.BodyId,
db.BodyRev,
db.BodyDeleted,
"*",
}

// This sync function routes into channels based on top-level properties contained in doc
Expand Down Expand Up @@ -131,6 +133,7 @@ func TestSyncFnOldDocBodyProperties(t *testing.T) {
expectedProperties := []string{
testdataKey,
db.BodyId,
"*",
}

// This sync function routes into channels based on top-level properties contained in oldDoc
Expand Down Expand Up @@ -180,6 +183,7 @@ func TestSyncFnOldDocBodyPropertiesTombstoneResurrect(t *testing.T) {
testdataKey,
db.BodyId,
db.BodyDeleted,
"*",
}

// This sync function routes into channels based on top-level properties contained in oldDoc
Expand Down

0 comments on commit ed9d48f

Please sign in to comment.