コード例 #1
0
ファイル: test_learnf.py プロジェクト: zippyy/program-y
    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))
コード例 #2
0
ファイル: test_learnf.py プロジェクト: whackur/chatbot
    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()))
コード例 #3
0
    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)
コード例 #4
0
    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))
コード例 #5
0
    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)
コード例 #6
0
ファイル: assert_learnf.py プロジェクト: keiffster/program-y
    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)
コード例 #7
0
ファイル: test_learnf.py プロジェクト: ksenia1997/program-y
    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)
コード例 #8
0
    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)
コード例 #9
0
    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))
コード例 #10
0
    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)
コード例 #11
0
ファイル: test_learn.py プロジェクト: Freiza/program-y
    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))
コード例 #12
0
    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'))
コード例 #13
0
    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)