1

I am having this method below:

    def pagination_logic(self, topic_name):
        """Pagination logic to fetch the complete data.

        :param topic_name:str, kafka topic name.
        """
        while self.next_page_cursor:

            self.fetch_response_from_api()

            records = self.extract_records()

            self.publish_records(records, topic_name)

            if not self.flag:
                break

            self.extract_next_page_cursor()

            self.page += 1

        else:
            logger.info("Finished fetching data")

I need to write a unit test method. Below is my unit test method

    def test_pagination_logic(self):
        """Test for pagination logic."""
        allow(self.slack).fetch_response_from_api.and_return(None)

        allow(self.slack).extract_records.and_return(RESPONSE.get('entries'))

        allow(self.slack).publish_records.and_return(None)

        allow(self.slack).extract_next_page_cursor.and_return(None)

        self.slack.next_page_cursor = 'abc'
        self.slack.flag = 0

        result = self.slack.pagination_logic('topic_name')

        assert result is None

I know that I can achieve 100% coverage for this by setting the value of self.flag as 1 for the first iteration and 0 for the second iteration. But how can I achieve that?.

1 Answer 1

1

Try doing like this. This is a bit hacky but I think this might work:

class Helper:
    
    def __init__(self):
        self.s_obj = None

    def update_flag():
        if self.s_obj.flag == 1:
            self.s_obj.flag = 0

class YourTestClass:
    def test_pagination_logic(self):
        """Test for pagination logic."""
        
        ...
        h_obj = Helper()
        h_obj.s_obj = self.slack
 
 
        allow(self.slack).extract_next_page_cursor.and_return_result_of(h_obj.update_flag)
        
        self.slack.flag = 1
    
        result = self.slack.pagination_logic('topic_name')

        ....

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.