This commit is contained in:
shimon committed 2025-09-11 16:21:11 +03:00
1 parent a582d06621
commit 21384092fb
1 file changed
+65 -66
+65 -66
View File
@@ -267,86 +267,85 @@ class MongoTest extends TestCase
$collectionName = 'count_test';
$this->getDatabase()->createCollection($collectionName);
try {
$documents = [];
for ($i = 1; $i <= 30; $i++) {
$documents[] = [
'name' => "Document {$i}",
'number' => $i,
'category' => 'test',
'created_at' => new \DateTime()
];
}
$documents = [];
for ($i = 1; $i <= 30; $i++) {
$documents[] = [
'name' => "Document {$i}",
'number' => $i,
'category' => 'test',
'created_at' => new \DateTime()
];
}
$this->getDatabase()->insertMany($collectionName, $documents);
$this->getDatabase()->insertMany($collectionName, $documents);
$total = $this->getDatabase()->count($collectionName, [], []);
self::assertEquals(30, $total);
$total = $this->getDatabase()->count($collectionName, [], []);
self::assertEquals(30, $total);
// Test count with filter (should be 1 for each specific number)
$total = $this->getDatabase()->count($collectionName, ['number' => 15], []);
self::assertEquals(1, $total);
// Test count with filter (should be 1 for each specific number)
$total = $this->getDatabase()->count($collectionName, ['number' => 15], []);
self::assertEquals(1, $total);
// Test count with range filter (should be 10 for numbers 1-10)
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], []);
self::assertEquals(10, $total);
// Test count with range filter (should be 10 for numbers 1-10)
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], []);
self::assertEquals(10, $total);
// Test count with limit (should be 5 for first 5 documents)
$total = $this->getDatabase()->count($collectionName, [], ['limit' => 5]);
self::assertEquals(5, $total);
// Test count with limit (should be 5 for first 5 documents)
$total = $this->getDatabase()->count($collectionName, [], ['limit' => 5]);
self::assertEquals(5, $total);
// Test count with filter and limit (should be 3 for first 3 documents with number <= 10)
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], ['limit' => 3]);
self::assertEquals(3, $total);
// Test count with filter and limit (should be 3 for first 3 documents with number <= 10)
$total = $this->getDatabase()->count($collectionName, ['number' => ['$lte' => 10]], ['limit' => 3]);
self::assertEquals(3, $total);
// Test count with $or operator and comparison (should be 2 documents with number <= 2 OR number >= 29)
$total = $this->getDatabase()->count($collectionName, ['$or' => [['number' => ['$lte' => 2]], ['number' => ['$gte' => 29]]]], []);
self::assertEquals(4, $total);
// Test count with $or operator and comparison (should be 2 documents with number <= 2 OR number >= 29)
$total = $this->getDatabase()->count($collectionName, ['$or' => [['number' => ['$lte' => 2]], ['number' => ['$gte' => 29]]]], []);
self::assertEquals(4, $total);
// Test aggregation count - total documents
$aggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$count' => 'total']
]);
self::assertEquals(30, $aggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count - total documents
$aggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$count' => 'total']
]);
self::assertEquals(30, $aggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with filter
$filteredAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['number' => ['$lte' => 10]]],
['$count' => 'total']
]);
self::assertEquals(10, $filteredAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with filter
$filteredAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['number' => ['$lte' => 10]]],
['$count' => 'total']
]);
self::assertEquals(10, $filteredAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with limit
$limitedAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$limit' => 7],
['$count' => 'total']
]);
self::assertEquals(7, $limitedAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with limit
$limitedAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$limit' => 7],
['$count' => 'total']
]);
self::assertEquals(7, $limitedAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with group by
$groupedAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$group' => [
'_id' => '$category', // Group by category
'count' => ['$sum' => 1] // Count of documents in the group
]]
]);
self::assertEquals(30, $groupedAggregationResult->cursor->firstBatch[0]->count);
self::assertEquals('test', $groupedAggregationResult->cursor->firstBatch[0]->_id);
// Test aggregation count with group by
$groupedAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$group' => [
'_id' => '$category', // Group by category
'count' => ['$sum' => 1] // Count of documents in the group
]]
]);
self::assertEquals(30, $groupedAggregationResult->cursor->firstBatch[0]->count);
self::assertEquals('test', $groupedAggregationResult->cursor->firstBatch[0]->_id);
// Test aggregation count with $or operator
$orAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['$or' => [['number' => 5], ['number' => 15], ['number' => 25]]]],
['$count' => 'total']
]);
self::assertEquals(3, $orAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with complex $or and range
$complexOrAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['$or' => [['number' => ['$lte' => 3]], ['number' => ['$gte' => 28]]]]],
['$count' => 'total']
]);
self::assertEquals(6, $complexOrAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with $or operator
$orAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['$or' => [['number' => 5], ['number' => 15], ['number' => 25]]]],
['$count' => 'total']
]);
self::assertEquals(3, $orAggregationResult->cursor->firstBatch[0]->total);
// Test aggregation count with complex $or and range
$complexOrAggregationResult = $this->getDatabase()->aggregate($collectionName, [
['$match' => ['$or' => [['number' => ['$lte' => 3]], ['number' => ['$gte' => 28]]]]],
['$count' => 'total']
]);
self::assertEquals(6, $complexOrAggregationResult->cursor->firstBatch[0]->total);
} finally {
$this->getDatabase()->dropCollection($collectionName);
}