def test_default_realint(self): """ Test if default int variable is returned if given as string Note: In the configuration file, an int can be given as string, so the string should be converted to an int. For the wrapper a string value has to work as well. """ config = RawConfigParser() configtext = """ [test] """ config.read_string(configtext) with self.assertRaises(NoOptionError): _ = config.getfloat("test", "var1") wrap = ConfigWrapper( config, { "var1": { "default": "1", "description": "this is just a test for int var1" } }) var1 = wrap.getint("test", "var1") print(f"From ConfigWrapper -> var1: {var1}") self.assertIsInstance(var1, int) self.assertEqual(var1, 1)
def test_existing_int(self): """Test if int variable given in config file is correctly returned""" config = RawConfigParser() configtext = """ [test] var1 = 10 """ config.read_string(configtext) var1 = config.getint("test", "var1") print(f"From RawConfigParser -> var1: {var1}") self.assertEqual(var1, 10) wrap = ConfigWrapper( config, { "var1": { "default": -1, "description": "this is just a test for integer var1" } }) var1 = wrap.getint("test", "var1") print(f"From ConfigWrapper -> var1: {var1}") self.assertIsInstance(var1, int) self.assertEqual(var1, 10)
def test_nodefault_exception_int(self): """Test if undefined or value without default raises exception""" config = RawConfigParser() configtext = """ [test] """ config.read_string(configtext) with self.assertRaises(NoOptionError): _ = config.getint("test", "var1") with self.assertRaises(NoOptionError): _ = config.getint("test", "var2") wrap = ConfigWrapper( config, {"var1": { "description": "this is just a test for string var1" }}) with self.assertRaises(NoOptionError): _ = wrap.getint("test", "var2") with self.assertRaises(NoOptionError): _ = wrap.getint("test", "var1")
def test_manual_fallback_int(self): """Test if explicit fallback int has precedence""" config = RawConfigParser() configtext = """ [test] """ config.read_string(configtext) with self.assertRaises(NoOptionError): _ = config.getint("test", "var1") wrap = ConfigWrapper( config, { "var1": { "default": -1, "description": "this is just a test for int var1" } }) var1 = wrap.getint("test", "var1", fallback=999) print(f"From ConfigWrapper -> var1: {var1}") self.assertEqual(var1, 999) self.assertIsInstance(var1, int)