diff --git a/cmsplugin_blog_categories/templatetags/cmsplugin_blog_categories_tags.py b/cmsplugin_blog_categories/templatetags/cmsplugin_blog_categories_tags.py index 0c1bebc..8820dd2 100644 --- a/cmsplugin_blog_categories/templatetags/cmsplugin_blog_categories_tags.py +++ b/cmsplugin_blog_categories/templatetags/cmsplugin_blog_categories_tags.py @@ -9,8 +9,13 @@ @register.assignment_tag -def get_category(entry): - return EntryCategory.objects.get(entry=entry).category +def get_category(entry=None): + if entry: + try: + return EntryCategory.objects.get(entry=entry).category + except EntryCategory.DoesNotExist: + pass + return None @register.inclusion_tag( diff --git a/cmsplugin_blog_categories/tests/cmsplugin_tests.py b/cmsplugin_blog_categories/tests/cmsplugin_tests.py new file mode 100644 index 0000000..f386dea --- /dev/null +++ b/cmsplugin_blog_categories/tests/cmsplugin_tests.py @@ -0,0 +1,21 @@ +"""Tests for models of the ``cmsplugin_blog_categories``` application.""" +from django.test import TestCase + +from cmsplugin_blog_categories.cms_plugins import CMSCategoryPlugin +from cmsplugin_blog_categories.tests.factories import CategoryPluginFactory + + +class CMSCategoryPluginTestCase(TestCase): + """Tests for the ``CMSCategoryPlugin`` cmsplugin.""" + longMessage = True + + def setUp(self): + self.plugin = CategoryPluginFactory() + self.cmsplugin = CMSCategoryPlugin() + + def test_render(self): + self.assertEqual( + self.cmsplugin.render(context={}, instance=self.plugin, + placeholder=None), + {'category': self.plugin.category, 'category_entries': [], + 'placeholder': None}) diff --git a/cmsplugin_blog_categories/tests/factories.py b/cmsplugin_blog_categories/tests/factories.py index f9b73a8..5d210ed 100644 --- a/cmsplugin_blog_categories/tests/factories.py +++ b/cmsplugin_blog_categories/tests/factories.py @@ -25,7 +25,21 @@ class CategoryTitleFactoryBase(factory.Factory): class CategoryTitleENFactory(CategoryTitleFactoryBase): """Factory for english ``CategoryTitle`` objects.""" - title = factory.LazyAttribute(lambda a: 'title-{0}'.format(a)) + title = 'Category Title' + language = 'en-us' + + +class CategoryTitleCNFactory(CategoryTitleFactoryBase): + """Factory for chinese ``CategoryTitle`` objects.""" + title = unichr(34900) + language = 'zh-cn' + + +class CategoryPluginFactory(factory.Factory): + """Base factory for factories for ``CategoryPlugin`` models.""" + FACTORY_FOR = models.CategoryPlugin + + category = factory.SubFactory(CategoryFactory) class EntryFactory(factory.Factory): @@ -46,5 +60,5 @@ class EntryCategoryFactory(factory.Factory): """Base factory for factories for ``EntryCategory`` models.""" FACTORY_FOR = models.EntryCategory - category = factory.SubFactory(CategoryTitleENFactory) + category = factory.SubFactory(CategoryFactory) entry = factory.SubFactory(EntryFactory) diff --git a/cmsplugin_blog_categories/tests/integration_tests/views_tests.py b/cmsplugin_blog_categories/tests/integration_tests/views_tests.py index 698c33d..e0057d6 100644 --- a/cmsplugin_blog_categories/tests/integration_tests/views_tests.py +++ b/cmsplugin_blog_categories/tests/integration_tests/views_tests.py @@ -3,7 +3,10 @@ from django_libs.tests.mixins import ViewTestMixin -from cmsplugin_blog_categories.tests.factories import CategoryTitleENFactory +from cmsplugin_blog_categories.tests.factories import ( + CategoryTitleENFactory, + EntryFactory, +) class CategoryListViewTestCase(ViewTestMixin, TestCase): @@ -11,6 +14,7 @@ class CategoryListViewTestCase(ViewTestMixin, TestCase): def setUp(self): self.category_title = CategoryTitleENFactory() self.category = self.category_title.category + self.entry = EntryFactory() def get_view_name(self): return 'blog_archive_category' @@ -19,4 +23,4 @@ def get_view_kwargs(self): return {'category': self.category.slug} def test_view(self): - pass + self.should_be_callable_when_anonymous() diff --git a/cmsplugin_blog_categories/tests/models_tests.py b/cmsplugin_blog_categories/tests/models_tests.py index f3976cc..7a54a54 100644 --- a/cmsplugin_blog_categories/tests/models_tests.py +++ b/cmsplugin_blog_categories/tests/models_tests.py @@ -3,6 +3,7 @@ from cmsplugin_blog_categories.tests.factories import ( CategoryFactory, + CategoryTitleCNFactory, CategoryTitleENFactory, EntryCategoryFactory, EntryFactory, @@ -19,10 +20,23 @@ def setUp(self): def test_model(self): self.assertTrue(self.obj.pk) - def test_translation(self): - self.assertFalse(self.obj.get_translation()) - CategoryTitleENFactory(enquiry=self.obj) - self.assertEqual(self.obj.get_translation().title, 'A question?') + def test_get_title(self): + self.assertEqual(self.obj.get_title(), 'None') + CategoryTitleENFactory(category=self.obj) + self.assertEqual(self.obj.get_title(), 'Category Title') + + +class CategoryTitleTestCase(TestCase): + """Tests for the ``CategoryTitle`` model class.""" + longMessage = True + + def setUp(self): + self.obj_en = CategoryTitleENFactory() + self.obj_cn = CategoryTitleCNFactory() + + def test_model(self): + self.assertTrue(self.obj_en.pk) + self.assertTrue(self.obj_cn.pk) class EntryTestCase(TestCase): diff --git a/cmsplugin_blog_categories/tests/tags_tests.py b/cmsplugin_blog_categories/tests/tags_tests.py new file mode 100644 index 0000000..255d01b --- /dev/null +++ b/cmsplugin_blog_categories/tests/tags_tests.py @@ -0,0 +1,41 @@ +"""Tests for tags of the ``cmsplugin_blog_categories``` application.""" +from django.test import TestCase + +from cmsplugin_blog_categories.templatetags import ( + cmsplugin_blog_categories_tags as tags, +) +from cmsplugin_blog_categories.tests.factories import ( + CategoryFactory, + EntryFactory, + EntryCategoryFactory, +) + + +class GetCategoryTestCase(TestCase): + """Tests for the ``get_category`` tag.""" + longMessage = True + + def setUp(self): + self.entry = EntryFactory() + self.category = EntryCategoryFactory() + + def test_tag(self): + self.assertFalse(tags.get_category()) + self.assertFalse(tags.get_category(self.entry)) + self.assertEqual(tags.get_category(self.category.entry), + self.category.category) + + +class RenderCategoryLinksTestCase(TestCase): + """Tests for the ``render_category_links`` tag.""" + longMessage = True + + def setUp(self): + self.entry_category = EntryCategoryFactory() + self.category = CategoryFactory() + + def test_tag(self): + self.assertEqual(tags.render_category_links({}).get('categories')[0], + self.entry_category.category) + self.assertEqual( + tags.render_category_links({}, True).get('categories').count(), 1) diff --git a/cmsplugin_blog_categories/tests/test_settings.py b/cmsplugin_blog_categories/tests/test_settings.py index 755db8f..4b68f49 100644 --- a/cmsplugin_blog_categories/tests/test_settings.py +++ b/cmsplugin_blog_categories/tests/test_settings.py @@ -28,14 +28,6 @@ os.path.join(os.path.dirname(__file__), '../templates'), ) -COVERAGE_REPORT_HTML_OUTPUT_DIR = os.path.join( - os.path.dirname(__file__), 'coverage') - -COVERAGE_MODULE_EXCLUDES = [ - 'tests$', 'settings$', 'urls$', 'locale$', - 'migrations', 'fixtures', 'admin$', 'django_extensions', -] - # Settings needed to test a multilingual blog LANGUAGE_CODE = 'en-us' LANGUAGES = [ @@ -65,29 +57,39 @@ 'django.contrib.sites', # cms related apps - 'cms', 'sekizai', 'mptt', 'menus', 'cms.plugins.text', # blog related apps - 'cmsplugin_blog', 'djangocms_utils', 'simple_translation', 'tagging', 'missing', ] +COVERAGE_APPS = [ + # Needed to include cmsplugin_blog_categories, due to their relations + 'cms', + 'cmsplugin_blog', +] + INTERNAL_APPS = [ 'django_nose', 'cmsplugin_blog_categories.tests.test_app', 'cmsplugin_blog_categories', ] -INSTALLED_APPS = EXTERNAL_APPS + INTERNAL_APPS +INSTALLED_APPS = EXTERNAL_APPS + INTERNAL_APPS + COVERAGE_APPS -COVERAGE_MODULE_EXCLUDES += EXTERNAL_APPS +COVERAGE_REPORT_HTML_OUTPUT_DIR = os.path.join( + os.path.dirname(__file__), 'coverage') + +COVERAGE_MODULE_EXCLUDES = EXTERNAL_APPS + [ + 'tests$', 'settings$', 'urls$', 'locale$', + 'migrations', 'fixtures', 'admin$', 'django_extensions', +] MIDDLEWARE_CLASSES = [ 'django.middleware.common.CommonMiddleware',