I am writing the testcase for a signal but when coverage run only this file it is covering html page containing code but when coverage run The whole project it does not cover the code but passes the testcase. I am trying to cover the signals coverage with hitting the api url

def recreate_objects2():
    current_time = timezone.now()
    loan = Loan.objects.create(#create the objects here) ## signal is related to this model here when this model is created it will trigger the signal
    customer = Customer.objects.create()
    loan_customer = LoanCustomer.objects.create()
    customer_address = CustomerAddress.objects.create()
    loan_base_rate = LoanBaseRate.objects.create()
    return loan, loan_customer, customer, customer_address, loan_base_rate
 


class TestLoanAssignmentSignal(TestCase):

    @factory.django.mute_signals(pre_save, post_save) ## muted the signal
    def setUp(self):
        self.client = Client()
        self.url = reverse('loan_url')
        self.loan,  self.customer, self.loan_customer, self.customer_address, self.loan_base_rate = recreate_objects2() ## models created 

    def postReq(self, payload):
        return self.client.post(self.url, json.dumps(payload), content_type="application/json")

    def common_test_call(self, data):
        response = self.postReq(data)
        content = json.loads(response.content)
        return content
    
    output = {
        response for mock 
    }

    obj = EmptyClass()
    obj.role = TE
    obj.partner_id = 1
    obj.user_id = "1"
    @mock.patch("mymodule.get_request", return_value=obj)
    def test_assignment_signal_success(self, mock_api_call, *args):
        self.loan.partner_id = 1
        self.loan.state = 1
        self.loan.save()
        data = {
            "zyx": "xyz"
        }
        data_copy = copy.deepcopy(data)
        actual_response = self.common_test_call(data_copy)
        expected_response = success[STATUS_CODE]
        logger.debug("expected_response %s", expected_response)
        logger.debug("actual_response %s", actual_response)
        self.assertEqual(actual_response['status'], expected_response)


    obj2 = EmptyClass()
    obj2.role = TE
    obj2.partner_id = None
    obj2.user_id = "1"
    @mock.patch("mymodule.get_request", return_value=obj2)
    @mock.patch.object(ApiCall, "call", side_effect=[output,None, output])
    def test_assignment_signal_success2(self, mock_api_call, *args):
        self.loan.partner_id = None
        self.loan.state = 2
        self.loan.save() ## it is calling the testcase again so that it will hit the signal giving desired output or updating the value
        data = {
        "zyx": "xyz"
        }
        data_copy = copy.deepcopy(data)
        actual_response = self.common_test_call(data_copy)
        expected_response = success[STATUS_CODE]
        logger.debug("expected_response %s", expected_response)
        logger.debug("actual_response %s", actual_response)
        self.assertEqual(actual_response['status'], expected_response)

Here are the 2 testcase i have written created its models so that when it hits the signal it and covering the code.

I changed the setup(function inside the class) of the whole testcase even passing the testcase but does not cover the html page containing the code

I changed the whole testcase like

def setUp(self):
        self.client = Client()
        self.url = reverse('API URL')

    def postReq(self, payload):
        return self.client.post(self.url, json.dumps(payload), content_type="application/json")

    def common_test_call(self, data):
        response = self.postReq(data)
        content = json.loads(response.content)
        return content

obj = EmptyClass()
    obj.role = TE
    obj.partner_id = 1
    obj.user_id = "1"
    @mock.patch("mymodule.get_request", return_value=obj)
    def test_assignment_signal_success(self, mock_api_call, *args):
        loan,  customer, loan_customer, customer_address = recreate_objects2() ## unpacking the values of functions so that i can use the variables stored in the function
        loan.partner_id = 1
        loan.state = 1
        loan.save()
        data = {
            "zyx": "xyz"
        }
        data_copy = copy.deepcopy(data)
        actual_response = self.common_test_call(data_copy)
        expected_response = success[STATUS_CODE]
        logger.debug("expected_response %s", expected_response)
        logger.debug("actual_response %s", actual_response)
        self.assertEqual(actual_response['status'], expected_response)

After approaching this changes in the testcase it is still not covering code in the html. The values in the function are used for the purpose in data variable (no relation to problem). In this senario i am not muting the signals as it called when the loan variable containg the model data is created.

0

There are 0 best solutions below