-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always use insert for time series documents (#2728)
* Always use insert for time series documents * Skip time series tests on MongoDB < 6.3
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
tests/Doctrine/ODM/MongoDB/Tests/Functional/TimeSeriesTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ODM\MongoDB\Tests\Functional; | ||
|
||
use DateTime; | ||
use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
use Documents\TimeSeries\TimeSeriesDocument; | ||
use MongoDB\BSON\ObjectId; | ||
|
||
use function iterator_to_array; | ||
|
||
class TimeSeriesTest extends BaseTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->requireMongoDB63('Time series tests require MonogDB 6.3 or newer'); | ||
} | ||
|
||
public function testCreateTimeSeriesCollection(): void | ||
{ | ||
$this->createTimeSeriesCollection(TimeSeriesDocument::class); | ||
|
||
$indexes = iterator_to_array($this->dm->getDocumentCollection(TimeSeriesDocument::class)->listIndexes()); | ||
|
||
$this->assertCount(1, $indexes); | ||
|
||
self::assertSame(['metadata' => 1, 'time' => 1], $indexes[0]['key']); | ||
} | ||
|
||
public function testCreateTimeSeriesDocumentWithoutId(): void | ||
{ | ||
$this->createTimeSeriesCollection(TimeSeriesDocument::class); | ||
|
||
$document = new TimeSeriesDocument(); | ||
$document->time = new DateTime('2025-02-05T08:53:12+00:00'); | ||
$document->value = 9; | ||
$document->metadata = 'energy'; | ||
|
||
$this->dm->persist($document); | ||
$this->dm->flush(); | ||
|
||
$this->assertCount(1, $this->dm->getDocumentCollection(TimeSeriesDocument::class)->find()); | ||
} | ||
|
||
public function testCreateTimeSeriesDocumentWithId(): void | ||
{ | ||
$this->createTimeSeriesCollection(TimeSeriesDocument::class); | ||
|
||
$document = new TimeSeriesDocument(); | ||
$document->id = (string) new ObjectId(); | ||
$document->time = new DateTime('2025-02-05T08:53:12+00:00'); | ||
$document->value = 9; | ||
$document->metadata = 'energy'; | ||
|
||
$this->dm->persist($document); | ||
$this->dm->flush(); | ||
|
||
$this->assertCount(1, $this->dm->getDocumentCollection(TimeSeriesDocument::class)->find()); | ||
} | ||
|
||
private function createTimeSeriesCollection(string $documentClass): void | ||
{ | ||
$this->dm->getSchemaManager()->createDocumentCollection($documentClass); | ||
$this->dm->getSchemaManager()->ensureDocumentIndexes($documentClass); | ||
} | ||
} |