diff --git a/tests/test_collections_sharded.py b/tests/test_collections_sharded.py index ae632d32..de4d756a 100644 --- a/tests/test_collections_sharded.py +++ b/tests/test_collections_sharded.py @@ -98,6 +98,21 @@ def test_clone_document_sharded(t: Testing, phase: Runner.Phase): t.compare_all_sharded() +@pytest.mark.parametrize("phase", [Runner.Phase.CLONE]) +def test_clone_document_sharded_with_varying_sizes(t: Testing, phase: Runner.Phase): + with t.run(phase): + t.source["db_1"].create_collection("coll_1") + t.source.admin.command("shardCollection", "db_1.coll_1", key={"_id": "hashed"}) + # insert documents with varying sizes to produce fractional avgObjSize + docs = [ + {"_id": 1, "name": "Alice", "age": 30, "data": "x" * 15}, + {"_id": 2, "name": "Bob", "age": 25, "data": "y" * 47}, + {"_id": 3, "name": "Charlie", "age": 35, "data": "z" * 73}, + {"_id": 4, "name": "Diana", "age": 28, "data": "a" * 22}, + {"_id": 5, "name": "Eve", "age": 32, "data": "b" * 91}, + ] + t.source["db_1"]["coll_1"].insert_many(docs) + t.compare_all_sharded() @pytest.mark.parametrize("phase", [Runner.Phase.APPLY]) def test_shard_key_update_duplicate_key_error(t: Testing, phase: Runner.Phase): diff --git a/topo/topo.go b/topo/topo.go index 1348eaec..04c1d9e6 100644 --- a/topo/topo.go +++ b/topo/topo.go @@ -127,11 +127,11 @@ type DBStats struct { // CollStats represents the result of the [GetCollStats]. type CollStats struct { // Count is the number of documents in the collection. - Count int64 `bson:"count"` + Count int64 `bson:"count,truncate"` // Size is the total size of the collection. - Size int64 `bson:"size"` + Size int64 `bson:"size,truncate"` // AvgObjSize is the average size of documents in the collection. - AvgObjSize int64 `bson:"avgObjSize"` + AvgObjSize int64 `bson:"avgObjSize,truncate"` } // SayHello runs the db.hello() command and returns the [Hello]. diff --git a/topo/topo_test.go b/topo/topo_test.go index 70b5df25..d1f01cf8 100644 --- a/topo/topo_test.go +++ b/topo/topo_test.go @@ -6,9 +6,33 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" ) +func TestCollStats_DecodeFromFloat64(t *testing.T) { + t.Parallel() + + input := bson.M{ + "count": 0.1, + "size": 73179136.2, + "avgObjSize": 0.3, + } + + data, err := bson.Marshal(input) + require.NoError(t, err) + + var stats CollStats + err = bson.Unmarshal(data, &stats) + require.NoError(t, err) + + assert.Equal(t, int64(0), stats.Count) + assert.Equal(t, int64(73179136), stats.Size) + assert.Equal(t, int64(0), stats.AvgObjSize) +} + func TestRunWithRetry_NonTransientError(t *testing.T) { t.Parallel()