Skip to content

Commit

Permalink
Fix Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Jan 9, 2024
1 parent ffab662 commit c419e21
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"ByJG\\AnyDataset\\NoSql\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
Expand Down
9 changes: 6 additions & 3 deletions src/MongoDbDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function getDbConnection(): ?Manager
public function getDocumentById(string $idDocument, mixed $collection = null): ?NoSqlDocument
{
$filter = new IteratorFilter();
$filter->addRelation('_id', Relation::EQUAL, $idDocument);
$filter->addRelation('_id', Relation::EQUAL, new ObjectID($idDocument));
$document = $this->getDocuments($filter, $collection);

if (empty($document)) {
Expand Down Expand Up @@ -253,14 +253,17 @@ public function save(NoSqlDocument $document): NoSqlDocument
$idDocument = $data['_id'] ?? null;
}

$data['updated'] = new UTCDateTime((new DateTime())->getTimestamp()*1000);
if (empty($idDocument)) {
$data['_id'] = $idDocument = new ObjectID();
$data['created'] = new UTCDateTime((new DateTime())->getTimestamp()*1000);
$bulkWrite->insert($data);
} else {
if (!($idDocument instanceof ObjectID)) {
$idDocument = new ObjectID($idDocument);
}
$data['_id'] = $idDocument;
$bulkWrite->update(['_id' => $idDocument], ["\$set" => $data]);
$data['updated'] = new UTCDateTime((new DateTime())->getTimestamp()*1000);
$bulkWrite->update(['_id' => $idDocument], ['$set' => $data], ['multi' => false, 'upsert' => true]);
}

$this->mongoManager->executeBulkWrite(
Expand Down
2 changes: 2 additions & 0 deletions tests/AwsDynamoDbDriverTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests;

use Aws\DynamoDb\Exception\DynamoDbException;
use ByJG\AnyDataset\NoSql\AwsDynamoDbDriver;
use ByJG\AnyDataset\NoSql\Factory;
Expand Down
2 changes: 2 additions & 0 deletions tests/AwsS3DriverTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests;

use ByJG\AnyDataset\NoSql\AwsS3Driver;
use ByJG\AnyDataset\NoSql\Factory;
use ByJG\Util\Uri;
Expand Down
5 changes: 3 additions & 2 deletions tests/MongoDbDriverTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests;

use ByJG\AnyDataset\Core\Enum\Relation;
use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\NoSql\Factory;
Expand Down Expand Up @@ -98,8 +100,7 @@ public function testSaveDocument()
$data = $document[0]->getDocument();
$this->assertNotEmpty($data['_id']);
$this->assertNotEmpty($data['created']);
$this->assertNotEmpty($data['updated']);
$this->assertEquals($data['created']->toDatetime(), $data['updated']->toDatetime());
$this->assertArrayNotHasKey('updated', $data);
unset($data['_id']);
unset($data['created']);
unset($data['updated']);
Expand Down

0 comments on commit c419e21

Please sign in to comment.