mercredi 10 octobre 2018

Django tests: self.client.post is not being executed for the second time

I am trying to create an object using POST request, and then simply edit it using PUT request.

The code itself works whenever I am doing all of necessary steps on my own, by hand.

Unfortunately, whenever I am trying to test it using TestCase, it goes completely wrong.

The POST request is being executed in a proper way, but whenever I am trying to send second request with PUT operation (within the same block of code), it is not being created

def test_fully_working_editing_items(self):
        self.test_operation_type = "POST"
        self.chosen_date_from = str(datetime.now().month) + "/" + str(datetime.now().day) + "/" + str(datetime.now().year)
        self.chosen_date_to = str((datetime.now() + timedelta(days=1)).month) + "/" + str((datetime.now() + timedelta(days=1)).day) + "/" + str((datetime.now() + timedelta(days=1)).year)

        self.placement = self.test_placement_offer.id

        # THIS ONE WORKS

        response = self.client.post(reverse('advertisement_user_basket:user_basket_controller'), 
            data={
                    'operation': self.test_operation_type,
                    'chosen_date_to': self.chosen_date_to,
                    'chosen_date_from': self.chosen_date_from,
                    'placement': self.placement,
                    'chosen_impression_number': self.test_impression_number.number
                }
        )

        self.assertEqual(response.status_code, 200)

        self.assertEqual(
            json.loads(response.content.decode('utf-8'))["is_valid"],
            True
        )    


        self.test_obtained_advertisement_item_id = json.loads(response.content.decode('utf-8'))["items"][0]["id"]

        '''
        ============================================================
            EDITING ITEMS
        ============================================================
        '''

        self.test_operation_type = "PUT"
        self.chosen_date_from = str(datetime.now().month) + "/" + str(datetime.now().day) + "/" + str(datetime.now().year)
        self.chosen_date_to_new = str((datetime.now() + timedelta(days=3)).day) + "/" + str((datetime.now() + timedelta(days=3)).month) + "/" + str((datetime.now() + timedelta(days=3)).year)
        self.placement = self.test_placement_offer.id

        logger.error('111111111111111111111111')

        # THIS ONE DOES NOT WORK

        second_response = self.client.post(reverse('advertisement_user_basket:user_basket_controller'), 
            data = {
                    'operation': self.test_operation_type,
                    'chosen_date_to': self.chosen_date_to_new,
                    'chosen_date_from': self.chosen_date_from,
                    'placement': self.placement,
                    'chosen_impression_number': self.test_impression_number.number,
                    'item': self.test_obtained_advertisement_item_id
                }
        )

        logger.error('222222222222222222222222')

        self.assertEqual(second_response.status_code, 200)

        self.assertEqual(
            json.loads(second_response.content.decode('utf-8'))["is_valid"],
            True
        )    
        logger.error('second_response.content.' + str(second_response.content.decode('utf-8')))

What is more, logger does not print the response from second PUT request (named "second_response") Messages from logger:

ERROR:advertisement_user_basket.tests.test_views:response.content.{"is_valid": true} ERROR:advertisement_user_basket.tests.test_views:111111111111111111111111 ERROR:advertisement_user_basket.tests.test_views:222222222222222222222222

Traceback:

Traceback (most recent call last): File "/Project/user_basket/tests/test_views.py", line 715, in test_fully_working_editing_items True AssertionError: False != True

Line 715 is connected with following assertion:

self.assertEqual(
            json.loads(second_response.content.decode('utf-8'))["is_valid"],
            True
        )

It seems that the code responsible for sending second request is not being executed. I literally have no idea of what is happening with it

Aucun commentaire:

Enregistrer un commentaire