def test_register_duplicate(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' group1 = Group() group1.id = 'group-1' questionnaire.add_group(group1) questionnaire.register(group1) group = questionnaire.get_item_by_id('group-1') self.assertEqual(group1, group) group2 = Group() group2.id = 'group-1' # Duplicate id self.assertRaises(QuestionnaireException, questionnaire.register, group2)
def test_get_item_by_id(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' # The order that items is added is important otherwise, # items are ot registered on the questionnaire group1 = Group() group1.id = 'group-1' questionnaire.add_group(group1) block1 = Block() block1.id = 'block-1' group1.add_block(block1) group2 = Group() group2.id = 'group-2' questionnaire.add_group(group2) block2 = Block() block2.id = 'block-2' group2.add_block(block2) self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-2') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-2') questionnaire.register(group1) questionnaire.register(group2) questionnaire.register(block1) questionnaire.register(block2) self.assertEqual(questionnaire.get_item_by_id('group-1'), group1) self.assertEqual(questionnaire.get_item_by_id('group-2'), group2) self.assertEqual(questionnaire.get_item_by_id('block-1'), block1) self.assertEqual(questionnaire.get_item_by_id('block-2'), block2)
def parse(self): """Parse the schema :returns: A questionnaire object :raises: A SchemaParserException if there is a problem while parsing the schema """ questionnaire = Questionnaire() questionnaire.id = ParserUtils.get_required_string( self._schema, "questionnaire_id") questionnaire.title = ParserUtils.get_required_string( self._schema, "title") questionnaire.survey_id = ParserUtils.get_required_string( self._schema, "survey_id") logger.debug("title: " + questionnaire.title) questionnaire.description = ParserUtils.get_optional_string( self._schema, "description") questionnaire.theme = ParserUtils.get_required_string( self._schema, "theme") questionnaire.data_version = ParserUtils.get_required_string( self._schema, "data_version") if "introduction" in self._schema.keys(): questionnaire.introduction = self._parse_introduction( self._schema['introduction']) if "groups" in self._schema.keys(): for group_schema in self._schema['groups']: questionnaire.add_group( self._parse_group(group_schema, questionnaire)) else: raise SchemaParserException( 'Questionnaire must contain at least one group') if 'messages' in self._schema.keys(): # re-use the parse validation method self._parse_validation(questionnaire, self._schema) questionnaire.register_aliases() return questionnaire
def parse(self): """Parse the schema :returns: A questionnaire object :raises: A SchemaParserException if there is a problem while parsing the schema """ questionnaire = None try: questionnaire = Questionnaire() questionnaire.id = ParserUtils.get_required_string(self._schema, "questionnaire_id") questionnaire.eq_id = ParserUtils.get_required_string(self._schema, "eq_id") logger.debug("eq_id: " + questionnaire.eq_id) questionnaire.title = ParserUtils.get_required_string(self._schema, "title") questionnaire.survey_id = ParserUtils.get_required_string(self._schema, "survey_id") logger.debug("title: " + questionnaire.title) questionnaire.description = ParserUtils.get_required_string(self._schema, "description") questionnaire.theme = ParserUtils.get_required_string(self._schema, "theme") questionnaire.submission_page = ParserUtils.get_optional_string(self._schema, "submission_page", questionnaire.submission_page) except Exception as e: logging.error('Error parsing schema') logging.info(e) raise e if questionnaire: if "introduction" in self._schema.keys(): questionnaire.introduction = self._parse_introduction(self._schema['introduction']) if "groups" in self._schema.keys(): for group_schema in self._schema['groups']: questionnaire.add_group(self._parse_group(group_schema, questionnaire)) else: raise SchemaParserException('Questionnaire must contain at least one group') if 'messages' in self._schema.keys(): # re-use the parse validation method self._parse_validation(questionnaire, self._schema) return questionnaire
def test_basics(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' group1 = Group() group1.id = 'group-1' group2 = Group() group2.id = 'group-2' questionnaire.add_group(group1) questionnaire.add_group(group2) self.assertEqual(questionnaire.id, 'some-id') self.assertEqual(questionnaire.title, 'my questionnaire object') self.assertEqual(len(questionnaire.groups), 2) self.assertEqual(questionnaire.groups[0], group1) self.assertEqual(questionnaire.groups[1], group2) self.assertEqual(group1.container, questionnaire) self.assertEqual(group2.container, questionnaire)
def test_basics(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' group1 = Group() group1.id = 'group-1' group2 = Group() group2.id = 'group-2' questionnaire.add_group(group1) questionnaire.add_group(group2) self.assertEquals(questionnaire.id, 'some-id') self.assertEquals(questionnaire.title, 'my questionnaire object') self.assertEquals(len(questionnaire.groups), 2) self.assertEquals(questionnaire.groups[0], group1) self.assertEquals(questionnaire.groups[1], group2) self.assertEquals(group1.container, questionnaire) self.assertEquals(group2.container, questionnaire)
def test_get_item_by_id(self): questionnaire = Questionnaire() questionnaire.id = 'some-id' questionnaire.title = 'my questionnaire object' # The order that items is added is important otherwise, # items are ot registered on the questionnaire group1 = Group() group1.id = 'group-1' questionnaire.add_group(group1) block1 = Block() block1.id = 'block-1' group1.add_block(block1) group2 = Group() group2.id = 'group-2' questionnaire.add_group(group2) block2 = Block() block2.id = 'block-2' group2.add_block(block2) self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'group-2') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-1') self.assertRaises(QuestionnaireException, questionnaire.get_item_by_id, 'block-2') questionnaire.register(group1) questionnaire.register(group2) questionnaire.register(block1) questionnaire.register(block2) self.assertEquals(questionnaire.get_item_by_id('group-1'), group1) self.assertEquals(questionnaire.get_item_by_id('group-2'), group2) self.assertEquals(questionnaire.get_item_by_id('block-1'), block1) self.assertEquals(questionnaire.get_item_by_id('block-2'), block2)