def test_filter_active(self): eight_months_ago = timezone.now() - timedelta(days=8 * 365 / 12) two_years_ago = timezone.now() - timedelta(days=365 * 2) # create company with a recent production active_production_company = ProductionCompanyFactory() ProductionFactory(production_company=active_production_company, start_date=eight_months_ago) # create company with a recent audition active_audition_company = ProductionCompanyFactory() AuditionFactory(production_company=active_audition_company, start_date=eight_months_ago) # create company with an old production inactive_production_company = ProductionCompanyFactory() ProductionFactory(production_company=inactive_production_company, start_date=two_years_ago) # create_company with an old audition inactive_audition_company = ProductionCompanyFactory() AuditionFactory(production_company=inactive_audition_company, start_date=two_years_ago) # create completely inactive company inactive_company = ProductionCompanyFactory() active_companies = ProductionCompany.objects.filter_active() self.assertIn(active_production_company, active_companies) self.assertIn(active_audition_company, active_companies) self.assertNotIn(inactive_production_company, active_companies) self.assertNotIn(inactive_audition_company, active_companies) self.assertNotIn(inactive_company, active_companies)
def test_get_slug(self): audition = AuditionFactory( title='Test Audition Title', start_date=datetime(2017, 1, 3), ) self.assertEqual( audition.get_slug(), u'20170103-test-audition-title', )
def test_filter_upcoming(self): today = timezone.now() yesterday = today - timedelta(days=1) tomorrow = today + timedelta(days=1) past_audition = AuditionFactory(start_date=yesterday) ongoing_audition = AuditionFactory(start_date=yesterday, end_date=tomorrow) future_audition = AuditionFactory(start_date=tomorrow) upcoming = Audition.objects.filter_upcoming() self.assertNotIn(past_audition, upcoming) self.assertIn(ongoing_audition, upcoming) self.assertIn(future_audition, upcoming)
def test_get_queryset(self): company = ProductionCompanyFactory() company_audition = AuditionFactory(production_company=company) other_audition = AuditionFactory() view = CompanyPastAuditionListView() view.company = company with patch.object( PastAuditionListView, 'get_queryset', return_value=Audition.objects.all() ): queryset = view.get_queryset() self.assertIn(company_audition, queryset) self.assertNotIn(other_audition, queryset)
def test_get_context_data(self): request = HttpRequest() request.GET = {'page': 2} view = PastAuditionListView(request=request) view.object_list = [AuditionFactory()] with patch.object(Paginator, 'page', return_value='page') as mock_page: context = view.get_context_data() mock_page.assert_called_once_with(2) self.assertEqual(context['page'], 'page') with patch.object( Paginator, 'page', side_effect=PageNotAnInteger() ) as mock_page: try: view.get_context_data() except PageNotAnInteger: pass mock_page.has_call(1) with patch.object(Paginator, 'page', side_effect=EmptyPage()) as mock: try: view.get_context_data() except EmptyPage: pass mock.has_call(1)
def test_get_context_data(self): production = ProductionFactory(poster=FileObject('poster')) no_poster_production = ProductionFactory() review = ReviewFactory( is_published=True, cover_image=FileObject('image') ) unpublished_review = ReviewFactory( is_published=False, cover_image=FileObject('x') ) no_image_review = ReviewFactory(is_published=True) context = self.view.get_context_data() self.assertIn(production, context['productions']) self.assertNotIn(no_poster_production, context['productions']) self.assertIn(review, context['reviews']) self.assertNotIn(unpublished_review, context['reviews']) self.assertNotIn(no_image_review, context['reviews']) self.assertIsNone(context['audition_groups']) self.assertIsNone(context['media_news']) self.assertIsNone(context['news_groups']) audition = AuditionFactory() media_news = ArtsNewsFactory(video_embed='<iframe />') news = ArtsNewsFactory() context = self.view.get_context_data() self.assertIn([audition], context['audition_groups']) self.assertEqual(context['media_news'], media_news) self.assertIn([news], context['news_groups'])
def test_get_queryset(self): company = ProductionCompanyFactory() audition_1 = AuditionFactory( production_company=company, start_date=timezone.now() + timedelta(days=1) ) audition_2 = AuditionFactory( production_company=company, start_date=timezone.now() + timedelta(days=2) ) view = CompanyAuditionListView() view.company = company queryset = view.get_queryset() self.assertEqual(queryset[0], audition_1) self.assertEqual(queryset[1], audition_2)
def test_get_queryset(self): future_audition = AuditionFactory( start_date=timezone.now() + timedelta(days=1) ) audition_1 = AuditionFactory( start_date=timezone.now() - timedelta(days=1), play=PlayFactory(title='1') ) audition_2 = AuditionFactory( start_date=timezone.now() - timedelta(days=2), play=PlayFactory(title='2') ) view = PastAuditionListView() past_auditions = view.get_queryset() self.assertNotIn(future_audition, past_auditions) self.assertEqual(past_auditions[0], audition_1) self.assertEqual(past_auditions[1], audition_2)
def test_get_context_data(self): news = ArtsNewsFactory() audition = AuditionFactory() upcoming_audition = AuditionFactory( start_date=timezone.now() + timedelta(days=1) ) view = AuditionDetailView() view.object = audition with patch.object( AuditionDetailView, 'get_object', return_value=audition ): context = view.get_context_data() self.assertIn(upcoming_audition, context['upcoming_auditions']) self.assertIn(news, context['recent_news']) self.assertEqual(context['company_productions'], [])
def test_item_categories(self): prod = ProductionFactory() self.assertEqual(self.feed.item_categories(prod), ['Productions']) review = ReviewFactory() self.assertEqual(self.feed.item_categories(review), ['Reviews']) audition = AuditionFactory() self.assertEqual(self.feed.item_categories(audition), ['Auditions']) news = ArtsNewsFactory() self.assertEqual(self.feed.item_categories(news), ['News']) reviewer = ReviewerFactory() self.assertEqual(self.feed.item_categories(reviewer), [])
def test_save(self): audition = AuditionFactory(pk=None, title=None, slug=None) with patch('django.db.models.Model.save') as mock_save: audition.save() mock_save.assert_called_once_with() self.assertEqual(audition.title, audition.get_title()) self.assertEqual(audition.slug, audition.get_slug())
def test_items(self): now = timezone.now() one_day_ago = now - timedelta(days=1) news = ArtsNewsFactory() audition = AuditionFactory() production = ProductionFactory() review = ReviewFactory( is_published=True, published_on=one_day_ago, production=production, ) self.assertEqual( self.feed.items(), [production, audition, news, review] )
def test_get_absolute_url(self): audition = AuditionFactory() self.assertIsInstance(audition.get_absolute_url(), unicode)
def test_get_title(self): audition = AuditionFactory() self.assertEqual(audition.get_title(), u'Auditions') audition = AuditionFactory(title='Test Title') self.assertEqual(audition.get_title(), u'Test Title') audition = AuditionFactory(play=PlayFactory()) self.assertEqual(audition.get_title(), u'Auditions for {}'.format(str(audition.play))) audition = AuditionFactory( production_company=ProductionCompanyFactory()) self.assertEqual( audition.get_title(), u'Auditions for {}'.format(str(audition.production_company))) audition = AuditionFactory( play=PlayFactory(), production_company=ProductionCompanyFactory(), ) self.assertEqual( audition.get_title(), u'Auditions for {play}, by {company}'.format( play=str(audition.play), company=str(audition.production_company), ))
def test_duration(self): audition = AuditionFactory() self.assertEqual(audition.duration(), audition.start_date.strftime('%b. %d')) audition = AuditionFactory() self.assertEqual(audition.duration(date_format='%y'), audition.start_date.strftime('%y')) audition = AuditionFactory(end_date=timezone.now() + timedelta(days=1)) self.assertEqual( audition.duration(), '{} - {}'.format( audition.start_date.strftime('%b. %d'), audition.end_date.strftime('%b. %d'), )) audition = AuditionFactory( start_date=timezone.now() - timedelta(days=365), end_date=timezone.now() - timedelta(days=364)) self.assertEqual( audition.duration(), '{} - {} ({})'.format(audition.start_date.strftime('%b. %d'), audition.end_date.strftime('%b. %d'), audition.start_date.year)) audition = AuditionFactory(start_date=timezone.now() - timedelta(days=365)) self.assertEqual(audition.duration(date_format='%Y'), str(audition.start_date.year))
def test_get_alt_description(self): audition = AuditionFactory() self.assertEqual( audition.get_alt_description(), 'Auditions on {}.'.format(audition.start_date.strftime('%b %d'))) audition = AuditionFactory(play=PlayFactory()) self.assertEqual( audition.get_alt_description(), 'Auditions for a role in {} on {}.'.format( str(audition.play), audition.start_date.strftime('%b %d'), )) audition = AuditionFactory( production_company=ProductionCompanyFactory()) self.assertEqual( audition.get_alt_description(), 'Auditions with {} on {}.'.format( str(audition.production_company), audition.start_date.strftime('%b %d'), )) audition = AuditionFactory(end_date=timezone.now() + timedelta(days=1)) self.assertEqual( audition.get_alt_description(), 'Auditions on {} through {}.'.format( audition.start_date.strftime('%b %d'), audition.end_date.strftime('%b %d'), )) audition = AuditionFactory( play=PlayFactory(), production_company=ProductionCompanyFactory(), end_date=timezone.now() + timedelta(days=1)) self.assertEqual( audition.get_alt_description(), 'Auditions for a role in {} with {} on {} through {}.'.format( str(audition.play), str(audition.production_company), audition.start_date.strftime('%b %d'), audition.end_date.strftime('%b %d'), ))
def test_unicode(self): audition = AuditionFactory() self.assertEqual(audition.__unicode__(), unicode(audition.get_title()))