|
1 | 1 | # encoding: utf-8
|
| 2 | +from enum import Enum |
| 3 | + |
2 | 4 | import pytest
|
3 | 5 | import sqlalchemy
|
4 |
| -from enum import Enum |
5 | 6 | from flask_babel import lazy_gettext as _
|
6 |
| -from mock import create_autospec, MagicMock |
| 7 | +from mock import MagicMock, create_autospec |
7 | 8 | from parameterized import parameterized
|
8 | 9 | from sqlalchemy.exc import IntegrityError
|
9 | 10 |
|
10 |
| -from ...testing import DatabaseTest |
11 |
| -from ...config import ( |
12 |
| - CannotLoadConfiguration, |
13 |
| - Configuration, |
14 |
| -) |
15 |
| -from ...model import ( |
16 |
| - create, |
17 |
| - get_one, |
18 |
| -) |
| 11 | +from ...config import CannotLoadConfiguration, Configuration |
| 12 | +from ...model import create, get_one |
19 | 13 | from ...model.collection import Collection
|
20 | 14 | from ...model.configuration import (
|
21 |
| - ConfigurationSetting, |
22 |
| - ExternalIntegration, |
23 |
| - ExternalIntegrationLink, |
24 |
| - ConfigurationStorage, |
25 | 15 | ConfigurationAttribute,
|
26 |
| - ConfigurationMetadata, |
| 16 | + ConfigurationAttributeType, |
27 | 17 | ConfigurationGrouping,
|
| 18 | + ConfigurationMetadata, |
28 | 19 | ConfigurationOption,
|
29 |
| - ConfigurationAttributeType |
| 20 | + ConfigurationSetting, |
| 21 | + ConfigurationStorage, |
| 22 | + ExternalIntegration, |
| 23 | + ExternalIntegrationLink, |
| 24 | + HasExternalIntegration, |
30 | 25 | )
|
31 | 26 | from ...model.datasource import DataSource
|
| 27 | +from ...testing import DatabaseTest |
32 | 28 |
|
33 | 29 |
|
34 | 30 | class TestConfigurationSetting(DatabaseTest):
|
@@ -697,6 +693,21 @@ class TestConfiguration(ConfigurationGrouping):
|
697 | 693 | )
|
698 | 694 |
|
699 | 695 |
|
| 696 | +class ConfigurationWithBooleanProperty(ConfigurationGrouping): |
| 697 | + boolean_setting = ConfigurationMetadata( |
| 698 | + key='boolean_setting', |
| 699 | + label='Boolean Setting', |
| 700 | + description='Boolean Setting', |
| 701 | + type=ConfigurationAttributeType.SELECT, |
| 702 | + required=True, |
| 703 | + default='true', |
| 704 | + options=[ |
| 705 | + ConfigurationOption('true', 'True'), |
| 706 | + ConfigurationOption('false', 'False') |
| 707 | + ] |
| 708 | + ) |
| 709 | + |
| 710 | + |
700 | 711 | class TestConfiguration2(ConfigurationGrouping):
|
701 | 712 | setting1 = ConfigurationMetadata(
|
702 | 713 | key='setting1',
|
@@ -837,3 +848,45 @@ def test_to_settings_considers_explicit_indices(self):
|
837 | 848 | assert settings[1][ConfigurationAttribute.REQUIRED.value] == SETTING1_REQUIRED
|
838 | 849 | assert settings[1][ConfigurationAttribute.DEFAULT.value] == SETTING1_DEFAULT
|
839 | 850 | assert settings[1][ConfigurationAttribute.CATEGORY.value] == SETTING1_CATEGORY
|
| 851 | + |
| 852 | + |
| 853 | +class TestBooleanConfigurationMetadata(DatabaseTest): |
| 854 | + @parameterized.expand([ |
| 855 | + ('true', 'true', True), |
| 856 | + ('t', 't', True), |
| 857 | + ('yes', 'yes', True), |
| 858 | + ('y', 'y', True), |
| 859 | + (1, 1, False), |
| 860 | + ('false', 'false', False), |
| 861 | + ]) |
| 862 | + def test_configuration_metadata_correctly_recognize_bool_values(self, _, value, expected_result): |
| 863 | + """Ensure that ConfigurationMetadata.to_bool correctly translates different values into boolean (True/False). |
| 864 | +
|
| 865 | + :param _: Name of the test case |
| 866 | + :type _: str |
| 867 | +
|
| 868 | + :param value: Configuration setting's value |
| 869 | + :type value: Any |
| 870 | +
|
| 871 | + :param expected_result: Expected boolean result |
| 872 | + :type expected_result: bool |
| 873 | + """ |
| 874 | + # Arrange |
| 875 | + external_integration = self._external_integration('test') |
| 876 | + |
| 877 | + external_integration_association = create_autospec(spec=HasExternalIntegration) |
| 878 | + external_integration_association.external_integration = MagicMock(return_value=external_integration) |
| 879 | + |
| 880 | + configuration_storage = ConfigurationStorage(external_integration_association) |
| 881 | + |
| 882 | + configuration = ConfigurationWithBooleanProperty(configuration_storage, self._db) |
| 883 | + |
| 884 | + # We set a new value using ConfigurationMetadata.__set__ |
| 885 | + configuration.boolean_setting = value |
| 886 | + |
| 887 | + # Act |
| 888 | + # We read the existing value using ConfigurationMetadata.__get__ |
| 889 | + result = ConfigurationMetadata.to_bool(configuration.boolean_setting) |
| 890 | + |
| 891 | + # Assert |
| 892 | + assert expected_result == result |
0 commit comments