def __init__(self, date_format="%c", style="days"): TemplateNode.__init__(self) self._interval_from = None self._interval_to = None if isinstance(date_format, str): self._format = TemplateWordNode(date_format) else: self._format = date_format if isinstance(style, str): self._style = TemplateWordNode(style) else: self._style = style
def test_node_no_default_get(self): self._client_context.brain.configuration.defaults._default_get = None root = TemplateNode() node = TemplateBotNode() node.name = TemplateWordNode("location") root.append(node) result = node.resolve(self._client_context) self.assertIsNotNone(result) self.assertEquals("unknown", result)
def test_init_local_with_loop(self): var = TemplateConditionVariable( name="var1", value=TemplateWordNode("value1"), var_type=TemplateConditionVariable.LOCAL, loop=True) self.assertIsNotNone(var) self.assertEquals(var.name, "var1") self.assertEquals(var.value.word, "value1") self.assertEquals(var.var_type, TemplateConditionVariable.LOCAL) self.assertTrue(var.loop)
def test_type1_node_bot_nomatch(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("name1", TemplateWordNode("value1"), var_type=TemplateConditionNode.BOT) self.assertIsNotNone(node) node.append(TemplateWordNode("Hello")) root.append(node) self.assertEqual(len(root.children), 1) self.bot.brain.properties.add_property('name1', "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual(result, "")
def test_type1_node_global_nomatch(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("name1", TemplateWordNode("value1"), var_type=TemplateConditionNode.GLOBAL) self.assertIsNotNone(node) node.append(TemplateWordNode("Hello")) root.append(node) self.assertEqual(len(root.children), 1) self.bot.conversation(self.clientid).set_property('name1', "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual(result, "")
def test_to_xml(self): root = TemplateNode() node = TemplateNormalizeNode() root.append(node) node.append(TemplateWordNode("Test")) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual("<template><normalize>Test</normalize></template>", xml_str)
def test_execute_single_var(self): bot = TestBot() bot.brain.rdf.load_from_text(""" ACCOUNT:hasPurpose:to track money ACCOUNT:hasSize:0 ACCOUNT:hasSyllables:2 ACCOUNT:isa:Concept ACCOUNT:lifeArea:Finances ACT:hasPurpose:to entertain by performing """) query = RDFQuery(rdf_subject=TemplateWordNode("ACCOUNT"), rdf_predicate=TemplateWordNode("hasSize"), rdf_object=TemplateWordNode("?x")) uniq = RDFUniqueStatement(query) result = uniq.execute(bot, "testid") self.assertIsNotNone(result) self.assertEquals(result, [['0']])
def test_type1_node_local_nomatch(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("var1", TemplateWordNode("value1"), var_type=TemplateConditionNode.LOCAL) self.assertIsNotNone(node) node.append(TemplateWordNode("Hello")) root.append(node) self.assertEqual(len(root.children), 1) question = Question.create_from_text(self._client_context, "Hello") self._client_context.bot.get_conversation(self._client_context).record_dialog(question) self._client_context.bot.get_conversation(self._client_context).current_question().set_property("var1", "value2") result = root.resolve(self._client_context) self.assertIsNotNone(result) self.assertEqual(result, "")
def test_to_xml(self): root = TemplateNode() node = TemplateSearchNode() root.append(node) node.append(TemplateWordNode("programy")) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual("<template><search>programy</search></template>", xml_str)
def test_nluslot_no_item(self): root = TemplateNode() node = TemplateNluSlotNode() node._slotName = TemplateWordNode("nlu_slot") node._itemName = TemplateWordNode("startOffset") root.append(node) conversation = self._client_context.bot.get_conversation( self._client_context) question = Question.create_from_text( self._client_context, "Hello", self._client_context.bot.sentence_splitter) conversation.record_dialog(question) nlu_result = '{"intents": [], "slots": [{"slot": "nlu_slot", "entity": "value"}]}' conversation.current_question().set_property("__SYSTEM_NLUDATA__", nlu_result) result = root.resolve(self._client_context) self.assertIsNotNone(result) self.assertEqual("unknown", result)
def test_call_no_CustomService_exists(self): root = TemplateNode() node = TemplateSRAIXNode() node.service = "mock1" root.append(node) node.append(TemplateWordNode("Hello")) with self.assertRaises(Exception): node.resolve(self._client_context)
def test_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node1 = TemplateWordNode("Hello") root.append(node1) node2 = TemplateWordNode("World!") root.append(node2) self.assertEqual(len(root.children), 2) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEqual(resolved, "Hello World!") node2.word = "Again!" resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEqual(resolved, "Hello Again!")
def test_to_xml(self): root = TemplateNode() node = TemplateImplodeNode() root.append(node) node.append(TemplateWordNode("Test")) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual("<template><implode>Test</implode></template>", xml_str)
def test_to_xml_typevar_get(self): root = TemplateNode() node = TemplateGetNode() node.name = TemplateWordNode("name") node.property_type = "var" 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><get var="name" /></template>', xml_str)
def test_to_xml_local_get(self): root = TemplateNode() node = TemplateGetNode() node.name = TemplateWordNode("name") node.local = True 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><get var="name" /></template>', xml_str)
def test_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateMapNode() self.assertEqual("", node.resolve_children(self.bot, self.clientid)) node.name = TemplateWordNode("COLOURS") node.append(TemplateWordNode("BLACK")) root.append(node) self.assertEqual(len(root.children), 1) self.bot.brain.maps._maps['COLOURS'] = {'BLACK': 'WHITE'} result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("WHITE", result)
def test_node_ymdhms(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("ymdhms") node.interval_from = TemplateWordNode("Thu Jul 14 16:33:09 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "2 years, 2 months, 23 days, 0 hours, 2 minutes, 2 seconds")
def test_node_weeks(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("weeks") node.interval_from = TemplateWordNode("Thu Oct 1 16:35:11 2016") node.interval_to = TemplateWordNode("Fri Oct 14 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "1")
def test_to_xml_defaults(self): root = TemplateNode() node = TemplateDateNode() root.append(node) node.append(TemplateWordNode("Mon Sep 30 07:06:05 2013")) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual( '<template><date format="%c">Mon Sep 30 07:06:05 2013</date></template>', xml_str)
def test_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node1 = TemplateWordNode("Hello") root.append(node1) node2 =TemplateWordNode("World!") root.append(node2) self.assertEqual(len(root.children), 2) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEqual(resolved, "Hello World!") node2.word = "Again!" resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEqual(resolved, "Hello Again!")
def test_node_seconds(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("seconds") node.interval_from = TemplateWordNode("Thu Oct 7 16:35:09 2016") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._bot, self._clientid) self.assertIsNotNone(response) self.assertEqual(response, "2")
def test_node_unknown(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("unknown") node.interval_from = TemplateWordNode("Thu Jul 14 16:33:09 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self.bot, self.clientid) self.assertIsNotNone(response) self.assertEqual(response, "")
def test_url_postback_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) button = TemplateButtonNode() button._text = TemplateWordNode("SAY HELLO") button._postback = TemplateWordNode("HELLO") root.append(button) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEquals( "<button><text>SAY HELLO</text><postback>HELLO</postback></button>", resolved) self.assertEquals( "<button><text>SAY HELLO</text><postback>HELLO</postback></button>", root.to_xml(self._client_context))
def test_url_button_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) button = TemplateButtonNode() button._text = TemplateWordNode("Servusai.com") button._url = TemplateWordNode("http://Servusai.com") root.append(button) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEquals( "<button><text>Servusai.com</text><url>http://Servusai.com</url></button>", resolved) self.assertEquals( "<button><text>Servusai.com</text><url>http://Servusai.com</url></button>", root.to_xml(self._client_context))
def test_to_xml_with_attribs(self): root = TemplateNode() xml = TemplateXMLNode() xml._name = "dial" xml._attribs['leave_message'] = "true" root.append(xml) xml.append(TemplateWordNode("07777777777")) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><dial leave_message="true">07777777777</dial></template>', xml_str)
def test_text_postback__replynode(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) reply = TemplateReplyNode() reply._text = TemplateWordNode("SAY HELLO") reply._postback = TemplateWordNode("HELLO") root.append(reply) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEquals( "<reply><text>SAY HELLO</text><postback>HELLO</postback></reply>", resolved) self.assertEquals( "<reply><text>SAY HELLO</text><postback>HELLO</postback></reply>", root.to_xml(self._client_context))
def test_olist_node(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) list = TemplateOrderedListNode() list._items.append(TemplateWordNode("Item1")) list._items.append(TemplateWordNode("Item2")) root.append(list) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEqual("<olist><item>Item1</item><item>Item2</item></olist>", resolved) self.assertEqual("<olist><item>Item1</item><item>Item2</item></olist>", root.to_xml(self._client_context))
def test_type1_resolve_exception(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateConditionNode("name1", TemplateWordNode("value1"), var_type=TemplateConditionNode.GLOBAL) self.assertIsNotNone(node) node.append(TemplateWordNode("Hello")) root.append(node) self.assertEqual(len(root.children), 1) self._client_context.bot.get_conversation( self._client_context).set_property('name1', "value1") result = root.resolve(self._client_context) self.assertIsNotNone(result) self.assertEqual(result, "")
def test_node_years(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() self.assertEqual("[INTERVAL]", node.to_string()) node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("years") node.interval_from = TemplateWordNode("Thu Oct 06 16:35:11 2014") node.interval_to = TemplateWordNode("Fri Oct 07 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "2")
def test_to_xml(self): root = TemplateNode() xml = TemplateXMLNode() xml._name = "dial" root.append(xml) xml.append(TemplateWordNode("07777777777")) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual("<template><dial>07777777777</dial></template>", xml_str)
def test_global_get_no_value(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateGetNode() node.name = TemplateWordNode("name") node.local = False node.append(TemplateWordNode("Fred")) self.assertIsNotNone(node) self.assertEqual("[GET [Global] - [WORD]name]", node.to_string()) root.append(node) self.assertEqual(len(root.children), 1) self.bot.brain.properties.add_property("default-get", "unknown") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("unknown", result)
def test_to_xml_no_timeout(self): root = TemplateNode() node = TemplateSystemNode() root.append(node) node.append(TemplateWordNode('echo "Hello World"')) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual( '<template><system>echo "Hello World"</system></template>', xml_str)
class TemplateIntervalNode(TemplateNode): def __init__(self, date_format="%c", style="days"): TemplateNode.__init__(self) self._interval_from = None self._interval_to = None if isinstance(date_format, str): self._format = TemplateWordNode(date_format) else: self._format = date_format if isinstance(style, str): self._style = TemplateWordNode(style) else: self._style = style @property def format(self): return self._format @format.setter def format(self, format): self._format = format @property def interval_from(self): return self._interval_from @interval_from.setter def interval_from(self, interval_from): self._interval_from = interval_from @property def interval_to(self): return self._interval_to @interval_to.setter def interval_to(self, interval_to): self._interval_to = interval_to @property def style(self): return self._style @style.setter def style(self, style): self._style = style def resolve(self, bot, clientid): try: format_str = self._format.resolve(bot, clientid) from_str = self.interval_from.resolve(bot, clientid) from_time = datetime.datetime.strptime(from_str, format_str) to_str = self.interval_to.resolve(bot, clientid) to_time = datetime.datetime.strptime(to_str, format_str) style = self._style.resolve(bot, clientid) diff = to_time - from_time difference = relativedelta(to_time, from_time) if style == "years": resolved = str(difference.years) elif style == "months": resolved = str(difference.months) elif style == "weeks": resolved = str(difference.weeks) elif style == "days": resolved = str(diff.days) elif style == "hours": resolved = str(difference.hours) elif style == "minutes": resolved = str(difference.minutes) elif style == "seconds": resolved = str(difference.seconds) elif style == "microseconds": resolved = str(difference.microseconds) elif style == "ymd": resolved = "%d years, %d months, %d days" % \ (difference.years, difference.months, difference.days) elif style == "hms": resolved = "%d hours, %d minutes, %d seconds" % \ (difference.hours, difference.minutes, difference.seconds) elif style == "ymdhms": resolved = "%d years, %d months, %d days, %d hours, %d minutes, %d seconds" % \ (difference.years, difference.months, difference.days, difference.hours, difference.minutes, difference.seconds) else: logging.error("Unknown interval style [%s]", style) resolved = "" logging.debug("[INTERVAL] resolved to [%s]", resolved) return resolved except Exception as excep: logging.exception(excep) return "" def to_string(self): return "[INTERVAL]" def to_xml(self, bot, clientid): xml = '<interval' xml += ' format="%s"' % self._format.to_xml(bot, clientid) xml += ' style="%s"' % self._style.to_xml(bot, clientid) xml += '>' xml += '<from>' xml += self._interval_from.to_xml(bot, clientid) xml += '</from>' xml += '<to>' xml += self._interval_to.to_xml(bot, clientid) xml += '</to>' xml += '</interval>' return xml