def test_get_stats(self): event = EventFactory.create(initiative=self.initiative, status='succeeded') ParticipantFactory.create_batch(3, activity=event, status='succeeded', time_spent=3) assignment = AssignmentFactory.create(initiative=self.initiative, status='succeeded', duration=3) ApplicantFactory.create_batch(3, activity=assignment, status='succeeded', time_spent=3) funding = FundingFactory.create(initiative=self.initiative, status='succeeded') DonationFactory.create_batch(3, activity=funding, status='succeeded', amount=Money(10, 'EUR')) DonationFactory.create_batch(3, activity=funding, status='succeeded', amount=Money(10, 'USD')) response = self.client.get(self.url, user=self.owner) self.assertEqual(response.status_code, status.HTTP_200_OK) stats = response.json()['data']['meta']['stats'] self.assertEqual(stats['hours'], 18) self.assertEqual(stats['activities'], 3) self.assertEqual(stats['contributions'], 12) self.assertEqual(stats['amount'], 75.0)
def setUp(self): super(ImpactTypeListAPITestCase, self).setUp() self.client = JSONAPITestClient() self.url = reverse('statistic-list') self.user = BlueBottleUserFactory() initiative = InitiativeFactory.create() event = EventFactory.create( initiative=initiative, owner=initiative.owner, start=timezone.now() - datetime.timedelta(hours=1), duration=0.1 ) initiative.states.submit(save=True) initiative.states.approve(save=True) event.refresh_from_db() ParticipantFactory.create_batch(5, activity=event) self.impact_type = ImpactTypeFactory.create() self.impact_goal = ImpactGoalFactory.create( type=self.impact_type, target=100, realized=50 ) self.manual = ManualStatisticFactory.create() self.impact = ImpactStatisticFactory(impact_type=self.impact_type) self.database = DatabaseStatisticFactory(query='people_involved')
def test_date_changed(self): event = EventFactory( title='Test Title', status='open', location=GeolocationFactory.create(position=Point(20.0, 10.0)), start=now() + timedelta(days=4), ) ParticipantFactory.create_batch(3, activity=event, status='new') ParticipantFactory.create(activity=event, status='withdrawn') mail.outbox = [] event.start = event.start + timedelta(days=1) event.save() recipients = [message.to[0] for message in mail.outbox] self.assertTrue( event.local_timezone_name in mail.outbox[0].body ) self.assertTrue( formats.time_format(event.local_start) in mail.outbox[0].body ) for participant in event.contributions.instance_of(Participant): if participant.status == 'new': self.assertTrue(participant.user.email in recipients) else: self.assertFalse(participant.user.email in recipients)
def setUp(self): super(StatisticsDateTest, self).setUp() user = BlueBottleUserFactory.create() other_user = BlueBottleUserFactory.create() for diff in (10, 5, 1): initiative = InitiativeFactory.create(owner=user) past_date = timezone.now() - datetime.timedelta(days=diff) initiative.created = past_date initiative.save() initiative.states.submit() initiative.states.approve(save=True) event = EventFactory( start=past_date, initiative=initiative, duration=1, transition_date=past_date, status='succeeded', owner=BlueBottleUserFactory.create(), ) ParticipantFactory.create(activity=event, status='succeeded', time_spent=1, user=other_user)
def test_event_end_task(self): user = BlueBottleUserFactory.create(first_name='Nono') start = timezone.now() + timedelta(hours=5) event = EventFactory.create( owner=user, initiative=self.initiative, title='Finish them translations, Rolfertjan!', start=start, duration=1 ) event.states.submit(save=True) ParticipantFactory.create_batch(3, activity=event) tenant = connection.tenant mail.outbox = [] future = timezone.now() + timedelta(hours=6) with mock.patch.object(timezone, 'now', return_value=future): event_tasks() with LocalTenant(tenant, clear_tenant=True): event = Event.objects.get(pk=event.pk) self.assertEqual(event.status, EventStateMachine.succeeded.value) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, u'Your event "{}" took place! \U0001f389'.format(event.title)) self.assertTrue("Hi Nono,", mail.outbox[0].body)
def test_change_capacity_no_transition(self): self.event.states.submit(save=True) ParticipantFactory.create_batch(self.event.capacity - 1, activity=self.event) self.event.capacity = self.event.capacity + 1 self.event.save() self.assertEqual(self.event.status, EventStateMachine.open.value)
def test_delete_event_admin(self): self.client.force_login(self.superuser) ParticipantFactory.create_batch(3, activity=self.event) url = reverse('admin:events_event_delete', args=(self.event.id, )) response = self.client.post(url, {'post': 'yes'}) self.assertEqual(response.status_code, status.HTTP_302_FOUND) self.assertEqual(Event.objects.count(), 0) self.assertEqual(Participant.objects.count(), 0)
def test_participant(self): ParticipantFactory.create(activity=self.event, user=self.other_user) self.event.states.succeed(save=True) self.assertEqual(self.stats.activities_online, 0) self.assertEqual(self.stats.activities_succeeded, 1) self.assertEqual(self.stats.events_succeeded, 1) self.assertEqual(self.stats.time_spent, 0.1) self.assertEqual(self.stats.event_members, 1) self.assertEqual(self.stats.people_involved, 2)
def test_succeed_when_passed(self): self.passed_event.states.submit(save=True) ParticipantFactory.create(activity=self.passed_event) self.assertEqual(self.passed_event.status, EventStateMachine.succeeded.value) for participant in self.passed_event.contributions.instance_of( Participant): self.assertEqual(participant.status, ParticipantStateMachine.succeeded.value)
def test_full(self): self.event.states.submit(save=True) ParticipantFactory.create_batch(self.event.capacity, activity=self.event) self.event.refresh_from_db() self.assertEqual(self.event.status, EventStateMachine.full.value) for participant in self.event.contributions.instance_of(Participant): self.assertEqual(participant.status, ParticipantStateMachine.new.value)
def test_event_scheduled_task_start(self): ParticipantFactory.create_batch(2, activity=self.event) self.assertEqual(self.event.status, 'full') tenant = connection.tenant with mock.patch.object(timezone, 'now', return_value=(timezone.now() + timedelta(days=10, hours=12))): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() event = Event.objects.get(pk=self.event.pk) self.assertEqual(event.status, 'running')
def test_change_capacity_open(self): self.event.states.submit(save=True) ParticipantFactory.create_batch(self.event.capacity, activity=self.event) self.event.capacity = self.event.capacity + 1 effects = list(self.event.current_effects) self.assertEqual(len(effects), 1) self.assertEqual(effects[0].name, 'reopen') self.event.save() self.assertEqual(self.event.status, EventStateMachine.open.value)
def test_not_succeed_change_start(self): self.event.states.submit(save=True) self.assertEqual(self.event.status, EventStateMachine.open.value) ParticipantFactory.create(activity=self.event) self.event.start = timezone.now() + timedelta(hours=2) self.event.save() self.assertEqual(self.event.status, EventStateMachine.open.value) for participant in self.event.contributions.instance_of(Participant): self.assertEqual(participant.status, ParticipantStateMachine.new.value)
def test_event_properties(self): start = now() - timedelta(hours=1) event = EventFactory.create( title='The greatest event', start=start, duration=3, capacity=10, initiative=InitiativeFactory.create(status='approved') ) event.states.submit(save=True) ParticipantFactory.create_batch(3, activity=event, status='new') self.assertEqual(event.participants.count(), 3)
def test_list_my_participation(self): response = self.client.get(self.participant_url, {'filter[user.id]': self.user.id}, user=self.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 0) # Create 4 participant objects for this user for different events ParticipantFactory.create_batch(4, status='closed', user=self.user) response = self.client.get(self.participant_url, {'filter[user.id]': self.user.id}, user=self.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 4)
def test_no_capacity(self): start = now() + timedelta(hours=1) event = EventFactory.create( title='The greatest event', start=start, duration=3, initiative=InitiativeFactory.create(status='approved'), capacity=None ) event.states.submit(save=True) ParticipantFactory.create(activity=event, status='new') self.assertEqual(event.status, 'open')
def test_list_my_participation_by_event(self): # Filtering by user and activity user should only see # own participation for that event ParticipantFactory.create_batch(4, status='closed', user=self.user) ParticipantFactory.create(activity=self.event, status='closed', user=self.user) response = self.client.get(self.participant_url, { 'filter[activity.id]': self.event.id, 'filter[user.id]': self.user.id }, user=self.user) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), 1)
def test_event_scheduled_task_reminder(self): ParticipantFactory.create_batch(2, activity=self.event) tenant = connection.tenant mail.outbox = [] with mock.patch.object(timezone, 'now', return_value=(timezone.now() + timedelta(days=7))): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() event = Event.objects.get(pk=self.event.pk) self.assertEqual(event.status, 'full') self.assertEqual(len(mail.outbox), 2)
def test_sort_matching_popularity(self): first = EventFactory.create(status='open') second = EventFactory.create(status='open') ParticipantFactory.create(activity=second, created=now() - timedelta(days=7)) third = EventFactory.create(status='open') ParticipantFactory.create(activity=third, created=now() - timedelta(days=5)) fourth = EventFactory.create(status='open') ParticipantFactory.create(activity=fourth, created=now() - timedelta(days=7)) ParticipantFactory.create(activity=fourth, created=now() - timedelta(days=5)) response = self.client.get(self.url + '?sort=popularity', user=self.owner) data = json.loads(response.content) self.assertEqual(data['meta']['pagination']['count'], 4) self.assertEqual(data['data'][0]['id'], str(fourth.pk)) self.assertEqual(data['data'][1]['id'], str(third.pk)) self.assertEqual(data['data'][2]['id'], str(second.pk)) self.assertEqual(data['data'][3]['id'], str(first.pk))
def test_sort_matching_theme(self): theme = ProjectThemeFactory.create() self.owner.favourite_themes.add(theme) self.owner.save() initiative = InitiativeFactory.create(theme=theme) first = EventFactory.create(status='open', capacity=1) ParticipantFactory.create(activity=first) second = EventFactory.create(status='open', initiative=initiative, capacity=1) ParticipantFactory.create(activity=second) third = EventFactory.create(status='open') ParticipantFactory.create(activity=third) fourth = EventFactory.create(status='open', initiative=initiative) ParticipantFactory.create(activity=fourth) response = self.client.get(self.url + '?sort=popularity', user=self.owner) data = json.loads(response.content) self.assertEqual(data['meta']['pagination']['count'], 4) self.assertEqual(data['data'][0]['id'], str(fourth.pk)) self.assertEqual(data['data'][1]['id'], str(third.pk)) self.assertEqual(data['data'][2]['id'], str(second.pk)) self.assertEqual(data['data'][3]['id'], str(first.pk))
def setUp(self): super(ContributionListAPITestCase, self).setUp() self.client = JSONAPITestClient() self.user = BlueBottleUserFactory.create() ParticipantFactory.create_batch(2, user=self.user) ApplicantFactory.create_batch(2, user=self.user) DonationFactory.create_batch(2, user=self.user, status='succeeded') DonationFactory.create_batch(2, user=self.user, status='new') ParticipantFactory.create() ApplicantFactory.create() DonationFactory.create() self.url = reverse('contribution-list')
def test_date_not_changed(self): event = EventFactory( title='Test Title', status='open', start=now() + timedelta(days=4), ) ParticipantFactory.create_batch(3, activity=event, status='new') ParticipantFactory.create(activity=event, status='withdrawn') mail.outbox = [] event.title = 'New title' event.save() self.assertEqual(len(mail.outbox), 0)
def test_full(self): start = now() + timedelta(hours=2) event = EventFactory.create( title='The greatest event', start=start, duration=1, capacity=10, initiative=InitiativeFactory.create(status='approved') ) event.states.submit(save=True) ParticipantFactory.create_batch(event.capacity, activity=event) event.refresh_from_db() self.assertEqual(event.status, 'full')
def setUp(self): super(ParticipantTransitionTestCase, self).setUp() self.client = JSONAPITestClient() self.url = reverse('event-list') self.participant_user = BlueBottleUserFactory() self.initiative = InitiativeFactory.create() self.initiative.states.submit() self.initiative.states.approve(save=True) self.event = EventFactory.create(owner=self.initiative.owner, initiative=self.initiative) self.participant = ParticipantFactory.create( user=self.participant_user, activity=self.event) self.transition_url = reverse('participant-transition-list') self.event_url = reverse('event-detail', args=(self.event.pk, )) self.data = { 'data': { 'type': 'contributions/participant-transitions', 'attributes': { 'transition': 'withdraw', }, 'relationships': { 'resource': { 'data': { 'type': 'contributions/participants', 'id': self.participant.pk } } } } }
def test_delete_participant_event_admin(self): self.client.force_login(self.superuser) self.participants = ParticipantFactory.create_batch( 3, activity=self.event) self.assertEqual(Participant.objects.count(), 3) url = reverse('admin:events_event_change', args=(self.event.id, )) data = { 'title': 'New title', 'slug': self.event.slug, 'owner': self.event.owner_id, 'initiative': self.event.initiative_id, 'description': self.event.description, 'capacity': self.event.capacity, 'start_0': str(self.event.start.date()), 'start_1': str(self.event.start.time()), 'duration': self.event.duration, 'status': self.event.status, 'registration_deadline': str(self.event.registration_deadline), 'is_online': self.event.is_online, 'location': self.event.location_id, 'location_hint': self.event.location_hint, '_continue': 'Save and continue editing', 'confirm': 'Yes', 'follow-follow-content_type-instance_id-INITIAL_FORMS': '0', 'follow-follow-content_type-instance_id-TOTAL_FORMS': '0', 'wallposts-wallpost-content_type-object_id-TOTAL_FORMS': '0', 'wallposts-wallpost-content_type-object_id-INITIAL_FORMS': '0', 'notifications-message-content_type-object_id-TOTAL_FORMS': '0', 'notifications-message-content_type-object_id-INITIAL_FORMS': '0', 'contributions-TOTAL_FORMS': '3', 'contributions-INITIAL_FORMS': '3', 'contributions-0-contribution_ptr': self.participants[0].contribution_ptr_id, 'contributions-0-activity': self.event.id, 'contributions-0-user': self.participants[0].user_id, 'contributions-0-time_spent': self.participants[0].time_spent, 'contributions-0-DELETE': 'on', 'contributions-1-contribution_ptr': self.participants[1].contribution_ptr_id, 'contributions-1-activity': self.event.id, 'contributions-1-user': self.participants[1].user_id, 'contributions-1-time_spent': self.participants[1].time_spent, 'contributions-2-contribution_ptr': self.participants[2].contribution_ptr_id, 'contributions-2-activity': self.event.id, 'contributions-2-user': self.participants[2].user_id, 'contributions-2-time_spent': self.participants[2].time_spent, 'contributions-2-DELETE': 'on', } response = self.client.post(url, data) self.assertEqual( response.status_code, status.HTTP_302_FOUND, 'Deleting participants failed. ' 'Did you change admin fields for EventAdmin? ' 'Please adjust the data in this test.') self.event.refresh_from_db() self.assertEqual(self.event.title, 'New title') self.assertEqual(Participant.objects.count(), 1)
def test_member_export(self): member = BlueBottleUserFactory.create(username='******') CustomMemberFieldSettings.objects.create(name='Extra Info') field = CustomMemberFieldSettings.objects.create(name='How are you') CustomMemberField.objects.create(member=member, value='Fine', field=field) ParticipantFactory.create(time_spent=5, user=member, status='succeeded') ParticipantFactory.create(time_spent=12, user=member, status='succeeded') ApplicantFactory.create_batch(3, time_spent=10, user=member, status='succeeded') DonationFactory.create_batch(7, amount=Money(5, 'EUR'), user=member, status='succeeded') export_action = self.member_admin.actions[0] response = export_action(self.member_admin, self.request, self.member_admin.get_queryset(self.request)) data = response.content.decode('utf-8').split("\r\n") headers = data[0].split(",") user_data = [] for row in data: if row.startswith('malle-eppie'): user_data = row.split(',') # Test basic info and extra field are in the csv export self.assertEqual(headers, [ 'username', 'email', 'remote_id', 'first_name', 'last name', 'date joined', 'is initiator', 'is supporter', 'is volunteer', 'amount donated', 'time spent', 'subscribed to matching projects', 'Extra Info', 'How are you' ]) self.assertEqual(user_data[0], 'malle-eppie') self.assertEqual(user_data[7], 'True') self.assertEqual(user_data[8], 'True') self.assertEqual(user_data[9], u'35.00 €') self.assertEqual(user_data[10], '47.0') self.assertEqual(user_data[13], 'Fine')
def test_mark_absent_no_change(self): self.passed_event.states.submit(save=True) ParticipantFactory.create(activity=self.passed_event) participant = ParticipantFactory.create(activity=self.passed_event) self.assertEqual(self.passed_event.status, EventStateMachine.succeeded.value) self.assertEqual(participant.status, EventStateMachine.succeeded.value) participant.states.mark_absent(user=self.passed_event.owner) participant.save() self.assertEqual(participant.status, ParticipantStateMachine.no_show.value) self.passed_event.refresh_from_db() self.assertEqual(self.passed_event.status, EventStateMachine.succeeded.value)
def test_event_start_task(self): start = timezone.now() + timedelta(hours=1) event = EventFactory.create( initiative=self.initiative, start=start, duration=3 ) event.states.submit(save=True) ParticipantFactory.create(activity=event) self.assertEqual(event.status, 'open') tenant = connection.tenant future = timezone.now() + timedelta(hours=2) with mock.patch.object(timezone, 'now', return_value=future): event_tasks() with LocalTenant(tenant, clear_tenant=True): event = Event.objects.get(pk=event.pk) self.assertEqual(event.status, 'running')
def test_database(self): initiative = InitiativeFactory.create() event = EventFactory.create(initiative=initiative, owner=initiative.owner, start=timezone.now() - datetime.timedelta(hours=1), duration=0.1) initiative.states.submit(save=True) initiative.states.approve(save=True) event.refresh_from_db() ParticipantFactory.create_batch(5, activity=event) stat = DatabaseStatisticFactory.create(name='Test', query='people_involved') self.assertEqual(stat.get_value(), 6)
def test_succeed_in_future(self): self.event.states.submit(save=True) ParticipantFactory.create(activity=self.event) future = self.event.start + timedelta(days=2) self.event.save() tenant = connection.tenant with mock.patch.object(timezone, 'now', return_value=future): event_tasks() with LocalTenant(tenant, clear_tenant=True): self.event.refresh_from_db() self.assertEqual(self.event.status, EventStateMachine.succeeded.value) self.event.refresh_from_db() for participant in self.event.contributions.instance_of(Participant): self.assertEqual(participant.status, ParticipantStateMachine.succeeded.value) self.assertEqual(participant.time_spent, self.event.duration)