Unverified Commit e17cce70 authored by alexpott's avatar alexpott
Browse files

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

(cherry picked from commit 34585896)
parent f891a9bd
......@@ -47,6 +47,26 @@ protected function setUp(): void {
$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 @@ protected function addConnection() {
* @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 @@ protected function getConnectionId() {
* 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 @@ protected function assertConnection($id) {
* 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 @@ protected function assertNoConnection($id) {
* @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 @@ public function testOpenClose() {
* 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 @@ public function testOpenQueryClose() {
$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 @@ public function testOpenQueryClose() {
* 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 @@ public function testOpenQueryPrefetchClose() {
$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 @@ public function testOpenQueryPrefetchClose() {
* 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.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment