Example #1
0
    def setUp(self):
        super(TestTaskSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.project_response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )
Example #2
0
    def setUp(self):
        super(TestProjectStatusUpdate, self).setUp()

        self.init_projects()

        self.theme = ProjectThemeFactory(slug='test-theme')
        self.project = ProjectFactory(theme=self.theme)
        self.task = TaskFactory(project=self.project)

        self.survey = SurveyFactory(link='https://example.com/survey/1/', active=True)
Example #3
0
    def test_inactive_survey_complete(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=False)
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)

        self.assertFalse('survey' in email.body)
Example #4
0
    def setUp(self):
        super(TestSimpleProjectSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory()
        self.project_2 = ProjectFactory()

        self.survey = SurveyFactory(title='test survey', active=True)

        self.response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )
Example #5
0
    def test_complete(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=True)
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)

        self.assertTrue(
            self.survey.url(self.project, user_type='initiator') in email.body)
        self.assertTrue('survey' in email.body)
Example #6
0
    def setUp(self):
        super(TestTaskSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.project_response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )
Example #7
0
    def setUp(self):
        super(TestProjectMails, self).setUp()

        self.init_projects()
        self.status_running = ProjectPhase.objects.get(slug='campaign')
        self.complete = ProjectPhase.objects.get(slug='done-complete')
        self.incomplete = ProjectPhase.objects.get(slug='done-incomplete')
        self.project = ProjectFactory.create(status=self.status_running, organization=None)
        self.survey = SurveyFactory(link='https://example.com/survey/1/')
Example #8
0
    def test_member_realized_mail_with_survey(self):
        survey = SurveyFactory(link='https://example.com/survey/1/', active=True)

        task_member = TaskMemberFactory.create(
            task=self.task,
            status='accepted'
        )

        task_member.status = 'realized'
        task_member.save()

        self.assertEquals(len(mail.outbox), 2)

        email = mail.outbox[-1]

        self.assertNotEquals(email.subject.find("realised"), -1)
        self.assertEquals(email.to[0], task_member.member.email)
        self.assertTrue(survey.url(self.task) in email.body)
        self.assertTrue('survey' in email.body)
Example #9
0
    def setUp(self):
        super(TestProjectStatusUpdate, self).setUp()

        self.init_projects()

        self.theme = ProjectThemeFactory(slug='test-theme')
        self.project = ProjectFactory(theme=self.theme)
        self.task = TaskFactory(project=self.project)

        self.survey = SurveyFactory(link='https://example.com/survey/1/')
Example #10
0
    def test_complete_inactive_survey_with_organization(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=False)
        self.project.organization = OrganizationFactory.create(
            email='*****@*****.**')
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 2)
        initiator_email = mail.outbox[0]
        self.assertEqual(initiator_email.to, [self.project.owner.email])
        self.assertTrue('has been realised' in initiator_email.subject)
        self.assertFalse('survey' in initiator_email.body)

        organization_email = mail.outbox[1]
        self.assertEqual(organization_email.to,
                         [self.project.organization.email])
        self.assertTrue('has been realised' in organization_email.subject)
        self.assertFalse('survey' in organization_email.body)
Example #11
0
    def setUp(self):
        super(TestCombinedSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.intitiator_response = ResponseFactory.create(
            project=self.project,
            user_type='initiator',
            survey=self.survey
        )

        self.organization_response = ResponseFactory.create(
            project=self.project,
            user_type='organization',
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )
Example #12
0
    def setUp(self):
        super(TestSurveyGizmoAdapter, self).setUp()

        self.init_projects()

        self.theme = ProjectThemeFactory(slug='test-theme')
        self.project = ProjectFactory()

        self.task = TaskFactory(project=self.project)

        self.survey = SurveyFactory(remote_id='test-id',
                                    last_synced=timezone.now())
Example #13
0
    def setUp(self):
        super(TestSimpleProjectSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory()
        self.project_2 = ProjectFactory()

        self.survey = SurveyFactory(title='test survey')

        self.response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )
Example #14
0
    def setUp(self):
        super(ProjectSurveyAPITestCase, self).setUp()

        self.init_projects()

        phase = ProjectPhase.objects.get(slug='campaign')

        self.project = ProjectFactory.create(status=phase)
        self.survey = SurveyFactory(title='test survey')

        questions = (('Question 1', 'string', None, {}),
                     ('Question 2', 'number', 'average', {
                         'max_number': 100,
                         'min_number': 100
                     }), ('Question 3', 'slider', 'sum', {
                         'max_number': 100,
                         'min_number': 100
                     }))

        questions = [
            QuestionFactory.create(survey=self.survey,
                                   title=title,
                                   type=type,
                                   aggregation=aggregation,
                                   properties=properties)
            for title, type, aggregation, properties in questions
        ]

        response = ResponseFactory.create(project=self.project,
                                          survey=self.survey)

        for question in questions:
            AnswerFactory.create(question=question,
                                 response=response,
                                 value=random.randint(0, 100))

        self.survey_url = reverse('project_survey_list')
Example #15
0
    def test_results_survey(self):
        survey = SurveyFactory.create()

        SurveyContent.objects.create_for_placeholder(self.placeholder, survey=survey)

        response = self.client.get(self.url)
        self.assertEquals(response.status_code, status.HTTP_200_OK)

        self.assertEqual(response.data['title'], self.page.title)
        self.assertEqual(response.data['description'], self.page.description)

        survey = response.data['blocks'][0]
        self.assertEqual(survey['type'], 'survey')
        self.assertTrue('response_count' in survey)
        self.assertEqual(survey['answers'], [])
Example #16
0
    def test_results_list(self):
        survey = SurveyFactory.create()
        SurveyContent.objects.create_for_placeholder(self.placeholder, survey=survey)

        quote_block = QuotesContent.objects.create_for_placeholder(self.placeholder)
        self.quote = QuoteFactory(block=quote_block)

        stat_block = StatsContent.objects.create_for_placeholder(self.placeholder)
        self.stat = StatFactory(block=stat_block)

        response = self.client.get(self.url)
        self.assertEquals(response.status_code, status.HTTP_200_OK)
        self.assertEquals(len(response.data['blocks']), 3)
        self.assertEquals(response.data['blocks'][0]['type'], 'survey')
        self.assertEquals(response.data['blocks'][1]['type'], 'quotes')
        self.assertEquals(response.data['blocks'][2]['type'], 'statistics')
Example #17
0
    def setUp(self):
        super(TestCombinedSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.intitiator_response = ResponseFactory.create(
            project=self.project,
            user_type='initiator',
            survey=self.survey
        )

        self.organization_response = ResponseFactory.create(
            project=self.project,
            user_type='organization',
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )
Example #18
0
    def setUp(self):
        super(ProjectSurveyAPITestCase, self).setUp()

        self.init_projects()

        phase = ProjectPhase.objects.get(slug='campaign')

        self.project = ProjectFactory.create(status=phase)
        self.survey = SurveyFactory(title='test survey')

        questions = (
            ('Question 1', 'string', None, {}),
            ('Question 2', 'number', 'average', {'max_number': 100, 'min_number': 100}),
            ('Question 3', 'slider', 'sum', {'max_number': 100, 'min_number': 100})
        )

        questions = [
            QuestionFactory.create(
                survey=self.survey,
                title=title,
                type=type,
                aggregation=aggregation,
                properties=properties
            ) for title, type, aggregation, properties in questions]

        response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

        for question in questions:
            AnswerFactory.create(
                question=question,
                response=response,
                value=random.randint(0, 100)
            )

        self.survey_url = reverse('project_survey_list')
Example #19
0
class TestTaskSurveyAggregation(BluebottleTestCase):
    """
    Check some task survey aggregations.
    """

    def setUp(self):
        super(TestTaskSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.project_response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )

    def test_average(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='average', type='number')

        for value in ['10', '20', '30']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['50']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['80']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1,
                                                     project=self.project)
        self.assertEqual(aggregate.value, 20.0)

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task2,
                                                     project=self.project)
        self.assertEqual(aggregate.value, 50.0)

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task3,
                                                     project=self.project)
        self.assertEqual(aggregate.value, 80.0)

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 50.0)

    def test_sum(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['10', '20', '30']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['50']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['90']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 160.0)

    def test_list(self):
        question = QuestionFactory(survey=self.survey, title='shoot', type='list')

        for value in ['Panda', 'Elephant', 'Rhino']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['Hedgehog']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['Bear', 'Lion']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1,
                                                     project=self.project)
        self.assertListEqual(aggregate.list, ['Elephant', 'Panda', 'Rhino'])

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.list, ['Bear', 'Elephant', 'Hedgehog', 'Lion', 'Panda', 'Rhino'])

    def test_multiple_choice_radio(self):
        question = QuestionFactory(survey=self.survey, title='test', type='table-radio')
        SubQuestionFactory(question=question, title='test')
        SubQuestionFactory(question=question, title='toast')

        for values in [{'test': 2, 'toast': 8}, {'test': 4, 'toast': 9}, {'test': 3, 'toast': 7}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                options=values
            )

        for values in [{'test': 1, 'toast': 9}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                options=values
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1)
        self.assertEqual(aggregate.options, {'test': 3.0, 'toast': 8.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task2)
        self.assertEqual(aggregate.options, {'test': 1.0, 'toast': 9.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 2.0, 'toast': 8.5})
Example #20
0
class TestCombinedSurveyAggregation(BluebottleTestCase):
    """
    Check some task survey aggregations.
    """

    def setUp(self):
        super(TestCombinedSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.intitiator_response = ResponseFactory.create(
            project=self.project,
            user_type='initiator',
            survey=self.survey
        )

        self.organization_response = ResponseFactory.create(
            project=self.project,
            user_type='organization',
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )

    def test_project_aggregates(self):

        question1 = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['110', '130']:
            AnswerFactory.create(
                question=question1,
                response=self.intitiator_response,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question1.aggregateanswer_set.get(question=question1,
                                                      aggregation_type='project',
                                                      project=self.project)

        self.assertEqual(aggregate.value, 120.0)

    def test_combined_number(self):

        question = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['110', '130']:
            AnswerFactory.create(
                question=question,
                response=self.intitiator_response,
                value=value,
            )

        for value in ['200']:
            AnswerFactory.create(
                question=question,
                response=self.organization_response,
                value=value,
            )

        for value in ['10', '20', '30']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['50']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['90']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate1 = question.aggregateanswer_set.get(question=question,
                                                      aggregation_type='combined',
                                                      project=self.project)
        # Expected value is calculated
        # initiator:        (110 + 130) / 2
        # organization:     200
        # tasks:            (10 + 20 + 30) / 3 + 50 + 90
        # Mean:             (120 + 200 + 160)/ 3 = 160
        self.assertEqual(aggregate1.value, 160.0)

    def test_combined_table_radio(self):

        question = QuestionFactory(survey=self.survey, title='test', type='table-radio')
        SubQuestionFactory(question=question, title='test')
        SubQuestionFactory(question=question, title='toast')

        for values in [{'test': 4, 'toast': 6}]:
            AnswerFactory.create(
                question=question,
                response=self.intitiator_response,
                options=values
            )

        for values in [{'test': 5, 'toast': 9}, {'test': 7, 'toast': 10}]:
            AnswerFactory.create(
                question=question,
                response=self.organization_response,
                options=values
            )

        for values in [{'test': 2, 'toast': 8}, {'test': 4, 'toast': 9}, {'test': 3, 'toast': 7}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                options=values
            )

        for values in [{'test': 1, 'toast': 9}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                options=values
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1)
        self.assertEqual(aggregate.options, {'test': 3.0, 'toast': 8.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 2.0, 'toast': 8.5})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 5.333333333333333, 'toast': 8.333333333333334})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='organization',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 6.0, 'toast': 9.5})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='initiator',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 4.0, 'toast': 6.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='combined',
                                                     project=self.project)
        # Calculation of combined
        # Initiator mean:    {'test': 4.0, 'toast': 6.0}
        # Organization mean: {'test': 6.0, 'toast': 9.5}
        # Tasks mean:        {'test': 2.0, 'toast': 8.5}
        # Combined:          {'test': 4.0, 'toast': 8.0}
        self.assertEqual(aggregate.options, {'test': 4.0, 'toast': 8.0})
Example #21
0
class TestProjectMails(BluebottleTestCase):
    """
    Test the sending of email notifications when a Task' status changes
    """
    def setUp(self):
        super(TestProjectMails, self).setUp()

        self.init_projects()
        self.status_running = ProjectPhase.objects.get(slug='campaign')
        self.complete = ProjectPhase.objects.get(slug='done-complete')
        self.incomplete = ProjectPhase.objects.get(slug='done-incomplete')
        self.project = ProjectFactory.create(status=self.status_running,
                                             organization=None)

    def test_complete(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=True)
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)

        self.assertTrue(
            self.survey.url(self.project, user_type='initiator') in email.body)
        self.assertTrue('survey' in email.body)

    def test_inactive_survey_complete(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=False)
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)

        self.assertFalse('survey' in email.body)

    def test_complete_no_celebration(self):
        self.project.celebrate_results = False
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)
        self.assertFalse('survey' in email.body)

    def test_complete_with_organization(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=True)
        self.project.organization = OrganizationFactory.create(
            email='*****@*****.**')
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 2)
        initiator_email = mail.outbox[0]
        self.assertEqual(initiator_email.to, [self.project.owner.email])
        self.assertTrue('has been realised' in initiator_email.subject)
        self.assertTrue(
            Survey.url(self.project, user_type='initiator') in
            initiator_email.body)

        organization_email = mail.outbox[1]
        self.assertEqual(organization_email.to,
                         [self.project.organization.email])
        self.assertTrue('has been realised' in organization_email.subject)
        self.assertTrue(
            Survey.url(self.project, user_type='organization') in
            organization_email.body)

    def test_complete_inactive_survey_with_organization(self):
        self.survey = SurveyFactory(link='https://example.com/survey/1/',
                                    active=False)
        self.project.organization = OrganizationFactory.create(
            email='*****@*****.**')
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 2)
        initiator_email = mail.outbox[0]
        self.assertEqual(initiator_email.to, [self.project.owner.email])
        self.assertTrue('has been realised' in initiator_email.subject)
        self.assertFalse('survey' in initiator_email.body)

        organization_email = mail.outbox[1]
        self.assertEqual(organization_email.to,
                         [self.project.organization.email])
        self.assertTrue('has been realised' in organization_email.subject)
        self.assertFalse('survey' in organization_email.body)

    def test_incomplete(self):
        self.project.status = self.incomplete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        self.assertNotEquals(mail.outbox[0].subject.find("has expired"), -1)

    def test_state_other(self):
        self.project.status = ProjectPhase.objects.get(slug="plan-new")
        self.project.save()

        self.assertEquals(len(mail.outbox), 0)

        self.project.status = ProjectPhase.objects.get(slug="closed")
        self.project.save()

        self.assertEquals(len(mail.outbox), 0)
Example #22
0
class TestSimpleProjectSurveyAggregation(BluebottleTestCase):
    """
    Check some simple project survey aggregations.
    """

    def setUp(self):
        super(TestSimpleProjectSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory()
        self.project_2 = ProjectFactory()

        self.survey = SurveyFactory(title='test survey', active=True)

        self.response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

    def test_ignore_sum(self):
        """
        Aggregation=sum only is only intended for tasks-by-project aggregations.
        For project aggregations it should still be mean.
        """
        question = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['30', '10', '20.0']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 20.0)

    def test_average(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='average', type='number')

        for value in ['30', '10', '20.0']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 20.0)

    def test_average_non_number(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='average', type='number')

        for value in ['20', '10', 'onzin test']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 10.0)

    def test_slider(self):
        question = QuestionFactory(survey=self.survey, title='how', aggregation='average', type='slider')

        for value in [80, 70, 90]:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 80.0)

    def test_multiple_choice_radio(self):
        question = QuestionFactory(survey=self.survey, title='test', type='radio')

        for value in ['test', 'toast', 'test']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                options=[value]
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 2, 'toast': 1})

    def test_multiple_choice_checkbox(self):
        question = QuestionFactory(survey=self.survey, title='test', type='checkbox')

        for values in [['test'], ['test', 'toast'], ['test'], ['toast', 'wokkel']]:
            AnswerFactory.create(
                question=question,
                response=self.response,
                options=values
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {"test": 3, "wokkel": 1, "toast": 2})

    def test_table_radio(self):
        question = QuestionFactory(survey=self.survey, title='test', type='table-radio')
        SubQuestionFactory(question=question, title='toast')
        SubQuestionFactory(question=question, title='test')

        for values in [{'test': 2, 'toast': 8}, {'test': 4, 'toast': 9}, {'test': 3, 'toast': 7}]:
            AnswerFactory.create(
                question=question,
                response=self.response,
                options=values
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        # Answers should follow the ordering of sub questions
        self.assertEqual(aggregate.options, {'toast': 8.0, 'test': 3.0})
Example #23
0
class TestProjectMails(BluebottleTestCase):
    """
    Test the sending of email notifications when a Task' status changes
    """

    def setUp(self):
        super(TestProjectMails, self).setUp()

        self.init_projects()
        self.status_running = ProjectPhase.objects.get(slug='campaign')
        self.complete = ProjectPhase.objects.get(slug='done-complete')
        self.incomplete = ProjectPhase.objects.get(slug='done-incomplete')
        self.project = ProjectFactory.create(status=self.status_running, organization=None)
        self.survey = SurveyFactory(link='https://example.com/survey/1/')

    def test_complete(self):
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)

        self.assertTrue(self.survey.url(self.project, user_type='initiator') in email.body)

    def test_complete_no_celebration(self):
        self.project.celebrate_results = False
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        email = mail.outbox[0]

        self.assertNotEquals(email.subject.find("has been realised"), -1)
        self.assertFalse('survey' in email.body)

    def test_complete_with_organization(self):
        self.project.organization = OrganizationFactory.create(email='*****@*****.**')
        self.project.status = self.complete
        self.project.save()

        self.assertEquals(len(mail.outbox), 2)
        initiator_email = mail.outbox[0]
        self.assertEqual(initiator_email.to, [self.project.owner.email])
        self.assertTrue('has been realised' in initiator_email.subject)
        self.assertTrue(Survey.url(self.project, user_type='initiator') in initiator_email.body)

        organization_email = mail.outbox[1]
        self.assertEqual(organization_email.to, [self.project.organization.email])
        self.assertTrue('has been realised' in organization_email.subject)
        self.assertTrue(Survey.url(self.project, user_type='organization') in organization_email.body)

    def test_incomplete(self):
        self.project.status = self.incomplete
        self.project.save()

        self.assertEquals(len(mail.outbox), 1)
        self.assertNotEquals(mail.outbox[0].subject.find("has expired"), -1)

    def test_state_other(self):
        self.project.status = ProjectPhase.objects.get(slug="plan-new")
        self.project.save()

        self.assertEquals(len(mail.outbox), 0)

        self.project.status = ProjectPhase.objects.get(slug="closed")
        self.project.save()

        self.assertEquals(len(mail.outbox), 0)
Example #24
0
class ProjectSurveyAPITestCase(BluebottleTestCase):
    """
    """
    def setUp(self):
        super(ProjectSurveyAPITestCase, self).setUp()

        self.init_projects()

        phase = ProjectPhase.objects.get(slug='campaign')

        self.project = ProjectFactory.create(status=phase)
        self.survey = SurveyFactory(title='test survey')

        questions = (
            ('Question 1', 'string', None, {}),
            ('Question 2', 'number', 'average', {'max_number': 100, 'min_number': 100}),
            ('Question 3', 'slider', 'sum', {'max_number': 100, 'min_number': 100})
        )

        questions = [
            QuestionFactory.create(
                survey=self.survey,
                title=title,
                type=type,
                aggregation=aggregation,
                properties=properties
            ) for title, type, aggregation, properties in questions]

        response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

        for question in questions:
            AnswerFactory.create(
                question=question,
                response=response,
                value=random.randint(0, 100)
            )

        self.survey_url = reverse('project_survey_list')

    def test_get_survey(self):
        self.survey.aggregate()

        response = self.client.get(self.survey_url, {'project': self.project.slug})
        self.assertEqual(response.status_code, 200)

        data = response.data[0]

        self.assertEqual(data['title'], self.survey.title)

        for answer in data['answers']:
            self.assertTrue('type' in answer)
            self.assertTrue('value' in answer)
            self.assertTrue('title' in answer)
            self.assertTrue('properties' in answer)

    def test_get_survey_no_aggregate(self):
        response = self.client.get(self.survey_url, {'project': self.project.slug})
        self.assertEqual(response.status_code, 200)

        data = response.data[0]

        self.assertEqual(data['title'], self.survey.title)

        for answer in data['answers']:
            self.assertIsNone(answer['value'])
Example #25
0
class TestSimpleProjectSurveyAggregation(BluebottleTestCase):
    """
    Check some simple project survey aggregations.
    """

    def setUp(self):
        super(TestSimpleProjectSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory()
        self.project_2 = ProjectFactory()

        self.survey = SurveyFactory(title='test survey')

        self.response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

    def test_ignore_sum(self):
        """
        Aggregation=sum only is only intended for tasks-by-project aggregations.
        For project aggregations it should still be mean.
        """
        question = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['30', '10', '20.0']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 20.0)

    def test_average(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='average', type='number')

        for value in ['30', '10', '20.0']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 20.0)

    def test_average_non_number(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='average', type='number')

        for value in ['20', '10', 'onzin test']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 10.0)

    def test_slider(self):
        question = QuestionFactory(survey=self.survey, title='how', aggregation='average', type='slider')

        for value in [80, 70, 90]:
            AnswerFactory.create(
                question=question,
                response=self.response,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 80.0)

    def test_multiple_choice_radio(self):
        question = QuestionFactory(survey=self.survey, title='test', type='radio')

        for value in ['test', 'toast', 'test']:
            AnswerFactory.create(
                question=question,
                response=self.response,
                options=[value]
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 2, 'toast': 1})

    def test_multiple_choice_checkbox(self):
        question = QuestionFactory(survey=self.survey, title='test', type='checkbox')

        for values in [['test'], ['test', 'toast'], ['test'], ['toast', 'wokkel']]:
            AnswerFactory.create(
                question=question,
                response=self.response,
                options=values
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {"test": 3, "wokkel": 1, "toast": 2})

    def test_table_radio(self):
        question = QuestionFactory(survey=self.survey, title='test', type='table-radio')
        SubQuestionFactory(question=question, title='toast')
        SubQuestionFactory(question=question, title='test')

        for values in [{'test': 2, 'toast': 8}, {'test': 4, 'toast': 9}, {'test': 3, 'toast': 7}]:
            AnswerFactory.create(
                question=question,
                response=self.response,
                options=values
            )

        self.survey.aggregate()
        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        # Answers should follow the ordering of sub questions
        self.assertEqual(aggregate.options, {'toast': 8.0, 'test': 3.0})
Example #26
0
class TestCombinedSurveyAggregation(BluebottleTestCase):
    """
    Check some task survey aggregations.
    """

    def setUp(self):
        super(TestCombinedSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.intitiator_response = ResponseFactory.create(
            project=self.project,
            user_type='initiator',
            survey=self.survey
        )

        self.organization_response = ResponseFactory.create(
            project=self.project,
            user_type='organization',
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )

    def test_project_aggregates(self):

        question1 = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['110', '130']:
            AnswerFactory.create(
                question=question1,
                response=self.intitiator_response,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question1.aggregateanswer_set.get(question=question1,
                                                      aggregation_type='project',
                                                      project=self.project)

        self.assertEqual(aggregate.value, 120.0)

    def test_combined_number(self):

        question = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['110', '130']:
            AnswerFactory.create(
                question=question,
                response=self.intitiator_response,
                value=value,
            )

        for value in ['200']:
            AnswerFactory.create(
                question=question,
                response=self.organization_response,
                value=value,
            )

        for value in ['10', '20', '30']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['50']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['90']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate1 = question.aggregateanswer_set.get(question=question,
                                                      aggregation_type='combined',
                                                      project=self.project)
        # Expected value is calculated
        # initiator:        (110 + 130) / 2
        # organization:     200
        # tasks:            (10 + 20 + 30) / 3 + 50 + 90
        # Mean:             (120 + 200 + 160)/ 3 = 160
        self.assertEqual(aggregate1.value, 160.0)

    def test_combined_table_radio(self):

        question = QuestionFactory(survey=self.survey, title='test', type='table-radio')
        SubQuestionFactory(question=question, title='test')
        SubQuestionFactory(question=question, title='toast')

        for values in [{'test': 4, 'toast': 6}]:
            AnswerFactory.create(
                question=question,
                response=self.intitiator_response,
                options=values
            )

        for values in [{'test': 5, 'toast': 9}, {'test': 7, 'toast': 10}]:
            AnswerFactory.create(
                question=question,
                response=self.organization_response,
                options=values
            )

        for values in [{'test': 2, 'toast': 8}, {'test': 4, 'toast': 9}, {'test': 3, 'toast': 7}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                options=values
            )

        for values in [{'test': 1, 'toast': 9}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                options=values
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1)
        self.assertEqual(aggregate.options, {'test': 3.0, 'toast': 8.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 2.0, 'toast': 8.5})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 5.333333333333333, 'toast': 8.333333333333334})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='organization',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 6.0, 'toast': 9.5})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='initiator',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 4.0, 'toast': 6.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='combined',
                                                     project=self.project)
        # Calculation of combined
        # Initiator mean:    {'test': 4.0, 'toast': 6.0}
        # Organization mean: {'test': 6.0, 'toast': 9.5}
        # Tasks mean:        {'test': 2.0, 'toast': 8.5}
        # Combined:          {'test': 4.0, 'toast': 8.0}
        self.assertEqual(aggregate.options, {'test': 4.0, 'toast': 8.0})
Example #27
0
class TestTaskSurveyAggregation(BluebottleTestCase):
    """
    Check some task survey aggregations.
    """

    def setUp(self):
        super(TestTaskSurveyAggregation, self).setUp()

        self.init_projects()

        self.project = ProjectFactory.create()
        self.task1 = TaskFactory.create(project=self.project)
        self.task2 = TaskFactory.create(project=self.project)
        self.task3 = TaskFactory.create(project=self.project)

        self.survey = SurveyFactory(title='test survey')

        self.project_response = ResponseFactory.create(
            project=self.project,
            survey=self.survey
        )

        self.task_response1 = ResponseFactory.create(
            project=self.project,
            task=self.task1,
            survey=self.survey
        )

        self.task_response2 = ResponseFactory.create(
            project=self.project,
            task=self.task2,
            survey=self.survey
        )

        self.task_response3 = ResponseFactory.create(
            project=self.project,
            task=self.task3,
            survey=self.survey
        )

    def test_average(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='average', type='number')

        for value in ['10', '20', '30']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['50']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['80']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1,
                                                     project=self.project)
        self.assertEqual(aggregate.value, 20.0)

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task2,
                                                     project=self.project)
        self.assertEqual(aggregate.value, 50.0)

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task3,
                                                     project=self.project)
        self.assertEqual(aggregate.value, 80.0)

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 50.0)

    def test_sum(self):
        question = QuestionFactory(survey=self.survey, title='test', aggregation='sum', type='number')

        for value in ['10', '20', '30']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['50']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['90']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.value, 160.0)

    def test_list(self):
        question = QuestionFactory(survey=self.survey, title='shoot', type='list')

        for value in ['Panda', 'Elephant', 'Rhino']:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                value=value,
            )

        for value in ['Hedgehog']:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                value=value,
            )

        for value in ['Bear', 'Lion']:
            AnswerFactory.create(
                question=question,
                response=self.task_response3,
                value=value,
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1,
                                                     project=self.project)
        self.assertListEqual(aggregate.list, ['Elephant', 'Panda', 'Rhino'])

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.list, ['Bear', 'Elephant', 'Hedgehog', 'Lion', 'Panda', 'Rhino'])

    def test_multiple_choice_radio(self):
        question = QuestionFactory(survey=self.survey, title='test', type='table-radio')
        SubQuestionFactory(question=question, title='test')
        SubQuestionFactory(question=question, title='toast')

        for values in [{'test': 2, 'toast': 8}, {'test': 4, 'toast': 9}, {'test': 3, 'toast': 7}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response1,
                options=values
            )

        for values in [{'test': 1, 'toast': 9}]:
            AnswerFactory.create(
                question=question,
                response=self.task_response2,
                options=values
            )

        self.survey.aggregate()

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task1)
        self.assertEqual(aggregate.options, {'test': 3.0, 'toast': 8.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='task',
                                                     task=self.task2)
        self.assertEqual(aggregate.options, {'test': 1.0, 'toast': 9.0})

        aggregate = question.aggregateanswer_set.get(question=question,
                                                     aggregation_type='project_tasks',
                                                     project=self.project)
        self.assertEqual(aggregate.options, {'test': 2.0, 'toast': 8.5})
Example #28
0
    def setUp(self):
        super(PlatformAggregateTest, self).setUp()
        now = timezone.now()
        last_year = now - datetime.timedelta(days=365)

        self.init_projects()

        survey = SurveyFactory.create()

        old_project = ProjectFactory.create(campaign_ended=last_year)
        new_project = ProjectFactory.create(campaign_ended=now)

        old_task1 = TaskFactory.create(project=old_project)
        old_task2 = TaskFactory.create(project=new_project)

        question1 = QuestionFactory.create(remote_id=1,
                                           type='number',
                                           survey=survey)
        question2 = QuestionFactory.create(remote_id=2,
                                           type='table-radio',
                                           survey=survey)
        SubQuestionFactory.create(question=question2, title='before')
        SubQuestionFactory.create(question=question2, title='after')
        question3 = QuestionFactory.create(remote_id=3,
                                           type='checkbox',
                                           survey=survey)
        question4 = QuestionFactory.create(remote_id=4,
                                           type='number',
                                           aggregation='average',
                                           survey=survey)

        for answer1, answer2, answer3, answer4 in ((4, {
                'before': 1,
                'after': 3
        }, {
                'test': 3,
                'tast': 4
        }, 62), (6, {
                'before': 3,
                'after': 5
        }, {
                'test': 4,
                'tast': 3
        }, 65), (8, {
                'before': 5,
                'after': 5
        }, {
                'test': 5,
                'tast': 5
        }, 55), (2, {
                'before': 7,
                'after': 3
        }, {
                'test': 6,
                'tast': 5
        }, 62)):
            response = ResponseFactory(project=old_project, survey=survey)
            AnswerFactory.create(question=question1,
                                 response=response,
                                 value=answer1)
            AnswerFactory.create(question=question2,
                                 response=response,
                                 options=answer2)
            AnswerFactory.create(question=question3,
                                 response=response,
                                 options=answer3)
            AnswerFactory.create(question=question4,
                                 response=response,
                                 value=answer4)

        for answer1, answer2, answer3, answer4 in (
            (2, {
                'before': 6,
                'after': 8
            }, {
                'test': 4,
                'tast': 5
            }, 60),
            (4, {
                'before': 2,
                'after': 4
            }, {
                'test': 4,
                'tast': 2
            }, 40),
        ):
            response = ResponseFactory(task=old_task1, survey=survey)
            AnswerFactory.create(question=question1,
                                 response=response,
                                 value=answer1)
            AnswerFactory.create(question=question2,
                                 response=response,
                                 options=answer2)
            AnswerFactory.create(question=question3,
                                 response=response,
                                 options=answer3)
            AnswerFactory.create(question=question4,
                                 response=response,
                                 value=answer4)

        for answer1, answer2, answer3, answer4 in (
            (11, {
                'before': 0,
                'after': 2
            }, {
                'test': 2,
                'tast': 3
            }, 12),
            (9, {
                'before': 2,
                'after': 4
            }, {
                'test': 4,
                'tast': 2
            }, 16),
        ):
            response = ResponseFactory(task=old_task2, survey=survey)
            AnswerFactory.create(question=question1,
                                 response=response,
                                 value=answer1)
            AnswerFactory.create(question=question2,
                                 response=response,
                                 options=answer2)
            AnswerFactory.create(question=question3,
                                 response=response,
                                 options=answer3)
            AnswerFactory.create(question=question4,
                                 response=response,
                                 value=answer4)

        for answer1, answer2, answer3, answer4 in ((3, {
                'before': 0,
                'after': 2
        }, {
                'test': 2,
                'tast': 3
        }, 23), (5, {
                'before': 2,
                'after': 4
        }, {
                'test': 4,
                'tast': 2
        }, 12), (7, {
                'before': 5,
                'after': 4
        }, {
                'test': 4,
                'tast': 5
        }, 14), (1, {
                'before': 6,
                'after': 2
        }, {
                'test': 5,
                'tast': 4
        }, 500)):
            response = ResponseFactory(project=new_project, survey=survey)
            AnswerFactory.create(question=question1,
                                 response=response,
                                 value=answer1)
            AnswerFactory.create(question=question2,
                                 response=response,
                                 options=answer2)
            AnswerFactory.create(question=question3,
                                 response=response,
                                 options=answer3)
            AnswerFactory.create(question=question4,
                                 response=response,
                                 value=answer4)

        survey.aggregate()
Example #29
0
class ProjectSurveyAPITestCase(BluebottleTestCase):
    """
    """
    def setUp(self):
        super(ProjectSurveyAPITestCase, self).setUp()

        self.init_projects()

        phase = ProjectPhase.objects.get(slug='campaign')

        self.project = ProjectFactory.create(status=phase)
        self.survey = SurveyFactory(title='test survey')

        questions = (('Question 1', 'string', None, {}),
                     ('Question 2', 'number', 'average', {
                         'max_number': 100,
                         'min_number': 100
                     }), ('Question 3', 'slider', 'sum', {
                         'max_number': 100,
                         'min_number': 100
                     }))

        questions = [
            QuestionFactory.create(survey=self.survey,
                                   title=title,
                                   type=type,
                                   aggregation=aggregation,
                                   properties=properties)
            for title, type, aggregation, properties in questions
        ]

        response = ResponseFactory.create(project=self.project,
                                          survey=self.survey)

        for question in questions:
            AnswerFactory.create(question=question,
                                 response=response,
                                 value=random.randint(0, 100))

        self.survey_url = reverse('project_survey_list')

    def test_get_survey(self):
        self.survey.aggregate()

        response = self.client.get(self.survey_url,
                                   {'project': self.project.slug})
        self.assertEqual(response.status_code, 200)

        data = response.data[0]

        self.assertEqual(data['title'], self.survey.title)

        for answer in data['answers']:
            self.assertTrue('type' in answer)
            self.assertTrue('value' in answer)
            self.assertTrue('title' in answer)
            self.assertTrue('properties' in answer)

    def test_get_survey_no_aggregate(self):
        response = self.client.get(self.survey_url,
                                   {'project': self.project.slug})
        self.assertEqual(response.status_code, 200)

        data = response.data[0]

        self.assertEqual(data['title'], self.survey.title)

        for answer in data['answers']:
            self.assertIsNone(answer['value'])
Example #30
0
class TestProjectStatusUpdate(BluebottleTestCase):
    """
    save() automatically updates some fields, specifically
    the status field. Make sure it picks the right one
    """

    def setUp(self):
        super(TestProjectStatusUpdate, self).setUp()

        self.init_projects()

        self.theme = ProjectThemeFactory(slug='test-theme')
        self.project = ProjectFactory(theme=self.theme)
        self.task = TaskFactory(project=self.project)

        self.survey = SurveyFactory(link='https://example.com/survey/1/')

    def test_survey_url(self):
        url = urlparse(
            Survey.url(self.task)
        )
        query = parse_qs(url.query)

        self.assertEqual(url.netloc, 'example.com')
        self.assertEqual(query['theme'], [self.theme.slug])
        self.assertEqual(query['project_id'], [str(self.project.id)])
        self.assertEqual(query['task_id'], [str(self.task.id)])
        self.assertEqual(query['user_type'], ['task_member'])

    def test_survey_url_no_survey(self):
        self.survey.delete()

        self.assertIsNone(Survey.url(self.task))

    def test_survey_url_project(self):
        url = urlparse(
            Survey.url(self.project)
        )
        query = parse_qs(url.query)

        self.assertEqual(url.netloc, 'example.com')
        self.assertEqual(query['theme'], [self.theme.slug])
        self.assertEqual(query['project_id'], [str(self.project.id)])
        self.assertTrue('task_id' not in query)

    def test_survey_url_user_type(self):
        url = urlparse(
            Survey.url(self.task, user_type='initiator'),
        )
        query = parse_qs(url.query)

        self.assertEqual(url.netloc, 'example.com')
        self.assertEqual(query['theme'], [self.theme.slug])
        self.assertEqual(query['project_id'], [str(self.project.id)])
        self.assertEqual(query['task_id'], [str(self.task.id)])
        self.assertEqual(query['user_type'], ['initiator'])

    def test_survey_url_no_celebration(self):
        self.project.celebrate_results = False
        self.project.save()

        self.assertIsNone(Survey.url(self.project))
Example #31
0
class TestProjectStatusUpdate(BluebottleTestCase):
    """
    save() automatically updates some fields, specifically
    the status field. Make sure it picks the right one
    """

    def setUp(self):
        super(TestProjectStatusUpdate, self).setUp()

        self.init_projects()

        self.theme = ProjectThemeFactory(slug='test-theme')
        self.project = ProjectFactory(theme=self.theme)
        self.task = TaskFactory(project=self.project)

        self.survey = SurveyFactory(link='https://example.com/survey/1/', active=True)

    def test_survey_url(self):
        url = urlparse(
            Survey.url(self.task)
        )
        query = parse_qs(url.query)

        self.assertEqual(url.netloc, 'example.com')
        self.assertEqual(query['theme'], [self.theme.slug])
        self.assertEqual(query['project_id'], [str(self.project.id)])
        self.assertEqual(query['task_id'], [str(self.task.id)])
        self.assertEqual(query['user_type'], ['task_member'])

    def test_survey_url_no_survey(self):
        self.survey.delete()

        self.assertIsNone(Survey.url(self.task))

    def test_survey_url_project(self):
        url = urlparse(
            Survey.url(self.project)
        )
        query = parse_qs(url.query)

        self.assertEqual(url.netloc, 'example.com')
        self.assertEqual(query['theme'], [self.theme.slug])
        self.assertEqual(query['project_id'], [str(self.project.id)])
        self.assertTrue('task_id' not in query)

    def test_survey_url_user_type(self):
        url = urlparse(
            Survey.url(self.task, user_type='initiator'),
        )
        query = parse_qs(url.query)

        self.assertEqual(url.netloc, 'example.com')
        self.assertEqual(query['theme'], [self.theme.slug])
        self.assertEqual(query['project_id'], [str(self.project.id)])
        self.assertEqual(query['task_id'], [str(self.task.id)])
        self.assertEqual(query['user_type'], ['initiator'])

    def test_survey_url_no_celebration(self):
        self.project.celebrate_results = False
        self.project.save()

        self.assertIsNone(Survey.url(self.project))