def test_archive_month_includes_only_month(self):
        "Regression for #3031: Archives around Feburary include only one month"
        author = Author(name="John Smith")
        author.save()

        # 2004 was a leap year, so it should be weird enough to not cheat
        first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1)
        first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1)
        two_seconds = timedelta(0, 2, 0)
        article = Article(title="example", author=author)

        article.date_created = first_second_of_feb
        article.save()
        response = self.client.get('/views/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 200)

        article.date_created = first_second_of_feb - two_seconds
        article.save()
        response = self.client.get('/views/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 404)

        article.date_created = first_second_of_mar - two_seconds
        article.save()
        response = self.client.get('/views/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 200)

        article.date_created = first_second_of_mar
        article.save()
        response = self.client.get('/views/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 404)
 def test_archive_month_date_list(self):
     author = Author(name="John Smith")
     author.save()
     date1 = datetime(2010, 1, 1, 0, 0, 0)
     date2 = datetime(2010, 1, 2, 0, 0, 0)
     Article.objects.create(title='example1',
                            author=author,
                            date_created=date1)
     Article.objects.create(title='example2',
                            author=author,
                            date_created=date2)
     response = self.client.get('/date_based/archive_month/2010/1/')
     self.assertEqual(response.status_code, 200)
     self.assertEqual(len(response.context['date_list']), 2)
     self.assertEqual(response.context['date_list'][0], date1)
     # Checks that the same date is not included more than once in the list
     Article.objects.create(title='example2',
                            author=author,
                            date_created=date2)
     response = self.client.get('/date_based/archive_month/2010/1/')
     self.assertEqual(len(response.context['date_list']), 2)
    def test_archive_month_includes_only_month(self):
        "Regression for #3031: Archives around Feburary include only one month"
        author = Author(name="John Smith")
        author.save()

        # 2004 was a leap year, so it should be weird enough to not cheat
        first_second_of_feb = datetime(2004, 2, 1, 0, 0, 1)
        first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1)
        two_seconds = timedelta(0, 2, 0)
        article = Article(title="example", author=author)

        article.date_created = first_second_of_feb
        article.save()
        response = self.client.get('/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))

        article.date_created = first_second_of_feb - two_seconds
        article.save()
        response = self.client.get('/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 404)

        article.date_created = first_second_of_mar - two_seconds
        article.save()
        response = self.client.get('/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))

        article.date_created = first_second_of_mar
        article.save()
        response = self.client.get('/date_based/archive_month/2004/02/')
        self.assertEqual(response.status_code, 404)

        article2 = DateArticle(title="example", author=author)

        article2.date_created = first_second_of_feb.date()
        article2.save()
        response = self.client.get(
            '/date_based/datefield/archive_month/2004/02/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))

        article2.date_created = (first_second_of_feb - two_seconds).date()
        article2.save()
        response = self.client.get(
            '/date_based/datefield/archive_month/2004/02/')
        self.assertEqual(response.status_code, 404)

        article2.date_created = (first_second_of_mar - two_seconds).date()
        article2.save()
        response = self.client.get(
            '/date_based/datefield/archive_month/2004/02/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['next_month'], date(2004, 3, 1))
        self.assertEqual(response.context['previous_month'], date(2004, 1, 1))

        article2.date_created = first_second_of_mar.date()
        article2.save()
        response = self.client.get(
            '/date_based/datefield/archive_month/2004/02/')
        self.assertEqual(response.status_code, 404)

        now = datetime.now()
        prev_month = now.date().replace(day=1)
        if prev_month.month == 1:
            prev_month = prev_month.replace(year=prev_month.year - 1, month=12)
        else:
            prev_month = prev_month.replace(month=prev_month.month - 1)
        article2.date_created = now
        article2.save()
        response = self.client.get('/date_based/datefield/archive_month/%s/' %
                                   now.strftime('%Y/%m'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['next_month'], None)
        self.assertEqual(response.context['previous_month'], prev_month)