TransactionManagementError “You can't execute queries until the end of the 'atomic' block” while using signals, but only during Unit Testing

后端 未结 11 2070
有刺的猬
有刺的猬 2020-12-02 06:03

I am getting TransactionManagementError when trying to save a Django User model instance and in its post_save signal, I\'m saving some models that have the user as the forei

11条回答
  •  無奈伤痛
    2020-12-02 06:41

    def test_wrong_user_country_db_constraint(self):
            """
            Check whether or not DB constraint doesnt allow to save wrong country code in DB.
            """
            self.test_user_data['user_country'] = 'XX'
            expected_constraint_name = "country_code_within_list_of_countries_check"
    
            with transaction.atomic():
                with self.assertRaisesRegex(IntegrityError, expected_constraint_name) as cm:
                    get_user_model().objects.create_user(**self.test_user_data)
    
            self.assertFalse(
                get_user_model().objects.filter(email=self.test_user_data['email']).exists()
            )
    
    with transaction.atomic() seems do the job correct
    

提交回复
热议问题