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_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_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_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)
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_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_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_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_init_global_as_default_no_name(self): var = TemplateConditionListItemNode(name="var1") self.assertIsNotNone(var) self.assertEqual(var.name, "var1") self.assertEqual(var.var_type, TemplateConditionListItemNode.GLOBAL) self.assertFalse(var.loop) self.assertTrue(var.is_default()) self.assertEqual("[CONDITIONLIST(var1)]", var.to_string()) self.assertEqual('<li name="var1"></li>', var.to_xml(self._client_context))
def test_init_defaults(self): var = TemplateConditionListItemNode() self.assertIsNotNone(var) self.assertIsNone(var.name) self.assertIsNone(var.value) self.assertEquals(var.var_type, TemplateConditionListItemNode.GLOBAL) self.assertFalse(var.loop) self.assertTrue(var.is_default()) self.assertEquals("[CONDITIONLIST]", var.to_string()) self.assertEquals("<li></li>", var.to_xml(self._bot, self._clientid))
def test_init_global_as_default(self): var = TemplateConditionListItemNode(name="var1", value=TemplateWordNode("value1")) self.assertIsNotNone(var) self.assertEqual(var.name, "var1") self.assertEqual(var.value.word, "value1") self.assertEqual(var.var_type, TemplateConditionListItemNode.GLOBAL) self.assertFalse(var.loop) self.assertFalse(var.is_default()) self.assertEqual("[CONDITIONLIST(var1=[WORD]value1)]", var.to_string()) self.assertEqual('<li name="var1"><value>value1</value></li>', var.to_xml(self._client_context))
def test_init_bot_with_loop(self): var = TemplateConditionListItemNode(name="var1", value=TemplateWordNode("value1"), var_type=TemplateConditionListItemNode.BOT, loop=True) self.assertIsNotNone(var) self.assertEqual(var.name, "var1") self.assertEqual(var.value.word, "value1") self.assertEqual(var.var_type, TemplateConditionListItemNode.BOT) self.assertTrue(var.loop) self.assertFalse(var.is_default()) self.assertEqual("[CONDITIONLIST(var1=[WORD]value1)]", var.to_string()) self.assertEqual('<li bot="var1"><value>value1</value><loop /></li>', var.to_xml(self._client_context))
def test_init_local(self): var = TemplateConditionListItemNode( name="var1", value=TemplateWordNode("value1"), var_type=TemplateConditionListItemNode.LOCAL) self.assertIsNotNone(var) self.assertEquals(var.name, "var1") self.assertEquals(var.value.word, "value1") self.assertEquals(var.var_type, TemplateConditionListItemNode.LOCAL) self.assertFalse(var.loop) self.assertFalse(var.is_default()) self.assertEquals("[CONDITIONLIST(var1=[WORD]value1)]", var.to_string()) self.assertEquals('<li var="var1"><value>value1</value></li>', var.to_xml(self._bot, self._clientid))
def test_init_global_with_loop(self): var = TemplateConditionListItemNode( name="var1", value=TemplateWordNode("value1"), var_type=TemplateConditionListItemNode.GLOBAL, loop=True) self.assertIsNotNone(var) self.assertEquals(var.name, "var1") self.assertEquals(var.value.word, "value1") self.assertEquals(var.var_type, TemplateConditionListItemNode.GLOBAL) self.assertTrue(var.loop) self.assertFalse(var.is_default()) self.assertEquals("[CONDITIONLIST(var1=[WORD]value1)]", var.to_string()) self.assertEquals('<li name="var1"><value>value1</value><loop /></li>', var.to_xml(self.bot, self.clientid))
def test_to_xml_unknown_without_loop(self): var = TemplateConditionListItemNode(name="var1", value=TemplateWordNode("value1"), var_type="Unknown", loop=False) self.assertEquals('<li unknown="var1"><value>value1</value></li>', var.to_xml(self._client_context))
def test_to_xml_default_with_loop(self): var = TemplateConditionListItemNode(name="var1", value=TemplateWordNode("value1"), var_type=TemplateConditionListItemNode.DEFAULT, loop=True) self.assertEquals('<li default="var1"><value>value1</value><loop /></li>', var.to_xml(self._client_context))