def _process_redis_result_store(self, data: Dict) -> None: meta_data = data['meta_data'] repo_name = meta_data['repo_name'] repo_id = meta_data['repo_id'] parent_id = meta_data['repo_parent_id'] metrics = data['data'] self.logger.debug( "Saving %s state: _no_of_releases=%s, _last_monitored=%s", repo_name, metrics['no_of_releases'], meta_data['last_monitored']) self.redis.hset_multiple( Keys.get_hash_parent(parent_id), { Keys.get_github_no_of_releases(repo_id): str(metrics['no_of_releases']), Keys.get_github_last_monitored(repo_id): str(meta_data['last_monitored']), })
def test_process_redis_store_redis_is_called_correctly( self, mock_github_data, mock_hset_multiple) -> None: data = eval(mock_github_data) self.test_store._process_redis_store(data) meta_data = data['result']['meta_data'] repo_id = meta_data['repo_id'] parent_id = meta_data['repo_parent_id'] metrics = data['result']['data'] call_1 = call( Keys.get_hash_parent(parent_id), { Keys.get_github_no_of_releases(repo_id): str(metrics['no_of_releases']), Keys.get_github_last_monitored(repo_id): str(meta_data['last_monitored']), }) mock_hset_multiple.assert_has_calls([call_1])
def test_process_redis_store_redis_stores_correctly( self, mock_github_data) -> None: data = eval(mock_github_data) self.test_store._process_redis_store(data) meta_data = data['result']['meta_data'] repo_id = meta_data['repo_id'] parent_id = meta_data['repo_parent_id'] metrics = data['result']['data'] self.assertEqual( str(metrics['no_of_releases']), self.redis.hget( Keys.get_hash_parent(parent_id), Keys.get_github_no_of_releases(repo_id)).decode("utf-8")) self.assertEqual( str(meta_data['last_monitored']), self.redis.hget( Keys.get_hash_parent(parent_id), Keys.get_github_last_monitored(repo_id)).decode("utf-8"))
def load_state(self, repo: Union[System, GitHubRepo]) \ -> Union[System, GitHubRepo]: # If Redis is down, the data passed as default will be stored as # the repo state. self.logger.debug("Loading the state of %s from Redis", repo) redis_hash = Keys.get_hash_parent(repo.parent_id) repo_id = repo.repo_id # Below, we will try and get the data stored in redis and store it # in the repo's state. If the data from Redis cannot be obtained, the # state won't be updated. # Load no_of_releases from Redis state_no_of_releases = repo.no_of_releases redis_no_of_releases = self.redis.hget( redis_hash, Keys.get_github_no_of_releases(repo_id), state_no_of_releases) no_of_releases = \ convert_to_int_if_not_none(redis_no_of_releases, None) repo.set_no_of_releases(no_of_releases) # Load last_monitored from Redis state_last_monitored = repo.last_monitored redis_last_monitored = self.redis.hget( redis_hash, Keys.get_github_last_monitored(repo_id), state_last_monitored) last_monitored = \ convert_to_float_if_not_none(redis_last_monitored, None) repo.set_last_monitored(last_monitored) self.logger.debug( "Restored %s state: _no_of_releases=%s, _last_monitored=%s", repo, no_of_releases, last_monitored) return repo
def test_process_data_saves_in_redis(self, mock_github_data, mock_send_hb, mock_ack) -> None: self.rabbitmq.connect() mock_ack.return_value = None try: self.test_store._initialise_rabbitmq() data = eval(mock_github_data) blocking_channel = self.test_store.rabbitmq.channel method_chains = pika.spec.Basic.Deliver( routing_key=GITHUB_STORE_INPUT_ROUTING_KEY) properties = pika.spec.BasicProperties() self.test_store._process_data(blocking_channel, method_chains, properties, json.dumps(data).encode()) mock_ack.assert_called_once() mock_send_hb.assert_called_once() meta_data = data['result']['meta_data'] repo_id = meta_data['repo_id'] parent_id = meta_data['repo_parent_id'] metrics = data['result']['data'] self.assertEqual( str(metrics['no_of_releases']), self.redis.hget( Keys.get_hash_parent(parent_id), Keys.get_github_no_of_releases(repo_id)).decode("utf-8")) self.assertEqual( str(meta_data['last_monitored']), self.redis.hget( Keys.get_hash_parent(parent_id), Keys.get_github_last_monitored(repo_id)).decode("utf-8")) except Exception as e: self.fail("Test failed: {}".format(e))