Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isEmpty returning true if $id is not selected #305

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2238,12 +2238,13 @@ public function getDocument(string $collection, string $id, array $queries = [])
}

$document = $this->adapter->getDocument($collection->getId(), $id, $queries);
$document->setAttribute('$collection', $collection->getId());

if ($document->isEmpty()) {
return $document;
}

$document->setAttribute('$collection', $collection->getId());

if ($collection->getId() !== self::METADATA) {
if (!$validator->isValid([
...$collection->getRead(),
Expand Down Expand Up @@ -4072,7 +4073,10 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu
}
$node = $this->casting($collection, $node);
$node = $this->decode($collection, $node, $selections);
$node->setAttribute('$collection', $collection->getId());

if (!$node->isEmpty()) {
$node->setAttribute('$collection', $collection->getId());
}
}

$results = $this->applyNestedQueries($results, $nestedQueries, $relationships);
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ public function findAndRemove(string $key, $find, string $subject = ''): bool
*/
public function isEmpty(): bool
{
return empty($this->getId());
return !\count($this);
}

/**
Expand Down
5 changes: 0 additions & 5 deletions tests/Database/Adapter/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,4 @@ public static function getDatabase(): Database

return self::$database = $database;
}

public static function killDatabase(): void
{
self::$database = null;
}
}
5 changes: 0 additions & 5 deletions tests/Database/Adapter/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,4 @@ public function testKeywords(): void
{
$this->assertTrue(true);
}

public static function killDatabase(): void
{
self::$database = null;
}
}
5 changes: 0 additions & 5 deletions tests/Database/Adapter/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,4 @@ public static function getDatabase(): Database

return self::$database = $database;
}

public static function killDatabase(): void
{
self::$database = null;
}
}
5 changes: 0 additions & 5 deletions tests/Database/Adapter/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,4 @@ public static function getDatabase(): Database

return self::$database = $database;
}

public static function killDatabase(): void
{
self::$database = null;
}
}
5 changes: 0 additions & 5 deletions tests/Database/Adapter/SQLiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,4 @@ public static function getDatabase(): Database

return self::$database = $database;
}

public static function killDatabase(): void
{
self::$database = null;
}
}
177 changes: 85 additions & 92 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ abstract class Base extends TestCase
*/
abstract protected static function getDatabase(): Database;

abstract protected static function killDatabase(): void;

/**
* @return string
*/
Expand Down Expand Up @@ -1056,7 +1054,8 @@ public function testGetDocumentSelect(Document $document): Document
Query::select(['string', 'integer']),
]);

$this->assertNotEmpty(true, $document->getId());
$this->assertEmpty($document->getId());
$this->assertFalse($document->isEmpty());
$this->assertIsString($document->getAttribute('string'));
$this->assertEquals('text📝', $document->getAttribute('string'));
$this->assertIsInt($document->getAttribute('integer'));
Expand Down Expand Up @@ -11691,6 +11690,89 @@ public function testLabels(): void
$this->assertCount(1, $documents);
}

public function testEmptyOperatorValues(): void
{
try {
static::getDatabase()->findOne('documents', [
Query::equal('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Equal queries require at least one value.', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::search('string', null),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::notEqual('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::lessThan('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::lessThanEqual('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::greaterThan('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::greaterThanEqual('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::contains('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Contains queries require at least one value.', $e->getMessage());
}
}

public function testEvents(): void
{
Authorization::skip(function () {
Expand Down Expand Up @@ -11782,93 +11864,4 @@ public function testEvents(): void
$database->delete('hellodb');
});
}

public function testEmptyOperatorValues(): void
{
try {
static::getDatabase()->findOne('documents', [
Query::equal('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Equal queries require at least one value.', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::search('string', null),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::notEqual('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::lessThan('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::lessThanEqual('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::greaterThan('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::greaterThanEqual('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Query type does not match expected: string', $e->getMessage());
}

try {
static::getDatabase()->findOne('documents', [
Query::contains('string', []),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$this->assertEquals('Invalid query: Contains queries require at least one value.', $e->getMessage());
}
}

public function testLast(): void
{
$this->expectNotToPerformAssertions();
static::killDatabase();
}
}