def setUp(self): self.config = Config('tests/data') self.qbs = qbs.Qbs('/usr/bin/qbs', VersionNumber('1.5.0')) copyfile(str(self.default_config_file), str(self.config.path.joinpath(Config.FILENAME)))
class TestConfig(unittest.TestCase): @classmethod def setUpClass(cls): cls.default_config_file = Path('tests/data/sqt.conf.default').resolve() # resolve the path to an absolute path before join the configuration file is ok, because the # Config.FILENAME (here, sqt.conf) file doesn't exist until the tests has not begun. cls.config_file = Path('tests/data').resolve().joinpath(Config.FILENAME) @classmethod def tearDownClass(cls): # must wait the ending of the tests before to start the deletion of the configuration file. if cls.config_file.exists(): os.remove(str(cls.config_file)) def setUp(self): self.config = Config('tests/data') self.qbs = qbs.Qbs('/usr/bin/qbs', VersionNumber('1.5.0')) copyfile(str(self.default_config_file), str(self.config.path.joinpath(Config.FILENAME))) def test_open(self): async def wrapper(): async with self.config.open() as cfg: pass self.loop.run_until_complete(wrapper()) def test_read(self): async def wrapper(): async with self.config.open() as cfg: data = await cfg.read('qbs') result = qbs.Qbs(data['filepath'], data['version']) self.assertEqual(result, self.qbs) none_section = await cfg.read('fringe') self.assertIsNone(none_section) self.loop.run_until_complete(wrapper()) def test_update(self): async def wrapper(): async with self.config.open() as cfg: await cfg.update('fringe', {'walter': 'bishop', 'peter': 'bishop'}) async with self.config.open() as cfg: data = await cfg.read('fringe') self.assertEqual(len(data), 2) self.assertEqual(data['walter'], 'bishop') self.assertEqual(data['peter'], 'bishop') with self.assertRaises(KeyError): self.assertTrue(data['september']) async with self.config.open() as cfg: data = await cfg.read('fringe') data['olivia'] = 'dunham' await cfg.update('fringe', data) async with self.config.open() as cfg: data = await cfg.read('fringe') self.assertEqual(len(data), 3) self.assertEqual(data['walter'], 'bishop') self.assertEqual(data['peter'], 'bishop') self.assertEqual(data['olivia'], 'dunham') self.loop.run_until_complete(wrapper()) def test_update_data_typerror(self): async def wrapper(): async with self.config.open() as cfg: with self.assertRaises(TypeError): await cfg.update('shape-shifting', 73) await cfg.update('shape-shifting', 3.1415) await cfg.update('shape-shifting', 'device') await cfg.update('shape-shifting', b'device') await cfg.update('shape-shifting', False) self.loop.run_until_complete(wrapper()) def test_update_empty_section(self): async def wrapper(): async with self.config.open() as cfg: await cfg.update('observers', {}) async with self.config.open() as cfg: data = await cfg.read('observers') self.assertIsNone(data) self.loop.run_until_complete(wrapper()) def test_update_reset_section(self): async def wrapper(): async with self.config.open() as cfg: data = await cfg.read('qbs') self.assertEqual(len(data), 2) self.assertEqual(data['filepath'], '/usr/bin/qbs') self.assertEqual(data['version'], '1.5.0') await cfg.update('qbs', {'path': '/usr/bin'}, reset=True) async with self.config.open() as cfg: data = await cfg.read('qbs') self.assertEqual(len(data), 1) self.assertEqual(data['path'], '/usr/bin') self.loop.run_until_complete(wrapper())