mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Update database tests to prepare for more (#6500)
* Update database tests to prepare for more Add test_db_name, test_db_user, test_db_pass to allow developers to have a dedicated testing database. Extract DBTestCase to include common functionality for database based tests. Use transactions to isolate tests. Enable STRICT_TRANS_TABLE, test for it's existence (only when we have a user that can set it) Move the database cleanup to register_shutdown_function, this makes it happen every time at the end of tests. If the was not empty, only truncate the tables (that aren't prepopulated) instead of drop the database. Use our schema functions for schema tests. Fix some missing array indexes so it doesn't clutter test output. * Fix style
This commit is contained in:
@@ -27,29 +27,8 @@ namespace LibreNMS\Tests;
|
||||
|
||||
use PHPUnit_Framework_ExpectationFailedException as PHPUnitException;
|
||||
|
||||
class DBSetupTest extends \PHPUnit_Framework_TestCase
|
||||
class DBSetupTest extends DBTestCase
|
||||
{
|
||||
public static function tearDownAfterClass()
|
||||
{
|
||||
if (getenv('DBTEST')) {
|
||||
global $config, $empty_db;
|
||||
|
||||
|
||||
if ($empty_db) {
|
||||
dbQuery("DROP DATABASE " . $config['db_name']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
if (getenv('DBTEST')) {
|
||||
dbConnect();
|
||||
} else {
|
||||
$this->markTestSkipped('Database tests not enabled. Set DBTEST=1 to enable.');
|
||||
}
|
||||
}
|
||||
|
||||
public function testSetupDB()
|
||||
{
|
||||
global $schema;
|
||||
@@ -62,15 +41,9 @@ class DBSetupTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testSchema()
|
||||
{
|
||||
global $config;
|
||||
|
||||
$schema = (int)@dbFetchCell('SELECT `version` FROM `dbSchema` LIMIT 1');
|
||||
$schema = get_db_schema();
|
||||
$this->assertGreaterThan(0, $schema, "Database has no schema!");
|
||||
|
||||
$files = glob($config['install_dir'] . '/sql-schema/*.sql');
|
||||
end($files);
|
||||
$expected = (int)basename(current($files), '.sql');
|
||||
$this->assertEquals($expected, $schema, 'Schema not fully up-to-date');
|
||||
$this->assertTrue(db_schema_is_current(), "Schema not fully up-to-date, at $schema");
|
||||
}
|
||||
|
||||
public function testCheckDBCollation()
|
||||
@@ -114,6 +87,19 @@ class DBSetupTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
global $sql_mode;
|
||||
$this->assertNotNull($sql_mode, 'Query to save SQL Mode in bootstrap.php failed');
|
||||
|
||||
// sql_mode can only be set by users with access
|
||||
$access = array(
|
||||
'GRANT ALL PRIVILEGES ON *.*',
|
||||
'SUPER'
|
||||
);
|
||||
|
||||
if (str_contains(join(PHP_EOL, dbFetchColumn('SHOW GRANTS')), $access)) {
|
||||
$this->assertEquals(
|
||||
array('ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'),
|
||||
dbFetchColumn("SELECT @@global.sql_mode")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testValidateSchema()
|
||||
|
Reference in New Issue
Block a user