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

@@ -18,35 +18,35 @@ class ParametersTest extends TestCase
#[TestDox('ID succeeds with string')]
public function testIdSucceedsWithString(): void
{
$this->assertEquals(['@id' => 'key'], Parameters::id('key'), 'ID parameter not constructed correctly');
$this->assertEquals([':id' => 'key'], Parameters::id('key'), 'ID parameter not constructed correctly');
}
#[TestDox('ID succeeds with non string')]
public function testIdSucceedsWithNonString(): void
{
$this->assertEquals(['@id' => '7'], Parameters::id(7), 'ID parameter not constructed correctly');
$this->assertEquals([':id' => '7'], Parameters::id(7), 'ID parameter not constructed correctly');
}
public function testJsonSucceeds(): void
{
$this->assertEquals(['@it' => '{"id":18,"url":"https://www.unittest.com"}'],
Parameters::json('@it', ['id' => 18, 'url' => 'https://www.unittest.com']),
$this->assertEquals([':it' => '{"id":18,"url":"https://www.unittest.com"}'],
Parameters::json(':it', ['id' => 18, 'url' => 'https://www.unittest.com']),
'JSON parameter not constructed correctly');
}
public function testNameFieldsSucceeds(): void
{
$named = Parameters::nameFields([Field::EQ('it', 17), Field::EQ('also', 22, '@also'), Field::EQ('other', 24)]);
$named = Parameters::nameFields([Field::EQ('it', 17), Field::EQ('also', 22, ':also'), Field::EQ('other', 24)]);
$this->assertCount(3, $named, 'There should be 3 parameters in the array');
$this->assertEquals('@field0', $named[0]->paramName, 'Parameter 1 not named correctly');
$this->assertEquals('@also', $named[1]->paramName, 'Parameter 2 not named correctly');
$this->assertEquals('@field2', $named[2]->paramName, 'Parameter 3 not named correctly');
$this->assertEquals(':field0', $named[0]->paramName, 'Parameter 1 not named correctly');
$this->assertEquals(':also', $named[1]->paramName, 'Parameter 2 not named correctly');
$this->assertEquals(':field2', $named[2]->paramName, 'Parameter 3 not named correctly');
}
public function testAddFieldsSucceeds(): void
{
$this->assertEquals(['@a' => 1, '@b' => 'two', '@z' => 18],
Parameters::addFields([Field::EQ('b', 'two', '@b'), Field::EQ('z', 18, '@z')], ['@a' => 1]),
$this->assertEquals([':a' => 1, ':b' => 'two', ':z' => 18],
Parameters::addFields([Field::EQ('b', 'two', ':b'), Field::EQ('z', 18, ':z')], [':a' => 1]),
'Field parameters not added correctly');
}
@@ -55,8 +55,8 @@ class ParametersTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals(['@names' => "ARRAY['one','two','seven']"],
Parameters::fieldNames('@names', ['one', 'two', 'seven']), 'Field name parameters not correct');
$this->assertEquals([':names' => "ARRAY['one','two','seven']"],
Parameters::fieldNames(':names', ['one', 'two', 'seven']), 'Field name parameters not correct');
} finally {
Configuration::$mode = null;
}
@@ -67,8 +67,8 @@ class ParametersTest extends TestCase
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals(['@it0' => 'test', '@it1' => 'unit', '@it2' => 'wow'],
Parameters::fieldNames('@it', ['test', 'unit', 'wow']), 'Field name parameters not correct');
$this->assertEquals([':it0' => 'test', ':it1' => 'unit', ':it2' => 'wow'],
Parameters::fieldNames(':it', ['test', 'unit', 'wow']), 'Field name parameters not correct');
} finally {
Configuration::$mode = null;
}

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', [], []);
}
}

View File

@@ -20,52 +20,52 @@ class QueryTest extends TestCase
public function testWhereByFieldsSucceedsForSingleField(): void
{
$this->assertEquals("data->>'test_field' <= @it",
Query::whereByFields([Field::LE('test_field', '', '@it')]), 'WHERE fragment not constructed correctly');
$this->assertEquals("data->>'test_field' <= :it",
Query::whereByFields([Field::LE('test_field', '', ':it')]), 'WHERE fragment not constructed correctly');
}
public function testWhereByFieldsSucceedsForMultipleFields(): void
{
$this->assertEquals("data->>'test_field' <= @it AND data->>'other_field' = @other",
Query::whereByFields([Field::LE('test_field', '', '@it'), Field::EQ('other_field', '', '@other')]),
$this->assertEquals("data->>'test_field' <= :it AND data->>'other_field' = :other",
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')]),
'WHERE fragment not constructed correctly');
}
public function testWhereByFieldsSucceedsForMultipleFieldsWithOr(): void
{
$this->assertEquals("data->>'test_field' <= @it OR data->>'other_field' = @other",
Query::whereByFields([Field::LE('test_field', '', '@it'), Field::EQ('other_field', '', '@other')], 'OR'),
$this->assertEquals("data->>'test_field' <= :it OR data->>'other_field' = :other",
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')], 'OR'),
'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');
$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');
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
}
public function testInsertSucceeds(): void
{
$this->assertEquals('INSERT INTO my_table VALUES (@data)', Query::insert('my_table'),
$this->assertEquals('INSERT INTO my_table VALUES (:data)', Query::insert('my_table'),
'INSERT statement not constructed correctly');
}
public function testSaveSucceeds(): void
{
$this->assertEquals(
"INSERT INTO test_tbl VALUES (@data) ON CONFLICT ((data->>'id')) DO UPDATE SET data = EXCLUDED.data",
"INSERT INTO test_tbl VALUES (:data) ON CONFLICT ((data->>'id')) DO UPDATE SET data = EXCLUDED.data",
Query::save('test_tbl'), 'INSERT ON CONFLICT statement not constructed correctly');
}
public function testUpdateSucceeds()
{
$this->assertEquals("UPDATE testing SET data = @data WHERE data->>'id' = @id", Query::update('testing'),
$this->assertEquals("UPDATE testing SET data = :data WHERE data->>'id' = :id", Query::update('testing'),
'UPDATE statement not constructed correctly');
}
}