0
  1. I have a module 'initialize' which returns a pub sub instance based on users input currently returns 2(Google PubSub and RabbitMQ pub sub).
def create_pubsub( required input parameters
) -> VAPubSub:
    if env == "gcp":
        return GooglePubSub(project_id, topic, logger)
    if env == "onprem":
        return RabbitMQ(host, v_host, port, username, password, exchange, logger)`
  1. I have an interface of pubsub class which is implemented by different message brokers currently 2(Google pubSub, RabbitMQ).
class PubSub:
    def publish(self, message: bytes, attributes: dict, **kwargs):
        pass

    def subscribe(
        self, name: str, callback: Callable[[bytes, dict], None], **kwargs
    ) -> StreamingPullFuture | None:
        pass
  1. I have a google pubsub class which implements the pubsub interface
class GooglePubSub(PubSub):
    def __init__(self, project_id: str, topic_id: str, logger: logging.Logger):
        self.logger = logger
        self.project_id = project_id
        self.publisher = pubsub_v1.PublisherClient()
        self.topic_path = self.publisher.topic_path(project_id, topic_id)
        try:
            topic = self.publisher.create_topic(request={"name": self.topic_path})
            self.logger.info(f"Topic created {topic.name}")
        except AlreadyExists:
            self.logger.info(f"Topic already exists")

    def publish(self, message: bytes, attributes: dict, **kwargs):
        if len(message) == 0:
            self.logger.error(f"message cannot be null")
            return

        publish_future = self.publisher.publish(self.topic_path, message, **attributes)
        publish_future.add_done_callback(get_callback(publish_future))
  1. And finally I have my test file in which I am currently testing the GooglePubSub's publish method, but I am mocking the creds and pubsub using patch and mock still getting this 'grpc._channel._InactiveRpcError' error from 'topic = self.publisher.create_topic(request={"name": self.topic_path})'
class MyTestCase(unittest.TestCase):
    @patch('google.auth.default')
    def test_publish(self, mock_auth):
        with patch('src.va_pubsub.google_pubsub.GooglePubSub') as MockGooglePubSub:
            mock_pubsub = MockGooglePubSub.return_value
            mock_auth.return_value = (None, 'mock_credentials')
            pubsub = create_pubsub(env='gcp', project_id='test_project_id', topic='test_topic_id', logger=logging.getLogger('test_logger'))

            self.assertIsInstance(pubsub, GooglePubSub)

            test_message = b'test message'

            test_attributes = {
                "test_key_1": "test_one",
            }

            pubsub.publish(test_byte_message, test_attributes)

            mock_pubsub.publish.assert_called_once_with(test_byte_message, test_attributes)

what am I doing wrong and what do I need to do to run the test_publish() without making any API calls? Any leads would be appreciated as I am new to python..!!

Shivam Kumar
  • 85
  • 1
  • 9
  • **You have to pay attention where to patch()**. Could you edit your question and add the import instruction present in the various files and the path of every python files. Thanks – frankfalse Jul 10 '23 at 10:09

0 Answers0