Пример #1
0
 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._client_context))
Пример #2
0
 def test_init_global_as_default(self):
     var = TemplateConditionListItemNode(name="var1",
                                         value=TemplateWordNode("value1"))
     self.assertIsNotNone(var)
     self.assertEquals(var.name, "var1")
     self.assertEquals(var.value.word, "value1")
     self.assertEquals(var.var_type, TemplateConditionListItemNode.GLOBAL)
     self.assertFalse(var.loop)
     self.assertFalse(var.is_default())
     self.assertEquals("[CONDITIONLIST(var1=[WORD]value1)]",
                       var.to_string())
     self.assertEquals('<li name="var1"><value>value1</value></li>',
                       var.to_xml(self._client_context))
Пример #3
0
 def test_init_bot_with_loop(self):
     var = TemplateConditionListItemNode(
         name="var1",
         value=TemplateWordNode("value1"),
         var_type=TemplateConditionListItemNode.BOT,
         loop=True)
     self.assertIsNotNone(var)
     self.assertEquals(var.name, "var1")
     self.assertEquals(var.value.word, "value1")
     self.assertEquals(var.var_type, TemplateConditionListItemNode.BOT)
     self.assertTrue(var.loop)
     self.assertFalse(var.is_default())
     self.assertEquals("[CONDITIONLIST(var1=[WORD]value1)]",
                       var.to_string())
     self.assertEquals('<li bot="var1"><value>value1</value><loop /></li>',
                       var.to_xml(self._client_context))
Пример #4
0
    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)
Пример #5
0
    def test_type3_node(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.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("Word1", result)

        self._client_context.bot.conversation(
            self._client_context).set_property('name1', "value2")

        self._client_context.brain.properties.add_property('name3', "value3")
        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("Word3", result)
Пример #6
0
    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._client_context)
        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)
Пример #7
0
    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._client_context.brain.properties.add_property('cond1', "value2")

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("Word2", result)
Пример #8
0
    def test_type2_node_local(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateConditionNode("var1",
                                     var_type=TemplateConditionNode.LOCAL,
                                     condition_type=2)
        self.assertIsNotNone(node)
        cond1 = TemplateConditionListItemNode(
            value=TemplateWordNode("value1"),
            var_type=TemplateConditionNode.LOCAL)
        cond1.append(TemplateWordNode("Word1"))
        node.append(cond1)
        cond2 = TemplateConditionListItemNode(
            value=TemplateWordNode("value2"),
            var_type=TemplateConditionNode.LOCAL)
        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(
            self._client_context.brain.nlp.tokenizer, "Hello")
        self._client_context.bot.conversation(
            self._client_context).record_question(question)
        self._client_context.bot.conversation(
            self._client_context).current_question().set_property(
                "var1", "value2")

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("Word2", result)