# Path: django_fortunes/models.py
# class Fortune(models.Model):
#     author = models.ForeignKey(User)
#     title = models.CharField(max_length=200, blank=False)
#     slug = models.SlugField(_('slug'), db_index=True, max_length=255, unique_for_date='pub_date')
#     content = models.TextField(blank=False)
#     pub_date = models.DateTimeField(_('published date'), db_index=True, default=datetime.now())
#     votes = models.IntegerField(default=0)
#     comments = generic.GenericRelation(
#         Comment,
#         content_type_field='content_type',
#         object_id_field='object_pk'
#     )
# 
#     objects = FortuneManager()
# 
#     def __unicode__(self):
#         return _("%(title)s, from %(author)s") % {
#             'title': self.title,
#             'author': self.author.username ,
#         }
# 
#     def check_slug(self):
#         """
#         If no slug has been generated yet for the current Fortune, tries to generate a
#         unique one
#         """
#         if not self.slug:
#             prefix = ""
#             i = 0
#             while True:
#                 self.slug = prefix + slugify(self.title)
#                 try:
#                     self._default_manager.values('id').get(slug=self.slug)
#                     i += 1
#                     prefix = str(i) + "-"
#                 except Fortune.DoesNotExist:
#                     break
#         return self.slug
# 
#     @models.permalink
#     def get_absolute_url(self):
#         "Retrieves the absolute django url of a fortune"
#         return ('fortune_detail', (), {
#             'slug': self.slug,
#             'year': self.pub_date.year,
#             'month': self.pub_date.month,
#             'day': self.pub_date.day
#         })
# 
#     def save(self):
#         "Saves a fortune after havng checked and generated its slug"
#         self.check_slug()
#         super(Fortune, self).save()


import unittest
import time
from utils import FortuneTransactionTestCase
from datetime import datetime
from django.test.testcases import TestCase, TransactionTestCase
from django_fortunes.models import Fortune


class FortuneManagerTest(FortuneTransactionTestCase):
    def test_latest(self):
        latest = Fortune.objects.latest()
        self.assertEqual(len(latest), 3)
        self.assertEqual(latest[0].author.username, 'Dude')
        self.assertEqual(latest[0].title, u'Another One')
        self.assertEqual(latest[1].author.username, 'NiKo')
        self.assertEqual(latest[1].title, u'A funny one')
        self.assertEqual(latest[2].author.username, 'NiKo')
        self.assertEqual(latest[2].title, u'My first fortune')
    
    def test_latest_by_author(self):
        latest_from_niko = Fortune.objects.latest_by_author('NiKo')
        self.assertEqual(len(latest_from_niko), 2)
        self.assertEqual(latest_from_niko[0].author.username, 'NiKo')
        self.assertEqual(latest_from_niko[0].title, u'A funny one')
        self.assertEqual(latest_from_niko[1].author.username, 'NiKo')
        self.assertEqual(latest_from_niko[1].title, u'My first fortune')
    
    def test_published(self):
        latest = Fortune.objects.published()
        self.assertEqual(len(latest), 3)
        f = self.create('foo', content='bar', pub_date=datetime.fromtimestamp(time.time() + 86400))
        new_latest = Fortune.objects.published()
        self.assertEqual(len(new_latest), 3)

    def test_top_authors(self):

# Path: django_fortunes/models.py
# class Fortune(models.Model):
#     author = models.ForeignKey(User)
#     title = models.CharField(max_length=200, blank=False)
#     slug = models.SlugField(_('slug'), db_index=True, max_length=255, unique_for_date='pub_date')
#     content = models.TextField(blank=False)
#     pub_date = models.DateTimeField(_('published date'), db_index=True, default=datetime.now())
#     votes = models.IntegerField(default=0)
#     comments = generic.GenericRelation(
#         Comment,
#         content_type_field='content_type',
#         object_id_field='object_pk'
#     )
# 
#     objects = FortuneManager()
# 
#     def __unicode__(self):
#         return _("%(title)s, from %(author)s") % {
#             'title': self.title,
#             'author': self.author.username ,
#         }
# 
#     def check_slug(self):
#         """
#         If no slug has been generated yet for the current Fortune, tries to generate a
#         unique one
#         """
#         if not self.slug:
#             prefix = ""
#             i = 0
#             while True:
#                 self.slug = prefix + slugify(self.title)
#                 try:
#                     self._default_manager.values('id').get(slug=self.slug)
#                     i += 1
#                     prefix = str(i) + "-"
#                 except Fortune.DoesNotExist:
#                     break
#         return self.slug
# 
#     @models.permalink
#     def get_absolute_url(self):
#         "Retrieves the absolute django url of a fortune"
#         return ('fortune_detail', (), {
#             'slug': self.slug,
#             'year': self.pub_date.year,
#             'month': self.pub_date.month,
#             'day': self.pub_date.day
#         })
# 
#     def save(self):
#         "Saves a fortune after havng checked and generated its slug"
#         self.check_slug()
#         super(Fortune, self).save()


# Path: django_fortunes/tests/managers.py
import unittest
import time
from utils import FortuneTransactionTestCase
from datetime import datetime
from django.test.testcases import TestCase, TransactionTestCase
from django_fortunes.models import Fortune



class FortuneManagerTest(FortuneTransactionTestCase):
    def test_latest(self):
        latest = Fortune.objects.latest()
        self.assertEqual(len(latest), 3)
        self.assertEqual(latest[0].author.username, 'Dude')
        self.assertEqual(latest[0].title, u'Another One')
        self.assertEqual(latest[1].author.username, 'NiKo')
        self.assertEqual(latest[1].title, u'A funny one')
        self.assertEqual(latest[2].author.username, 'NiKo')
        self.assertEqual(latest[2].title, u'My first fortune')
    
    def test_latest_by_author(self):
        latest_from_niko = Fortune.objects.latest_by_author('NiKo')
        self.assertEqual(len(latest_from_niko), 2)
        self.assertEqual(latest_from_niko[0].author.username, 'NiKo')
        self.assertEqual(latest_from_niko[0].title, u'A funny one')
        self.assertEqual(latest_from_niko[1].author.username, 'NiKo')
        self.assertEqual(latest_from_niko[1].title, u'My first fortune')
    
    def test_published(self):
        latest = Fortune.objects.published()
        self.assertEqual(len(latest), 3)
        f = self.create('foo', content='bar', pub_date=datetime.fromtimestamp(time.time() + 86400))
        new_latest = Fortune.objects.published()
        self.assertEqual(len(new_latest), 3)

    def test_top_authors(self):

        top_authors = Fortune.objects.top_authors()