def _read_settings(self): io = SettingsIO(path=self.settings_file) return io.read()
class TestSettingsIO(unittest.TestCase): def setUp(self): self.settings_file = os.path.join(os.path.dirname(__file__), 'data', 'testsettings.py') self.settings = SettingsIO(path=self.settings_file) def tearDown(self): self.settings.remove() def test_get_paths_without_file_name(self): actual = self.settings._get_paths('toolx', None)[1] expected_end = '%s%s%s' % ('toolx', os.sep, 'toolxsettings.py') self.assertTrue(actual.endswith(expected_end)) def test_get_paths_with_file_name(self): actual = self.settings._get_paths('toolx', 'name')[1] self.assertTrue(actual.endswith('%s%s%s' % ('toolx', os.sep, 'name'))) def test_no_settings_exists(self): self.assertEquals(self.settings.read(), {}) def test_invalid_setting_name(self): self.assertRaises(InvalidSettings, self.settings.write, {'name with space':0}) def test_settings_does_not_exist_anymore(self): self._test_settings_io({'string':'value'}) self.settings.remove() self.assertEquals(self.settings.read(), {}) def test_invalid_settings_exists(self): f = open(self.settings_file, 'w') f.write('foo = invalid syntax::') f.close() self.assertRaises(InvalidSettings, self.settings.read) def test_setting_name_with_spaces(self): self.assertRaises(InvalidSettings, self._test_settings_io, {'setting name spaces': 'value'}) def test_writing_string_setting(self): self._test_settings_io({'string':'value'}) def test_writing_unicode_setting(self): self._test_settings_io({'unicode_string':u'non-ascii character \xe4'}) def test_writing_list_setting(self): self._test_settings_io({'unicode_string': [1, 'string', u'non-ascii character \xe4']}) def test_writing_dictionary_setting(self): self._test_settings_io({'dictionary': {'a': 1, 'b': 2, 'c': 3}}) def test_writing_none_setting(self): self._test_settings_io({'none': None}) def test_writing_boolean_setting(self): self._test_settings_io({'boolean': True}) def test_writing_multiline_string_setting(self): multiline = u"""Multi line string with non-ascii chars \xe4 and quotes "foo" 'bar' and even triple quotes \"\"\" ''' """ self._test_settings_io({'multiline': multiline}) def test_multiple_settings(self): multiline = u"""Multi line string with non-ascii chars \xe4 and quotes "foo" 'bar' and even triple quotes \"\"\" ''' """ self._test_settings_io({'multiline': multiline, 'string': u'some', 'bool': False, 'int':1, 'float':2.4}) def test_updating_setting(self): self._test_settings_io({'string': u'some', 'bool': False, 'int':1}) self.settings.write({'string': u'new', 'bool': True, 'int':1}) self._test_settings_io({'string': u'new', 'bool': False, 'int':4}) def _test_settings_io(self, expected): self.settings.write(expected) actual = self.settings.read() self.assertEqual(expected, actual)