def read_model(self, idioms, file_name = None): """ Read model from file, stored as a JSON dict with enough information for an idiom to recreate it. :param idioms: Dicitionary of idioms to use to recreate the model. :type model: dict :param file_name: The path to write the model from, if none use last or default path. :type file_name: str :return: Returns a model, an empty model (not None) if it not able to read file. :rtype: Model """ file_name = self._make_file_name(file_name) self._write_last() model = Model() try: fd = open(file_name, "r+") json_model = json.load(fd) for asset_dict in json_model['assets']: asset = idioms[asset_dict['service']].create_asset(asset_dict['name'], asset_dict['real id'], asset_dict['product name']) asset.uuid = uuid.UUID(asset_dict['uuid']) model.add_asset(asset) return model except FileNotFoundError: return model except ValueError: return model
class TestModel(unittest.TestCase): def setUp(self): self.model = Model() self.model.add_service('upb') self.asset = BlankAsset('blah', 'upb') self.model.add_asset(self.asset) def test_get(self): uuid = self.model.get_asset_uuid('upb', 'blah') asset = self.model.get_asset(uuid) self.assertIs(asset, self.asset) def test_serializable(self): uuid = self.model.get_asset_uuid('upb', 'blah') info = self.model.serializable_asset_info(uuid) self.assertEqual(info['product'], '') self.assertEqual(info['name'], '')
class TestStorage(unittest.TestCase): def setUp(self): self.store = ModelStorage('none dir') self.model = Model() self.idiom = InsteonIdiom('test') self.model.add_asset(self.idiom.create_asset('test', 'aaaaaa', 'ApplianceLinc V2')) def test_none(self): self.assertIsInstance(self.store.read_model({'test' : self.idiom}, 'none'), Model) def test_store(self): self.store.write_model(self.model, 'hmm') test = self.store.read_model({'test' : self.idiom}, 'hmm') self.assertEquals(test.get_asset(test.get_all_asset_uuids()[0]).get_service(), 'test') files = self.store.get_files() self.assertEquals(['hmm'], files) def test_remove(self): self.store.remove_files() self.assertFalse(os.path.exists('none dir')) def tearDown(self): if os.path.exists('none dir'): self.store.remove_files()