示例#1
0
class CreateSurveyTestCase(unittest.TestCase):

    def setUp(self):
        self.client = QualtricsAPIClient('http://qualtrics.com/api', 'token-456')

    def test_create_survey_asserts_survey_name_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.create_survey(None)

        with self.assertRaises(AssertionError):
            _ = self.client.create_survey('')

        with self.assertRaises(AssertionError):
            _ = self.client.create_survey(1)

    def test_create_survey_asserts_language_code_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.create_survey('Survey Name', language_code='XX')

    def test_create_survey_asserts_project_category_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.create_survey('Survey Name', project_category='ZZ')

    @responses.activate
    def test_makes_request_as_expected(self):
        create_survey_json = {
            'result': {
                "SurveyID": "SV_sUrV3Y1d",
                "DefaultBlockID": "BL_d3FaUlT8L0cK1d",
            },
            'meta': {
                'requestId': '888a0f7d-1cf7-4eea-8b61-850edfcf409d',
                'httpStatus': '200 - OK'
            }
        }

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

        survey_id, default_block_id = self.client.create_survey('My New Survey Name')

        self.assertEqual(survey_id, create_survey_json['result']['SurveyID'])
        self.assertEqual(default_block_id, create_survey_json['result']['DefaultBlockID'])

    @responses.activate
    def test_raises_http_error_for_failed_requests(self):
        responses.add(
            responses.POST, 'http://qualtrics.com/api/survey-definitions', json={}, status=404
        )
        with self.assertRaises(requests.HTTPError):
            _, _ = self.client.create_survey('400 Survey Name')

        responses.replace(
            responses.POST, 'http://qualtrics.com/api/survey-definitions', json={}, status=500
        )
        with self.assertRaises(requests.HTTPError):
            _, _ = self.client.create_survey('500 Survey Name')
示例#2
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 PublishSurveyTestCase(unittest.TestCase):
    def setUp(self):
        self.client = QualtricsAPIClient('http://qualtrics.com/api',
                                         'token-456')

    def test_publish_survey_asserts_survey_id_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.publish_survey(None, 'description')

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

        with self.assertRaises(AssertionError):
            _ = self.client.publish_survey(1.23, 'description')

    def test_publish_survey_asserts_description_parameter(self):
        with self.assertRaises(AssertionError):
            _ = self.client.publish_survey('SV_51PEzLvt33771Mp', '')

        with self.assertRaises(AssertionError):
            _ = self.client.publish_survey('SV_51PEzLvt33771Mp', {})

    def test_publish_survey_validates_survey_id(self):
        with self.assertRaises(AssertionError):
            _ = self.client.publish_survey('invalid-format-survey-id',
                                           'description')

    @responses.activate
    def test_makes_request_as_expected(self):
        publish_survey_json = {
            "meta": {
                "httpStatus": "200 - OK",
                "requestId": "7999edd9-a232-4735-ae70-1c8cc7e30e09"
            },
            "result": {
                "metadata": {
                    "surveyID":
                    "SV_51PEzLvt33771Mp",
                    "versionID":
                    "9223370492291465912",
                    "versionNumber":
                    2,
                    "description":
                    "2019 New Survey Version",
                    "userID":
                    "UR_3fIVFlGaWYcfVml",
                    "creationDate":
                    "2019-27-09T07:48:49Z",
                    "published":
                    True,
                    "wasPublished":
                    True,
                    "publishEvents": [{
                        "date": "2019-26-09T21:21:50Z",
                        "userID": "UR_3fIVFlGaWYcfVml"
                    }]
                }
            }
        }

        responses.add(
            responses.POST,
            'http://qualtrics.com/api/survey-definitions/SV_51PEzLvt33771Mp/versions',
            json=publish_survey_json)

        version_id, version_number, creation_date = self.client.publish_survey(
            'SV_51PEzLvt33771Mp', '2019 v2 of My Survey')

        self.assertEqual(
            version_id, publish_survey_json['result']['metadata']['versionID'])
        self.assertEqual(
            version_number,
            publish_survey_json['result']['metadata']['versionNumber'])
        self.assertEqual(
            creation_date,
            publish_survey_json['result']['metadata']['creationDate'])

    @responses.activate
    def test_raises_http_error_for_failed_requests(self):
        responses.add(responses.POST,
                      'http://qualtrics.com/api/survey-definitions',
                      json={},
                      status=404)
        with self.assertRaises(requests.HTTPError):
            _, _ = self.client.create_survey('400 Survey Name')

        responses.replace(responses.POST,
                          'http://qualtrics.com/api/survey-definitions',
                          json={},
                          status=500)
        with self.assertRaises(requests.HTTPError):
            _, _ = self.client.create_survey('500 Survey Name')