Add mode, bring in definition/patch queries
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Mode;
|
||||
use BitBadger\PDODocument\Query\Definition;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -10,10 +14,35 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class DefinitionTest extends TestCase
|
||||
{
|
||||
public function testEnsureTableForSucceeds(): void
|
||||
#[TestDox('Ensure table succeeds for PosgtreSQL')]
|
||||
public function testEnsureTableSucceedsForPostgreSQL(): void
|
||||
{
|
||||
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSON NOT NULL)',
|
||||
Definition::ensureTableFor('documents', 'JSON'), 'CREATE TABLE statement not generated correctly');
|
||||
try {
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)',
|
||||
Definition::ensureTable('documents'), 'CREATE TABLE statement not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('Ensure table succeeds for SQLite')]
|
||||
public function testEnsureTableSucceedsForSQLite(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
$this->assertEquals('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)', Definition::ensureTable('dox'),
|
||||
'CREATE TABLE statement not generated correctly');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function testEnsureTableFailsWhenModeNotSet(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::$mode = null;
|
||||
Definition::ensureTable('boom');
|
||||
}
|
||||
|
||||
public function testEnsureIndexOnSucceedsWithoutSchemaSingleAscendingField(): void
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Delete;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -11,6 +12,7 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class DeleteTest extends TestCase
|
||||
{
|
||||
#[TestDox('By ID succeeds')]
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = @id", Delete::byId('over_there'),
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Exists;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -17,6 +18,7 @@ class ExistsTest extends TestCase
|
||||
'Existence query not generated correctly');
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds')]
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = @id)", Exists::byId('dox'),
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query\Find;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -11,6 +12,7 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class FindTest extends TestCase
|
||||
{
|
||||
#[TestDox('By ID succeeds')]
|
||||
public function testByIdSucceeds(): void
|
||||
{
|
||||
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = @id", Find::byId('here'),
|
||||
|
||||
83
tests/unit/Query/PatchTest.php
Normal file
83
tests/unit/Query/PatchTest.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Test\Unit\Query;
|
||||
|
||||
use BitBadger\PDODocument\Configuration;
|
||||
use BitBadger\PDODocument\DocumentException;
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Mode;
|
||||
use BitBadger\PDODocument\Query\Patch;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for the Patch class
|
||||
*/
|
||||
class PatchTest extends TestCase
|
||||
{
|
||||
#[TestDox('By ID succeeds for PostgreSQL')]
|
||||
public function testByIdSucceedsForPostgreSQL(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
$this->assertEquals("UPDATE doc_table SET data = data || @data WHERE data->>'id' = @id",
|
||||
Patch::byId('doc_table'), 'Patch UPDATE statement is not correct');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('By ID succeeds for SQLite')]
|
||||
public function testByIdSucceedsForSQLite(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
$this->assertEquals("UPDATE my_table SET data = json_patch(data, json(@data)) WHERE data->>'id' = @id",
|
||||
Patch::byId('my_table'), 'Patch UPDATE statement is not correct');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('By ID fails when mode not set')]
|
||||
public function testByIdFailsWhenModeNotSet(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::$mode = null;
|
||||
Patch::byId('oof');
|
||||
}
|
||||
|
||||
#[TestDox('By fields succeeds for PostgreSQL')]
|
||||
public function testByFieldsSucceedsForPostgreSQL(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::PgSQL;
|
||||
$this->assertEquals("UPDATE that SET data = data || @data WHERE data->>'something' < @some",
|
||||
Patch::byFields('that', [Field::LT('something', 17, '@some')]),
|
||||
'Patch UPDATE statement is not correct');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
}
|
||||
}
|
||||
|
||||
#[TestDox('By fields succeeds for SQLite')]
|
||||
public function testByFieldsSucceedsForSQLite(): void
|
||||
{
|
||||
try {
|
||||
Configuration::$mode = Mode::SQLite;
|
||||
$this->assertEquals(
|
||||
"UPDATE a_table SET data = json_patch(data, json(@data)) WHERE data->>'something' > @it",
|
||||
Patch::byFields('a_table', [Field::GT('something', 17, '@it')]),
|
||||
'Patch UPDATE statement is not correct');
|
||||
} finally {
|
||||
Configuration::$mode = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function testByFieldsFailsWhenModeNotSet(): void
|
||||
{
|
||||
$this->expectException(DocumentException::class);
|
||||
Configuration::$mode = null;
|
||||
Patch::byFields('oops', []);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Test\Unit;
|
||||
|
||||
use BitBadger\PDODocument\Field;
|
||||
use BitBadger\PDODocument\Query;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@@ -37,11 +38,13 @@ class QueryTest extends TestCase
|
||||
'WHERE fragment not constructed correctly');
|
||||
}
|
||||
|
||||
#[TestDox('Where by ID succeeds with default parameter')]
|
||||
public function testWhereByIdSucceedsWithDefaultParameter(): void
|
||||
{
|
||||
$this->assertEquals("data->>'id' = @id", Query::whereById(), 'WHERE fragment not constructed correctly');
|
||||
}
|
||||
|
||||
#[TestDox('Where by ID succeeds with specific parameter')]
|
||||
public function testWhereByIdSucceedsWithSpecificParameter(): void
|
||||
{
|
||||
$this->assertEquals("data->>'id' = @di", Query::whereById('@di'), 'WHERE fragment not constructed correctly');
|
||||
|
||||
Reference in New Issue
Block a user