def test_type3_to_xml(self): root = TemplateNode() node = TemplateConditionNode(condition_type=3) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(name="name1", value=TemplateWordNode("value1"), var_type=TemplateConditionNode.GLOBAL) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(name="name2", value=TemplateWordNode("value1"), var_type=TemplateConditionNode.LOCAL ) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode(name="name3", value=TemplateWordNode("value3"), var_type=TemplateConditionNode.BOT ) cond3.append(TemplateWordNode("Word3")) node.append(cond3) cond4 = TemplateConditionListItemNode(name="name4") cond4.append(TemplateWordNode("Word4")) node.append(cond4) root.append(node) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><condition><li name="name1"><value>value1</value>Word1</li> <li var="name2"><value>value1</value>Word2</li> <li bot="name3"><value>value3</value>Word3</li> <li name="name4">Word4</li></condition></template>', xml_str)
def test_type3_node_with_loop(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = MyLoopTemplateConditionNode(condition_type=3, next_value=('name1', "value1"), next_value_step=1) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(name="name1", value=TemplateWordNode("value1"), var_type=TemplateConditionNode.GLOBAL ) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(name="name2", value=TemplateWordNode("value1"), var_type=TemplateConditionNode.LOCAL ) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode(name="name3", value=TemplateWordNode("value3"), var_type=TemplateConditionNode.BOT ) cond3.append(TemplateWordNode("Word3")) node.append(cond3) cond4 = TemplateConditionListItemNode(name="name4", loop=True) # Default Value cond4.append(TemplateWordNode("Word4")) node.append(cond4) root.append(node) self.assertEqual(len(root.children), 1) self._client_context.bot.get_conversation(self._client_context).set_property('name1', "value5") result = root.resolve(self._client_context) self.assertIsNotNone(result) self.assertEqual("Word4 Word1", result)
def test_type3_exception(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode(condition_type=3) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode(name="name1", value=TemplateWordNode("value1"), var_type=TemplateConditionNode.GLOBAL ) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode(name="name2", value=TemplateWordNode("value1"), var_type=TemplateConditionNode.LOCAL ) cond2.append(TemplateWordNode("Word2")) node.append(cond2) cond3 = TemplateConditionListItemNode(name="name3", value=TemplateWordNode("value3"), var_type=TemplateConditionNode.BOT ) cond3.append(TemplateWordNode("Word3")) node.append(cond3) cond3 = TemplateConditionListItemNode(name="name3") cond3.append(TemplateWordNode("Word3")) node.append(cond3) root.append(node) self.assertEqual(len(root.children), 1) self._client_context.bot.get_conversation(self._client_context).set_property('name1', "value1") self._client_context.brain.properties.add_property('name3', "value3") result = root.resolve(self._client_context) self.assertIsNotNone(result) self.assertEqual("", result)
def test_type3_to_xml(self): root = TemplateNode() node = TemplateConditionNode(type=3) 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) 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><li name="name1"><value>value1</value>Word1</li> <li var="name2"><value>value1</value>Word2</li> <li name="name3">Word3</li></condition></template>', xml_str)
def test_type3_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode(type=3) 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_type2_to_xml_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("var1", local=True, type=2) 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_type2_node_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("var1", local=True, type=2) 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_type2_to_xml_bot(self): root = TemplateNode() node = TemplateConditionNode("cond1", var_type=TemplateConditionNode.BOT, condition_type=2) 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 bot="cond1"><li><value>value1</value>Word1</li> <li><value>value2</value>Word2</li> <li>Word3</li></condition></template>', xml_str)
def test_type2_node_bot(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("cond1", var_type=TemplateConditionNode.BOT, condition_type=2) self.assertIsNotNone(node) cond1 = TemplateConditionListItemNode( value=TemplateWordNode("value1"), var_type=TemplateConditionNode.BOT) cond1.append(TemplateWordNode("Word1")) node.append(cond1) cond2 = TemplateConditionListItemNode( value=TemplateWordNode("value2"), var_type=TemplateConditionNode.BOT) 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.brain.properties.add_property('cond1', "value2") result = root.resolve(self._bot, self._clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)
def test_type2_node_global(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("cond1", condition_type=2) 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).set_property('cond1', "value2") question = Question.create_from_text(self._bot.brain.tokenizer, "Hello") self._bot.conversation(self._clientid).record_dialog(question) self._bot.conversation(self._clientid).current_question().set_property( "cond1", "value2") result = root.resolve(self._bot, self._clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)