Finish tests

- Add sub-doc handling for SQLite fields
- Add casting for PostgreSQL BT on numeric value
- Add hasItems() to DocumentList
- Fix SQL syntax problems exposed by tests
This commit is contained in:
2024-06-08 10:49:52 -04:00
parent a96fdf9d14
commit 2d8f8b6e87
21 changed files with 691 additions and 42 deletions

View File

@@ -2,7 +2,7 @@
namespace Test\Unit;
use BitBadger\PDODocument\{Field, Op};
use BitBadger\PDODocument\{Configuration, Field, Mode, Op};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -36,46 +36,236 @@ class FieldTest extends TestCase
'Field parameter not returned correctly');
}
#[TestDox('To where succeeds for EX without qualifier')]
public function testToWhereSucceedsForEXWithoutQualifier(): void
#[TestDox('To where succeeds for EX without qualifier for PostgreSQL')]
public function testToWhereSucceedsForEXWithoutQualifierForPostgreSQL(): void
{
$this->assertEquals("data->>'that_field' IS NOT NULL", Field::EX('that_field')->toWhere(),
'WHERE fragment not generated correctly');
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals("data->>'that_field' IS NOT NULL", Field::EX('that_field')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for NEX without qualifier')]
public function testToWhereSucceedsForNEXWithoutQualifier(): void
#[TestDox('To where succeeds for EX without qualifier for SQLite')]
public function testToWhereSucceedsForEXWithoutQualifierForSQLite(): void
{
$this->assertEquals("data->>'a_field' IS NULL", Field::NEX('a_field')->toWhere(),
'WHERE fragment not generated correctly');
Configuration::$mode = Mode::SQLite;
try {
$this->assertEquals("data->>'that_field' IS NOT NULL", Field::EX('that_field')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for BT without qualifier')]
public function testToWhereSucceedsForBTWithoutQualifier(): void
#[TestDox('To where succeeds for NEX without qualifier for PostgreSQL')]
public function testToWhereSucceedsForNEXWithoutQualifierForPostgreSQL(): void
{
$this->assertEquals("data->>'age' BETWEEN @agemin AND @agemax", Field::BT('age', 13, 17, '@age')->toWhere(),
'WHERE fragment not generated correctly');
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals("data->>'a_field' IS NULL", Field::NEX('a_field')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
public function testToWhereSucceedsForOthersWithoutQualifier(): void
#[TestDox('To where succeeds for NEX without qualifier for SQLite')]
public function testToWhereSucceedsForNEXWithoutQualifierForSQLite(): void
{
$this->assertEquals("data->>'some_field' = @value", Field::EQ('some_field', '', '@value')->toWhere(),
'WHERE fragment not generated correctly');
Configuration::$mode = Mode::SQLite;
try {
$this->assertEquals("data->>'a_field' IS NULL", Field::NEX('a_field')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
public function testToWhereSucceedsWithQualifierNoParameter(): void
#[TestDox('To where succeeds for BT without qualifier for SQLite')]
public function testToWhereSucceedsForBTWithoutQualifierForSQLite(): void
{
$field = Field::EX('no_field');
$field->qualifier = 'test';
$this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(),
'WHERE fragment not generated correctly');
Configuration::$mode = Mode::SQLite;
try {
$this->assertEquals("data->>'age' BETWEEN @agemin AND @agemax", Field::BT('age', 13, 17, '@age')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
public function testToWhereSucceedsWithQualifierAndParameter(): void
#[TestDox('To where succeeds for BT without qualifier for PostgreSQL with numeric range')]
public function testToWhereSucceedsForBTWithoutQualifierForPostgreSQLWithNumericRange(): void
{
$field = Field::LE('le_field', 18, '@it');
$field->qualifier = 'q';
$this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(), 'WHERE fragment not generated correctly');
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals("(data->>'age')::numeric BETWEEN @agemin AND @agemax",
Field::BT('age', 13, 17, '@age')->toWhere(), 'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for BT without qualifier for PostgreSQL with non-numeric range')]
public function testToWhereSucceedsForBTWithoutQualifierForPostgreSQLWithNonNumericRange(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals("data->>'city' BETWEEN :citymin AND :citymax",
Field::BT('city', 'Atlanta', 'Chicago', ':city')->toWhere(), 'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for BT with qualifier for SQLite')]
public function testToWhereSucceedsForBTWithQualifierForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
try {
$field = Field::BT('age', 13, 17, '@age');
$field->qualifier = 'me';
$this->assertEquals("me.data->>'age' BETWEEN @agemin AND @agemax", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for BT with qualifier for PostgreSQL with numeric range')]
public function testToWhereSucceedsForBTWithQualifierForPostgreSQLWithNumericRange(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::BT('age', 13, 17, '@age');
$field->qualifier = 'me';
$this->assertEquals("(me.data->>'age')::numeric BETWEEN @agemin AND @agemax", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for BT with qualifier for PostgreSQL with non-numeric range')]
public function testToWhereSucceedsForBTWithQualifierForPostgreSQLWithNonNumericRange(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::BT('city', 'Atlanta', 'Chicago', ':city');
$field->qualifier = 'me';
$this->assertEquals("me.data->>'city' BETWEEN :citymin AND :citymax", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for others without qualifier for PostgreSQL')]
public function testToWhereSucceedsForOthersWithoutQualifierForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$this->assertEquals("data->>'some_field' = @value", Field::EQ('some_field', '', '@value')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds for others without qualifier for SQLite')]
public function testToWhereSucceedsForOthersWithoutQualifierForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
try {
$this->assertEquals("data->>'some_field' = @value", Field::EQ('some_field', '', '@value')->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds with qualifier no parameter for PostgreSQL')]
public function testToWhereSucceedsWithQualifierNoParameterForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::EX('no_field');
$field->qualifier = 'test';
$this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds with qualifier no parameter for SQLite')]
public function testToWhereSucceedsWithQualifierNoParameterForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
try {
$field = Field::EX('no_field');
$field->qualifier = 'test';
$this->assertEquals("test.data->>'no_field' IS NOT NULL", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds with qualifier and parameter for PostgreSQL')]
public function testToWhereSucceedsWithQualifierAndParameterForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::LE('le_field', 18, '@it');
$field->qualifier = 'q';
$this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds with qualifier and parameter for SQLite')]
public function testToWhereSucceedsWithQualifierAndParameterForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
try {
$field = Field::LE('le_field', 18, '@it');
$field->qualifier = 'q';
$this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds with sub-document for PostgreSQL')]
public function testToWhereSucceedsWithSubDocumentForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::EQ('sub.foo.bar', 22, '@it');
$this->assertEquals("data->>'sub.foo.bar' = @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('To where succeeds with sub-document for SQLite')]
public function testToWhereSucceedsWithSubDocumentForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
try {
$field = Field::EQ('sub.foo.bar', 22, '@it');
$this->assertEquals("data->>'sub'->>'foo'->>'bar' = @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
}
}
#[TestDox('EQ succeeds without parameter')]

View File

@@ -63,7 +63,7 @@ class ParametersTest extends TestCase
{
try {
Configuration::$mode = Mode::SQLite;
$this->assertEquals([':it0' => 'test', ':it1' => 'unit', ':it2' => 'wow'],
$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

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\Query\Count;
use PHPUnit\Framework\TestCase;
@@ -20,7 +20,12 @@ 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')]));
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;
}
}
}

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\Query\Delete;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -12,6 +12,16 @@ use PHPUnit\Framework\TestCase;
*/
class DeleteTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
}
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\Query\Exists;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -12,6 +12,16 @@ use PHPUnit\Framework\TestCase;
*/
class ExistsTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
}
public function testQuerySucceeds(): void
{
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),

View File

@@ -2,7 +2,7 @@
namespace Test\Unit\Query;
use BitBadger\PDODocument\Field;
use BitBadger\PDODocument\{Configuration, Field, Mode};
use BitBadger\PDODocument\Query\Find;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -12,6 +12,16 @@ use PHPUnit\Framework\TestCase;
*/
class FindTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
}
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{

View File

@@ -2,7 +2,7 @@
namespace Test\Unit;
use BitBadger\PDODocument\{Field, Query};
use BitBadger\PDODocument\{Configuration, Field, Mode, Query};
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
@@ -11,6 +11,16 @@ use PHPUnit\Framework\TestCase;
*/
class QueryTest extends TestCase
{
protected function setUp(): void
{
Configuration::$mode = Mode::SQLite;
}
protected function tearDown(): void
{
Configuration::$mode = null;
}
public function testSelectFromTableSucceeds(): void
{
$this->assertEquals('SELECT data FROM testing', Query::selectFromTable('testing'),