Source

pentasubmitter / src / submitter / tests / models.py

Full commit
"""
unittests for submitter application
"""

import sys
import unittest
import datetime
from markdown import markdown
from django.test import Client, TestCase
from django.contrib.auth.models import User
from django.db import IntegrityError
from submitter import admin, models, views
from pentasubmitter import settings, template
from show.models import PentaShow, ShowType

class SubmitterCheckSettings(unittest.TestCase):
    "general settings in settings.py"

    def test_installed_application(self):
        "Am I installed?"
        self.assertTrue(
            'submitter' in settings.INSTALLED_APPS,
            "Submitter app not registered in settings.py"
        )


class EntryCategoryTest(TestCase):
    """EntryCategory() Tests"""

    def setUp(self):
        self.entry_category1 = models.EntryCategory(
            title=u"Fnord",
            description=u"The ususal Fnords",
            tags_string=u"fnord general",
        )
        self.entry_category1.save()

        self.entry_category2 = models.EntryCategory(
            title="Fnord",
            description="The ususal Fnords",
            tags_string="fnord general"
        )
        self.entry_category2.save()

        self.entry_category3 = models.EntryCategory(
            title="Fnord",
            description="The ususal Fnords",
            tags_string="fnord general"
        )
        self.entry_category3.save()

    def test_entry_categories(self):
        "Test EntryCategory()"
        self.assertEquals(self.entry_category1.title, 'Fnord')
        self.assertNotEquals(self.entry_category1.title, 'Nnord')
        self.assertEquals(self.entry_category1.description,
                          'The ususal Fnords')
        self.assertEquals([t.name for t in self.entry_category1.tags],
                          [u'fnord', u'general'])
        self.assertEquals(self.entry_category1.slug, 'fnord')
        self.assertNotEquals(self.entry_category1.get_absolute_url(),
                          '/submit/news-category/fnord001/')
        self.assertEquals(self.entry_category1.get_absolute_url(),
                          '/submit/news-category/fnord/')
        self.entry_category2.save()
        self.assertEquals(self.entry_category2.slug, 'fnord001')
        # need to save again to see that slug doesn't get increased
        self.entry_category2.save()
        self.assertEquals(self.entry_category2.slug, 'fnord001')
        self.assertEquals(self.entry_category2.get_absolute_url(),
                          '/submit/news-category/fnord001/')
        self.entry_category3.save()
        self.assertEquals(self.entry_category3.slug, 'fnord002')


class NewsEntryTest(TestCase):
    """NewsEntry() Tests"""

    def setUp(self):
        "Fixtures"

        user = User()
        user.save()
        self.user = models.Submitter()
        self.user.user = user
        self.user.save()
        self.show_type1=ShowType(
            show_type=1,
        )
        self.show_type1.save()
        date_now = datetime.datetime.now()

        self.penta_show1 = PentaShow(
            title=u"Hacking is not a crime",
            excerpt=u"We talk about the motto: Hacking is not a crime.",
            description=(u'Fridolin is the next King of the Land of the free\n'
                  u'=================================================\n\n'
                  u'As we have learned from [c3d2](http://www.c3d2.de/ "c3d2") '
                  u'news Fridolin is the next King of Hacking.'),
            show_type=1,
        )
        self.penta_show1.save()

        self.entry_category1 = models.EntryCategory(
            title=u"Fnord",
            description=u"The ususal Fnords",
            tags_string=u"fnord general",
        )
        self.entry_category1.save()

        self.entry_category2 = models.EntryCategory(
            title="Fnord",
            description="The ususal Fnords",
            tags_string = "fnord general"
        )
        self.entry_category2.save()

        self.date_test=datetime.datetime.today()
        self.news_entry1 = models.NewsEntry(
            title=u"Fridolin got King",
            excerpt=u"Fridolin was made the next *King*",
            body=(u'Fridolin is the next King of the Land of the free\n'
                  u'=================================================\n\n'
                  u'As we have learned from [c3d2](http://www.c3d2.de/ "c3d2") '
                  u'news Fridolin is the next King of Hacking.'),
            author=self.user,
            enable_comments=False,
            show=self.penta_show1,
            featured=True,
            status=1,
            submit_date=self.date_test,
            external_url="http://www.c3d2.de",
        )
        self.news_entry1.save()
        self.news_entry1.tags = "Foo, Bar"
        self.news_entry1.categories.add(self.entry_category1,
                                        self.entry_category2)

    def test_news_entry(self):
        "Test NewsEntry()"

        self.assertEquals(self.news_entry1.title, u"Fridolin got King")
        self.assertEquals(self.news_entry1.excerpt,
                          u"Fridolin was made the next *King*")
        self.assertEquals(self.news_entry1.excerpt_html,
                          u"<p>Fridolin was made the next <em>King</em></p>")
        self.assertEquals(
            self.news_entry1.body_html,
            (u'<h1>Fridolin is the next King of the Land of the free</h1>\n'
             u'<p>As we have learned from <a href="http://www.c3d2.de/" '
             u'title="c3d2">c3d2</a> news Fridolin is the next King of '
             u'Hacking.</p>')
        )
        self.assertEquals(self.news_entry1.external_url, u"http://www.c3d2.de")
        self.assert_(self.entry_category1 in self.news_entry1.categories.all())
        self.assert_(self.entry_category2 in self.news_entry1.categories.all())
        self.assert_(u'Foo'.lower() in [ t.name for t in self.news_entry1.tags])
        self.assert_(u'Bar'.lower() in self.news_entry1.get_tag_list())
        self.assertEquals(self.news_entry1.slug, u"fridolingotking")
        self.assertEquals(
            self.news_entry1.get_absolute_url(),
            '/submit/%(year)s/%(month)s/%(day)s/fridolingotking/' % (
                {'year': self.date_test.strftime("%Y"),
                 'month': self.date_test.strftime("%m"),
                 'day': self.date_test.strftime("%d"),
                }
            )
        )

    def test_set_title_news_entry(self):
        "Test if NewsEntry.title is set automatically"

        self.news_entry2 = models.NewsEntry(
            excerpt=u"Fridolin was made the next *King*",
            body=(u'Fridolin is the next King of the Land of the free\n'
                  u'=================================================\n\n'
                  u'As we have learned from [c3d2](http://www.c3d2.de/ "c3d2") '
                  u'news Fridolin is the next King of Hacking.'),
            author=self.user,
            enable_comments=False,
            show=self.penta_show1,
            featured=False,
            status=1,
            submit_date=self.date_test,
            external_url="http://www.alien8.de",
            tags_string=u"a8 alien8",
        )
        self.news_entry2.save()
        self.news_entry2.categories.add(self.entry_category1,
                                        self.entry_category2)
        self.news_entry2.save()
        self.news_entry2 = models.NewsEntry(
            excerpt=u"Fridolin was made the next *King*",
            body=(u'Fridolin is the next King of the Land of the free\n'
                  u'=================================================\n\n'
                  u'As we have learned from [c3d2](http://www.c3d2.de/ "c3d2") '
                  u'news Fridolin is the next King of Hacking.'),
            author=self.user,
            enable_comments=False,
            show=self.penta_show1,
            featured=False,
            status=1,
            submit_date=self.date_test,
            external_url="http://www.alien8.de",
            tags_string=u"a8 alien8",
        )
        self.news_entry2.save()
        self.news_entry2.categories.add(self.entry_category1,
                                        self.entry_category2)
        self.assertEquals(self.news_entry2.title, u"Alien8: Home")


# vim: ts=4 et sw=4 fileencoding=utf-8 :