2015-12-17 18:47:11 +00:00
|
|
|
from __future__ import print_function
|
2014-11-07 18:12:21 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2016-02-21 18:36:24 +00:00
|
|
|
from functools import partial
|
2014-11-07 18:12:21 +00:00
|
|
|
from senpy.extensions import Senpy
|
2016-02-21 18:36:24 +00:00
|
|
|
from senpy.models import Error
|
2014-11-07 18:12:21 +00:00
|
|
|
from flask import Flask
|
|
|
|
from flask.ext.testing import TestCase
|
|
|
|
|
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
class ExtensionsTest(TestCase):
|
2015-02-24 06:15:25 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
def create_app(self):
|
|
|
|
self.app = Flask("test_extensions")
|
2016-02-19 18:24:09 +00:00
|
|
|
self.dir = os.path.join(os.path.dirname(__file__))
|
2015-09-29 09:14:28 +00:00
|
|
|
self.senpy = Senpy(plugin_folder=self.dir, default_plugins=False)
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.init_app(self.app)
|
|
|
|
self.senpy.activate_plugin("Dummy", sync=True)
|
2014-11-07 18:12:21 +00:00
|
|
|
return self.app
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
""" Initialising the app with the extension. """
|
|
|
|
assert hasattr(self.app, "senpy")
|
|
|
|
tapp = Flask("temp app")
|
2014-11-20 18:29:49 +00:00
|
|
|
self.senpy.init_app(tapp)
|
2014-11-07 18:12:21 +00:00
|
|
|
assert hasattr(tapp, "senpy")
|
|
|
|
|
|
|
|
def test_discovery(self):
|
|
|
|
""" Discovery of plugins in given folders. """
|
2014-11-20 18:29:49 +00:00
|
|
|
# noinspection PyProtectedMember
|
2014-11-07 18:12:21 +00:00
|
|
|
assert self.dir in self.senpy._search_folders
|
2015-12-17 18:47:11 +00:00
|
|
|
print(self.senpy.plugins)
|
2014-12-01 17:27:20 +00:00
|
|
|
assert "Dummy" in self.senpy.plugins
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_enabling(self):
|
|
|
|
""" Enabling a plugin """
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.activate_all(sync=True)
|
|
|
|
assert len(self.senpy.plugins) == 2
|
|
|
|
assert self.senpy.plugins["Sleep"].is_activated
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_disabling(self):
|
|
|
|
""" Disabling a plugin """
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.deactivate_all(sync=True)
|
2015-02-24 06:15:25 +00:00
|
|
|
assert not self.senpy.plugins["Dummy"].is_activated
|
|
|
|
assert not self.senpy.plugins["Sleep"].is_activated
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
""" Default plugin should be set """
|
|
|
|
assert self.senpy.default_plugin
|
2015-02-24 06:15:25 +00:00
|
|
|
assert self.senpy.default_plugin.name == "Dummy"
|
|
|
|
self.senpy.deactivate_all(sync=True)
|
|
|
|
logging.debug("Default: {}".format(self.senpy.default_plugin))
|
|
|
|
assert self.senpy.default_plugin is None
|
|
|
|
|
|
|
|
def test_noplugin(self):
|
|
|
|
""" Don't analyse if there isn't any plugin installed """
|
|
|
|
self.senpy.deactivate_all(sync=True)
|
2016-02-21 18:36:24 +00:00
|
|
|
self.assertRaises(Error, partial(self.senpy.analyse, input="tupni"))
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_analyse(self):
|
|
|
|
""" Using a plugin """
|
2015-02-24 06:15:25 +00:00
|
|
|
# I was using mock until plugin started inheriting
|
|
|
|
# Leaf (defaultdict with __setattr__ and __getattr__.
|
|
|
|
r1 = self.senpy.analyse(
|
|
|
|
algorithm="Dummy", input="tupni", output="tuptuo")
|
2015-02-23 01:13:31 +00:00
|
|
|
r2 = self.senpy.analyse(input="tupni", output="tuptuo")
|
|
|
|
assert r1.analysis[0].id[:5] == "Dummy"
|
|
|
|
assert r2.analysis[0].id[:5] == "Dummy"
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_filtering(self):
|
|
|
|
""" Filtering plugins """
|
2014-12-01 17:27:20 +00:00
|
|
|
assert len(self.senpy.filter_plugins(name="Dummy")) > 0
|
2014-11-07 18:12:21 +00:00
|
|
|
assert not len(self.senpy.filter_plugins(name="notdummy"))
|
2014-12-01 17:27:20 +00:00
|
|
|
assert self.senpy.filter_plugins(name="Dummy", is_activated=True)
|
|
|
|
self.senpy.deactivate_plugin("Dummy", sync=True)
|
2015-02-24 06:15:25 +00:00
|
|
|
assert not len(
|
|
|
|
self.senpy.filter_plugins(name="Dummy", is_activated=True))
|