Skip to content

Commit 84ed657

Browse files
PCSM-261. Add test case for avgObjSize decoding error (#171)
Co-authored-by: Adnan <chupe@chupe.ba>
1 parent 3009507 commit 84ed657

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

tests/test_collections_sharded.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ def test_clone_document_sharded(t: Testing, phase: Runner.Phase):
9898

9999
t.compare_all_sharded()
100100

101+
@pytest.mark.parametrize("phase", [Runner.Phase.CLONE])
102+
def test_clone_document_sharded_with_varying_sizes(t: Testing, phase: Runner.Phase):
103+
with t.run(phase):
104+
t.source["db_1"].create_collection("coll_1")
105+
t.source.admin.command("shardCollection", "db_1.coll_1", key={"_id": "hashed"})
106+
# insert documents with varying sizes to produce fractional avgObjSize
107+
docs = [
108+
{"_id": 1, "name": "Alice", "age": 30, "data": "x" * 15},
109+
{"_id": 2, "name": "Bob", "age": 25, "data": "y" * 47},
110+
{"_id": 3, "name": "Charlie", "age": 35, "data": "z" * 73},
111+
{"_id": 4, "name": "Diana", "age": 28, "data": "a" * 22},
112+
{"_id": 5, "name": "Eve", "age": 32, "data": "b" * 91},
113+
]
114+
t.source["db_1"]["coll_1"].insert_many(docs)
115+
t.compare_all_sharded()
101116

102117
@pytest.mark.parametrize("phase", [Runner.Phase.APPLY])
103118
def test_shard_key_update_duplicate_key_error(t: Testing, phase: Runner.Phase):

topo/topo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ type DBStats struct {
127127
// CollStats represents the result of the [GetCollStats].
128128
type CollStats struct {
129129
// Count is the number of documents in the collection.
130-
Count int64 `bson:"count"`
130+
Count int64 `bson:"count,truncate"`
131131
// Size is the total size of the collection.
132-
Size int64 `bson:"size"`
132+
Size int64 `bson:"size,truncate"`
133133
// AvgObjSize is the average size of documents in the collection.
134-
AvgObjSize int64 `bson:"avgObjSize"`
134+
AvgObjSize int64 `bson:"avgObjSize,truncate"`
135135
}
136136

137137
// SayHello runs the db.hello() command and returns the [Hello].

topo/topo_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,33 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
"go.mongodb.org/mongo-driver/v2/bson"
912
"go.mongodb.org/mongo-driver/v2/mongo"
1013
)
1114

15+
func TestCollStats_DecodeFromFloat64(t *testing.T) {
16+
t.Parallel()
17+
18+
input := bson.M{
19+
"count": 0.0,
20+
"size": 73179136.0,
21+
"avgObjSize": 0.0,
22+
}
23+
24+
data, err := bson.Marshal(input)
25+
require.NoError(t, err)
26+
27+
var stats CollStats
28+
err = bson.Unmarshal(data, &stats)
29+
require.NoError(t, err)
30+
31+
assert.Equal(t, int64(0), stats.Count)
32+
assert.Equal(t, int64(73179136), stats.Size)
33+
assert.Equal(t, int64(0), stats.AvgObjSize)
34+
}
35+
1236
func TestRunWithRetry_NonTransientError(t *testing.T) {
1337
t.Parallel()
1438

0 commit comments

Comments
 (0)