def test_set_per_key_priorities(self): attribute = SettingsAttribute( BaseSettings({'one': 10, 'two': 20}, 0), 0) new_dict = {'one': 11, 'two': 21} attribute.set(new_dict, 10) self.assertEqual(attribute.value['one'], 11) self.assertEqual(attribute.value['two'], 21) new_settings = BaseSettings() new_settings.set('one', 12, 20) new_settings.set('two', 12, 0) attribute.set(new_settings, 0) self.assertEqual(attribute.value['one'], 12) self.assertEqual(attribute.value['two'], 21)
def test_repr(self): settings = BaseSettings() self.assertEqual(repr(settings), "<BaseSettings {}>") attr = SettingsAttribute('testval', 15) settings['testkey'] = attr self.assertEqual(repr(settings), "<BaseSettings {'testkey': %s}>" % repr(attr))
def test_set_instance_identity_on_update(self): attr = SettingsAttribute('value', 0) self.settings.attributes = {'TEST_OPTION': attr} self.settings.set('TEST_OPTION', 'othervalue', 10) self.assertIn('TEST_OPTION', self.settings.attributes) self.assertIs(attr, self.settings.attributes['TEST_OPTION'])
def test_get(self): test_configuration = { 'TEST_ENABLED1': '1', 'TEST_ENABLED2': True, 'TEST_ENABLED3': 1, 'TEST_DISABLED1': '0', 'TEST_DISABLED2': False, 'TEST_DISABLED3': 0, 'TEST_INT1': 123, 'TEST_INT2': '123', 'TEST_FLOAT1': 123.45, 'TEST_FLOAT2': '123.45', 'TEST_LIST1': ['one', 'two'], 'TEST_LIST2': 'one,two', 'TEST_STR': 'value', 'TEST_DICT1': {'key1': 'val1', 'ke2': 3}, 'TEST_DICT2': '{"key1": "val1", "ke2": 3}', } settings = self.settings settings.attributes = {key: SettingsAttribute(value, 0) for key, value in six.iteritems(test_configuration)} self.assertTrue(settings.getbool('TEST_ENABLED1')) self.assertTrue(settings.getbool('TEST_ENABLED2')) self.assertTrue(settings.getbool('TEST_ENABLED3')) self.assertFalse(settings.getbool('TEST_ENABLEDx')) self.assertTrue(settings.getbool('TEST_ENABLEDx', True)) self.assertFalse(settings.getbool('TEST_DISABLED1')) self.assertFalse(settings.getbool('TEST_DISABLED2')) self.assertFalse(settings.getbool('TEST_DISABLED3')) self.assertEqual(settings.getint('TEST_INT1'), 123) self.assertEqual(settings.getint('TEST_INT2'), 123) self.assertEqual(settings.getint('TEST_INTx'), 0) self.assertEqual(settings.getint('TEST_INTx', 45), 45) self.assertEqual(settings.getfloat('TEST_FLOAT1'), 123.45) self.assertEqual(settings.getfloat('TEST_FLOAT2'), 123.45) self.assertEqual(settings.getfloat('TEST_FLOATx'), 0.0) self.assertEqual(settings.getfloat('TEST_FLOATx', 55.0), 55.0) self.assertEqual(settings.getlist('TEST_LIST1'), ['one', 'two']) self.assertEqual(settings.getlist('TEST_LIST2'), ['one', 'two']) self.assertEqual(settings.getlist('TEST_LISTx'), []) self.assertEqual(settings.getlist('TEST_LISTx', ['default']), ['default']) self.assertEqual(settings['TEST_STR'], 'value') self.assertEqual(settings.get('TEST_STR'), 'value') self.assertEqual(settings['TEST_STRx'], None) self.assertEqual(settings.get('TEST_STRx'), None) self.assertEqual(settings.get('TEST_STRx', 'default'), 'default') self.assertEqual(settings.getdict('TEST_DICT1'), {'key1': 'val1', 'ke2': 3}) self.assertEqual(settings.getdict('TEST_DICT2'), {'key1': 'val1', 'ke2': 3}) self.assertEqual(settings.getdict('TEST_DICT3'), {}) self.assertEqual(settings.getdict('TEST_DICT3', {'key1': 5}), {'key1': 5}) self.assertRaises(ValueError, settings.getdict, 'TEST_LIST1')
def test_set_calls_settings_attributes_methods_on_update(self): attr = SettingsAttribute('value', 10) with mock.patch.object(attr, '__setattr__') as mock_setattr, \ mock.patch.object(attr, 'set') as mock_set: self.settings.attributes = {'TEST_OPTION': attr} for priority in (0, 10, 20): self.settings.set('TEST_OPTION', 'othervalue', priority) mock_set.assert_called_once_with('othervalue', priority) self.assertFalse(mock_setattr.called) mock_set.reset_mock() mock_setattr.reset_mock()
def test_overwrite_basesettings(self): original_dict = {'one': 10, 'two': 20} original_settings = BaseSettings(original_dict, 0) attribute = SettingsAttribute(original_settings, 0) new_dict = {'three': 11, 'four': 21} attribute.set(new_dict, 10) self.assertIsInstance(attribute.value, BaseSettings) six.assertCountEqual(self, attribute.value, new_dict) six.assertCountEqual(self, original_settings, original_dict) new_settings = BaseSettings({'five': 12}, 0) attribute.set(new_settings, 0) # Insufficient priority six.assertCountEqual(self, attribute.value, new_dict) attribute.set(new_settings, 10) six.assertCountEqual(self, attribute.value, new_settings)
def test_set_per_key_priorities(self): attribute = SettingsAttribute(BaseSettings({ 'one': 10, 'two': 20 }, 0), 0) new_dict = {'one': 11, 'two': 21} attribute.set(new_dict, 10) self.assertEqual(attribute.value['one'], 11) self.assertEqual(attribute.value['two'], 21) new_settings = BaseSettings() new_settings.set('one', 12, 20) new_settings.set('two', 12, 0) attribute.set(new_settings, 0) self.assertEqual(attribute.value['one'], 12) self.assertEqual(attribute.value['two'], 21)
class SettingsAttributeTest(unittest.TestCase): def setUp(self): self.attribute = SettingsAttribute('value', 10) def test_set_greater_priority(self): self.attribute.set('value2', 20) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 20) def test_set_equal_priority(self): self.attribute.set('value2', 10) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 10) def test_set_less_priority(self): self.attribute.set('value2', 0) self.assertEqual(self.attribute.value, 'value') self.assertEqual(self.attribute.priority, 10)
class SettingsAttributeTest(unittest.TestCase): def setUp(self): self.attribute = SettingsAttribute('value', 10) def test_set_greater_priority(self): self.attribute.set('value2', 20) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 20) def test_set_equal_priority(self): self.attribute.set('value2', 10) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 10) def test_set_less_priority(self): self.attribute.set('value2', 0) self.assertEqual(self.attribute.value, 'value') self.assertEqual(self.attribute.priority, 10) def test_set_per_key_priorities(self): attribute = SettingsAttribute(BaseSettings({ 'one': 10, 'two': 20 }, 0), 0) new_dict = {'one': 11, 'two': 21} attribute.set(new_dict, 10) self.assertEqual(attribute.value['one'], 11) self.assertEqual(attribute.value['two'], 21) new_settings = BaseSettings() new_settings.set('one', 12, 20) new_settings.set('two', 12, 0) attribute.set(new_settings, 0) self.assertEqual(attribute.value['one'], 12) self.assertEqual(attribute.value['two'], 21) def test_repr(self): self.assertEqual(repr(self.attribute), "<SettingsAttribute value='value' priority=10>")
class SettingsAttributeTest(unittest.TestCase): def setUp(self): self.attribute = SettingsAttribute('value', 10) def test_set_greater_priority(self): self.attribute.set('value2', 20) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 20) def test_set_equal_priority(self): self.attribute.set('value2', 10) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 10) def test_set_less_priority(self): self.attribute.set('value2', 0) self.assertEqual(self.attribute.value, 'value') self.assertEqual(self.attribute.priority, 10) def test_overwrite_basesettings(self): original_dict = {'one': 10, 'two': 20} original_settings = BaseSettings(original_dict, 0) attribute = SettingsAttribute(original_settings, 0) new_dict = {'three': 11, 'four': 21} attribute.set(new_dict, 10) self.assertIsInstance(attribute.value, BaseSettings) six.assertCountEqual(self, attribute.value, new_dict) six.assertCountEqual(self, original_settings, original_dict) new_settings = BaseSettings({'five': 12}, 0) attribute.set(new_settings, 0) # Insufficient priority six.assertCountEqual(self, attribute.value, new_dict) attribute.set(new_settings, 10) six.assertCountEqual(self, attribute.value, new_settings) def test_repr(self): self.assertEqual(repr(self.attribute), "<SettingsAttribute value='value' priority=10>")
class SettingsAttributeTest(unittest.TestCase): def setUp(self): self.attribute = SettingsAttribute('value', 10) def test_set_greater_priority(self): self.attribute.set('value2', 20) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 20) def test_set_equal_priority(self): self.attribute.set('value2', 10) self.assertEqual(self.attribute.value, 'value2') self.assertEqual(self.attribute.priority, 10) def test_set_less_priority(self): self.attribute.set('value2', 0) self.assertEqual(self.attribute.value, 'value') self.assertEqual(self.attribute.priority, 10) def test_set_per_key_priorities(self): attribute = SettingsAttribute( BaseSettings({'one': 10, 'two': 20}, 0), 0) new_dict = {'one': 11, 'two': 21} attribute.set(new_dict, 10) self.assertEqual(attribute.value['one'], 11) self.assertEqual(attribute.value['two'], 21) new_settings = BaseSettings() new_settings.set('one', 12, 20) new_settings.set('two', 12, 0) attribute.set(new_settings, 0) self.assertEqual(attribute.value['one'], 12) self.assertEqual(attribute.value['two'], 21) def test_repr(self): self.assertEqual(repr(self.attribute), "<SettingsAttribute value='value' priority=10>")
def test_set_settingsattribute(self): myattr = SettingsAttribute(0, 30) # Note priority 30 self.settings.set('TEST_ATTR', myattr, 10) self.assertEqual(self.settings.get('TEST_ATTR'), 0) self.assertEqual(self.settings.getpriority('TEST_ATTR'), 30)
def setUp(self): self.attribute = SettingsAttribute('value', 10)