Add PostgreSQL version of SQLite integ tests

- Add PostgreSQL throwaway database
- Add optional length parameter for random string
- Fix syntax for PostgreSQL in several areas
- Add optional ID for whereById type determination
This commit is contained in:
2024-06-12 15:21:53 -04:00
parent 330e272187
commit 65fd46835c
37 changed files with 1111 additions and 84 deletions

View File

@@ -221,7 +221,7 @@ class FieldTest extends TestCase
try {
$field = Field::LE('le_field', 18, '@it');
$field->qualifier = 'q';
$this->assertEquals("q.data->>'le_field' <= @it", $field->toWhere(),
$this->assertEquals("(q.data->>'le_field')::numeric <= @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;
@@ -248,7 +248,7 @@ class FieldTest extends TestCase
Configuration::$mode = Mode::PgSQL;
try {
$field = Field::EQ('sub.foo.bar', 22, '@it');
$this->assertEquals("data->>'sub.foo.bar' = @it", $field->toWhere(),
$this->assertEquals("(data#>>'{sub,foo,bar}')::numeric = @it", $field->toWhere(),
'WHERE fragment not generated correctly');
} finally {
Configuration::$mode = null;

View File

@@ -51,7 +51,7 @@ class ParametersTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals([':names' => "ARRAY['one','two','seven']"],
$this->assertEquals([':names' => "{one,two,seven}"],
Parameters::fieldNames(':names', ['one', 'two', 'seven']), 'Field name parameters not correct');
} finally {
Configuration::$mode = null;

View File

@@ -49,7 +49,7 @@ class PatchTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE that SET data = data || :data WHERE data->>'something' < :some",
$this->assertEquals("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some",
Patch::byFields('that', [Field::LT('something', 17, ':some')]),
'Patch UPDATE statement is not correct');
} finally {

View File

@@ -17,8 +17,8 @@ class RemoveFieldsTest extends TestCase
{
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'),
$this->assertEquals('UPDATE taco SET data = data - :names::text[] WHERE it = true',
RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'),
'UPDATE statement not correct');
} finally {
Configuration::$mode = null;
@@ -50,7 +50,7 @@ class RemoveFieldsTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE churro SET data = data - :bite WHERE data->>'id' = :id",
$this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id",
RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])),
'UPDATE statement not correct');
} finally {
@@ -84,7 +84,7 @@ class RemoveFieldsTest extends TestCase
{
try {
Configuration::$mode = Mode::PgSQL;
$this->assertEquals("UPDATE enchilada SET data = data - :sauce WHERE data->>'cheese' = :queso",
$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');

View File

@@ -90,8 +90,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 +118,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 +147,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;