def test_save_json_item_in_collection(self): persistence = Persistence() item = {"field1": "value1"} collection = "col1" id = "one" expected_file_name = os.path.join(self.cfg_dir, collection, "{}.cfg".format(id)) self.assertFalse(os.path.isfile(expected_file_name)) persistence.save(item, id, collection=collection) self.assertTrue(os.path.isfile(expected_file_name)) items = persistence.load_collection("col1") self.assertEqual(len(items), 1)
def config_id(self, config_id): if self._config_id != config_id: self.logger.debug("Configuration ID set to: {}".format(config_id)) self._config_id = config_id # persist value so that it can be read eventually # when component starts again Persistence().save(self.config_id, "configuration_id")
def configure(self, context): """ Configures component Establish dependency to RESTManager Fetches settings Args: context (CoreContext): component initialization context """ super().configure(context) # Register dependency to rest manager self._rest_manager = self.get_dependency('RESTManager') self._configuration_manager = \ self.get_dependency('ConfigurationManager') self._api_key_manager = self.get_dependency('APIKeyManager') # fetch component settings self._config_api_url_prefix = \ Settings.get("configuration", "config_api_url_prefix", fallback="https://api.n.io/v1") self._config_id = Persistence().load("configuration_id", default=Settings.get( "configuration", "config_id")) self._config_version_id = Persistence().load( "configuration_version_id", default=Settings.get("configuration", "config_version_id")) self._start_stop_services = Settings.getboolean("configuration", "start_stop_services", fallback=True) self._delete_missing = Settings.getboolean("configuration", "delete_missing", fallback=True) self._poll_interval = Settings.getint("configuration", "config_poll_interval", fallback=0) self._poll_on_start = Settings.getboolean("configuration", "config_poll_on_start", fallback=False)
def __init__(self): """Initializes the component """ super().__init__() self._handler = None # dependency components self._rest_manager = None # allocate persistence instance to use to load/save cloned blocks self._persistence = Persistence()
def test_collection(self): """ Asserts collection persistence """ items = {"item1": "item1_val", "item2": "item2_val"} persist = Persistence() persist.save_collection(items, 'item_id') # load it back and assert results self.assertDictEqual(persist.load_collection('item_id'), items) # Remove item persist.remove_collection('item_id') self.assertEqual( persist.load_collection('item_id', default='default_val'), 'default_val')
def test_item(self): """ Asserts persistence at the item level """ persist = Persistence() persist.save('item_val', 'item_id') # Make sure we get the saved value self.assertEqual(persist.load('item_id', default='not found'), 'item_val') # Make sure we get a default value if one hasn't been saved self.assertEqual(persist.load('invalid_id', default='default_val'), 'default_val') # Remove item persist.remove('item_id') # not it behaves as it never existed self.assertEqual(persist.load('item_id', default='default_val'), 'default_val')
def test_collection_item(self): """ Asserts item persistence within a collection """ persist = Persistence() persist.save('collection_item_val', 'item_id', collection='item_collection') persist.save('item_val', 'item_id') # Make sure we get the correct value depending on collection argument self.assertEqual(persist.load('item_id', collection='item_collection'), 'collection_item_val') self.assertEqual(persist.load('item_id'), 'item_val') # Remove item persist.remove('item_id', collection='item_collection') self.assertEqual( persist.load('item_id', collection='item_collection', default='default_val'), 'default_val')