From b9ac41fac58840ee6d3e84aff0bdf59cb58af0fe Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sun, 18 Oct 2015 13:16:43 +0200 Subject: [PATCH] Move BaseTest to fix import issues --- tests/__init__.py | 187 ------------------------------------------ tests/base.py | 185 +++++++++++++++++++++++++++++++++++++++++ tests/test_menu.py | 2 +- tests/test_models.py | 2 +- tests/test_plugins.py | 3 +- tests/test_setup.py | 2 +- tests/test_toolbar.py | 2 +- tests/test_views.py | 2 +- 8 files changed, 191 insertions(+), 194 deletions(-) create mode 100644 tests/base.py diff --git a/tests/__init__.py b/tests/__init__.py index dcfbd5f..40a96af 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,188 +1 @@ # -*- coding: utf-8 -*- -""" -Tests for `djangocms_blog` module. -""" -from __future__ import absolute_import, print_function, unicode_literals - -from copy import deepcopy - -from cmsplugin_filer_image.models import ThumbnailOption -from django.contrib.auth import get_user_model -from django.contrib.sites.models import Site -from djangocms_helper.base_test import BaseTestCase -from parler.utils.context import smart_override - -from djangocms_blog.cms_appconfig import BlogConfig -from djangocms_blog.models import BlogCategory, Post - -User = get_user_model() - - -def _get_cat_pk(lang, name): - return lambda: BlogCategory.objects.translated(lang, name=name).get().pk - - -class BaseTest(BaseTestCase): - """ - Base class with utility function - """ - category_1 = None - thumb_1 = None - thumb_2 = None - - _pages_data = ( - {'en': {'title': 'page one', 'template': 'blog.html', 'publish': True}, - 'fr': {'title': 'page un', 'publish': True}, - 'it': {'title': 'pagina uno', 'publish': True}}, - {'en': {'title': 'page two', 'template': 'blog.html', 'publish': True, - 'apphook': 'BlogApp', 'apphook_namespace': 'sample_app'}, - 'fr': {'title': 'page deux', 'publish': True}, - 'it': {'title': 'pagina due', 'publish': True}}, - {'en': {'title': 'page three', 'template': 'blog.html', 'publish': True, - 'apphook': 'BlogApp', 'apphook_namespace': 'sample_app2'}, - 'fr': {'title': 'page trois', 'publish': True}, - 'it': {'title': 'pagina tre', 'publish': True}}, - ) - - _post_data = ( - {'en': {'title': 'First post', 'abstract': '

first line

', - 'description': 'This is the description', 'keywords': 'keyword1, keyword2', - 'text': 'Post text', 'app_config': 'sample_app', 'publish': True}, - 'it': {'title': 'Primo post', 'abstract': '

prima riga

', - 'description': 'Questa è la descrizione', 'keywords': 'keyword1, keyword2', - 'text': 'Testo del post'}, - }, - {'en': {'title': 'Second post', 'abstract': '

second post first line

', - 'description': 'Second post description', 'keywords': 'keyword3, keyword4', - 'text': 'Second post text', 'app_config': 'sample_app', 'publish': False}, - 'it': {'title': 'Secondo post', 'abstract': '

prima riga del secondo post

', - 'description': 'Descrizione del secondo post', 'keywords': 'keyword3, keyword4', - 'text': 'Testo del secondo post', 'app_config': 'sample_app'}, - }, - {'en': {'title': 'Third post', 'abstract': '

third post first line

', - 'description': 'third post description', 'keywords': 'keyword5, keyword6', - 'text': 'Third post text', 'app_config': 'sample_app', 'publish': False}, - 'it': {'title': 'Terzo post', 'abstract': '

prima riga del terzo post

', - 'description': 'Descrizione del terzo post', 'keywords': 'keyword5, keyword6', - 'text': 'Testo del terzo post'}, - }, - {'en': {'title': 'Different appconfig', 'abstract': '

Different appconfig first line

', - 'description': 'Different appconfig description', 'keywords': 'keyword5, keyword6', - 'text': 'Different appconfig text', 'app_config': 'sample_app2', 'publish': True}, - 'it': {'title': 'Altro appconfig', 'abstract': '

prima riga del Altro appconfig

', - 'description': 'Descrizione Altro appconfig', 'keywords': 'keyword5, keyword6', - 'text': 'Testo del Altro appconfig'}, - }, - ) - - _categories_data = ( - {'en': {'name': 'Very loud', 'app_config': 'sample_app'}, - 'it': {'name': 'Fortissimo'}, - }, - {'en': {'name': 'Very very silent', 'app_config': 'sample_app'}, - 'it': {'name': 'Pianississimo'}, - }, - {'en': {'name': 'Almost', 'app_config': 'sample_app'}, - 'it': {'name': 'Mezzo'}, - }, - {'en': {'name': 'Loud', 'parent_id': _get_cat_pk('en', 'Almost'), 'app_config': 'sample_app'}, - 'it': {'name': 'Forte', 'parent_id': _get_cat_pk('it', 'Mezzo')}, - }, - {'en': {'name': 'Silent', 'parent_id': _get_cat_pk('en', 'Almost'), 'app_config': 'sample_app'}, - }, - {'en': {'name': 'Drums', 'app_config': 'sample_app2'}, - 'it': {'name': 'Tamburi'}, - }, - {'en': {'name': 'Guitars', 'app_config': 'sample_app2'}, - 'it': {'name': 'Chitarre'}, - }, - ) - - @classmethod - def setUpClass(cls): - super(BaseTest, cls).setUpClass() - cls.thumb_1 = ThumbnailOption.objects.create( - name='base', width=100, height=100, crop=True, upscale=False - ) - cls.thumb_2 = ThumbnailOption.objects.create( - name='main', width=200, height=200, crop=False, upscale=False - ) - cls.app_config_1 = BlogConfig.objects.create( - namespace='sample_app', app_title='app1' - ) - cls.app_config_2 = BlogConfig.objects.create( - namespace='sample_app2', app_title='app2' - ) - cls.app_config_1.app_data.config.paginate_by = 1 - cls.app_config_1.save() - cls.app_config_2.app_data.config.paginate_by = 2 - cls.app_config_2.save() - cls.app_configs = { - 'sample_app': cls.app_config_1, - 'sample_app2': cls.app_config_2, - } - cls.category_1 = BlogCategory.objects.create(name='category 1', app_config=cls.app_config_1) - cls.category_1.set_current_language('it', initialize=True) - cls.category_1.name = 'categoria 1' - cls.category_1.save() - cls.site_2 = Site.objects.create(domain='http://example2.com', name='example 2') - - @classmethod - def tearDownClass(cls): - super(BaseTest, cls).tearDownClass() - BlogConfig.objects.all().delete() - BlogCategory.objects.all().delete() - ThumbnailOption.objects.all().delete() - - def _get_category(self, data, category=None, lang='en'): - data = deepcopy(data) - for k, v in data.items(): - if hasattr(v, '__call__'): - data[k] = v() - if not category: - with smart_override(lang): - data['app_config'] = self.app_configs[data['app_config']] - category = BlogCategory.objects.create(**data) - else: - category.set_current_language(lang, initialize=True) - for attr, val in data.items(): - setattr(category, attr, val) - category.save() - return category - - def _get_post(self, data, post=None, lang='en', sites=None): - if not post: - post_data = { - 'author': self.user, - 'title': data['title'], - 'abstract': data['abstract'], - 'meta_description': data['description'], - 'meta_keywords': data['keywords'], - 'app_config': self.app_configs[data['app_config']] - } - post = Post.objects.create(**post_data) - else: - post.create_translation( - lang, - title=data['title'], - abstract=data['abstract'], - meta_description=data['description'], - meta_keywords=data['keywords'] - ) - post = self.reload_model(post) - post.categories.add(self.category_1) - if sites: - for site in sites: - post.sites.add(site) - return post - - def get_posts(self, sites=None): - posts = [] - for post in self._post_data: - post1 = self._get_post(post['en'], sites=sites) - post1 = self._get_post(post['it'], post=post1, lang='it') - post1.publish = post['en']['publish'] - post1.main_image = self.create_filer_image_object() - post1.save() - posts.append(post1) - return posts diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..fc96f81 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,185 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, print_function, unicode_literals + +from copy import deepcopy + +from cmsplugin_filer_image.models import ThumbnailOption +from django.contrib.auth import get_user_model +from django.contrib.sites.models import Site +from djangocms_helper.base_test import BaseTestCase +from parler.utils.context import smart_override + +from djangocms_blog.cms_appconfig import BlogConfig +from djangocms_blog.models import BlogCategory, Post + +User = get_user_model() + + +def _get_cat_pk(lang, name): + return lambda: BlogCategory.objects.translated(lang, name=name).get().pk + + +class BaseTest(BaseTestCase): + """ + Base class with utility function + """ + category_1 = None + thumb_1 = None + thumb_2 = None + + _pages_data = ( + {'en': {'title': 'page one', 'template': 'blog.html', 'publish': True}, + 'fr': {'title': 'page un', 'publish': True}, + 'it': {'title': 'pagina uno', 'publish': True}}, + {'en': {'title': 'page two', 'template': 'blog.html', 'publish': True, + 'apphook': 'BlogApp', 'apphook_namespace': 'sample_app'}, + 'fr': {'title': 'page deux', 'publish': True}, + 'it': {'title': 'pagina due', 'publish': True}}, + {'en': {'title': 'page three', 'template': 'blog.html', 'publish': True, + 'apphook': 'BlogApp', 'apphook_namespace': 'sample_app2'}, + 'fr': {'title': 'page trois', 'publish': True}, + 'it': {'title': 'pagina tre', 'publish': True}}, + ) + + _post_data = ( + {'en': {'title': 'First post', 'abstract': '

first line

', + 'description': 'This is the description', 'keywords': 'keyword1, keyword2', + 'text': 'Post text', 'app_config': 'sample_app', 'publish': True}, + 'it': {'title': 'Primo post', 'abstract': '

prima riga

', + 'description': 'Questa è la descrizione', 'keywords': 'keyword1, keyword2', + 'text': 'Testo del post'}, + }, + {'en': {'title': 'Second post', 'abstract': '

second post first line

', + 'description': 'Second post description', 'keywords': 'keyword3, keyword4', + 'text': 'Second post text', 'app_config': 'sample_app', 'publish': False}, + 'it': {'title': 'Secondo post', 'abstract': '

prima riga del secondo post

', + 'description': 'Descrizione del secondo post', 'keywords': 'keyword3, keyword4', + 'text': 'Testo del secondo post', 'app_config': 'sample_app'}, + }, + {'en': {'title': 'Third post', 'abstract': '

third post first line

', + 'description': 'third post description', 'keywords': 'keyword5, keyword6', + 'text': 'Third post text', 'app_config': 'sample_app', 'publish': False}, + 'it': {'title': 'Terzo post', 'abstract': '

prima riga del terzo post

', + 'description': 'Descrizione del terzo post', 'keywords': 'keyword5, keyword6', + 'text': 'Testo del terzo post'}, + }, + {'en': {'title': 'Different appconfig', 'abstract': '

Different appconfig first line

', + 'description': 'Different appconfig description', 'keywords': 'keyword5, keyword6', + 'text': 'Different appconfig text', 'app_config': 'sample_app2', 'publish': True}, + 'it': {'title': 'Altro appconfig', 'abstract': '

prima riga del Altro appconfig

', + 'description': 'Descrizione Altro appconfig', 'keywords': 'keyword5, keyword6', + 'text': 'Testo del Altro appconfig'}, + }, + ) + + _categories_data = ( + {'en': {'name': 'Very loud', 'app_config': 'sample_app'}, + 'it': {'name': 'Fortissimo'}, + }, + {'en': {'name': 'Very very silent', 'app_config': 'sample_app'}, + 'it': {'name': 'Pianississimo'}, + }, + {'en': {'name': 'Almost', 'app_config': 'sample_app'}, + 'it': {'name': 'Mezzo'}, + }, + {'en': {'name': 'Loud', 'parent_id': _get_cat_pk('en', 'Almost'), 'app_config': 'sample_app'}, + 'it': {'name': 'Forte', 'parent_id': _get_cat_pk('it', 'Mezzo')}, + }, + {'en': {'name': 'Silent', 'parent_id': _get_cat_pk('en', 'Almost'), 'app_config': 'sample_app'}, + }, + {'en': {'name': 'Drums', 'app_config': 'sample_app2'}, + 'it': {'name': 'Tamburi'}, + }, + {'en': {'name': 'Guitars', 'app_config': 'sample_app2'}, + 'it': {'name': 'Chitarre'}, + }, + ) + + @classmethod + def setUpClass(cls): + super(BaseTest, cls).setUpClass() + cls.thumb_1 = ThumbnailOption.objects.create( + name='base', width=100, height=100, crop=True, upscale=False + ) + cls.thumb_2 = ThumbnailOption.objects.create( + name='main', width=200, height=200, crop=False, upscale=False + ) + cls.app_config_1 = BlogConfig.objects.create( + namespace='sample_app', app_title='app1' + ) + cls.app_config_2 = BlogConfig.objects.create( + namespace='sample_app2', app_title='app2' + ) + cls.app_config_1.app_data.config.paginate_by = 1 + cls.app_config_1.save() + cls.app_config_2.app_data.config.paginate_by = 2 + cls.app_config_2.save() + cls.app_configs = { + 'sample_app': cls.app_config_1, + 'sample_app2': cls.app_config_2, + } + cls.category_1 = BlogCategory.objects.create(name='category 1', app_config=cls.app_config_1) + cls.category_1.set_current_language('it', initialize=True) + cls.category_1.name = 'categoria 1' + cls.category_1.save() + cls.site_2 = Site.objects.create(domain='http://example2.com', name='example 2') + + @classmethod + def tearDownClass(cls): + super(BaseTest, cls).tearDownClass() + BlogConfig.objects.all().delete() + BlogCategory.objects.all().delete() + ThumbnailOption.objects.all().delete() + + def _get_category(self, data, category=None, lang='en'): + data = deepcopy(data) + for k, v in data.items(): + if hasattr(v, '__call__'): + data[k] = v() + if not category: + with smart_override(lang): + data['app_config'] = self.app_configs[data['app_config']] + category = BlogCategory.objects.create(**data) + else: + category.set_current_language(lang, initialize=True) + for attr, val in data.items(): + setattr(category, attr, val) + category.save() + return category + + def _get_post(self, data, post=None, lang='en', sites=None): + if not post: + post_data = { + 'author': self.user, + 'title': data['title'], + 'abstract': data['abstract'], + 'meta_description': data['description'], + 'meta_keywords': data['keywords'], + 'app_config': self.app_configs[data['app_config']] + } + post = Post.objects.create(**post_data) + else: + post.create_translation( + lang, + title=data['title'], + abstract=data['abstract'], + meta_description=data['description'], + meta_keywords=data['keywords'] + ) + post = self.reload_model(post) + post.categories.add(self.category_1) + if sites: + for site in sites: + post.sites.add(site) + return post + + def get_posts(self, sites=None): + posts = [] + for post in self._post_data: + post1 = self._get_post(post['en'], sites=sites) + post1 = self._get_post(post['it'], post=post1, lang='it') + post1.publish = post['en']['publish'] + post1.main_image = self.create_filer_image_object() + post1.save() + posts.append(post1) + return posts diff --git a/tests/test_menu.py b/tests/test_menu.py index b98c02e..36380ac 100644 --- a/tests/test_menu.py +++ b/tests/test_menu.py @@ -12,7 +12,7 @@ from djangocms_blog.settings import ( ) from djangocms_blog.views import CategoryEntriesView, PostDetailView -from . import BaseTest +from .base import BaseTest class MenuTest(BaseTest): diff --git a/tests/test_models.py b/tests/test_models.py index 743dd5a..77cd4f8 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -24,7 +24,7 @@ from djangocms_blog.cms_appconfig import BlogConfig, BlogConfigForm from djangocms_blog.models import BlogCategory, Post from djangocms_blog.settings import get_setting -from . import BaseTest +from .base import BaseTest class AdminTest(BaseTest): diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 73b0e2a..cb209f2 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -7,12 +7,11 @@ import re from cms.api import add_plugin from django.core.urlresolvers import reverse from django.utils.timezone import now -from djangocms_helper.utils import get_user_model from taggit.models import Tag from djangocms_blog.models import BlogCategory -from . import BaseTest +from .base import BaseTest class PluginTest(BaseTest): diff --git a/tests/test_setup.py b/tests/test_setup.py index 7741307..13428fc 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -10,7 +10,7 @@ from django.utils.translation import override from djangocms_blog.cms_appconfig import BlogConfig -from . import BaseTest +from .base import BaseTest class SetupTest(BaseTest): diff --git a/tests/test_toolbar.py b/tests/test_toolbar.py index 180191d..90b6ccb 100644 --- a/tests/test_toolbar.py +++ b/tests/test_toolbar.py @@ -6,7 +6,7 @@ from django.core.urlresolvers import reverse from djangocms_blog.models import BLOG_CURRENT_POST_IDENTIFIER -from . import BaseTest +from .base import BaseTest class ToolbarTest(BaseTest): diff --git a/tests/test_views.py b/tests/test_views.py index 3bcb5fc..6bb8a4c 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -23,7 +23,7 @@ from djangocms_blog.views import ( TaggedListView, ) -from . import BaseTest +from .base import BaseTest class ViewTest(BaseTest):