Add PostgreSQL Support (#3)

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2024-06-21 13:46:41 +00:00
parent 330e272187
commit 124426fa12
61 changed files with 2290 additions and 223 deletions

View File

@@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Configuration class
*/
#[TestDox('Configuration (Unit tests)')]
class ConfigurationTest extends TestCase
{
#[TestDox('ID field default succeeds')]

View File

@@ -4,11 +4,13 @@ namespace Test\Unit;
use BitBadger\PDODocument\DocumentException;
use Exception;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the DocumentException class
*/
#[TestDox('Document Exception (Unit tests)')]
class DocumentExceptionTest extends TestCase
{
public function testConstructorSucceedsWithCodeAndPriorException()

View File

@@ -3,11 +3,13 @@
namespace Test\Unit;
use BitBadger\PDODocument\FieldMatch;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the FieldMatch enum
*/
#[TestDox('Field Match (Unit tests)')]
class FieldMatchTest extends TestCase
{
public function testToStringSucceedsForAll(): void

View File

@@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Field class
*/
#[TestDox('Field (Unit tests)')]
class FieldTest extends TestCase
{
#[TestDox('Append parameter succeeds for EX')]
@@ -221,7 +222,7 @@ class FieldTest extends TestCase
try {
$field = Field::LE('le_field', 18, '@it');
$field->qualifier = 'q';
$this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(),
$this->assertEquals("(q.data->>'le_field')::numeric <= @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
@@ -248,7 +249,7 @@ class FieldTest extends TestCase
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::EQ('sub.foo.bar', 22, '@it');
$this->assertEquals("data->>'sub.foo.bar' = @it", $field->toWhere(),
$this->assertEquals("(data#>>'{sub,foo,bar}')::numeric = @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;

View File

@@ -3,11 +3,13 @@
namespace Test\Unit\Mapper;
use BitBadger\PDODocument\Mapper\ArrayMapper;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the ArrayMapper class
*/
#[TestDox('Array Mapper (Unit tests)')]
class ArrayMapperTest extends TestCase
{
public function testMapSucceeds(): void

View File

@@ -3,11 +3,13 @@
namespace Test\Unit\Mapper;
use BitBadger\PDODocument\Mapper\CountMapper;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the CountMapper class
*/
#[TestDox('Count Mapper (Unit tests)')]
class CountMapperTest extends TestCase
{
public function testMapSucceeds(): void

View File

@@ -22,6 +22,7 @@ class TestDocument
/**
* Unit tests for the DocumentMapper class
*/
#[TestDox('Document Mapper (Unit tests)')]
class DocumentMapperTest extends TestCase
{
public function testConstructorSucceedsWithDefaultField(): void

View File

@@ -10,6 +10,7 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the ExistsMapper class
*/
#[TestDox('Exists Mapper (Unit tests)')]
class ExistsMapperTest extends TestCase
{
#[TestDox('Map succeeds for PostgreSQL')]

View File

@@ -3,8 +3,13 @@
namespace Test\Unit\Mapper;
use BitBadger\PDODocument\Mapper\StringMapper;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the StringMapper class
*/
#[TestDox('String Mapper (Unit tests)')]
class StringMapperTest extends TestCase
{
public function testMapSucceedsWhenFieldIsPresentAndString()

View File

@@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Op enumeration
*/
#[TestDox('Op (Unit tests)')]
class OpTest extends TestCase
{
#[TestDox('To string succeeds for EQ')]

View File

@@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Parameters class
*/
#[TestDox('Parameters (Unit tests)')]
class ParametersTest extends TestCase
{
#[TestDox('ID succeeds with string')]
@@ -51,7 +52,7 @@ class ParametersTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals([':names' => "ARRAY['one','two','seven']"],
$this->assertEquals([':names' => "{one,two,seven}"],
Parameters::fieldNames(':names', ['one', 'two', 'seven']), 'Field name parameters not correct');
} finally {
Configuration::$mode = null;

View File

@@ -2,30 +2,64 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
use BitBadger\PDODocument\Query\Count;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Count class
*/
#[TestDox('Count Queries (Unit tests)')]
class CountTest extends TestCase
{
public function tearDown(): void
{
Configuration::$mode = null;
parent::tearDown();
}
public function testAllSucceeds()
public function testAllSucceeds(): void
{
$this->assertEquals('SELECT COUNT(*) FROM a_table', Count::all('a_table'),
'SELECT statement not generated correctly');
}
public function testByFieldsSucceeds()
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
try {
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]));
} finally {
Configuration::$mode = null;
}
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]),
'SELECT statement not generated correctly');
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('SELECT COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'),
'SELECT statement not generated correctly');
}
#[TestDox('By contains fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Count::byContains('');
}
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)',
Count::byJsonPath('a_table'), 'SELECT statement not generated correctly');
}
#[TestDox('By JSON Path fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Count::byJsonPath('');
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, DocumentException, Mode};
use BitBadger\PDODocument\{Configuration, DocumentException, DocumentIndex, Mode};
use BitBadger\PDODocument\Query\Definition;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -10,36 +10,34 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Definition class
*/
#[TestDox('Definition Queries (Unit tests)')]
class DefinitionTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
parent::tearDown();
}
#[TestDox('Ensure table succeeds for PosgtreSQL')]
public function testEnsureTableSucceedsForPostgreSQL(): void
{
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;
}
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');
}
#[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;
}
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');
}
public function testEnsureTableFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
Definition::ensureTable('boom');
}
@@ -57,9 +55,30 @@ class DefinitionTest extends TestCase
'CREATE INDEX statement not generated correctly');
}
public function testEnsureKey(): void
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');
}
public function testEnsureDocumentIndexOnSucceedsForSchemaAndFull(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)",
Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full));
}
public function testEnsureDocumentIndexOnSucceedsForNoSchemaAndOptimized(): void
{
Configuration::$mode = 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('Ensure document index on fails for non PostgreSQL')]
public function testEnsureDocumentIndexOnFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Definition::ensureDocumentIndexOn('', DocumentIndex::Full);
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
use BitBadger\PDODocument\Query\Delete;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -10,13 +10,9 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Delete class
*/
#[TestDox('Delete Queries (Unit tests)')]
class DeleteTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
@@ -25,14 +21,46 @@ class DeleteTest extends TestCase
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = :id", Delete::byId('over_there'),
'DELETE statement not constructed correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min",
Delete::byFields('my_table', [Field::LT('value', 99, ':max'), Field::GE('value', 18, ':min')]),
'DELETE statement not constructed correctly');
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('DELETE FROM somewhere WHERE data @> :criteria', Delete::byContains('somewhere'),
'DELETE statement not constructed correctly');
}
#[TestDox('By contains fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Delete::byContains('');
}
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)',
Delete::byJsonPath('here'), 'DELETE statement not constructed correctly');
}
#[TestDox('By JSON Path fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Delete::byJsonPath('');
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode};
use BitBadger\PDODocument\Query\Exists;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -10,13 +10,9 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Exists class
*/
#[TestDox('Exists Queries (Unit tests)')]
class ExistsTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
@@ -24,6 +20,7 @@ class ExistsTest extends TestCase
public function testQuerySucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),
'Existence query not generated correctly');
}
@@ -31,14 +28,46 @@ class ExistsTest extends TestCase
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'),
'Existence query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)",
Exists::byFields('box', [Field::NE('status', 'occupied', ':status')]),
'Existence query not generated correctly');
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM pocket WHERE data @> :criteria)',
Exists::byContains('pocket'), 'Existence query not generated correctly');
}
#[TestDox('By contains fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Exists::byContains('');
}
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM lint WHERE jsonb_path_exists(data, :path::jsonpath))',
Exists::byJsonPath('lint'), 'Existence query not generated correctly');
}
#[TestDox('By JSON Path fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Exists::byJsonPath('');
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\{Configuration, Field, FieldMatch, Mode};
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode};
use BitBadger\PDODocument\Query\Find;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -10,13 +10,9 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Find class
*/
#[TestDox('Find Queries (Unit tests)')]
class FindTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
@@ -25,15 +21,47 @@ class FindTest extends TestCase
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
'SELECT query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')],
FieldMatch::Any),
'SELECT query not generated correctly');
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('SELECT data FROM disc WHERE data @> :criteria', Find::byContains('disc'),
'SELECT query not generated correctly');
}
#[TestDox('By contains fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Find::byContains('');
}
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)',
Find::byJsonPath('light'), 'SELECT query not generated correctly');
}
#[TestDox('By JSON Path fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Find::byJsonPath('');
}
}

View File

@@ -10,71 +10,87 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Patch class
*/
#[TestDox('Patch Queries (Unit tests)')]
class PatchTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
parent::tearDown();
}
#[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;
}
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');
}
#[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;
}
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');
}
#[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;
}
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some",
Patch::byFields('that', [Field::LT('something', 17, ':some')]), 'Patch UPDATE statement is not correct');
}
#[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;
}
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');
}
public function testByFieldsFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
Patch::byFields('oops', []);
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('UPDATE this SET data = data || :data WHERE data @> :criteria', Patch::byContains('this'),
'Patch UPDATE statement is not correct');
}
#[TestDox('By contains fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Patch::byContains('');
}
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('UPDATE that SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)',
Patch::byJsonPath('that'), 'Patch UPDATE statement is not correct');
}
#[TestDox('By JSON Path fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
Patch::byJsonPath('');
}
}

View File

@@ -10,108 +10,118 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the RemoveFields class
*/
#[TestDox('Remove Fields Queries (Unit tests)')]
class RemoveFieldsTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
}
#[TestDox('Update succeeds for PostgreSQL')]
public function testUpdateSucceedsForPostgreSQL(): void
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('UPDATE taco SET data = data - :names WHERE it = true',
RemoveFields::update('taco', [':names' => "ARRAY['one','two']"], 'it = true'),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('UPDATE taco SET data = data - :names::text[] WHERE it = true',
RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'), 'UPDATE statement not correct');
}
#[TestDox('Update succeeds for SQLite')]
public function testUpdateSucceedsForSQLite(): void
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b',
RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b'),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
Configuration::$mode = Mode::SQLite;
$this->assertEquals('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b',
RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b'),
'UPDATE statement not correct');
}
public function testUpdateFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::update('wow', [], '');
}
#[TestDox('By ID succeeds for PostgreSQL')]
public function testByIdSucceedsForPostgreSQL()
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE churro SET data = data - :bite WHERE data->>'id' = :id",
RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id",
RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])), 'UPDATE statement not correct');
}
#[TestDox('By ID succeeds for SQLite')]
public function testByIdSucceedsForSQLite()
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id",
RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte'])),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
Configuration::$mode = Mode::SQLite;
$this->assertEquals("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id",
RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte'])),
'UPDATE statement not correct');
}
#[TestDox('By ID fails when mode not set')]
public function testByIdFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::byId('oof', []);
}
#[TestDox('By fields succeeds for PostgreSQL')]
public function testByFieldsSucceedsForPostgreSQL()
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE enchilada SET data = data - :sauce WHERE data->>'cheese' = :queso",
RemoveFields::byFields('enchilada', [Field::EQ('cheese', 'jack', ':queso')],
Parameters::fieldNames(':sauce', ['white'])),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso",
RemoveFields::byFields('enchilada', [Field::EQ('cheese', 'jack', ':queso')],
Parameters::fieldNames(':sauce', ['white'])),
'UPDATE statement not correct');
}
#[TestDox('By fields succeeds for SQLite')]
public function testByFieldsSucceedsForSQLite()
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals(
"UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice",
RemoveFields::byFields('chimichanga', [Field::EQ('side', 'beans', ':rice')],
Parameters::fieldNames(':filling', ['beef'])),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
}
Configuration::$mode = Mode::SQLite;
$this->assertEquals(
"UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice",
RemoveFields::byFields('chimichanga', [Field::EQ('side', 'beans', ':rice')],
Parameters::fieldNames(':filling', ['beef'])),
'UPDATE statement not correct');
}
public function testByFieldsFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::byFields('boing', [], []);
}
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria',
RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b'])),
'UPDATE statement not correct');
}
#[TestDox('By contains fails for non PostgreSQL')]
public function testByContainsFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
RemoveFields::byContains('', []);
}
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
$this->assertEquals(
'UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)',
RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c'])),
'UPDATE statement not correct');
}
#[TestDox('By JSON Path fails for non PostgreSQL')]
public function testByJsonPathFailsForNonPostgreSQL(): void
{
$this->expectException(DocumentException::class);
RemoveFields::byJsonPath('', []);
}
}

View File

@@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase;
/**
* Unit tests for the Query class
*/
#[TestDox('Query (Unit tests)')]
class QueryTest extends TestCase
{
protected function setUp(): void
@@ -60,6 +61,68 @@ class QueryTest extends TestCase
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
}
public function testWhereDataContainsSucceedsWithDefaultParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('data @> :criteria', Query::whereDataContains(),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
}
public function testWhereDataContainsSucceedsWithSpecifiedParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('data @> :it', Query::whereDataContains(':it'),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('Where data contains fails if not PostgreSQL')]
public function testWhereDataContainsFailsIfNotPostgreSQL(): void
{
Configuration::$mode = null;
$this->expectException(DocumentException::class);
Query::whereDataContains();
}
#[TestDox('Where JSON Path matches succeeds with default parameter')]
public function testWhereJsonPathMatchesSucceedsWithDefaultParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('jsonb_path_exists(data, :path::jsonpath)', Query::whereJsonPathMatches(),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('Where JSON Path matches succeeds with specified parameter')]
public function testWhereJsonPathMatchesSucceedsWithSpecifiedParameter(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals('jsonb_path_exists(data, :road::jsonpath)', Query::whereJsonPathMatches(':road'),
'WHERE fragment not constructed correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('Where JSON Path matches fails if not PostgreSQL')]
public function testWhereJsonPathMatchesFailsIfNotPostgreSQL(): void
{
Configuration::$mode = null;
$this->expectException(DocumentException::class);
Query::whereJsonPathMatches();
}
#[TestDox('Insert succeeds with no auto-ID for PostgreSQL')]
public function testInsertSucceedsWithNoAutoIdForPostgreSQL(): void
{
@@ -90,8 +153,8 @@ class QueryTest extends TestCase
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals(
"INSERT INTO test_tbl VALUES (:data || ('{\"id\":' "
. "|| (SELECT COALESCE(MAX(data->>'id'), 0) + 1 FROM test_tbl) || '}'))",
"INSERT INTO test_tbl VALUES (:data::jsonb || ('{\"id\":' "
. "|| (SELECT COALESCE(MAX((data->>'id')::numeric), 0) + 1 FROM test_tbl) || '}')::jsonb)",
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
} finally {
Configuration::$mode = null;
@@ -118,7 +181,7 @@ class QueryTest extends TestCase
Configuration::$mode = Mode::PgSQL;
try {
$query = Query::insert('test_tbl', AutoId::UUID);
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query,
'INSERT statement not constructed correctly');
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
} finally {
@@ -147,10 +210,10 @@ class QueryTest extends TestCase
Configuration::$idStringLength = 8;
try {
$query = Query::insert('test_tbl', AutoId::RandomString);
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", $query,
'INSERT statement not constructed correctly');
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
$id = str_replace(["INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", "\"}')"], '', $query);
$id = str_replace(["INSERT INTO test_tbl VALUES (:data::jsonb || '{\"id\":\"", "\"}')"], '', $query);
$this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long");
} finally {
Configuration::$mode = null;