Issue #3128880 by daffie, alexpott, mondrake: Make ConnectionUnitTest also run for PostgreSQL

(cherry picked from commit 345858969a)
merge-requests/64/head
Alex Pott 2020-05-26 09:19:28 +01:00
parent 101c9f31ff
commit dba227c7b7
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
1 changed files with 37 additions and 21 deletions

View File

@ -47,6 +47,26 @@ class ConnectionUnitTest extends KernelTestBase {
$this->monitor = Database::getConnection('monitor');
}
/**
* Returns a set of queries specific for the database in testing.
*/
protected function getQuery() {
if ($this->connection->databaseType() == 'pgsql') {
return [
'connection_id' => 'SELECT pg_backend_pid()',
'processlist' => 'SELECT pid FROM pg_stat_activity',
'show_tables' => 'SELECT * FROM pg_catalog.pg_tables',
];
}
else {
return [
'connection_id' => 'SELECT CONNECTION_ID()',
'processlist' => 'SHOW PROCESSLIST',
'show_tables' => 'SHOW TABLES',
];
}
}
/**
* Adds a new database connection info to Database.
*/
@ -67,7 +87,7 @@ class ConnectionUnitTest extends KernelTestBase {
* @return int
*/
protected function getConnectionId() {
return (int) Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SELECT CONNECTION_ID()')->fetchField();
return (int) Database::getConnection(static::TEST_TARGET_CONNECTION)->query($this->getQuery()['connection_id'])->fetchField();
}
/**
@ -77,7 +97,7 @@ class ConnectionUnitTest extends KernelTestBase {
* The connection ID to verify.
*/
protected function assertConnection($id) {
$list = $this->monitor->query('SHOW PROCESSLIST')->fetchAllKeyed(0, 0);
$list = $this->monitor->query($this->getQuery()['processlist'])->fetchAllKeyed(0, 0);
return $this->assertTrue(isset($list[$id]), new FormattableMarkup('Connection ID @id found.', ['@id' => $id]));
}
@ -88,7 +108,7 @@ class ConnectionUnitTest extends KernelTestBase {
* The connection ID to verify.
*/
protected function assertNoConnection($id) {
$list = $this->monitor->query('SHOW PROCESSLIST')->fetchAllKeyed(0, 0);
$list = $this->monitor->query($this->getQuery()['processlist'])->fetchAllKeyed(0, 0);
return $this->assertFalse(isset($list[$id]), new FormattableMarkup('Connection ID @id not found.', ['@id' => $id]));
}
@ -98,10 +118,9 @@ class ConnectionUnitTest extends KernelTestBase {
* @todo getConnectionId() executes a query.
*/
public function testOpenClose() {
// Only run this test for the 'mysql' driver.
$driver = $this->connection->driver();
if ($driver !== 'mysql') {
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
// Do not run this test for an SQLite database.
if ($this->connection->databaseType() == 'sqlite') {
$this->markTestSkipped("This tests can not run with an SQLite database.");
}
// Add and open a new connection.
@ -125,10 +144,9 @@ class ConnectionUnitTest extends KernelTestBase {
* Tests Database::closeConnection() with a query.
*/
public function testOpenQueryClose() {
// Only run this test for the 'mysql' driver.
$driver = $this->connection->driver();
if ($driver !== 'mysql') {
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
// Do not run this test for an SQLite database.
if ($this->connection->databaseType() == 'sqlite') {
$this->markTestSkipped("This tests can not run with an SQLite database.");
}
// Add and open a new connection.
@ -140,7 +158,7 @@ class ConnectionUnitTest extends KernelTestBase {
$this->assertConnection($id);
// Execute a query.
Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SHOW TABLES');
Database::getConnection(static::TEST_TARGET_CONNECTION)->query($this->getQuery()['show_tables']);
// Close the connection.
Database::closeConnection(static::TEST_TARGET_CONNECTION);
@ -155,10 +173,9 @@ class ConnectionUnitTest extends KernelTestBase {
* Tests Database::closeConnection() with a query and custom prefetch method.
*/
public function testOpenQueryPrefetchClose() {
// Only run this test for the 'mysql' driver.
$driver = $this->connection->driver();
if ($driver !== 'mysql') {
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
// Do not run this test for an SQLite database.
if ($this->connection->databaseType() == 'sqlite') {
$this->markTestSkipped("This tests can not run with an SQLite database.");
}
// Add and open a new connection.
@ -170,7 +187,7 @@ class ConnectionUnitTest extends KernelTestBase {
$this->assertConnection($id);
// Execute a query.
Database::getConnection(static::TEST_TARGET_CONNECTION)->query('SHOW TABLES')->fetchCol();
Database::getConnection(static::TEST_TARGET_CONNECTION)->query($this->getQuery()['show_tables'])->fetchCol();
// Close the connection.
Database::closeConnection(static::TEST_TARGET_CONNECTION);
@ -185,10 +202,9 @@ class ConnectionUnitTest extends KernelTestBase {
* Tests Database::closeConnection() with a select query.
*/
public function testOpenSelectQueryClose() {
// Only run this test for the 'mysql' driver.
$driver = $this->connection->driver();
if ($driver !== 'mysql') {
$this->markTestSkipped("MySql tests can not run for driver '$driver'.");
// Do not run this test for an SQLite database.
if ($this->connection->databaseType() == 'sqlite') {
$this->markTestSkipped("This tests can not run with an SQLite database.");
}
// Add and open a new connection.