|
| 1 | +""" |
| 2 | +Tests for the community.views.HomePageView class |
| 3 | +
|
| 4 | +""" |
| 5 | +from django.test import TestCase |
| 6 | + |
| 7 | +from community.views import HomePageView, RECENT_ITEMS_LEN |
| 8 | +from events.tests.factories import EventFactory |
| 9 | +from jobs.tests.factories import JobFactory |
| 10 | +from news.tests.factories import NewsArticleFactory |
| 11 | + |
| 12 | + |
| 13 | +class GetContextDataRecentKeyTests(TestCase): |
| 14 | + def setUp(self): |
| 15 | + # shortcut |
| 16 | + self.get_context_data = HomePageView().get_context_data |
| 17 | + |
| 18 | + def test_a_recent_key_is_added_to_the_return_value(self): |
| 19 | + self.assertIn('recent', self.get_context_data()) |
| 20 | + |
| 21 | + def test_events_are_included_in_recent(self): |
| 22 | + event = EventFactory() |
| 23 | + self.assertEqual([event], self.get_context_data()['recent']) |
| 24 | + |
| 25 | + def test_included_events_have_correct_fields(self): |
| 26 | + # Correct fields include 'category', 'created', 'title' and 'description' |
| 27 | + EventFactory() |
| 28 | + event = self.get_context_data()['recent'][0] |
| 29 | + self.assertEqual(event.category, 'Eventos') |
| 30 | + self.assertEqual(event.title, event.name) |
| 31 | + self.assertEqual(event.created, event.start_at) |
| 32 | + # Events already have a description field |
| 33 | + |
| 34 | + def test_jobs_are_included_in_recent(self): |
| 35 | + job = JobFactory() |
| 36 | + self.assertEqual([job], self.get_context_data()['recent']) |
| 37 | + |
| 38 | + def test_included_jobs_have_correct_fields(self): |
| 39 | + JobFactory() |
| 40 | + job = self.get_context_data()['recent'][0] |
| 41 | + self.assertEqual(job.category, 'Trabajos') |
| 42 | + # jobs already have 'created', 'title' and 'description' fields |
| 43 | + |
| 44 | + def test_news_are_included_in_recent(self): |
| 45 | + article = NewsArticleFactory() |
| 46 | + self.assertEqual([article], self.get_context_data()['recent']) |
| 47 | + |
| 48 | + def test_included_news_have_correct_fields(self): |
| 49 | + # Correct fields include 'category', 'created', 'title' and 'description' |
| 50 | + NewsArticleFactory() |
| 51 | + article = self.get_context_data()['recent'][0] |
| 52 | + self.assertEqual(article.category, 'Noticias') |
| 53 | + self.assertEqual(article.description, article.body) |
| 54 | + # NewsArticle already have a created and title fields |
| 55 | + |
| 56 | + # Independent of the Model type, all list items are sorted by their 'created' field |
| 57 | + def test_items_are_sorted_by_the_created_field(self): |
| 58 | + job = JobFactory(set_created='1985-10-26 09:00Z') # Middle-age ;-) |
| 59 | + event = EventFactory(start_at='1955-11-12 06:38Z') # Oldest |
| 60 | + article = NewsArticleFactory(set_created='2015-10-21 09:00Z') # Most recent |
| 61 | + # Assert the models are in chronological order |
| 62 | + self.assertListEqual([article, job, event], self.get_context_data()['recent']) |
| 63 | + |
| 64 | + def test_recent_is_a_list_with_at_most_10_items(self): |
| 65 | + # Create more than RECENT_ITEMS_LEN models and assert that only 10 are kept as recent |
| 66 | + for i in range(RECENT_ITEMS_LEN): |
| 67 | + JobFactory() |
| 68 | + EventFactory() |
| 69 | + NewsArticleFactory() |
| 70 | + # The loop above creates RECENT_ITEMS_LEN * 3 items |
| 71 | + self.assertEqual(len(self.get_context_data()['recent']), RECENT_ITEMS_LEN) |
0 commit comments