def test_survey_can_be_created_in_a_verbose_manner(self): s = Survey() s.name = "simple_survey" q = MultipleChoiceQuestion() q.name = "cow_color" q.type = "select one" q.add_choice(label="Green", name="green") s.add_child(q) expected_dict = { "name": "simple_survey", "children": [{ "name": "cow_color", "type": "select one", "children": [{ "label": "Green", "name": "green" }], }], } self.maxDiff = None self.assertEqual(s.to_json_dict(), expected_dict)
def test_survey_can_be_created_in_a_slightly_less_verbose_manner(self): option_dict_array = [ {"name": "red", "label": "Red"}, {"name": "blue", "label": "Blue"}, ] q = MultipleChoiceQuestion(name="Favorite_Color", choices=option_dict_array) q.type = "select one" s = Survey(name="Roses_are_Red", children=[q]) expected_dict = { "name": "Roses_are_Red", "children": [ { "name": "Favorite_Color", "type": "select one", "children": [ {"label": "Red", "name": "red"}, {"label": "Blue", "name": "blue"}, ], } ], } self.assertEqual(s.to_json_dict(), expected_dict)
def setUp(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name=u"age", sms_keyword=u"age", type=u"survey") question = InputQuestion(name=u"age") question.type = u"integer" question.label = u"How old are you?" survey_out.add_child(question) self.survey_out_dict = survey_out.to_json_dict() print_pyobj_to_json(self.survey_out_dict, utils.path_to_text_fixture("how_old_are_you.json"))
def setUp(self): self.this_directory = os.path.dirname(__file__) survey_out = Survey(name=u"age", type=u"survey") question = InputQuestion(name=u"age") question.type = u"integer" question.label = u"How old are you?" survey_out.add_child(question) self.survey_out_dict = survey_out.to_json_dict() print_pyobj_to_json(self.survey_out_dict, utils.path_to_text_fixture("how_old_are_you.json"))
def test_survey_can_be_created_in_a_slightly_less_verbose_manner(self): option_dict_array = [ { "name": "red", "label": "Red" }, { "name": "blue", "label": "Blue" }, ] q = MultipleChoiceQuestion(name="Favorite_Color", choices=option_dict_array) q.type = "select one" s = Survey(name="Roses_are_Red", children=[q]) expected_dict = { "name": "Roses_are_Red", "children": [{ "name": "Favorite_Color", "type": "select one", "children": [ { "label": "Red", "name": "red" }, { "label": "Blue", "name": "blue" }, ], }], } self.assertEqual(s.to_json_dict(), expected_dict)
def test_survey_can_be_created_in_a_verbose_manner(self): s = Survey() s.name = "simple_survey" q = MultipleChoiceQuestion() q.name = "cow_color" q.type = "select one" q.add_choice(label="Green", name="green") s.add_child(q) expected_dict = { "name": "simple_survey", "children": [ { "name": "cow_color", "type": "select one", "children": [{"label": "Green", "name": "green"}], } ], } self.maxDiff = None self.assertEqual(s.to_json_dict(), expected_dict)
class FloipSurvey(object): """ Converter of a FLOIP Result descriptor to Openrosa XForm. """ def __init__(self, descriptor=None, title=None, id_string=None): # Seek to begining of file if it has the seek attribute before loading # the file. if hasattr(descriptor, 'seek'): descriptor.seek(0) try: # descriptor is a file self.descriptor = json.load(descriptor) except AttributeError: try: # descriptor is a JSON string self.descriptor = json.loads(descriptor) except JSONDecodeError: # descriptor is a file path. self.descriptor = json.load( codecs.open(descriptor, encoding='utf-8')) if self.descriptor['profile'] == FLOW_RESULTS_PROFILE: del self.descriptor['profile'] self._package = Package(self.descriptor) self.descriptor = self._package.descriptor self._name = id_string or self._package.descriptor.get('name') assert self._name, "The 'name' property must be defined." title = title or self._package.descriptor.get('title') or self._name survey_dict = { constants.NAME: 'data', constants.ID_STRING: self._name, constants.TITLE: title, constants.TYPE: constants.SURVEY, } self._survey = Survey(**survey_dict) self.build() def build(self): """ Creates the survey questions for the XForm a FLOIP descriptor. """ if not self._package.resources: raise ValidationError("At least one data resource is required.") resource = self._package.resources[0] if 'schema' not in resource.descriptor: raise ValidationError("The 'schema' object is missing in resource") if 'questions' not in resource.descriptor['schema']: raise ValidationError( "The 'questions' object is missing from schema") questions = resource.descriptor['schema']['questions'] if isinstance(questions, dict): question_keys = list(questions.keys()) question_keys.sort() for name in question_keys: xform_from_floip_dict(self._survey, name, questions[name]) elif isinstance(questions, list): for question in questions: for name in question: xform_from_floip_dict(self._survey, name, question[name]) else: raise ValidationError( "Expecting 'questions' to be an object or array") meta_dict = { "name": "meta", "type": "group", "control": { "bodyless": True }, "children": [{ "name": "instanceID", "type": "calculate", "bind": { "calculate": "concat('uuid:', uuid())" } }, { "name": "contactID", "type": "string", }, { "name": "sessionID", "type": "string", }] } # yapf: disable self._survey.add_child(create_survey_element_from_dict(meta_dict)) self._survey.validate() # check that we can recreate the survey object from the survey JSON create_survey_element_from_dict(self._survey.to_json_dict()) @property def survey(self): """ Returns a pyxform `Survey` object """ return self._survey def xml(self): """ Returns a XForm XML """ return self._survey.to_xml() def survey_dict(self): """ Returns a XForm dict. """ return self._survey.to_json_dict()