Skip to content

Commit

Permalink
MDL-83811 dml: Make expected debugging message less specific
Browse files Browse the repository at this point in the history
* Plus minor fixes for better unit test assertions
  • Loading branch information
junpataleta committed Nov 28, 2024
1 parent 4e22073 commit e84d95e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 9 additions & 9 deletions lib/dml/tests/dml_mysqli_read_replica_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,18 @@ public function test_real_readreplica_connect_fail_host(): void {
$this->resetDebugging();
$db2 = \moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
$db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
$this->assertTrue(count($db2->get_records('user')) > 0);
$this->assertNotEmpty($db2->get_records('user'));

$debugging = array_map(function ($d) {
return $d->message;
}, $this->getDebuggingMessages());
$this->resetDebugging();
$this->assertEquals(2, count($debugging));
$this->assertCount(2, $debugging);
$this->assertMatchesRegularExpression(
sprintf(
'/%s%s/',
preg_quote("Readonly db connection failed for host {$invalidhost}:"),
'.* Name or service not known',
$cfg->dbname
'.*'
),
$debugging[0]
);
Expand Down Expand Up @@ -214,22 +213,23 @@ public function test_real_readreplica_connect_fail_dbname(): void {
return $d->message;
}, $this->getDebuggingMessages());
$this->resetDebugging();
$this->assertEquals(2, count($debugging));
$this->assertCount(2, $debugging);
// Read-only attempt to connect to the non-existent replica database will fail.
// Note: The expected regex pattern is a bit generic because the actual error message may vary between operating systems.
$this->assertMatchesRegularExpression(
sprintf(
'/%s%s/',
preg_quote("Readonly db connection failed for host {$cfg->dbhost}: "),
"Access denied for user .* to database '$invaliddb'",
$cfg->dbname
".* database '$invaliddb'"
),
$debugging[0]
);
// Read-write attempt to connect to the non-existent replica database will also fail.
$this->assertMatchesRegularExpression(
sprintf(
'/%s%s/',
preg_quote("Readwrite db connection failed for host {$cfg->dbhost}: "),
'Access denied for user .* '.preg_quote("to database '$invaliddb'"),
$cfg->dbname
".* '$invaliddb"
),
$debugging[1]
);
Expand Down
10 changes: 6 additions & 4 deletions lib/dml/tests/dml_pgsql_read_replica_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,22 +282,24 @@ public function test_real_readreplica_connect_fail(): void {
$this->resetDebugging();
$db2 = moodle_database::get_driver_instance($cfg->dbtype, $cfg->dblibrary);
$db2->connect($cfg->dbhost, $cfg->dbuser, $cfg->dbpass, $cfg->dbname, $cfg->prefix, $cfg->dboptions);
$this->assertTrue(count($db2->get_records('user')) > 0);
$this->assertNotEmpty($db2->get_records('user'));

$debugging = array_map(function ($d) {
return $d->message;
}, $this->getDebuggingMessages());
$this->resetDebugging();
$this->assertEquals(2, count($debugging));
$this->assertCount(2, $debugging);
// Attempt to connect to the non-existent replica host will fail.
// Note: The expected regex pattern is a bit generic because the actual error message may vary between operating systems.
$this->assertMatchesRegularExpression(
sprintf(
'/%s%s/',
preg_quote("Readonly db connection failed for host {$invalidhost}: "),
'.* Name or service not known',
$cfg->dbname
'.*'
),
$debugging[0]
);
// Attempt to connect to the existing DB host will succeed.
$this->assertEquals("Readwrite db connection succeeded for host {$cfg->dbhost}", $debugging[1]);
}
}

0 comments on commit e84d95e

Please sign in to comment.