Пример #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
Пример #2
0
class CreateBlockTestCase(unittest.TestCase):
    def setUp(self):
        self.client = QualtricsAPIClient('http://qualtrics.com/api',
                                         'token-456')

    def test_create_block_asserts_survey_id_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.create_block(None, 'description', 'Standard')

        with self.assertRaises(AssertionError):
            _ = self.client.create_block('', 'description', 'Standard')

        with self.assertRaises(AssertionError):
            _ = self.client.create_block(1, 'description', 'Standard')

    def test_create_block_asserts_description_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.create_block('SV_abcdefghijk',
                                         datetime(2019, 8, 16), 'Standard')

        with self.assertRaises(AssertionError):
            _ = self.client.create_block('SV_abcdefghijk', [], 'Standard')

        with self.assertRaises(AssertionError):
            _ = self.client.create_block('SV_abcdefghijk', {}, 'Standard')

    def test_create_block_asserts_block_type_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.create_block('SV_abcdefghijk', 'description',
                                         'unsupported-block-type-value')

    @responses.activate
    def test_makes_request_as_expected(self):
        create_block_json = {
            'result': {
                'BlockID': 'BL_abcdefghijk',
                'FlowID': 'FL_1'
            },
            'meta': {
                'requestId': '888a0f7d-1cf7-4eea-8b61-850edfcf409d',
                'httpStatus': '200 - OK'
            }
        }

        responses.add(
            responses.POST,
            'http://qualtrics.com/api/survey-definitions/SV_abcdefghijk/blocks',
            json=create_block_json)

        result, block_id = self.client.create_block('SV_abcdefghijk',
                                                    'My block description',
                                                    'Standard')

        self.assertEqual(result, create_block_json)
        self.assertEqual(block_id, create_block_json['result']['BlockID'])

    @responses.activate
    def test_raises_http_error_for_failed_requests(self):
        responses.add(
            responses.POST,
            'http://qualtrics.com/api/survey-definitions/SV_abcdefghijk/blocks',
            json={},
            status=404)
        with self.assertRaises(requests.HTTPError):
            _, _ = self.client.create_block('SV_abcdefghijk',
                                            'My block description', 'Standard')

        responses.replace(
            responses.POST,
            'http://qualtrics.com/api/survey-definitions/SV_abcdefghijk/blocks',
            json={},
            status=500)
        with self.assertRaises(requests.HTTPError):
            _, _ = self.client.create_block('SV_abcdefghijk',
                                            'My block description', 'Standard')