Skip to content

Commit

Permalink
test: update date related cases
Browse files Browse the repository at this point in the history
  • Loading branch information
niccofyren committed Dec 2, 2023
1 parent 2e83888 commit bf43190
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
11 changes: 5 additions & 6 deletions aktuelt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,12 @@ def get_main_image(self):
]

def schedule(self):
updated_at = self.custom_updated_at or self.last_published_at
published_at = self.custom_published_at or self.first_published_at

return {
"updated_at": self.custom_updated_at.isoformat()
if self.custom_updated_at
else self.last_published_at.isoformat(),
"published_at": self.custom_published_at.isoformat()
if self.custom_published_at
else self.first_published_at.isoformat(),
"updated_at": updated_at.isoformat() if updated_at else None,
"published_at": published_at.isoformat() if published_at else None,
"unpublished_at": self.expire_at.isoformat() if self.expire_at else None,
}

Expand Down
60 changes: 58 additions & 2 deletions aktuelt/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ def setUp(self):
title="Test news page",
intro="Test intro",
body="Test body",
date="2020-01-01",
)
newsIndexPage.add_child(instance=newsPage)

NewsPage(
title="Secret news page",
intro="This page isnt part of page tree",
body="Test body",
date="2020-01-01",
# Fake values, probably not possible using Wagtail page model.
# A nice way of demoing behaviour and difference between API
# endpoints and the "default" html endpoints, via tests below
Expand Down Expand Up @@ -65,10 +63,68 @@ def test_secret_news_page_html_is_not_readable_via_page_url(self):
response = self.client.get(newsPage.get_url() or "/invalid-url/")
self.assertEqual(response.status_code, 404)

def test_news_page_api_details_contains_schedule(self):
newsPage = NewsPage.objects.get(title="Test news page")
response = self.client.get(f"/api/v2/news/{newsPage.pk}/")
self.assertIsNotNone(response.json().get("meta").get("schedule"))


class AktueltNewsPageStructure(WagtailPageTestCase):
def test_news_page_can_only_be_created_under_news_index_page(self):
self.assertAllowedParentPageTypes(NewsPage, {NewsIndexPage})

def test_news_page_index_can_only_hace_news_pages_as_children(self):
self.assertAllowedSubpageTypes(NewsIndexPage, {NewsPage})


class NewsPageBehaviour(WagtailPageTestCase):
def setUp(self):
self.newsPage = NewsPage(
title="Test news page",
intro="Test intro",
body="Test body",
path="/",
depth=1,
)
self.newsPage.save()

def test_schedule_is_empty_by_default(self):
self.assertEqual(
self.newsPage.schedule(),
{
"updated_at": None,
"published_at": None,
"unpublished_at": None,
},
)

def test_schedule_reflects_publish_history(self):
self.newsPage.save_revision().publish()
self.newsPage.save_revision().publish()

self.newsPage.refresh_from_db()
self.assertEqual(
self.newsPage.schedule(),
{
"updated_at": self.newsPage.last_published_at.isoformat(),
"published_at": self.newsPage.first_published_at.isoformat(),
"unpublished_at": None,
},
)

def test_schedule_reflects_manual_overrides(self):
test_date = "2021-01-01T00:00:00+00:00"

self.newsPage.custom_published_at = test_date
self.newsPage.custom_updated_at = test_date
self.newsPage.save_revision().publish()

self.newsPage.refresh_from_db()
self.assertEqual(
self.newsPage.schedule(),
{
"updated_at": test_date,
"published_at": test_date,
"unpublished_at": None,
},
)

0 comments on commit bf43190

Please sign in to comment.