-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for the DependencyNotInstalled exception for xgboost-…
…sampler and gp-sampler
- Loading branch information
1 parent
b9a8834
commit bd5c2fe
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Black-box ABM Calibration Kit (Black-it) | ||
# Copyright (C) 2021-2023 Banca d'Italia | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Test utilities to test the correct working of the extras mechanism.""" | ||
import contextlib | ||
from types import ModuleType | ||
from typing import Any, Callable, Generator | ||
|
||
import pytest | ||
|
||
from black_it._load_dependency import DependencyNotInstalled | ||
|
||
|
||
@contextlib.contextmanager | ||
def _change_variable_value( | ||
module_obj: ModuleType, variable_name: str, value: Any | ||
) -> Generator[None, None, None]: | ||
"""Change, temporarily, the value of a variable in a module.""" | ||
old_value = getattr(module_obj, variable_name) | ||
setattr(module_obj, variable_name, value) | ||
yield | ||
setattr(module_obj, variable_name, old_value) | ||
|
||
|
||
def generic_test_import_error( | ||
module_obj: ModuleType, | ||
import_error_global_variable_name: str, | ||
component_initializer: Callable, | ||
expected_message_pattern: str, | ||
) -> None: | ||
""" | ||
Test that the correct exception is raised when a dependency is not installed. | ||
This function is an utility testing function to test that the correct exception is raised when a dependency is not | ||
installed. | ||
It assumes the module under testing, associated to some Black-it component (e.g. loss, sampler, etc.), has a global | ||
variable named import_error_global_variable_name, which is set to None if the dependency is installed, or to a | ||
DependencyNotInstalled exception if the dependency is not installed. This convention is the one used in the | ||
Black-it code. | ||
For example, samplers.xgboost has a global variable named _XGBOOST_IMPORT_ERROR, which is set to None if the xgboost | ||
package is installed, or to an DependencyNotInstalled if the xgboost package is not installed. Then, during the | ||
initialization of XGBoostSampler, the value of the _XGBOOST_IMPORT_ERROR variable is checked, and if it is not | ||
None, an exception is raised. | ||
This test function checks that the correct exception is raised when the import_error_global_variable_name variable | ||
is set to None and we try to initialize the Black-it component. | ||
Args: | ||
module_obj: the module object to test | ||
import_error_global_variable_name: the name of the global variable in the module object | ||
component_initializer: the function to call to initialize the component under testing | ||
expected_message_pattern: the pattern of the expected exception message | ||
""" | ||
with _change_variable_value( | ||
module_obj, import_error_global_variable_name, ImportError("fake error") | ||
): | ||
with pytest.raises(DependencyNotInstalled, match=expected_message_pattern): | ||
component_initializer() |