def test_save_learnf(self): config = FileStorageConfiguration() tmpdir = os.path.dirname(__file__) + os.sep + "learnf" config.learnf_storage._dirs = [tmpdir] engine = FileStorageEngine(config) engine.initialise() store = FileLearnfStore(engine) test_client = TestClient() client_context = test_client.create_client_context("test1") learnf_path = store._get_storage_path() learnf_fullpath = store.create_learnf_path(client_context, learnf_path) if os.path.exists(learnf_fullpath): os.remove(learnf_fullpath) self.assertFalse(os.path.exists(learnf_fullpath)) template = TemplateNode() template.append(TemplateWordNode("Hello")) category = LearnCategory("HELLO *", "*", "*", template) store.save_learnf(client_context, category) self.assertTrue(os.path.exists(learnf_fullpath)) shutil.rmtree(tmpdir) self.assertFalse(os.path.exists(tmpdir))
def test_node(self): if os.path.exists(self.get_os_specific_filename()): os.remove(self.get_os_specific_filename()) root = TemplateNode() self.assertIsNotNone(root) learn = TemplateLearnfNode() self.assertIsNotNone(learn) learn_cat = LearnCategory( ET.fromstring("<pattern>HELLO LEARN</pattern>"), ET.fromstring("<topic>*</topic>"), ET.fromstring("<that>*</that>"), TemplateWordNode("LEARN")) learn.append(learn_cat) root.append(learn) self.assertEqual(1, len(root.children)) self._client_context.brain.configuration.defaults._learn_filename = self.get_os_specific_filename( ) resolved = root.resolve(self._client_context) self.assertIsNotNone(resolved) self.assertEqual("", resolved) self.assertTrue(os.path.exists(self.get_os_specific_filename()))
def test_save_learnf_same_pattern(self): config = RedisStorageConfiguration() engine = RedisStorageEngine(config) engine.initialise() store = RedisLearnfStore(engine) test_client = TestClient() client_context = test_client.create_client_context("test1") pattern = ET.Element('pattern') pattern.text = "HELLO" topic = ET.Element('topic') topic.text = '*' that = ET.Element('that') that.text = '*' template = TemplateNode() template.append(TemplateWordNode("Hello")) category = LearnCategory(pattern, topic, that, template) is_replace = False store.save_learnf(client_context, category, is_replace) is_replace = True store.save_learnf(client_context, category, is_replace)
def test_save_learnf_with_exception(self): config = FileStorageConfiguration() tmpdir = os.path.dirname(__file__) + os.sep + "learnf" config.learnf_storage._dirs = [tmpdir] engine = FileStorageEngine(config) engine.initialise() store = FileLearnfStore(engine) test_client = TestClient() client_context = test_client.create_client_context("test1") learnf_path = store._get_storage_path() learnf_fullpath = store.create_learnf_path(client_context, learnf_path) if os.path.exists(learnf_fullpath): os.remove(learnf_fullpath) self.assertFalse(os.path.exists(learnf_fullpath)) pattern = ET.Element('pattern') pattern.text = "HELLO" topic = ET.Element('topic') topic.text = '*' that = ET.Element('that') that.text = '*' template = TemplateNode() template.append(TemplateWordNode("Hello")) category = LearnCategory(pattern, topic, that, template) self.assertFalse(store.save_learnf(client_context, category))
def assert_save_learnf(self, store): test_client = TestClient() client_context = test_client.create_client_context("test1") template = TemplateNode() template.append(TemplateWordNode("Hello")) category = LearnCategory("HELLO *", "*", "*", template.to_xml(client_context)) store.save_learnf(client_context, category)
def assert_save_learnf(self, store): store.empty() test_client = TestClient() client_context = test_client.create_client_context("test1") template = TemplateNode() template.append(TemplateWordNode("Hello")) category = LearnCategory(ET.Element("HELLO"), ET.Element("*"), ET.Element("*"), template) store.save_learnf(client_context, category)
def test_to_xml(self): root = TemplateNode() learn = TemplateLearnfNode() learn_cat = LearnCategory(ET.fromstring("<pattern>HELLO LEARN</pattern>"), ET.fromstring("<topic>*</topic>"), ET.fromstring("<that>*</that>"), TemplateWordNode("LEARN")) learn.append(learn_cat) root.append(learn) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual("<template><learnf><category><pattern>HELLO LEARN</pattern><topic>*</topic><that>*</that><template>LEARN</template></category></learnf></template>", xml_str)
def test_write_node_to_learnf_file(self): config = FileStorageConfiguration() tmpdir = FileStorageConfiguration.get_temp_dir() + os.sep + "learnf" tmpfile = tmpdir + os.sep + "tmp" + os.sep + "test.xml" config.learnf_storage._dirs = [tmpdir] engine = FileStorageEngine(config) engine.initialise() store = FileLearnfStore(engine) test_client = TestClient() client_context = test_client.create_client_context("test1") learn_cat = LearnCategory(ET.Element("HELLO"), ET.Element("*"), ET.Element("*"), TemplateWordNode("Hi")) node = store.create_category_xml_node(client_context, learn_cat) with self.assertRaises(Exception): store.write_node_to_learnf_file(client_context, node, tmpfile)
def test_init(self): learncat = LearnCategory("pattern", "topic", "that", "template") self.assertIsNotNone(learncat) self.assertIsNotNone(learncat._pattern) self.assertIsNotNone(learncat._topic) self.assertIsNotNone(learncat._that) self.assertIsNotNone(learncat._template) self.assertIsNotNone(learncat.children) self.assertEqual(0, len(learncat.children)) self.assertEqual("CATEGORY", learncat.to_string()) learncat.pattern = "pattern2" self.assertEqual("pattern2", learncat.pattern) learncat.topic = "topic2" self.assertEqual("topic2", learncat.topic) learncat.that = "that2" self.assertEqual("that2", learncat.that) learncat.template = "template2" self.assertEqual("template2", learncat.template) learncat.append("category1") learncat.append("category2") self.assertEqual(2, len(learncat.children))
def test_node(self): root = TemplateNode() self.assertIsNotNone(root) learn = TemplateLearnNode() self.assertIsNotNone(learn) learn_cat = LearnCategory( ET.fromstring("<pattern>HELLO LEARN</pattern>"), ET.fromstring("<topic>*</topic>"), ET.fromstring("<that>*</that>"), TemplateWordNode("LEARN")) learn.append(learn_cat) root.append(learn) self.assertEqual(1, len(root.children)) resolved = root.resolve(self.bot, self.clientid) self.assertIsNotNone(resolved) self.assertEqual("", resolved)
def test_init(self): learncat = LearnCategory("pattern", "topic", "that", "template") self.assertIsNotNone(learncat) self.assertIsNotNone(learncat._pattern) self.assertIsNotNone(learncat._topic) self.assertIsNotNone(learncat._that) self.assertIsNotNone(learncat._template) self.assertIsNotNone(learncat.children) self.assertEqual(0, len(learncat.children)) self.assertEqual("CATEGORY", learncat.to_string()) learncat.pattern = "pattern2" self.assertEqual("pattern2", learncat.pattern) learncat.topic = "topic2" self.assertEqual("topic2", learncat.topic) learncat.that = "that2" self.assertEqual("that2", learncat.that) learncat.template = "template2" self.assertEqual("template2", learncat.template) learncat.append("category1") learncat.append("category2") self.assertEqual(2, len(learncat.children))
def test_node(self): if os.path.exists('/tmp/leanf.aiml'): os.remove('/tmp/leanf.aiml') root = TemplateNode() self.assertIsNotNone(root) learn = TemplateLearnfNode() self.assertIsNotNone(learn) learn_cat = LearnCategory( ET.fromstring("<pattern>HELLO LEARN</pattern>"), ET.fromstring("<topic>*</topic>"), ET.fromstring("<that>*</that>"), TemplateWordNode("LEARN")) learn.append(learn_cat) root.append(learn) self.assertEqual(1, len(root.children)) self.bot.brain.configuration.defaults._learn_filename = '/tmp/learnf.aiml' resolved = root.resolve(self.bot, self.clientid) self.assertIsNotNone(resolved) self.assertEqual("", resolved) self.assertTrue(os.path.exists('/tmp/learnf.aiml'))
def test_write_node_to_learnf_file_already_exists(self): config = FileStorageConfiguration() tmpdir = FileStorageConfiguration.get_temp_dir() + os.sep + "learnf" tmpfile = tmpdir + os.sep + "tmp" + os.sep + "test.xml" config.learnf_storage._dirs = [tmpdir] engine = FileStorageEngine(config) engine.initialise() store = FileLearnfStore(engine) test_client = TestClient() client_context = test_client.create_client_context("test1") learn_cat = LearnCategory(ET.Element("HELLO"), ET.Element("*"), ET.Element("*"), TemplateWordNode("Hi")) node = store.create_category_xml_node(client_context, learn_cat) store.write_node_to_learnf_file(client_context, node, tmpfile) self.assertTrue(os.path.exists(tmpfile)) store.write_node_to_learnf_file(client_context, node, tmpfile) if os.path.exists(tmpdir): shutil.rmtree(tmpdir)