pdo-document/tests/unit/Query/DefinitionTest.php

97 lines
3.7 KiB
PHP

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