def test_to_xml_local(self): root = TemplateNode() node = TemplateConditionListItemNode("name1", value=TemplateWordNode("value1"), local=True) node.append(TemplateWordNode("Hello")) root.append(node) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><li var="name1"><value>value1</value>Hello</li></template>', xml_str)
def test_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType3ConditionNode() self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(name="name1", value=TemplateWordNode("value1") ) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(name="name2", value=TemplateWordNode("value1"), local=True ) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode(name="name3") cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) self.assertEqual(len(root.children), 1) self.bot.conversation(self.clientid)._predicates['name1'] = "value1" result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("Word1", result)
def test_node_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType2ConditionNode("var1", local=True) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1")) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2")) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode() cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) self.assertEqual(len(root.children), 1) question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_predicate("var1", "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)
def test_get_default(self): node = TemplateConditionNodeWithChildren() self.assertIsNotNone(node) node.append(TemplateConditionListItemNode("cond1", "value1")) node.append(TemplateConditionListItemNode("cond2", "value2")) node.append(TemplateConditionListItemNode("cond3")) self.assertEqual(3, len(node.children)) default = node.get_default() self.assertIsNotNone(default) self.assertEqual(default.name, "cond3")
def test_to_xml_global(self): root = TemplateNode() node = TemplateConditionListItemNode("name1", value=TemplateWordNode("value1")) node.append(TemplateWordNode("Hello")) root.append(node) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual( '<template><li name="name1"><value>value1</value>Hello</li></template>', xml_str)
def parse_type2_condition(self, expression, branch): response = self.get_condition_name(expression) name = response[0] local = response[1] condition = TemplateType2ConditionNode(name=name, local=local) branch.append(condition) for child in expression: if child.tag == 'name': # Pass on this attribute as we have already pulled it from the get_condition_name above pass elif child.tag == 'var': # Pass on this attribute as we have already pulled it from the get_condition_name above pass elif child.tag == 'li': list_item = TemplateConditionListItemNode() list_item.value = self.get_condition_value(child, False) list_item.local = condition.local condition.children.append(list_item) self.parse_text(self.get_text_from_element(child), list_item) for sub_pattern in child: if sub_pattern.tag == 'name': pass elif sub_pattern.tag == 'var': pass elif sub_pattern.tag == 'value': pass elif sub_pattern.tag == 'loop': list_item.loop = True else: self.parse_tag_expression(sub_pattern, list_item) tail_text = self.get_tail_from_element(sub_pattern) self.parse_text(tail_text, list_item) else: raise ParserException("Error, invalid element <%s> in condition element" % (child.tag), xml_element=expression)
def test_node_value(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionListItemNode("item1", value="value1") self.assertIsNotNone(node) self.assertEqual(node.name, "item1") self.assertEqual(node.value, "value1") self.assertFalse(node.local) self.assertFalse(node.loop) self.assertFalse(node.is_default()) root.append(node) self.assertEqual(len(root.children), 1)
def parse_type3_condition(self, expression, branch): condition = TemplateType3ConditionNode() branch.append(condition) for child in expression: if child.tag == 'name': pass elif child.tag == 'var': pass elif child.tag == 'li': list_item = TemplateConditionListItemNode() response = self.get_condition_name(child, False) list_item.name = response[0] list_item.local = response[1] list_item.value = self.get_condition_value(child, False) condition.append(list_item) self.parse_text(self.get_text_from_element(child), list_item) for sub_pattern in child: if sub_pattern.tag == 'name': pass elif sub_pattern.tag == 'var': pass elif sub_pattern.tag == 'value': pass elif sub_pattern.tag == 'loop': list_item.loop = True else: self.parse_tag_expression(sub_pattern, list_item) tail_text = self.get_tail_from_element(sub_pattern) self.parse_text(tail_text, list_item) else: raise ParserException("Error, invalid element <%s> in condition element" % (child.tag), xml_element=expression)
def test_to_xml_global(self): root = TemplateNode() node = TemplateType2ConditionNode("cond1") self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1")) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2")) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode() cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><condition name="cond1"><li><value>value1</value>Word1</li><li><value>value2</value>Word2</li><li>Word3</li></condition></template>', xml_str)
def test_to_xml_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType2ConditionNode("var1", local=True) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1")) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2")) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode() cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual( '<template><condition var="var1"><li><value>value1</value>Word1</li><li><value>value2</value>Word2</li><li>Word3</li></condition></template>', xml_str)
def test_node_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType2ConditionNode("var1", local=True) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1")) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2")) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode() cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) self.assertEqual(len(root.children), 1) question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_predicate( "var1", "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)
def test_node_global(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType2ConditionNode("cond1") self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1")) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2")) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode() cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) self.assertEqual(len(root.children), 1) self.bot.conversation(self.clientid)._predicates['cond1'] = "value2" result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)