def test_global_set_allow_overrides_with_default(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) self.bot._configuration.override_propertys = True self.bot.brain.properties.pairs.append(["name", "fred"]) node = TemplateSetNode() self.assertIsNotNone(node) node.name = TemplateWordNode("name") node.local = False node.append(TemplateWordNode("keith")) root.append(node) self.assertEqual(len(root.children), 1) conversation = self.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", conversation.property("name"))
def test_global_set_allow_overrides_with_default(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) self.bot._configuration.override_predicates = True self.bot.brain.properties.pairs.append(["name", "fred"]) node = TemplateSetNode() self.assertIsNotNone(node) node.name = TemplateWordNode("name") node.local = False node.append(TemplateWordNode("keith")) root.append(node) self.assertEqual(len(root.children), 1) conversation = self.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", conversation.predicate("name"))
def test_local_set(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateSetNode() self.assertIsNotNone(node) node.name = TemplateWordNode("name") node.local = True node.append(TemplateWordNode("keith")) root.append(node) self.assertEqual(len(root.children), 1) conversation = self.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", question.property("name"))
def test_local_set(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateSetNode() self.assertIsNotNone(node) node.name = TemplateWordNode("name") node.local = True node.append(TemplateWordNode("keith")) root.append(node) self.assertEqual(len(root.children), 1) conversation = self.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", question.predicate("name"))
def test_to_xml_local_set(self): root = TemplateNode() node = TemplateSetNode() node.name = TemplateWordNode("name") node.local = True root.append(node) node.append(TemplateWordNode("keith")) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><set var="name">keith</set></template>', xml_str)
def test_to_xml_local_set(self): root = TemplateNode() node = TemplateSetNode() node.name = TemplateWordNode("name") node.local = True root.append(node) node.append(TemplateWordNode("keith")) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><set var="name">keith</set></template>', xml_str)
def test_typedata_set(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateSetNode() self.assertIsNotNone(node) node.name = TemplateWordNode("name") node.property_type = "data" node.append(TemplateWordNode("keith")) root.append(node) self.assertEqual(len(root.children), 1) conversation = self._client_context.bot.get_conversation( self._client_context) self.assertIsNotNone(conversation) result = node.resolve(self._client_context) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", conversation.data_property("name"))
def parse_set_expression(self, expression, branch): set_node = TemplateSetNode() branch.children.append(set_node) name_found = False var_found = False if 'name' in expression.attrib: node = TemplateNode() name_node = TemplateWordNode(expression.attrib['name']) node.append(name_node) set_node.local = False name_found = True set_node.name = node if 'var' in expression.attrib: node = TemplateNode() name_node = TemplateWordNode(expression.attrib['var']) node.append(name_node) set_node.local = True var_found = True set_node.name = node self.parse_text(self.get_text_from_element(expression), set_node) for child in expression: if child.tag == 'name': node = TemplateNode() self.parse_text(self.get_text_from_element(child), node) for sub_child in child: self.parse_tag_expression(sub_child, node) self.parse_text(self.get_text_from_element(child), node) set_node.name = node set_node.local = False name_found = True elif child.tag == 'var': node = TemplateNode() self.parse_text(self.get_text_from_element(child), node) for sub_child in child: self.parse_tag_expression(sub_child, node) self.parse_text(self.get_text_from_element(child), node) set_node.name = node set_node.local = True var_found = True else: self.parse_tag_expression(child, set_node) self.parse_text(self.get_tail_from_element(child), set_node) if name_found is True and var_found is True: raise ParserException("Error, set node has both name AND var values", xml_element=expression) if name_found is False and var_found is False: raise ParserException("Error, set node has both name AND var values", xml_element=expression)