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