def test_deve_retornar_apenas_agendas_com_horarios_disponiveis(self): """ Deve retornar apenas agendas que possuam horários disponíveis, desconsiderando horários marcados e passados. """ dia = date(2020, 11, 15) agenda1 = baker.make(Agenda, dia=dia) baker.make('core.AgendaHora', agenda=agenda1, hora='9:00') agenda2 = baker.make(Agenda, dia=dia) baker.make('core.AgendaHora', agenda=agenda2, hora='11:00', disponivel=False) agenda3 = baker.make(Agenda, dia=dia) baker.make('core.AgendaHora', agenda=agenda3, hora='9:00') baker.make('core.AgendaHora', agenda=agenda3, hora='11:00') baker.make('core.AgendaHora', agenda=agenda3, hora='11:30', disponivel=False) with mock.patch( 'agenda.core.managers.timezone.now', return_value=datetime(2020, 11, 15, 10) # 15/11/2020 às 10h ): qs = Agenda.disponiveis.all() assertQuerysetEqual(qs, [agenda3.pk], lambda a: a.pk)
def test_trackedmodel_can_attach_record_codes(workbasket): tx = workbasket.new_transaction() with tx: # Note: regulation.Regulation implicitly creates a regulation.Group as well! factories.RegulationFactory.create() factories.FootnoteTypeFactory.create() tracked_models = ( TrackedModel.objects.annotate_record_codes() .select_related() .filter(transaction=tx) ) expected_models = [ (tx.pk, Group, "150", "00"), (tx.pk, Regulation, "285", "00"), (tx.pk, FootnoteType, "100", "00"), ] assertQuerysetEqual( tracked_models, expected_models, transform=lambda o: ( o.transaction.pk, o.__class__, o.record_code, o.subrecord_code, ), ordered=False, )
def test_future_question_and_past_question(self, client): """Even if both past and future questions exist, only past questions are displayed.""" create_question(question_text='Past question.', days=-30) create_question(question_text='Future question.', days=30) response = client.get(reverse('polls:index')) assertQuerysetEqual(response.context['latest_question_list'], ['<Question: Past question.>'])
def test_two_past_questions(self, client): """The questions index page may display multiple questions.""" create_question(question_text='Past question 1.', days=-30) create_question(question_text='Past question 2.', days=-5) response = client.get(reverse('polls:index')) assertQuerysetEqual( response.context['latest_question_list'], ['<Question: Past question 2.>', '<Question: Past question 1.>'])
def test_available_manager(self): ProductFactory(is_available=True) ProductFactory(is_available=False) assert Product.objects.all().count() == 2 assert Product.available.all().count() == 1 assertQuerysetEqual( Product.available.all(), Product.objects.filter(is_available=True), transform=lambda x: x, )
def test_nao_deve_retornar_agendas_de_datas_passadas(self): agenda1 = baker.make(Agenda, dia=date(2020, 11, 14)) baker.make('core.AgendaHora', agenda=agenda1) agenda2 = baker.make(Agenda, dia=date(2020, 11, 16)) baker.make('core.AgendaHora', agenda=agenda2) with mock.patch('agenda.core.managers.timezone.now', return_value=datetime(2020, 11, 15)): qs = Agenda.disponiveis.all() assertQuerysetEqual(qs, [agenda2.dia], lambda a: a.dia)
def test_get_current_officers_council(chapter, user_factory): result = chapter.get_current_officers_council(combine=False) assert result[1] is True assert result[0].count() == 0 regent = user_factory.create(chapter=chapter, make_officer="regent") vice = user_factory.create(chapter=chapter, make_officer="vice regent") treasurer = user_factory.create(chapter=chapter, make_officer="treasurer") scribe = user_factory.create(chapter=chapter, make_officer="scribe") corsec = user_factory.create(chapter=chapter, make_officer="corresponding secretary") officer_pks = [regent.pk, vice.pk, treasurer.pk, scribe.pk, corsec.pk] result = chapter.get_current_officers_council(combine=False) assert result[1] is False assertQuerysetEqual(result[0], officer_pks, lambda o: o.pk, ordered=False) result = chapter.get_current_officers_council_specific() assert [regent, scribe, vice, treasurer, corsec] == result
def test_get_current_officers_council_previous(chapter, user_factory): result = chapter.get_current_officers_council(combine=False) assert result[1] is True assert result[0].count() == 0 regent = user_factory.create(chapter=chapter, make_officer="regent", make_officer__current=False) vice = user_factory.create(chapter=chapter, make_officer="vice regent", make_officer__current=False) old_officer_pks = [regent.pk, vice.pk] result = chapter.get_current_officers_council(combine=False) assert result[1] is True assertQuerysetEqual(result[0], old_officer_pks, lambda o: o.pk, ordered=False)
def test_envelope_serializer_outputs_expected_items(approved_workbasket): """EnvelopeSerializer should output all the models passed to it and generated records for descriptions.""" # Transaction context manager is not used to create transactions, # as it creates phantom workbaskets and transactions which cause XSD failures later. tx = ApprovedTransactionFactory.create(workbasket=approved_workbasket) regulation = factories.RegulationFactory.create( transaction=tx, regulation_group=None, ) footnote = factories.FootnoteFactory.create( transaction=tx, ) # FootnoteFactory also creates a FootnoteType and FootnoteDescription expected_items = [ regulation, footnote.footnote_type, footnote, *footnote.descriptions.all(), ] # < Note: These are sorted by record_code, subrecord_code expected_item_record_codes = { (o.record_code, o.subrecord_code) for o in expected_items } assertQuerysetEqual( expected_items, approved_workbasket.tracked_models.all(), transform=lambda o: o, ordered=False, ) # Some of the factories create other data so sanity check expected items. output = io.BytesIO() with EnvelopeSerializer(output, random.randint(2, 9999)) as env: env.render_transaction( models=approved_workbasket.tracked_models.all(), transaction_id=tx.order, ) output_xml = etree.XML(output.getvalue()) output_record_codes = {*taric_xml_record_codes(output_xml)} # More items than models are output, as models for descriptions aren't kept. assert ( output_record_codes >= expected_item_record_codes ), f"Output ({output_record_codes}) missing some expected record codes"
def test_nao_deve_considerar_horarios_passados_como_disponiveis(self): agenda1 = baker.make( Agenda, dia=date(2020, 11, 15), ) baker.make('core.AgendaHora', agenda=agenda1, hora='8:00') baker.make('core.AgendaHora', agenda=agenda1, hora='9:00') agenda2 = baker.make( Agenda, dia=date(2020, 11, 15), ) baker.make('core.AgendaHora', agenda=agenda2, hora='10:30') baker.make('core.AgendaHora', agenda=agenda2, hora='11:00') with mock.patch( 'agenda.core.managers.timezone.now', return_value=datetime(2020, 11, 15, 10) # 15/11/2020 às 10h ): qs = Agenda.disponiveis.all() assertQuerysetEqual(qs, [agenda2.dia], lambda a: a.dia)
def test_chapter_list_filter(chapter_factory): chapters = chapter_factory.create_batch(10) candidate_chapters = chapter_factory.create_batch(10, candidate_chapter=True) from chapters.filters import ChapterListFilter all_chapters = chapters + candidate_chapters chapter_pks = {chapter.pk for chapter in all_chapters} qs = Chapter.objects.all() filter_default = ChapterListFilter(queryset=qs) assertQuerysetEqual(filter_default.qs, chapter_pks, lambda o: o.pk, ordered=False) filter_national = ChapterListFilter({"region": "national"}, queryset=qs) assertQuerysetEqual(filter_national.qs, chapter_pks, lambda o: o.pk, ordered=False) filter_candidate_chapter = ChapterListFilter( {"region": "candidate_chapter"}, queryset=qs) candidate_chapter_pks = { chapter.pk for chapter in candidate_chapters if chapter.candidate_chapter } assertQuerysetEqual( filter_candidate_chapter.qs, candidate_chapter_pks, lambda o: o.pk, ordered=False, ) regions = Region.objects.all() for region in regions: filter_region = ChapterListFilter({"region": region.slug}, queryset=qs) region_pks = { chapter.pk for chapter in all_chapters if chapter.region.slug == region.slug } assertQuerysetEqual(filter_region.qs, region_pks, lambda o: o.pk, ordered=False)
def test_favourites_list(authenticated_client, favourite): url = reverse('feed_items:favourites_list') response = authenticated_client.get(url) assertQuerysetEqual(response.context_data['object_list'], [repr(favourite)])
def assert_queryset(queryset, expected): assertQuerysetEqual(queryset, expected, transform=lambda o: o, ordered=False)
def test_feed_list(authenticated_client, feed): url = reverse('feeds:list') response = authenticated_client.get(url) assertQuerysetEqual(response.context_data['object_list'], [repr(feed)])
def test_future_question(self, client): """Questions with a pub_date in the future aren't displays on the index page.""" create_question(question_text='Future question.', days=30) response = client.get(reverse('polls:index')) assertContains(response, 'No polls are available.') assertQuerysetEqual(response.context['latest_question_list'], [])
def test_past_question(self, client): """Questions with a pub_date in the past are displayed on the index page.""" create_question(question_text='Past question.', days=-30) response = client.get(reverse('polls:index')) assertQuerysetEqual(response.context['latest_question_list'], ['<Question: Past question.>'])
def test_no_questions(self, client): """If no questions exist, an appropriate message is displayed""" response = client.get(reverse('polls:index')) assert response.status_code == 200 assertContains(response, 'No polls are available.') assertQuerysetEqual(response.context['latest_question_list'], [])