2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace Test\Unit\Query;
|
|
|
|
|
2024-06-21 13:46:41 +00:00
|
|
|
use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode};
|
2024-06-08 23:58:45 +00:00
|
|
|
use BitBadger\PDODocument\Query\Definition;
|
|
|
|
use PHPUnit\Framework\Attributes\TestDox;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for the Definition class
|
|
|
|
*/
|
2024-06-21 13:46:41 +00:00
|
|
|
#[TestDox('Definition Queries (Unit tests)')]
|
2024-06-08 23:58:45 +00:00
|
|
|
class DefinitionTest extends TestCase
|
|
|
|
{
|
2024-06-21 13:46:41 +00:00
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(null);
|
2024-06-21 13:46:41 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureTable() succeeds for PosgtreSQL')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testEnsureTableSucceedsForPostgreSQL(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)',
|
|
|
|
Definition::ensureTable('documents'), 'CREATE TABLE statement not generated correctly');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureTable() succeeds for SQLite')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testEnsureTableSucceedsForSQLite(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::SQLite);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)', Definition::ensureTable('dox'),
|
|
|
|
'CREATE TABLE statement not generated correctly');
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureTable() fails when mode not set')]
|
2024-06-08 23:58:45 +00:00
|
|
|
public function testEnsureTableFailsWhenModeNotSet(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Definition::ensureTable('boom');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureIndexOn() succeeds without schema single ascending field')]
|
2024-06-08 23:58:45 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureIndexOn() succeeds with schema multiple fields')]
|
2024-06-08 23:58:45 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureKey() succeeds')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testEnsureKeySucceeds(): void
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$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');
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureDocumentIndexOn() succeeds for schema and Full')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testEnsureDocumentIndexOnSucceedsForSchemaAndFull(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)",
|
|
|
|
Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full));
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureDocumentIndexOn() succeeds for no schema and Optimized')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testEnsureDocumentIndexOnSucceedsForNoSchemaAndOptimized(): void
|
|
|
|
{
|
2024-07-21 01:47:21 +00:00
|
|
|
Configuration::overrideMode(Mode::PgSQL);
|
2024-06-21 13:46:41 +00:00
|
|
|
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_it_document ON it USING GIN (data jsonb_path_ops)",
|
|
|
|
Definition::ensureDocumentIndexOn('it', DocumentIndex::Optimized));
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:18:17 +00:00
|
|
|
#[TestDox('ensureDocumentIndexOn() fails for non PostgreSQL')]
|
2024-06-21 13:46:41 +00:00
|
|
|
public function testEnsureDocumentIndexOnFailsForNonPostgreSQL(): void
|
|
|
|
{
|
|
|
|
$this->expectException(DocumentException::class);
|
|
|
|
Definition::ensureDocumentIndexOn('', DocumentIndex::Full);
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|