From 2aeef38c7b45485e25a5ef46ac6f6176d3afc1bb Mon Sep 17 00:00:00 2001 From: David Woloszyn Date: Wed, 5 Feb 2025 11:55:35 +1100 Subject: [PATCH] MDL-84107 core_analytics: Skip tests if mlbackend_python not configured --- .../classes/mlbackend_configuration_trait.php | 40 +++++++++++++++++++ analytics/tests/manager_test.php | 12 ++++++ analytics/tests/model_test.php | 19 +++++++++ analytics/tests/privacy/provider_test.php | 6 +++ analytics/tests/stats_test.php | 11 +++++ course/tests/analytics/indicators_test.php | 7 ++++ 6 files changed, 95 insertions(+) create mode 100644 analytics/tests/classes/mlbackend_configuration_trait.php diff --git a/analytics/tests/classes/mlbackend_configuration_trait.php b/analytics/tests/classes/mlbackend_configuration_trait.php new file mode 100644 index 00000000000..3c2ddc9b22e --- /dev/null +++ b/analytics/tests/classes/mlbackend_configuration_trait.php @@ -0,0 +1,40 @@ +. + +namespace core_analytics\tests; + +/** + * A trait to check machine learning configurations. + * + * @package core_analytics + * @category test + * @copyright 2024 David Woloszyn + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +trait mlbackend_configuration_trait { + /** + * Check if mlbackend_python is configured. + * + * @return bool + */ + public static function is_mlbackend_python_configured(): bool { + if (defined('TEST_MLBACKEND_PYTHON_HOST') && defined('TEST_MLBACKEND_PYTHON_PORT') + && defined('TEST_MLBACKEND_PYTHON_USERNAME') && defined('TEST_MLBACKEND_PYTHON_USERNAME')) { + return true; + } + return false; + } +} diff --git a/analytics/tests/manager_test.php b/analytics/tests/manager_test.php index 589f6021e5a..9688fa95767 100644 --- a/analytics/tests/manager_test.php +++ b/analytics/tests/manager_test.php @@ -16,6 +16,8 @@ namespace core_analytics; +use core_analytics\tests\mlbackend_configuration_trait; + defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/fixtures/test_indicator_max.php'); @@ -32,12 +34,18 @@ require_once(__DIR__ . '/fixtures/test_target_course_level_shortname.php'); * @covers \core_analytics\manager */ final class manager_test extends \advanced_testcase { + use mlbackend_configuration_trait; + /** * test_deleted_context */ public function test_deleted_context(): void { global $DB; + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); $this->setAdminuser(); set_config('enabled_stores', 'logstore_standard', 'tool_log'); @@ -115,6 +123,10 @@ final class manager_test extends \advanced_testcase { public function test_deleted_analysable(): void { global $DB; + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); $this->setAdminuser(); set_config('enabled_stores', 'logstore_standard', 'tool_log'); diff --git a/analytics/tests/model_test.php b/analytics/tests/model_test.php index 6c6ed23ebf4..1ae9fa58e7f 100644 --- a/analytics/tests/model_test.php +++ b/analytics/tests/model_test.php @@ -24,6 +24,8 @@ namespace core_analytics; +use core_analytics\tests\mlbackend_configuration_trait; + defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/fixtures/test_indicator_max.php'); @@ -42,6 +44,7 @@ require_once(__DIR__ . '/fixtures/test_analysis.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class model_test extends \advanced_testcase { + use mlbackend_configuration_trait; /** @var model Store Model. */ protected $model; @@ -95,6 +98,10 @@ final class model_test extends \advanced_testcase { public function test_delete(): void { global $DB; + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); set_config('enabled_stores', 'logstore_standard', 'tool_log'); @@ -140,6 +147,10 @@ final class model_test extends \advanced_testcase { public function test_clear(): void { global $DB; + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); set_config('enabled_stores', 'logstore_standard', 'tool_log'); @@ -379,6 +390,10 @@ final class model_test extends \advanced_testcase { * Test that import_model import models' configurations. */ public function test_import_model_config(): void { + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); $this->model->enable('\\core\\analytics\\time_splitting\\quarters'); @@ -421,6 +436,10 @@ final class model_test extends \advanced_testcase { * Test export_config */ public function test_export_config(): void { + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); $this->model->enable('\\core\\analytics\\time_splitting\\quarters'); diff --git a/analytics/tests/privacy/provider_test.php b/analytics/tests/privacy/provider_test.php index 7cdca40edc1..14fc1d95b65 100644 --- a/analytics/tests/privacy/provider_test.php +++ b/analytics/tests/privacy/provider_test.php @@ -28,6 +28,7 @@ use core_privacy\local\request\transform; use core_privacy\local\request\writer; use core_privacy\local\request\approved_contextlist; use core_privacy\local\request\approved_userlist; +use core_analytics\tests\mlbackend_configuration_trait; defined('MOODLE_INTERNAL') || die(); @@ -44,6 +45,7 @@ require_once(__DIR__ . '/../fixtures/test_target_course_users.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class provider_test extends \core_privacy\tests\provider_testcase { + use mlbackend_configuration_trait; /** @var \core_analytics\model Store Model 1. */ protected $model1; @@ -88,6 +90,10 @@ final class provider_test extends \core_privacy\tests\provider_testcase { protected $c2; public function setUp(): void { + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + parent::setUp(); $this->resetAfterTest(true); diff --git a/analytics/tests/stats_test.php b/analytics/tests/stats_test.php index fde0e36a717..3acdfe4bad6 100644 --- a/analytics/tests/stats_test.php +++ b/analytics/tests/stats_test.php @@ -16,6 +16,8 @@ namespace core_analytics; +use core_analytics\tests\mlbackend_configuration_trait; + defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/fixtures/test_indicator_fullname.php'); @@ -30,6 +32,7 @@ require_once(__DIR__ . '/fixtures/test_target_shortname.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class stats_test extends \advanced_testcase { + use mlbackend_configuration_trait; /** * Set up the test environment. @@ -70,6 +73,9 @@ final class stats_test extends \advanced_testcase { * Test the {@link \core_analytics\stats::predictions()} implementation. */ public function test_predictions(): void { + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } $this->resetAfterTest(true); @@ -116,6 +122,11 @@ final class stats_test extends \advanced_testcase { */ public function test_actions(): void { global $DB; + + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); $model = \core_analytics\model::create( diff --git a/course/tests/analytics/indicators_test.php b/course/tests/analytics/indicators_test.php index 5b1b9397ec9..a1b633291de 100644 --- a/course/tests/analytics/indicators_test.php +++ b/course/tests/analytics/indicators_test.php @@ -16,6 +16,8 @@ namespace core_course\analytics; +use core_analytics\tests\mlbackend_configuration_trait; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -32,6 +34,7 @@ require_once(__DIR__ . '/../../../analytics/tests/fixtures/test_target_course_us * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class indicators_test extends \advanced_testcase { + use mlbackend_configuration_trait; /** * test_no_teacher @@ -316,6 +319,10 @@ final class indicators_test extends \advanced_testcase { public function test_activities_due(): void { global $DB; + if (!self::is_mlbackend_python_configured()) { + $this->markTestSkipped('mlbackend_python is not configured.'); + } + $this->resetAfterTest(true); $this->setAdminuser();