# Path: dwitter/feed/views.py
# class NewHashtagFeed(HashtagFeed):
#     sort = SortMethod.NEW
#     feed_name = "new"
# 
#     def get_title(self):
#         hashtag_name = self.kwargs['hashtag_name']
#         return "New #" + hashtag_name + " dweets | Dwitter"
# 
# class TopHashtagFeed(HashtagFeed):
#     sort = SortMethod.TOP
#     feed_name = "top"
# 
#     def get_title(self):
#         hashtag_name = self.kwargs['hashtag_name']
#         return "Top #" + hashtag_name + " dweets | Dwitter"
# 
#     def get_dweet_list(self):
#         return super().get_dweet_list().annotate(num_likes=Count('likes'))
# 
# Path: dwitter/models.py
# class Comment(models.Model):
#     text = models.TextField()
#     posted = models.DateTimeField()
#     reply_to = models.ForeignKey(Dweet, on_delete=models.CASCADE,
#                                  related_name="comments")
#     author = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         ordering = ('posted',)
# 
#     def __str__(self):
#         return ('c/' +
#                 str(self.id) +
#                 ' (' +
#                 self.author.username +
#                 ') to ' +
#                 str(self.reply_to))
# 
# class Dweet(models.Model):
#     code = models.TextField()
#     posted = models.DateTimeField(db_index=True)
#     reply_to = models.ForeignKey("self", on_delete=models.DO_NOTHING,
#                                  related_name="remixes", null=True, blank=True)
# 
#     likes = models.ManyToManyField(User, related_name="liked", blank=True)
#     hotness = models.FloatField(default=1.0, db_index=True)
#     deleted = models.BooleanField(default=False)
# 
#     author = models.ForeignKey(User, on_delete=models.SET(get_sentinel_user),
#                                null=True, blank=True)
# 
#     objects = NotDeletedDweetManager()
#     with_deleted = models.Manager()
# 
#     class Meta:
#         ordering = ('-posted',)
# 
#     def delete(self):
#         self.deleted = True
#         self.save()
# 
#     def save(self, *args, **kwargs):
#         self.calculate_hotness((self.pk is None))
#         super(Dweet, self).save(*args, **kwargs)
# 
#     @cached_property
#     def top_comment(self):
#         """
#         Return the top comment. This is mainly a caching optimization to avoid queries
#         """
#         return self.comments.first()
# 
#     @cached_property
#     def dweet_length(self):
#         return length_of_code(self.code)
# 
#     @cached_property
#     def has_sticky_comment(self):
#         """
#         True when first comment should be stickied (first comment author == dweet author)
#         """
#         if self.top_comment is None:
#             return False
#         return self.top_comment.author == self.author
# 
#     def __str__(self):
#         return 'd/' + str(self.id) + ' (' + self.author.username + ')'
# 
#     def calculate_hotness(self, is_new):
#         """
#         Hotness is inspired by the Hackernews ranking algorithm
#         Read more here:
#         https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d
#         """
#         def epoch_seconds(date):
#             epoch = datetime(2015, 5, 5)  # arbitrary start date before Dwitter existed
#             naive = date.replace(tzinfo=None)
#             td = naive - epoch
#             return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
# 
#         likes = 1
#         if not is_new:
#             likes = max(self.likes.count(), 1)
# 
#         order = log(likes, 2)
#         # 86400 seconds = 24 hours.
#         # So for every log(2) likes on a dweet, its effective
#         # "posted time" moves 24 forward
#         # In other words, it takes log2(likes) * 24hrs before
#         # a dweet with a single like beat yours
#         self.hotness = round(order + epoch_seconds(self.posted)/86400, 7)


import random
from django.http import Http404
from django.test import TestCase
from dwitter.feed.views import NewHashtagFeed, TopHashtagFeed
from dwitter.models import Comment, Dweet
from django.test.client import RequestFactory
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import timedelta
        queryset = self.dweetFeed.get_queryset()
        self.assertEqual(queryset.count(), 1)

    def test_no_default_title(self):
        request = self.request_factory.get('/')
        request.session = {}
        response = self.dweetFeed.__class__.as_view()(request, hashtag_name='everyone')
        response.render()
        html = response.content.decode('utf8')
        self.assertNotIn('<title>' + self.dweetFeed.title + '</title>', html)

    def test_got_title(self):
        self.dweetFeed.kwargs = {'hashtag_name': 'everyone'}
        request = self.request_factory.get('/')
        request.session = {}
        response = self.dweetFeed.__class__.as_view()(request, hashtag_name='everyone')
        response.render()
        html = response.content.decode('utf8')
        self.assertIn('<title>' + self.dweetFeed.get_title() + '</title>', html)

    def test_html_response(self):
        request = self.request_factory.get('/')
        request.session = {}
        response = self.dweetFeed.__class__.as_view()(request, hashtag_name='everyone')
        response.render()
        html = response.content.decode('utf8')
        self.assertIn('<title>', html)


class TopHashtagFeedTests(HashtagFeedTestCase, TestCase):

# Path: dwitter/feed/views.py
# class NewHashtagFeed(HashtagFeed):
#     sort = SortMethod.NEW
#     feed_name = "new"
# 
#     def get_title(self):
#         hashtag_name = self.kwargs['hashtag_name']
#         return "New #" + hashtag_name + " dweets | Dwitter"
# 
# class TopHashtagFeed(HashtagFeed):
#     sort = SortMethod.TOP
#     feed_name = "top"
# 
#     def get_title(self):
#         hashtag_name = self.kwargs['hashtag_name']
#         return "Top #" + hashtag_name + " dweets | Dwitter"
# 
#     def get_dweet_list(self):
#         return super().get_dweet_list().annotate(num_likes=Count('likes'))
# 
# Path: dwitter/models.py
# class Comment(models.Model):
#     text = models.TextField()
#     posted = models.DateTimeField()
#     reply_to = models.ForeignKey(Dweet, on_delete=models.CASCADE,
#                                  related_name="comments")
#     author = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         ordering = ('posted',)
# 
#     def __str__(self):
#         return ('c/' +
#                 str(self.id) +
#                 ' (' +
#                 self.author.username +
#                 ') to ' +
#                 str(self.reply_to))
# 
# class Dweet(models.Model):
#     code = models.TextField()
#     posted = models.DateTimeField(db_index=True)
#     reply_to = models.ForeignKey("self", on_delete=models.DO_NOTHING,
#                                  related_name="remixes", null=True, blank=True)
# 
#     likes = models.ManyToManyField(User, related_name="liked", blank=True)
#     hotness = models.FloatField(default=1.0, db_index=True)
#     deleted = models.BooleanField(default=False)
# 
#     author = models.ForeignKey(User, on_delete=models.SET(get_sentinel_user),
#                                null=True, blank=True)
# 
#     objects = NotDeletedDweetManager()
#     with_deleted = models.Manager()
# 
#     class Meta:
#         ordering = ('-posted',)
# 
#     def delete(self):
#         self.deleted = True
#         self.save()
# 
#     def save(self, *args, **kwargs):
#         self.calculate_hotness((self.pk is None))
#         super(Dweet, self).save(*args, **kwargs)
# 
#     @cached_property
#     def top_comment(self):
#         """
#         Return the top comment. This is mainly a caching optimization to avoid queries
#         """
#         return self.comments.first()
# 
#     @cached_property
#     def dweet_length(self):
#         return length_of_code(self.code)
# 
#     @cached_property
#     def has_sticky_comment(self):
#         """
#         True when first comment should be stickied (first comment author == dweet author)
#         """
#         if self.top_comment is None:
#             return False
#         return self.top_comment.author == self.author
# 
#     def __str__(self):
#         return 'd/' + str(self.id) + ' (' + self.author.username + ')'
# 
#     def calculate_hotness(self, is_new):
#         """
#         Hotness is inspired by the Hackernews ranking algorithm
#         Read more here:
#         https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d
#         """
#         def epoch_seconds(date):
#             epoch = datetime(2015, 5, 5)  # arbitrary start date before Dwitter existed
#             naive = date.replace(tzinfo=None)
#             td = naive - epoch
#             return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
# 
#         likes = 1
#         if not is_new:
#             likes = max(self.likes.count(), 1)
# 
#         order = log(likes, 2)
#         # 86400 seconds = 24 hours.
#         # So for every log(2) likes on a dweet, its effective
#         # "posted time" moves 24 forward
#         # In other words, it takes log2(likes) * 24hrs before
#         # a dweet with a single like beat yours
#         self.hotness = round(order + epoch_seconds(self.posted)/86400, 7)


# Path: dwitter/tests/feed/test_hashtag_feed_views.py
import random
from django.http import Http404
from django.test import TestCase
from dwitter.feed.views import NewHashtagFeed, TopHashtagFeed
from dwitter.models import Comment, Dweet
from django.test.client import RequestFactory
from django.contrib.auth.models import User
from django.utils import timezone
from datetime import timedelta

        queryset = self.dweetFeed.get_queryset()
        self.assertEqual(queryset.count(), 1)

    def test_no_default_title(self):
        request = self.request_factory.get('/')
        request.session = {}
        response = self.dweetFeed.__class__.as_view()(request, hashtag_name='everyone')
        response.render()
        html = response.content.decode('utf8')
        self.assertNotIn('<title>' + self.dweetFeed.title + '</title>', html)

    def test_got_title(self):
        self.dweetFeed.kwargs = {'hashtag_name': 'everyone'}
        request = self.request_factory.get('/')
        request.session = {}
        response = self.dweetFeed.__class__.as_view()(request, hashtag_name='everyone')
        response.render()
        html = response.content.decode('utf8')
        self.assertIn('<title>' + self.dweetFeed.get_title() + '</title>', html)

    def test_html_response(self):
        request = self.request_factory.get('/')
        request.session = {}
        response = self.dweetFeed.__class__.as_view()(request, hashtag_name='everyone')
        response.render()
        html = response.content.decode('utf8')
        self.assertIn('<title>', html)


class TopHashtagFeedTests(HashtagFeedTestCase, TestCase):

    dweetFeed = TopHashtagFeed()