def __init__(self, config={}): Configurator.__init__(self, config) defaults = {"filename":"agents.json"} self.__defaults__(defaults) self.__clazzs__ = {} self.agents = []
def test_init_file_does_not_exists(self, mocked): """ Test Configurator with non existing file """ # Mock os.path.exists == False mocked.return_value = False co = Configurator({"filename":"afile", "folder" : "afolder"}) with self.assertRaises(ConfiguratorException): co.loadfile()
def test_loadfile_ok(self, exists_mocked): """ Test Configurator with non existing file """ # Mock os.path.exists == True exists_mocked.return_value = True m = mock_open(read_data = '{"test":"Hello"}') open_name = '%s.open' % "clu.common.config.configurator" with patch(open_name, m, create=True): co = Configurator({"filename":"afile", "folder" : "afolder"}) co.loadfile() self.assertTrue(co._loadedconfig.has_key("test"))
def test_init_file_bad_json(self, exists_mocked): """ Test Configurator with non existing file """ # Mock os.path.exists == True exists_mocked.return_value = True m = mock_open(read_data = "BAD JSON}") open_name = '%s.open' % "clu.common.config.configurator" with patch(open_name, m, create=True): co = Configurator({"filename":"afile", "folder" : "afolder"}) with self.assertRaises(ConfiguratorException): co.loadfile()
def test_loadfile_realconfig(self, exists_mocked): """ Test Configurator with non existing file """ # Mock os.path.exists == True exists_mocked.return_value = True m = mock_open(read_data = '{"agents":[{"name":"", "type":""}], "configs":[{"name":"", "config":{}}]}') open_name = '%s.open' % "clu.common.config.configurator" with patch(open_name, m, create=True): co = Configurator({"filename":"afile", "folder" : "afolder"}) co.loadfile() self.assertTrue(co._loadedconfig.has_key("agents")) self.assertTrue(type(co._loadedconfig["agents"]) == list) for item in co._loadedconfig["agents"]: self.assertTrue(item.has_key("name")) self.assertTrue(item.has_key("type")) for item in co._loadedconfig["configs"]: self.assertTrue(item.has_key("name")) self.assertTrue(item.has_key("config")) self.assertTrue(type(item["config"]) == dict)
def test_init_folder_none(self): """ Test Configurator with filename None """ co = Configurator({"filename" : "afile"}) with self.assertRaisesRegexp(ConfiguratorException, "Config folder is not defined"): co.loadfile()