Add PostgreSQL Support (#3)
Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
@@ -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('', []);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user