Update tests

This commit is contained in:
Joachim 2021-12-22 18:15:05 +01:00
parent b03b6f6d6f
commit 07f2d9a11c
2 changed files with 63 additions and 34 deletions

View file

@ -34,6 +34,7 @@ class AnnualSummary(TestCase):
local=True, local=True,
localname="mouse", localname="mouse",
remote_id="https://example.com/users/mouse", remote_id="https://example.com/users/mouse",
summary_keys={"2020": "0123456789"},
) )
self.work = models.Work.objects.create(title="Test Work") self.work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create( self.book = models.Edition.objects.create(
@ -42,18 +43,11 @@ class AnnualSummary(TestCase):
parent_work=self.work, parent_work=self.work,
pages=300, pages=300,
) )
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
self.review = models.Review.objects.create(
name="Review name",
content="test content",
rating=3.0,
user=self.local_user,
book=self.book,
)
self.anonymous_user = AnonymousUser self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False self.anonymous_user.is_authenticated = False
self.year = 2020 self.year = "2020"
models.SiteSettings.objects.create() models.SiteSettings.objects.create()
def test_annual_summary_not_authenticated(self, *_): def test_annual_summary_not_authenticated(self, *_):
@ -62,12 +56,24 @@ class AnnualSummary(TestCase):
request = self.factory.get("") request = self.factory.get("")
request.user = self.anonymous_user request.user = self.anonymous_user
with patch( with self.assertRaises(Http404):
"bookwyrm.views.annual_summary.is_year_available" view(request, self.local_user.localname, self.year)
) as is_year_available:
is_year_available.return_value = True def test_annual_summary_not_authenticated_with_key(self, *_):
with self.assertRaises(Http404): """there are so many views, this just makes sure it DOES LOAD"""
view(request, self.year) key = self.local_user.summary_keys[self.year]
view = views.AnnualSummary.as_view()
request_url = (
f"user/{self.local_user.localname}/{self.year}-in-the-books?key={key}"
)
request = self.factory.get(request_url)
request.user = self.anonymous_user
result = view(request, self.local_user.localname, self.year)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
def test_annual_summary_wrong_year(self, *_): def test_annual_summary_wrong_year(self, *_):
"""there are so many views, this just makes sure it DOESNT LOAD""" """there are so many views, this just makes sure it DOESNT LOAD"""
@ -75,12 +81,8 @@ class AnnualSummary(TestCase):
request = self.factory.get("") request = self.factory.get("")
request.user = self.anonymous_user request.user = self.anonymous_user
with patch( with self.assertRaises(Http404):
"bookwyrm.views.annual_summary.is_year_available" view(request, self.local_user.localname, self.year)
) as is_year_available:
is_year_available.return_value = False
with self.assertRaises(Http404):
view(request, self.year)
def test_annual_summary_empty_page(self, *_): def test_annual_summary_empty_page(self, *_):
"""there are so many views, this just makes sure it LOADS""" """there are so many views, this just makes sure it LOADS"""
@ -88,11 +90,8 @@ class AnnualSummary(TestCase):
request = self.factory.get("") request = self.factory.get("")
request.user = self.local_user request.user = self.local_user
with patch( result = view(request, self.local_user.localname, self.year)
"bookwyrm.views.annual_summary.is_year_available"
) as is_year_available:
is_year_available.return_value = True
result = view(request, self.year)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
validate_html(result.render()) validate_html(result.render())
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -114,11 +113,38 @@ class AnnualSummary(TestCase):
request = self.factory.get("") request = self.factory.get("")
request.user = self.local_user request.user = self.local_user
with patch( result = view(request, self.local_user.localname, self.year)
"bookwyrm.views.annual_summary.is_year_available"
) as is_year_available: self.assertIsInstance(result, TemplateResponse)
is_year_available.return_value = True validate_html(result.render())
result = view(request, self.year) self.assertEqual(result.status_code, 200)
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
def test_annual_summary_page_with_review(self, *_):
"""there are so many views, this just makes sure it LOADS"""
self.review = models.Review.objects.create(
name="Review name",
content="test content",
rating=3.0,
user=self.local_user,
book=self.book,
)
shelf = self.local_user.shelf_set.filter(identifier="read").first()
models.ShelfBook.objects.create(
book=self.book,
user=self.local_user,
shelf=shelf,
shelved_date=make_date(2020, 1, 1),
)
view = views.AnnualSummary.as_view()
request = self.factory.get("")
request.user = self.local_user
result = view(request, self.local_user.localname, self.year)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
validate_html(result.render()) validate_html(result.render())

View file

@ -178,7 +178,7 @@ def privacy_verification(request, user, year, year_key):
def is_year_available(user, year): def is_year_available(user, year):
"""return boolean""" """return boolean"""
earliest_year = get_earliest_year(user) earliest_year = int(get_earliest_year(user, year))
today = date.today() today = date.today()
year = int(year) year = int(year)
if earliest_year <= year < today.year: if earliest_year <= year < today.year:
@ -189,7 +189,7 @@ def is_year_available(user, year):
return False return False
def get_earliest_year(user): def get_earliest_year(user, year):
"""return the earliest finish_date or shelved_date year for user books in read shelf""" """return the earliest finish_date or shelved_date year for user books in read shelf"""
read_shelfbooks = models.ShelfBook.objects.filter(user__id=user.id).filter( read_shelfbooks = models.ShelfBook.objects.filter(user__id=user.id).filter(
@ -216,7 +216,10 @@ def get_earliest_year(user):
else: else:
book_dates.append(book["shelved_date"]) book_dates.append(book["shelved_date"])
return min(book_dates).year if book_dates:
return min(book_dates).year
return year
def get_read_book_ids_in_year(user, year): def get_read_book_ids_in_year(user, year):