Skip to content

Commit

Permalink
tried to fix views and wrote tests, which aren't working
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Lorenz committed Feb 19, 2013
1 parent 1a5d8e2 commit 58b7d2a
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 12 deletions.
19 changes: 14 additions & 5 deletions cmsplugin_blog_categories/admin.py
Expand Up @@ -20,14 +20,20 @@ class EntryCategoryAdmin(admin.ModelAdmin):

def category_title(self, obj):
lang = get_language()
return get_translation_queryset(obj.category).filter(
language=lang)[0].title
try:
return get_translation_queryset(obj.category).filter(
language=lang)[0].title
except IndexError:
return 'None'
category_title.short_description = _('Category')

def entry_title(self, obj):
lang = get_language()
return get_translation_queryset(obj.entry).filter(
language=lang)[0].title
try:
return get_translation_queryset(obj.entry).filter(
language=lang)[0].title
except IndexError:
return 'None'
entry_title.short_description = _('Entry title')


Expand All @@ -36,7 +42,10 @@ class CategoryAdmin(TranslationAdmin):

def title(self, obj):
lang = get_language()
return get_translation_queryset(obj).filter(language=lang)[0].title
try:
return get_translation_queryset(obj).filter(language=lang)[0].title
except IndexError:
return 'None'
title.short_description = _('Title')


Expand Down
6 changes: 5 additions & 1 deletion cmsplugin_blog_categories/models.py
Expand Up @@ -31,7 +31,11 @@ def get_absolute_url(self):

def get_title(self):
lang = get_language()
return get_translation_queryset(self).filter(language=lang)[0].title
try:
return get_translation_queryset(self).filter(
language=lang)[0].title
except IndexError:
return 'None'


class CategoryTitle(models.Model):
Expand Down
@@ -1,4 +1,4 @@
"""Templatetafs for the ``cmsplugin_blog_categories`` app."""
"""Templatetags for the ``cmsplugin_blog_categories`` app."""
from django import template
from django.db.models import Count

Expand Down
50 changes: 50 additions & 0 deletions cmsplugin_blog_categories/tests/factories.py
@@ -0,0 +1,50 @@
"""
Utilities for creating test objects related to the
``cmsplugin_blog_categories`` app.
"""
from cmsplugin_blog.models import Entry, EntryTitle
import factory

from cmsplugin_blog_categories import models


class CategoryFactory(factory.Factory):
"""Factory for the ``Category`` model."""
FACTORY_FOR = models.Category

slug = factory.LazyAttribute(lambda a: 'category-{0}'.format(a))


class CategoryTitleFactoryBase(factory.Factory):
"""Base factory for factories for ``CategoryTitle`` models."""
FACTORY_FOR = models.CategoryTitle

category = factory.SubFactory(CategoryFactory)


class CategoryTitleENFactory(CategoryTitleFactoryBase):
"""Factory for english ``CategoryTitle`` objects."""
title = factory.LazyAttribute(lambda a: 'title-{0}'.format(a))


class EntryFactory(factory.Factory):
"""Base factory for factories for ``Entry`` models."""
FACTORY_FOR = Entry

is_published = True


class EntryTitleFactory(factory.Factory):
"""Base factory for factories for ``EntryTitle`` models."""
FACTORY_FOR = EntryTitle

entry = factory.SubFactory(EntryFactory)


class EntryCategoryFactory(factory.Factory):
"""Base factory for factories for ``EntryCategory`` models."""
FACTORY_FOR = models.EntryCategory

category = factory.SubFactory(CategoryTitleENFactory)
entry = factory.SubFactory(EntryFactory)
22 changes: 22 additions & 0 deletions cmsplugin_blog_categories/tests/integration_tests/views_tests.py
@@ -0,0 +1,22 @@
"""Tests for the views of the ``cmsplugin_blog_categories`` app."""
from django.test import TestCase

from django_libs.tests.mixins import ViewTestMixin

from cmsplugin_blog_categories.tests.factories import CategoryTitleENFactory


class CategoryListViewTestCase(ViewTestMixin, TestCase):
"""Tests for the ``CategoryListView`` generic view class."""
def setUp(self):
self.category_title = CategoryTitleENFactory()
self.category = self.category_title.category

def get_view_name(self):
return 'blog_archive_category'

def get_view_kwargs(self):
return {'category': self.category.slug}

def test_view(self):
pass
47 changes: 47 additions & 0 deletions cmsplugin_blog_categories/tests/models_tests.py
@@ -0,0 +1,47 @@
"""Tests for models of the ``cmsplugin_blog_categories``` application."""
from django.test import TestCase

from cmsplugin_blog_categories.tests.factories import (
CategoryFactory,
CategoryTitleENFactory,
EntryCategoryFactory,
EntryFactory,
)


class CategoryTestCase(TestCase):
"""Tests for the ``Category`` model class."""
longMessage = True

def setUp(self):
self.obj = CategoryFactory()

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?')


class EntryTestCase(TestCase):
"""Tests for the ``Entry`` model class."""
longMessage = True

def setUp(self):
self.obj = EntryFactory()

def test_model(self):
self.assertTrue(self.obj.pk)


class EntryCategoryTestCase(TestCase):
"""Tests for the ``EntryCategory`` model class."""
longMessage = True

def setUp(self):
self.obj = EntryCategoryFactory()

def test_model(self):
self.assertTrue(self.obj.pk)
1 change: 1 addition & 0 deletions cmsplugin_blog_categories/tests/urls.py
Expand Up @@ -14,5 +14,6 @@
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('cmsplugin_blog_categories.urls')),
url(r'^', include('cms.urls')),
)
8 changes: 3 additions & 5 deletions cmsplugin_blog_categories/views.py
@@ -1,9 +1,7 @@
"""Views of the ``cmsplugin_blog_categories`` app."""
from django.views.generic import ListView

from cmsplugin_blog.models import Entry
from cmsplugin_blog_categories.models import Category
from simple_translation.middleware import filter_queryset_language


class CategoryListView(ListView):
Expand All @@ -21,6 +19,6 @@ def get_context_data(self, **kwargs):
return ctx

def get_queryset(self):
qs = Entry.objects.filter(entrycategory__category=self.category)
qs = filter_queryset_language(self.request, qs)
return qs
return [category.entry for category
in self.category.entry_categories.all()
if category.entry.is_published]
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -16,6 +16,7 @@ South==0.7.6
# ==============================================================
# Packages needed for running the tests. Needed by contributors.
# ==============================================================
django-libs==0.14.1
Fabric
mock
django-nose
Expand Down

0 comments on commit 58b7d2a

Please sign in to comment.