def test_save_gap_previous( self, usable_from, renewing_tier_previous, renewing_tier_new, effective_from, expected_until, ): participant = factories.ParticipantFactory() factories.MembershipFactory( participant=participant, tier=factories.TierFactory(needs_renewal=renewing_tier_previous, usable_from=usable_from), effective_from=date(2019, 1, 1), effective_until=date(2019, 12, 31), ) membership = models.Membership.objects.create( participant=participant, tier=factories.TierFactory(needs_renewal=renewing_tier_new, usable_from=usable_from), effective_from=effective_from, form_filled=effective_from, ) assert membership.effective_until == expected_until assert membership.group_first_membership is None
def test_save_existing(self, usable_from, renewing_tier_previous, renewing_tier_new): participant = factories.ParticipantFactory() previous_membership = factories.MembershipFactory( participant=participant, tier=factories.TierFactory(needs_renewal=renewing_tier_previous, usable_from=usable_from - timedelta(days=30)), effective_from=date(2019, 1, 1), ) with pytest.raises(ValidationError) as exc: models.Membership.objects.create( participant=participant, tier=factories.TierFactory(needs_renewal=renewing_tier_new, usable_from=usable_from), effective_from=date(2019, 11, 1), form_filled=date(2019, 11, 1), ) err = exc.value assert isinstance(err, ValidationError) assert isinstance(err.error_dict.get('effective_from'), list) assert err.message_dict['effective_from'][0] == ( 'Cannot create a new membership until the previous one ' f'({previous_membership}) has been closed')
def test_save_new(self, usable_from, renewing_tier, expected_until): membership = models.Membership.objects.create( participant=factories.ParticipantFactory(), tier=factories.TierFactory(needs_renewal=renewing_tier, usable_from=usable_from), effective_from=date(2019, 11, 1), form_filled=date(2019, 11, 1), ) assert membership.effective_until == expected_until assert membership.group_first_membership is None
def test_save_link_previous(self, usable_from, renewing_tier_previous, renewing_tier_new, can_vote, expected_until): participant = factories.ParticipantFactory() previous_membership = factories.MembershipFactory( participant=participant, tier=factories.TierFactory(needs_renewal=renewing_tier_previous, can_vote=can_vote, usable_from=usable_from), effective_from=date(2019, 1, 1), effective_until=date(2019, 12, 31), ) membership = models.Membership.objects.create( participant=participant, tier=factories.TierFactory(needs_renewal=renewing_tier_new, can_vote=can_vote, usable_from=usable_from), effective_from=date(2020, 1, 1), form_filled=date(2019, 11, 1), ) assert membership.effective_until == expected_until assert membership.group_first_membership == previous_membership
def test_eligible_for_vote_filter_full_pass(vote_date, is_id_expected): factories.GeneralSetupFactory( valid_from=date(2015, 1, 1), time_to_vote_since_membership=3, time_unit_to_vote_since_membership=TimeUnit.MONTHS.value, minimum_age_to_vote=18, renewal_month=1, ) participant = factories.ParticipantFactory(date_of_birth=date(1990, 12, 1)) tier = factories.TierFactory(needs_renewal=True, usable_from=date(2015, 1, 1)) membership = factories.MembershipFactory( participant=participant, tier=tier, effective_from=date(2019, 11, 8), form_filled=date(2019, 11, 8), ) assert membership.effective_until == date(2019, 12, 31) membership = factories.MembershipFactory( participant=participant, tier=tier, effective_from=date(2020, 1, 1), form_filled=date(2020, 1, 1), ) assert membership.effective_until == date(2020, 12, 31) vote_filter = EligibleForVoteParticipantFilter( request=None, params={ EligibleForVoteParticipantFilter.parameter_name: vote_date.strftime('%d/%m/%Y') }, model=None, model_admin=None, ) vote_filter_qs = vote_filter.queryset(None, models.Participant.objects.all()) assert (participant in list(vote_filter_qs)) is is_id_expected
def test_correct_grouping(self): usable_from = date(2015, 1, 1) tier_renewal_vote = factories.TierFactory(needs_renewal=True, can_vote=True, usable_from=usable_from) tier_no_renewal_vote = factories.TierFactory(needs_renewal=False, can_vote=True, usable_from=usable_from) tier_renewal_no_vote = factories.TierFactory(needs_renewal=True, can_vote=False, usable_from=usable_from) tier_no_renewal_no_vote = factories.TierFactory( needs_renewal=False, can_vote=False, usable_from=usable_from) participant1 = factories.ParticipantFactory() membership = factories.MembershipFactory( participant=participant1, tier=tier_renewal_vote, effective_from=date(2015, 1, 1), effective_until=date(2015, 12, 31), group_first_membership=None, ) factories.MembershipFactory( participant=participant1, tier=tier_no_renewal_vote, effective_from=date(2016, 1, 1), effective_until=date(2016, 12, 31), group_first_membership=membership, ) factories.MembershipFactory( participant=participant1, tier=tier_no_renewal_no_vote, effective_from=date(2017, 1, 1), effective_until=date(2017, 12, 31), group_first_membership=membership, ) factories.MembershipFactory( participant=participant1, tier=tier_renewal_no_vote, effective_from=date(2018, 1, 1), effective_until=date(2018, 12, 31), group_first_membership=membership, ) factories.MembershipFactory( participant=participant1, tier=tier_renewal_vote, effective_from=date(2019, 1, 1), effective_until=date(2019, 12, 31), group_first_membership=membership, ) participant2 = factories.ParticipantFactory() factories.MembershipFactory( participant=participant2, tier=tier_renewal_vote, effective_from=date(2015, 1, 1), effective_until=date(2015, 12, 31), group_first_membership=None, ) membership = factories.MembershipFactory( participant=participant2, tier=tier_renewal_vote, effective_from=date(2017, 1, 1), effective_until=date(2017, 12, 31), group_first_membership=None, ) factories.MembershipFactory( participant=participant2, tier=tier_renewal_no_vote, effective_from=date(2018, 1, 1), effective_until=date(2018, 12, 31), group_first_membership=membership, ) assert list( models.MembershipPeriod.objects.order_by( 'participant_id', 'effective_from').values()) == [ { 'id': mock.ANY, 'participant_id': participant1.pk, 'effective_from': date(2015, 1, 1), 'effective_until': date(2019, 12, 31), }, { 'id': mock.ANY, 'participant_id': participant2.pk, 'effective_from': date(2015, 1, 1), 'effective_until': date(2015, 12, 31), }, { 'id': mock.ANY, 'participant_id': participant2.pk, 'effective_from': date(2017, 1, 1), 'effective_until': date(2018, 12, 31), }, ]
def test_is_usable_for(self, usable_from, usable_until, expected_result): tier = factories.TierFactory(usable_from=usable_from, usable_until=usable_until) assert tier.is_usable_for(datetime(2019, 11, 1)) is expected_result