Change param prefix from @ to :

This commit is contained in:
2024-06-04 20:59:24 -04:00
parent 7390ae0f61
commit 3d45bbcabc
13 changed files with 175 additions and 54 deletions

View File

@@ -20,7 +20,7 @@ class CountTest extends TestCase
public function testByFieldsSucceeds()
{
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > @errors",
Count::byFields('somewhere', [Field::GT('errors', 10, '@errors')]));
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]));
}
}

View File

@@ -15,14 +15,14 @@ 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'),
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = :id", Delete::byId('over_there'),
'DELETE statement not constructed correctly');
}
public function testByFieldsSucceeds(): void
{
$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')]),
$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');
}
}

View File

@@ -21,14 +21,14 @@ class ExistsTest extends TestCase
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = @id)", Exists::byId('dox'),
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'),
'Existence query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> @status)",
Exists::byFields('box', [Field::NE('status', 'occupied', '@status')]),
$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');
}
}

View File

@@ -15,14 +15,14 @@ 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'),
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
'SELECT query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
$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')], 'OR'),
$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')], 'OR'),
'SELECT query not generated correctly');
}
}

View File

@@ -20,7 +20,7 @@ class PatchTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE doc_table SET data = data || @data WHERE data->>'id' = @id",
$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;
@@ -32,7 +32,7 @@ class PatchTest extends TestCase
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals("UPDATE my_table SET data = json_patch(data, json(@data)) WHERE data->>'id' = @id",
$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;
@@ -52,8 +52,8 @@ class PatchTest extends TestCase
{
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')]),
$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;
@@ -66,8 +66,8 @@ class PatchTest extends TestCase
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')]),
"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;

View File

@@ -0,0 +1,121 @@
<?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\Parameters;
use BitBadger\PDODocument\Query\RemoveFields;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the RemoveFields class
*/
class RemoveFieldsTest extends TestCase
{
#[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;
}
}
#[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;
}
}
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;
}
}
#[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;
}
}
#[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;
}
}
#[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;
}
}
public function testByFieldsFailsWhenModeNotSet(): void
{
$this->expectException(DocumentException::class);
Configuration::$mode = null;
RemoveFields::byFields('boing', [], []);
}
}