def test_fail_save(self): """ тест неправильного сохранения подменим метод _relate_with_text на ошибочный, проверим, как поведет сохранение """ class TestException(Exception): pass article = MfSystemArticle() article.save() count_articles = MfSystemArticle.objects.count() count_objects = MfSystemObject.objects.count() text = MfSystemText( title='1', annonce='2', content='3' ) text.save() count_texts = MfSystemText.objects.count() with patch.object( MfSystemArticle, '_relate_with_text', side_effect=TestException() ), patch.object( MfSystemArticle, "load_file", return_value='test_file_name' ), patch("tools.load_file.delete_file", return_value=1) as mock_file: try: article.create( dict( title='test_tile2', annonce='test_annonce2', content='test_content2', ), dict( image_file='file', image_title='file_title' ) ) is_except = False except MfSystemArticleException: is_except = True # убеждаемся что исклчение было брошено, что мок заменен верно self.assertTrue(is_except) # проверим что количество объектов не изменилось self.assertEquals(MfSystemArticle.objects.count(), count_articles) self.assertEquals(MfSystemObject.objects.count(), count_objects) self.assertEquals(MfSystemText.objects.count(), count_texts) # проверим что файл удалился правильный mock_file.assert_called_with('test_file_name')
def __init__(self, *args, **kwargs): # pylint: disable=E1002, E1101 super(EventAdminForm, self).__init__(*args, **kwargs) instance = kwargs['instance'] self.set_initial(instance) self.initial['icon_title'] = u'Икона ' + self.initial['title'] self.initial['alt_text'] = self.initial['annonce'].replace("\n", ' ') self.initial['smart_function'] = instance.smart_function self.fields['add_article'].choices = \ MfSystemArticle.get_unrelated_articles_select()
def _create_article(self, text_params): article = MfSystemArticle() article.save() article.create(text_params) return article