def test_set_value(self): my_value = "myValue" where_defined = "im defined here" key = "mysection.myName" c = ConfigOption(key) c.set_value(my_value, where_defined) self.assertEqual(my_value, c.value) self.assertEqual(where_defined, c.where_defined)
def test_set_value(self): my_value = 'myValue' where_defined = 'im defined here' key = 'mysection.myName' c = ConfigOption(key) c.set_value(my_value, where_defined) self.assertEqual(my_value, c.value) self.assertEqual(where_defined, c.where_defined)
def test_deprecated_unexpired(self): my_value = 'myValue' where_defined = 'im defined here' key = 'mysection.myName' c = ConfigOption( key, deprecated=True, deprecation_text='dep text', expiration_date='2100-01-01') c.set_value(my_value, where_defined) self.assertFalse(c.is_expired())
def test_replaced_by_expired(self): def config_getter(key): self.assertEqual(key, 'mysection.newName') return 'newValue' c = ConfigOption('mysection.oldName', description='My old description', replaced_by='mysection.newName', expiration_date='2000-01-01', config_getter=config_getter) self.assertTrue(c.deprecated) self.assertTrue(c.is_expired())
def test_deprecated_expired(self): my_value = 'myValue' where_defined = 'im defined here' key = 'mysection.myName' c = ConfigOption( key, deprecated=True, deprecation_text='dep text', expiration_date='2000-01-01') with self.assertRaises(DeprecationError): c.set_value(my_value, where_defined) self.assertTrue(c.is_expired())
def test_replaced_by_expired(self): def config_getter(key): self.assertEqual(key, "mysection.newName") return "newValue" c = ConfigOption( "mysection.oldName", description="My old description", replaced_by="mysection.newName", expiration_date="2000-01-01", config_getter=config_getter, ) self.assertTrue(c.deprecated) self.assertTrue(c.is_expired())
def test_constructor_default_values(self): key = 'mysection.myName' c = ConfigOption(key) self.assertEqual('mysection', c.section) self.assertEqual('myName', c.name) self.assertEqual(None, c.description) self.assertEqual(u'visible', c.visibility)
def test_invalid_key(self): key = 'broken' with pytest.raises(AssertionError) as e: ConfigOption(key) self.assertEqual( 'Key "%s" has invalid format.' % key, str(e.value))
def test_deprecated_unexpired(self): my_value = "myValue" where_defined = "im defined here" key = "mysection.myName" c = ConfigOption( key, deprecated=True, deprecation_text="dep text", expiration_date="2100-01-01", ) c.set_value(my_value, where_defined) self.assertFalse(c.is_expired())
def test_constructor_default_values(self): key = "mysection.myName" c = ConfigOption(key) self.assertEqual("mysection", c.section) self.assertEqual("myName", c.name) self.assertEqual(None, c.description) self.assertEqual("visible", c.visibility)
def test_deprecated_expired(self): my_value = "myValue" where_defined = "im defined here" key = "mysection.myName" c = ConfigOption( key, deprecated=True, deprecation_text="dep text", expiration_date="2000-01-01", ) with self.assertRaises(DeprecationError): c.set_value(my_value, where_defined) self.assertTrue(c.is_expired())
def _create_option(key, description=None, default_val=None, visibility='visible', deprecated=False, deprecation_text=None, expiration_date=None, replaced_by=None): '''Create a ConfigOption and store it globally in this module. There are two ways to create a ConfigOption: (1) Simple, constant config options are created as follows: _create_option('section.optionName', description = 'Put the description here.', default_val = 12345) (2) More complex, programmable config options use decorator syntax to resolve thier values at runtime: @_create_option('section.optionName') def _section_option_name(): """Put the description here.""" return 12345 To achieve this sugar, _create_option() returns a *callable object* of type ConfigObject, which then decorates the function. NOTE: ConfigObjects call their evaluation functions *every time* the option is requested. To prevent this, use the `streamlit.util.memoize` decorator as follows: @_create_option('section.memoizedOptionName') @util.memoize def _section_memoized_option_name(): """Put the description here.""" (This function is only called once.) """ return 12345 ''' option = ConfigOption(key, description=description, default_val=default_val, visibility=visibility, deprecated=deprecated, deprecation_text=deprecation_text, expiration_date=expiration_date, replaced_by=replaced_by) assert option.section in _section_descriptions, ( 'Section "%s" must be one of %s.' % (option.section, ', '.join(_section_descriptions.keys()))) assert key not in _config_options, ('Cannot define option "%s" twice.' % key) _config_options[key] = option return option
def test_call(self): key = 'mysection.myName' c = ConfigOption(key) @c def someRandomFunction(): """Random docstring.""" pass self.assertEqual('Random docstring.', c.description) self.assertEqual(someRandomFunction._get_val_func, c._get_val_func)
def test_value(self): my_value = 'myValue' key = 'mysection.myName' c = ConfigOption(key) @c def someRandomFunction(): """Random docstring.""" return my_value self.assertEqual(my_value, c.value)
def test_call_assert(self): key = 'mysection.myName' c = ConfigOption(key) with pytest.raises(AssertionError) as e: @c def someRandomFunction(): pass self.assertEqual( 'Complex config options require doc strings for their description.', str(e.value))
def test_simple_config_option(self): """Test creating a simple (constant) config option.""" # Create the config option. config_option = ConfigOption( "_test.simpleParam", description="Simple config option.", default_val=12345 ) # Test that it works. self.assertEqual(config_option.key, "_test.simpleParam") self.assertEqual(config_option.section, "_test") self.assertEqual(config_option.name, "simpleParam") self.assertEqual(config_option.description, "Simple config option.") self.assertEqual(config_option.where_defined, ConfigOption.DEFAULT_DEFINITION) self.assertEqual(config_option.value, 12345)
def test_convert_config_option_to_click_option(self): """Test that configurator_options adds dynamic commands based on a config lists. """ config_option = ConfigOption( "server.customKey", description="Custom description.\n\nLine one.", deprecated=False, type_=int, ) result = _convert_config_option_to_click_option(config_option) self.assertEqual(result["option"], "--server.customKey") self.assertEqual(result["param"], "server_customKey") self.assertEqual(result["type"], config_option.type) self.assertEqual(result["description"], config_option.description)
def test_invalid_config_name(self): """Test setting an invalid config section.""" with self.assertRaises(AssertionError): ConfigOption("_test.myParam.")