def test_evaluation_algorithm_survey_parent(self): from euphorie.content.survey import Survey view = self.EvaluationView() survey = Survey() survey.evaluation_algorithm = u"dummy" risk = self.Risk().__of__(survey) self.assertEqual(view.evaluation_algorithm(risk), u"dummy")
def test_update_with_module(self): survey = Survey() child = Mock(id="child", title="Child") alsoProvides(child, IModule) survey._setObject("child", child, suppress_events=True) view = SurveyView(survey, self._request()) view._morph = mock.Mock(return_value="info") self.assertEqual(view.modules_and_profile_questions, ["info"])
def addSurvey(self, country, language, title): from euphorie.content.survey import Survey from euphorie.client.sector import ClientSector if "sector" not in country: country["sector"] = ClientSector() country["sector"].title = u"Test sector" country["sector"].id = "sector" sector = country["sector"] survey = Survey() survey.title = title survey.id = language survey.language = language sector[language] = survey return survey
def testSurvey_IntroductionNoText(self): from euphorie.content.surveygroup import SurveyGroup from euphorie.content.survey import Survey surveygroup = SurveyGroup() surveygroup.title = u"Generic sector" surveygroup["standard"] = Survey() survey = surveygroup["standard"] # Acquisition wrap survey.title = u"Standard" survey.introduction = u"<p><br/></p>" survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" root = self.root() view = ExportSurvey(None, None) view.exportSurvey(root, survey) self.assertEqual( etree.tostring(root, pretty_print=True), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <survey>\n' ' <title>Generic sector</title>\n' ' <language>en-GB</language>\n' ' <evaluation-algorithm>kinney</evaluation-algorithm>\n' ' <evaluation-optional>false</evaluation-optional>\n' ' </survey>\n' '</root>\n')
def testRender(self): from euphorie.content.surveygroup import SurveyGroup from euphorie.content.survey import Survey from zope.publisher.browser import TestRequest surveygroup = SurveyGroup() surveygroup.id = "mysector" surveygroup.title = u"Generic sector" surveygroup["standard"] = Survey() survey = surveygroup["standard"] # Acquisition wrap survey.id = "dummy" survey.title = u"Standard" survey.introduction = None survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" view = ExportSurvey(survey, TestRequest()) output = view() response = view.request.response self.assertEqual(response.getHeader("Content-Type"), "text/xml") self.assertEqual(response.getHeader("Content-Disposition"), 'attachment; filename="mysector.xml"') self.assertEqual( output, '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n' '<sector xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <survey>\n' ' <title>Generic sector</title>\n' ' <language>en-GB</language>\n' ' <evaluation-algorithm>kinney</evaluation-algorithm>\n' ' <evaluation-optional>false</evaluation-optional>\n' ' </survey>\n' '</sector>\n')
def test_update_no_children(self): from euphorie.content.survey import Survey from euphorie.content.survey import View survey = Survey() view = View(survey, self._request()) view.update() self.assertEqual(view.children, [])
def testSurvey_IntroductionNoText(self): surveygroup = SurveyGroup() surveygroup.title = "Generic sector" surveygroup._setOb("standard", Survey()) survey = surveygroup["standard"] # Acquisition wrap survey.title = "Standard" survey.introduction = "<p><br/></p>" survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" root = self.root() view = ExportSurvey(None, None) view.exportSurvey(root, survey) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <survey>\n" " <title>Generic sector</title>\n" " <language>en-GB</language>\n" " <tool_type>classic</tool_type>\n" " <measures_text_handling>full</measures_text_handling>\n" " <integrated_action_plan>false</integrated_action_plan>\n" " <evaluation-algorithm>kinney</evaluation-algorithm>\n" " <evaluation-optional>false</evaluation-optional>\n" " </survey>\n" "</root>\n", )
def setUp(self): super(TestURLs, self).setUp() self.loginAsPortalOwner() self.client = self.portal.client # Set locals request = testRequest() locals.request = request # Add survey self.client['en'] = ClientCountry('en') country = self.client['en'] country["sector"] = ClientSector('sector') country["sector"].title = u"Test sector" country["sector"].id = "sector" sector = country["sector"] survey = Survey('survey') survey.title = u'Test Survey' survey.language = 'en' sector['survey'] = survey
def test_update_other_child(self): from euphorie.content.survey import Survey from euphorie.content.survey import View survey = Survey() view = View(survey, self._request()) child = Mock(id='child', title=u'Child') survey['child'] = child view.update() self.assertEqual(view.children, [])
def setUp(self): super(TestURLs, self).setUp() # Set locals request = testRequest() locals.request = request self.loginAsPortalOwner() self.client = self.portal.client # Add survey self.client._setOb("en", ClientCountry("en")) country = self.client["en"] country._setOb("sector", ClientSector("sector")) country["sector"].title = "Test sector" country["sector"].id = "sector" sector = country["sector"] survey = Survey("survey") survey.title = "Test Survey" survey.introduction = "This is a survey that is well suited for tests" survey.language = "en" sector._setOb("survey", survey)
def setUp(self): super(TestURLs, self).setUp() self.loginAsPortalOwner() self.client = self.portal.client # Set locals request = testRequest() locals.request = request # Add survey self.client['en'] = ClientCountry('en') country = self.client['en'] country["sector"] = ClientSector('sector') country["sector"].title = u"Test sector" country["sector"].id = "sector" sector = country["sector"] survey = Survey('survey') survey.title = u'Test Survey' survey.introduction = u"This is a survey that is well suited for tests" survey.language = 'en' sector['survey'] = survey
def test_get_no_profile(self): import mock from euphorie.content.survey import Survey survey = Survey(id='5') view = self.View(survey, None) view.survey = mock.Mock(return_value=survey) with mock.patch('euphorie.client.api.profile.extractProfile') \ as mock_extractProfile: mock_extractProfile.return_value = {} response = view.do_GET() self.assertEqual(response['profile'], [])
def test_survey_info_filter_by_language(self): from zope.publisher.browser import TestRequest from euphorie.content.survey import Survey from euphorie.client.sector import ClientSector sector = ClientSector(id='ict', title=u'ICT') sector['gaming'] = Survey(id='gaming', title=u'Gaming', language='nl') request = TestRequest() request.form['language'] = 'en' response = self.View(sector, request).do_GET() self.assertEqual(len(response['surveys']), 0) request.form['language'] = 'nl' response = self.View(sector, request).do_GET() self.assertEqual(len(response['surveys']), 1)
def test_update_with_module(self): import mock from zope.interface import alsoProvides from euphorie.content.survey import Survey from euphorie.content.module import IModule from euphorie.content.survey import View survey = Survey() child = Mock(id="child", title=u"Child") alsoProvides(child, IModule) survey['child'] = child view = View(survey, self._request()) view._morph = mock.Mock(return_value='info') view.update() self.assertEqual(view.children, ['info'])
def test_survey_info(self): from zope.publisher.browser import TestRequest from euphorie.content.survey import Survey from euphorie.client.sector import ClientSector sector = ClientSector(id='ict', title=u'ICT') sector['gaming'] = Survey(id='gaming', title=u'Gaming', language='nl') response = self.View(sector, TestRequest()).do_GET() self.assertEqual(len(response['surveys']), 1) survey_info = response['surveys'][0] self.assertTrue(isinstance(survey_info, dict)) self.assertEqual(set(survey_info), set(['id', 'title', 'language'])) self.assertEqual(survey_info['id'], 'gaming') self.assertEqual(survey_info['title'], u'Gaming') self.assertEqual(survey_info['language'], 'nl')
def test_put_too_much_data(self): import mock from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession from euphorie.content.survey import Survey account = addAccount(password='******') survey_session = SurveySession(title=u'Dummy session', zodb_path='nl/ict/software-development', account=account) view = self.View(survey_session, None) view.input = {'5': True} survey = Survey(id='survey') view.survey = mock.Mock(return_value=survey) response = view.do_PUT() self.assertEqual(response['type'], 'error')
def testRenderZip(self): surveygroup = SurveyGroup() surveygroup.id = "mysector" surveygroup.title = "Generic sector" surveygroup._setOb("standard", Survey()) survey = surveygroup["standard"] # Acquisition wrap survey.id = "dummy" survey.title = "Standard" survey.introduction = "Wie grün sind deine Blätter?" survey.classification_code = None survey.evaluation_optional = False survey.integrated_action_plan = True survey.language = "en-GB" survey.tool_type = "existing_measures" view = ExportSurvey(survey, TestRequest()) view.export_as_plain_text = True output = view.render_output() zip_file = ZipFile(BytesIO(output), mode="r") response = view.request.response self.assertEqual(response.getHeader("Content-Type"), "application/zip") self.assertEqual( response.getHeader("Content-Disposition"), 'attachment; filename="mysector.zip"', ) self.assertEqual(zip_file.namelist(), ["mysector_word_count.txt", "mysector.xml"]) with zip_file.open("mysector_word_count.txt") as txt_file: txt = txt_file.read().decode("utf-8").strip() self.assertEqual(txt, "Generic sector Wie grün sind deine Blätter?") with zip_file.open("mysector.xml") as xml_file: xml = xml_file.read().decode("utf-8") self.assertEqual( xml, "<?xml version='1.0' encoding='utf-8'?>\n" '<sector xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <survey>\n" " <title>Generic sector</title>\n" " <introduction>Wie grün sind deine Blätter?</introduction>\n" " <language>en-GB</language>\n" " <tool_type>existing_measures</tool_type>\n" " <measures_text_handling>full</measures_text_handling>\n" " <integrated_action_plan>true</integrated_action_plan>\n" " <evaluation-algorithm>kinney</evaluation-algorithm>\n" " <evaluation-optional>false</evaluation-optional>\n" " </survey>\n" "</sector>\n", )
def test_put_not_all_questions_answered(self): import mock from euphorie.content.survey import Survey from euphorie.content.profilequestion import ProfileQuestion from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession account = addAccount(password='******') survey_session = SurveySession(title=u'Dummy session', zodb_path='nl/ict/software-development', account=account) view = self.View(survey_session, None) view.input = {} self.portal.survey = Survey(id='survey') survey = self.portal.survey survey['5'] = ProfileQuestion(id='5', question=u'Locations') view.survey = mock.Mock(return_value=survey) response = view.do_PUT() self.assertEqual(response['type'], 'error')
def test_get_with_profile(self): import mock from euphorie.content.survey import Survey from euphorie.content.profilequestion import ProfileQuestion self.portal.survey = Survey(id='survey') survey = self.portal.survey survey['5'] = ProfileQuestion(id='5', question=u'Locations') view = self.View(survey, None) view.survey = mock.Mock(return_value=survey) with mock.patch('euphorie.client.api.profile.extractProfile') \ as mock_extractProfile: mock_extractProfile.return_value = {'5': [u'London', u'Tokyo']} response = view.do_GET() profile = response['profile'] self.assertEqual(profile, [{ 'id': '5', 'question': u'Locations', 'value': [u'London', u'Tokyo'] }])
def test_put_no_profile(self): import mock from z3c.saconfig import Session from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession from euphorie.content.survey import Survey account = addAccount(password='******') survey_session = SurveySession(title=u'Dummy session', zodb_path='nl/ict/software-development', account=account) view = self.View(survey_session, None) view.input = {} Session.add(survey_session) survey = Survey(id='survey') view.survey = mock.Mock(return_value=survey) with mock.patch('euphorie.client.api.profile.extractProfile') \ as mock_extractProfile: mock_extractProfile.return_value = {} response = view.do_PUT() self.assertEqual(response['profile'], [])
def testSurvey_WithProfileQuestion(self): surveygroup = SurveyGroup() surveygroup.title = "Generic sector" surveygroup._setOb("standard", Survey()) survey = surveygroup["standard"] # Acquisition wrap survey.title = "Generic sector" survey.introduction = None survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" profile = ProfileQuestion() profile.title = "Office buildings" profile.question = "Do you have an office buildings?" profile.description = "<p>Owning property brings risks.</p>" profile.type = "optional" survey._setOb("1", profile) root = self.root() view = ExportSurvey(None, None) view.exportSurvey(root, survey) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <survey>\n" " <title>Generic sector</title>\n" " <language>en-GB</language>\n" " <tool_type>classic</tool_type>\n" " <measures_text_handling>full</measures_text_handling>\n" " <integrated_action_plan>false</integrated_action_plan>\n" " <evaluation-algorithm>kinney</evaluation-algorithm>\n" " <evaluation-optional>false</evaluation-optional>\n" " <profile-question>\n" " <title>Office buildings</title>\n" " <question>Do you have an office buildings?</question>\n" " <description><p>Owning property brings " "risks.</p></description>\n" " <use-location-question>true</use-location-question>\n" " </profile-question>\n" " </survey>\n" "</root>\n", )
def testSurvey_WithProfileQuestion(self): from euphorie.content.surveygroup import SurveyGroup from euphorie.content.survey import Survey from euphorie.content.profilequestion import ProfileQuestion surveygroup = SurveyGroup() surveygroup.title = u"Generic sector" surveygroup["standard"] = Survey() survey = surveygroup["standard"] # Acquisition wrap survey.title = u"Generic sector" survey.introduction = None survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" profile = ProfileQuestion() profile.title = u"Office buildings" profile.question = u"Do you have an office buildings?" profile.description = u"<p>Owning property brings risks.</p>" profile.type = "optional" survey["1"] = profile root = self.root() view = ExportSurvey(None, None) view.exportSurvey(root, survey) self.assertEqual( etree.tostring(root, pretty_print=True), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <survey>\n' ' <title>Generic sector</title>\n' ' <language>en-GB</language>\n' ' <evaluation-algorithm>kinney</evaluation-algorithm>\n' ' <evaluation-optional>false</evaluation-optional>\n' ' <profile-question>\n' ' <title>Office buildings</title>\n' ' <question>Do you have an office buildings?</question>\n' ' <description><p>Owning property brings ' 'risks.</p></description>\n' ' <use-location-question>true</use-location-question>\n' ' </profile-question>\n' ' </survey>\n' '</root>\n')
def testSurvey_WithModule(self): from euphorie.content.surveygroup import SurveyGroup from euphorie.content.survey import Survey from euphorie.content.module import Module surveygroup = SurveyGroup() surveygroup.title = u"Generic sector" surveygroup["standard"] = Survey() survey = surveygroup["standard"] # Acquisition wrap survey.title = u"Generic sector" survey.introduction = None survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" module = Module() module.title = u"Office buildings" module.description = u"<p>Owning property brings risks.</p>" module.optional = False module.solution_direction = None survey["1"] = module root = self.root() view = ExportSurvey(None, None) view.exportSurvey(root, survey) self.assertEqual( etree.tostring(root, pretty_print=True), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <survey>\n' ' <title>Generic sector</title>\n' ' <language>en-GB</language>\n' ' <evaluation-algorithm>kinney</evaluation-algorithm>\n' ' <evaluation-optional>false</evaluation-optional>\n' ' <module optional="false">\n' ' <title>Office buildings</title>\n' ' <description><p>Owning property brings ' 'risks.</p></description>\n' ' </module>\n' ' </survey>\n' '</root>\n')
def testRender(self): surveygroup = SurveyGroup() surveygroup.id = "mysector" surveygroup.title = "Generic sector" surveygroup._setOb("standard", Survey()) survey = surveygroup["standard"] # Acquisition wrap survey.id = "dummy" survey.title = "Standard" survey.introduction = None survey.classification_code = None survey.evaluation_optional = False survey.integrated_action_plan = True survey.language = "en-GB" survey.tool_type = "existing_measures" view = ExportSurvey(survey, TestRequest()) output = view.render_output().decode("utf-8") response = view.request.response self.assertEqual(response.getHeader("Content-Type"), "text/xml") self.assertEqual( response.getHeader("Content-Disposition"), 'attachment; filename="mysector.xml"', ) self.assertEqual( output, "<?xml version='1.0' encoding='utf-8'?>\n" '<sector xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <survey>\n" " <title>Generic sector</title>\n" " <language>en-GB</language>\n" " <tool_type>existing_measures</tool_type>\n" " <measures_text_handling>full</measures_text_handling>\n" " <integrated_action_plan>true</integrated_action_plan>\n" " <evaluation-algorithm>kinney</evaluation-algorithm>\n" " <evaluation-optional>false</evaluation-optional>\n" " </survey>\n" "</sector>\n", )
def test_evaluation_algorithm_survey_parent(self): view = self.EvaluationView() survey = Survey() survey.evaluation_algorithm = "dummy" risk = self.Risk().__of__(survey) self.assertEqual(view.evaluation_algorithm(risk), "dummy")
def test_update_no_children(self): survey = Survey() view = SurveyView(survey, self._request()) self.assertEqual(view.modules_and_profile_questions, [])
def test_update_other_child(self): survey = Survey() view = SurveyView(survey, self._request()) child = Mock(id="child", title="Child") survey._setObject("child", child, suppress_events=True) self.assertEqual(view.modules_and_profile_questions, [])