Пример #1
0
def create_survey(name, blocks, questions, language_code='EN'):
    '''
    Create a new survey with the specified blocks and questions

    1. create survey
    2. update default block (depends on questions supplied)
    3. create additional blocks as required (again depends on questions supplied)
    4. create questions, per question: build payloads (inc. display logic), assign to blocks, call api
    5. return survey_id
    '''
    assert blocks and questions, "You must provide lists of blocks and questions for the survey"

    base_url, auth_token = get_details_for_client()
    api = QualtricsAPIClient(base_url, auth_token)

    survey_id = default_block_id = None

    try:
        survey_id, default_block_id = api.create_survey(name, language_code)
    except (QualtricsAPIException, AssertionError, HTTPError) as ex:
        raise QualtricsAPIException(ex)

    if not survey_id:
        raise QualtricsAPIException(
            'API call create_survey failed to return survey_id')

    if not default_block_id:
        raise QualtricsAPIException(
            'API call create_survey failed to return default_block_id')

    block_ids_dict = {1: default_block_id}

    blockBar = Bar('Creating Blocks', max=len(blocks))

    for index, block in enumerate(blocks):
        blockBar.next()

        if index == 0:
            try:
                api.update_block(survey_id, default_block_id,
                                 block['description'],
                                 QUALTRICS_API_BLOCK_TYPE_DEFAULT)
            except (AssertionError, HTTPError) as ex:
                raise QualtricsAPIException(ex)
        else:
            try:
                _, new_block_id = api.create_block(
                    survey_id, block['description'],
                    QUALTRICS_API_BLOCK_TYPE_STANDARD)

                block_ids_dict[index + 1] = new_block_id
            except (AssertionError, HTTPError) as ex:
                raise QualtricsAPIException(ex)

    blockBar.finish()

    questionBar = Bar('Creating Questions', max=len(questions))

    for question in questions:
        questionBar.next()

        question_payload = api.build_question_payload(
            question, survey_id, include_display_logic=True)

        try:
            block_id = block_ids_dict[question['block_number']]
        except KeyError:
            block_id = block_ids_dict[1]

        try:
            api.create_question(survey_id, question_payload, block_id)
        except (AssertionError, HTTPError) as ex:
            raise QualtricsAPIException(ex)

    questionBar.finish()

    return survey_id
class UpdateBlockTestCase(unittest.TestCase):
    def setUp(self):
        self.client = QualtricsAPIClient('http://qualtrics.com/api',
                                         'token-456')

    def test_update_block_asserts_survey_id_parameter(self):
        with self.assertRaises(AssertionError):
            self.client.update_block(None, 'BL_1234567890a', 'description',
                                     'Standard')

        with self.assertRaises(AssertionError):
            self.client.update_block('', 'BL_1234567890a', 'description',
                                     'Standard')

        with self.assertRaises(AssertionError):
            self.client.update_block(1, 'BL_1234567890a', 'description',
                                     'Standard')

    def test_update_block_asserts_block_id_parameter(self):
        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', None, 'description',
                                     'Standard')

        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', [1, 2, 3],
                                     'description', 'Standard')

        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', 123.45, 'description',
                                     'Standard')

    def test_update_block_asserts_block_description_parameter(self):
        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', 'BL_1234567890a', '',
                                     'Standard')

        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', 'BL_1234567890a', None,
                                     'Standard')

    def test_update_block_asserts_block_type_parameter(self):
        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', 'BL_1234567890a',
                                     'description', None)

        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', 'BL_1234567890a',
                                     'description', 'Unsupported_Block_Type')

    def test_update_block_validates_survey_id(self):
        with self.assertRaises(AssertionError):
            self.client.update_block('invalid_survey_id', 'BL_1234567890a',
                                     'description', 'Standard')

    def test_update_block_validates_block_id(self):
        with self.assertRaises(AssertionError):
            self.client.update_block('SV_1234567890a', 'invalid_block_id',
                                     'description', 'Standard')

    @responses.activate
    def test_makes_request_as_expected(self):
        responses.add(
            responses.PUT,
            'http://qualtrics.com/api/survey-definitions/SV_1234567890a/blocks/BL_1234567890a'
        )

        try:
            self.client.update_block('SV_1234567890a', 'BL_1234567890a',
                                     'Block Description', 'Standard')

        except AssertionError as ae:
            self.fail(
                "update_block() raised AssertionError unexpectedly: {}".format(
                    ae))

        except Exception as ex:  # pylint: disable=broad-except
            self.fail(
                "update_block() raised Exception unexpectedly: {}".format(ex))

    @responses.activate
    def test_raises_http_error_for_failed_requests(self):
        responses.add(
            responses.PUT,
            'http://qualtrics.com/api/survey-definitions/SV_1234567890a/blocks/BL_1234567890a',
            json={},
            status=404)
        with self.assertRaises(requests.HTTPError):
            self.client.update_block(
                'SV_1234567890a', 'BL_1234567890a',
                'I\'m still, i\'m still Jenny from the...', 'Standard')

        responses.replace(
            responses.PUT,
            'http://qualtrics.com/api/survey-definitions/SV_1234567890a/blocks/BL_1234567890a',
            json={},
            status=500)
        with self.assertRaises(requests.HTTPError):
            self.client.update_block(
                'SV_1234567890a', 'BL_1234567890a',
                'Back with another one of those... rocking beats', 'Standard')