def test_context_kind_and_counts(self): "The kind and counts for publications, books and periodicals should be correct." ReadingFactory(publication=PublicationFactory(title='Old Book', kind='book'), start_date=make_date('2016-06-15'), end_date=make_date('2016-07-15')) ReadingFactory(publication=PublicationFactory(title='2017 Book 2', kind='book'), start_date=make_date('2017-01-01'), end_date=make_date('2017-01-20')) ReadingFactory(publication=PublicationFactory(title='2017 Periodical', kind='periodical'), start_date=make_date('2017-02-01'), end_date=make_date('2017-02-20')) response = views.ReadingYearArchiveView.as_view()(self.request, year='2017', kind='books') data = response.context_data self.assertIn('publication_kind', data) self.assertEqual(data['publication_kind'], 'book') self.assertIn('publication_count', data) self.assertEqual(data['publication_count'], 3) self.assertIn('periodical_count', data) self.assertEqual(data['periodical_count'], 1) self.assertIn('book_count', data) self.assertEqual(data['book_count'], 2)
def test_queryset_book(self): "It should only include books in the publication_list" book = PublicationFactory(kind='book') periodical = PublicationFactory(kind='periodical') response = views.PublicationListView.as_view()(self.request) self.assertEqual(len(response.context_data['publication_list']), 1) self.assertEqual(response.context_data['publication_list'][0], book)
def test_last_page(self): "PaginatedListView should return last page with ?p=last" # Two pages' worth: PublicationFactory.create_batch(51) request = self.factory.get('/fake-path/?p=last') response = views.PublicationListView.as_view()(request) # Has the final one publication on the second page: self.assertEqual(len(response.context_data['publication_list']), 1)
def test_ordering_books(self): "Should be ordered by title_sort." book2 = PublicationFactory(kind='book', title='Book 2') book1 = PublicationFactory(kind='book', title='Book 1') response = views.PublicationListView.as_view()(self.request) pubs = response.context_data['publication_list'] self.assertEqual(pubs[0], book1) self.assertEqual(pubs[1], book2)
def test_context_counts(self): "It should include the book and periodical counts." PublicationFactory.create_batch(2, kind='book') PublicationFactory.create_batch(3, kind='periodical') response = views.PublicationListView.as_view()(self.request) self.assertIn('book_count', response.context_data) self.assertEqual(response.context_data['book_count'], 2) self.assertIn('periodical_count', response.context_data) self.assertEqual(response.context_data['periodical_count'], 3)
def test_ended_readings(self): "It returns publications whose ended readings were around the date" pub1 = PublicationFactory() pub2 = PublicationFactory() # pub1 started before and ended after the date: ReadingFactory(publication=pub1, start_date=make_date('2017-02-10'), end_date=make_date('2017-02-20')) # pub2 was over before the date: ReadingFactory(publication=pub2, start_date=make_date('2017-01-01'), end_date=make_date('2017-01-10')) qs = day_publications(make_date('2017-02-15')) self.assertEqual(len(qs), 1) self.assertEqual(qs[0], pub1)
def test_in_progress_readings(self): "It returns publications that were still in progress on the date." pub1 = PublicationFactory() pub2 = PublicationFactory() # pub1 started before the date: ReadingFactory(publication=pub1, start_date=make_date('2017-02-10'), end_date=None) # pub2 started after the date: ReadingFactory(publication=pub2, start_date=make_date('2017-03-01'), end_date=None) qs = day_publications(make_date('2017-02-15')) self.assertEqual(len(qs), 1) self.assertEqual(qs[0], pub1)
def test_context_reading_list(self): "Should include Readings ending in chosen year, earliest end_date first." ReadingFactory(publication=PublicationFactory(title='Old Pub'), start_date=make_date('2016-06-15'), end_date=make_date('2016-07-15')) ReadingFactory(publication=PublicationFactory(title='2017 Pub 2'), start_date=make_date('2017-01-01'), end_date=make_date('2017-01-20')) response = views.ReadingYearArchiveView.as_view()(self.request, year='2017') context = response.context_data self.assertIn('reading_list', context) self.assertEqual(len(context['reading_list']), 2) self.assertEqual(context['reading_list'][0].publication.title, '2017 Pub 2') self.assertEqual(context['reading_list'][1].publication.title, '2017 Pub 1')
def test_ordering_periodicals(self): "Should be ordered by series' title_sort, then publication's title_sort." series_a = PublicationSeriesFactory(title='Series A') series_b = PublicationSeriesFactory(title='Series B') pub_b1 = PublicationFactory(kind='periodical', series=series_b, title='Book 1') pub_a2 = PublicationFactory(kind='periodical', series=series_a, title='Book 2') pub_a1 = PublicationFactory(kind='periodical', series=series_a, title='Book 1') response = views.PublicationListView.as_view()(self.request, kind='periodical') pubs = response.context_data['publication_list'] self.assertEqual(pubs[0], pub_a1) self.assertEqual(pubs[1], pub_a2) self.assertEqual(pubs[2], pub_b1)
def test_two_readings(self): "Shouldn't return a pub if it it had a reading before, and a reading after, the chosen date." pub = PublicationFactory() r1 = ReadingFactory(publication=pub, start_date=make_date('2017-01-01'), end_date=make_date('2017-01-31')) r2 = ReadingFactory(publication=pub, start_date=make_date('2017-03-01'), end_date=make_date('2017-03-31')) qs = day_publications(make_date('2017-02-15')) self.assertEqual(len(qs), 0)
def test_unique_publications(self): "Should only list each publication once." pub = PublicationFactory() r1 = ReadingFactory(publication=pub, start_date=make_date('2017-01-01'), end_date=make_date('2017-01-31')) r2 = ReadingFactory(publication=pub, start_date=make_date('2017-01-31'), end_date=make_date('2017-02-15')) qs = day_publications(make_date('2017-01-31')) self.assertEqual(len(qs), 1)
def test_context_next_prev_years(self): "Context should include date objects representing next/prev years, if any." ReadingFactory(publication=PublicationFactory(title='Old Pub'), start_date=make_date('2016-06-15'), end_date=make_date('2016-07-15')) response = views.ReadingYearArchiveView.as_view()(self.request, year='2017') self.assertIn('previous_year', response.context_data) self.assertIn('next_year', response.context_data) self.assertEqual(response.context_data['previous_year'], make_date('2016-01-01'))
def test_show_creators_with_roles(self): "When a Publication has roles, display them." pub = PublicationFactory() PublicationRoleFactory(publication=pub, creator=IndividualCreatorFactory(name='Bob'), role_order=1) PublicationRoleFactory(publication=pub, creator=IndividualCreatorFactory(name='Terry'), role_order=2) ba = PublicationAdmin(Publication, self.site) self.assertEqual(ba.show_creators(pub), 'Bob, Terry')
def test_returns_queryset(self): "It should return 10 items by default." d = make_date('2017-02-15') for i in range(11): c = IndividualCreatorFactory() pub = PublicationFactory() PublicationRoleFactory(publication=pub, creator=c, role_name='') ReadingFactory(publication=pub, start_date=d, end_date=d) creators = most_read_creators() self.assertEqual(len(creators), 10)
def test_num(self): "It should return `num` items." d = make_date('2017-02-15') for i in range(4): c = IndividualCreatorFactory() pub = PublicationFactory() PublicationRoleFactory(publication=pub, creator=c, role_name='') ReadingFactory(publication=pub, start_date=d, end_date=d) creators = most_read_creators(num=3) self.assertEqual(len(creators), 3)
def setUp(self): # Books only in 2015: ReadingFactory.create_batch( 2, publication=PublicationFactory(kind='book'), end_date=make_date('2015-01-01')) # Nothing in 2016. # Books and periodicals in 2017: ReadingFactory.create_batch( 3, publication=PublicationFactory(kind='book'), end_date=make_date('2017-09-01')) ReadingFactory.create_batch( 2, publication=PublicationFactory(kind='periodical'), end_date=make_date('2017-09-01')) # Periodicals only in 2018: ReadingFactory.create_batch( 2, publication=PublicationFactory(kind='periodical'), end_date=make_date('2018-01-01'))
def test_num(self): "It should return `num` items." d = make_date('2017-02-15') for i in range(2, 6): c = IndividualCreatorFactory() pub = PublicationFactory() PublicationRoleFactory(publication=pub, creator=c, role_name='') # It'll cut off any with only 1 reading, so: ReadingFactory.create_batch(i, publication=pub, start_date=d, end_date=d) data = most_read_creators_card(num=3) self.assertIn('object_list', data) self.assertEqual(len(data['object_list']), 3)
def test_returns_correct_data(self): d = make_date('2017-02-15') for i in range(2, 13): c = IndividualCreatorFactory() pub = PublicationFactory() PublicationRoleFactory(publication=pub, creator=c, role_name='') # It'll cut off any with only 1 reading, so: ReadingFactory.create_batch(i, publication=pub, start_date=d, end_date=d) data = most_read_creators_card() self.assertIn('card_title', data) self.assertIn('score_attr', data) self.assertIn('object_list', data) self.assertEqual(data['card_title'], 'Most read authors') self.assertEqual(data['score_attr'], 'num_readings') self.assertEqual(len(data['object_list']), 10)
def test_show_creators_no_roles(self): "When a Publication has no roles, display '-'." pub = PublicationFactory() ba = PublicationAdmin(Publication, self.site) self.assertEqual(ba.show_creators(pub), '-')
def setUp(self): super().setUp() series = PublicationSeriesFactory(pk=123) PublicationFactory.create_batch(2, series=series)
def setUp(self): super().setUp() ReadingFactory(publication=PublicationFactory(title='2017 Pub 1', kind='book'), start_date=make_date('2016-12-15'), end_date=make_date('2017-01-31'))
def test_publication_detail_view(self): "Should use the correct view." PublicationFactory(title='My Book') self.assertEqual( resolve('/reading/publications/my-book/').func.__name__, views.PublicationDetailView.__name__)
def setUp(self): super().setUp() PublicationFactory(pk=123)
def test_publication_detail_url(self): PublicationFactory(title='My Book') self.assertEqual( reverse('spectator:reading:publication_detail', kwargs={'slug': 'my-book'}), '/reading/publications/my-book/')