39 lines
1.4 KiB
PHP
39 lines
1.4 KiB
PHP
|
<?php declare(strict_types=1);
|
||
|
|
||
|
namespace Test\Unit\Query;
|
||
|
|
||
|
use BitBadger\PDODocument\Query\Definition;
|
||
|
use PHPUnit\Framework\TestCase;
|
||
|
|
||
|
/**
|
||
|
* Unit tests for the Definition class
|
||
|
*/
|
||
|
class DefinitionTest extends TestCase
|
||
|
{
|
||
|
public function testEnsureTableForSucceeds(): void
|
||
|
{
|
||
|
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSON NOT NULL)',
|
||
|
Definition::ensureTableFor('documents', 'JSON'), 'CREATE TABLE statement not generated correctly');
|
||
|
}
|
||
|
|
||
|
public function testEnsureIndexOnSucceedsWithoutSchemaSingleAscendingField(): void
|
||
|
{
|
||
|
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_test_fields ON test ((data->>'details'))",
|
||
|
Definition::ensureIndexOn('test', 'fields', ['details']), 'CREATE INDEX statement not generated correctly');
|
||
|
}
|
||
|
|
||
|
public function testEnsureIndexOnSucceedsWithSchemaMultipleFields(): void
|
||
|
{
|
||
|
$this->assertEquals(
|
||
|
"CREATE INDEX IF NOT EXISTS idx_testing_json ON sch.testing ((data->>'group'), (data->>'sub_group') DESC)",
|
||
|
Definition::ensureIndexOn('sch.testing', 'json', ['group', 'sub_group DESC']),
|
||
|
'CREATE INDEX statement not generated correctly');
|
||
|
}
|
||
|
|
||
|
public function testEnsureKey(): void
|
||
|
{
|
||
|
$this->assertEquals("CREATE UNIQUE INDEX IF NOT EXISTS idx_tbl_key ON tbl ((data->>'id'))",
|
||
|
Definition::ensureKey('tbl'), 'CREATE INDEX statement for document key not generated correctly');
|
||
|
}
|
||
|
}
|