|
| 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 db |
| 10 | + |
| 11 | +import ( |
| 12 | + "fmt" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/couchbase/sync_gateway/base" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +func TestBlipSequenceProperty(t *testing.T) { |
| 20 | + testCases := []struct { |
| 21 | + name string |
| 22 | + seq SequenceID |
| 23 | + expectedString string |
| 24 | + expectedISGRV2String string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "Simple SequenceID", |
| 28 | + seq: SequenceID{Seq: 1}, |
| 29 | + expectedString: `1`, |
| 30 | + expectedISGRV2String: `1`, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "compound sequenceID", |
| 34 | + seq: SequenceID{LowSeq: 5, TriggeredBy: 10, Seq: 15}, |
| 35 | + expectedString: `"5:10:15"`, |
| 36 | + expectedISGRV2String: `5:10:15`, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "TriggeredBy only", |
| 40 | + seq: SequenceID{TriggeredBy: 20, Seq: 25}, |
| 41 | + expectedString: `"20:25"`, |
| 42 | + expectedISGRV2String: `20:25`, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "LowSeq and Seq", |
| 46 | + seq: SequenceID{LowSeq: 30, Seq: 35}, |
| 47 | + expectedString: `"30::35"`, |
| 48 | + expectedISGRV2String: `30::35`, |
| 49 | + }, |
| 50 | + } |
| 51 | + for _, tc := range testCases { |
| 52 | + t.Run(tc.name, func(t *testing.T) { |
| 53 | + // test norev sequence property |
| 54 | + norevMessage := NewNoRevMessage() |
| 55 | + require.NoError(t, norevMessage.SetSequence(tc.seq)) |
| 56 | + require.Equal(t, tc.expectedString, norevMessage.Properties[NorevMessageSequence]) |
| 57 | + // this string is only used by ISGR when CB_Mobile < 3 |
| 58 | + norevMessage.SetSeq(tc.seq) |
| 59 | + require.Equal(t, tc.expectedISGRV2String, norevMessage.Properties[NorevMessageSeq]) |
| 60 | + |
| 61 | + // test rev sequence property |
| 62 | + properties, err := blipRevMessageProperties(nil, false, tc.seq, "", nil) |
| 63 | + require.NoError(t, err) |
| 64 | + require.Equal(t, tc.expectedString, properties[RevMessageSequence]) |
| 65 | + changeEntry := &ChangeEntry{ |
| 66 | + Seq: tc.seq, |
| 67 | + ID: "docID", |
| 68 | + } |
| 69 | + // changes message format |
| 70 | + ctx := base.TestCtx(t) |
| 71 | + bh := &blipHandler{ |
| 72 | + loggingCtx: ctx, |
| 73 | + BlipSyncContext: &BlipSyncContext{}, |
| 74 | + } |
| 75 | + changeRow := bh.buildChangesRow(changeEntry, ChangeByVersionType{"rev": "1-abc"}) |
| 76 | + rowString, err := base.JSONMarshal(changeRow) |
| 77 | + require.NoError(t, err) |
| 78 | + require.Equal(t, fmt.Sprintf(`[%s,"docID","1-abc"]`, tc.expectedString), string(rowString)) |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments