__init__.py 3.12 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
import os
import logging
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
3

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
5
6
7
8
9
10
11
12
try:
    import unittest.mock as mock
except ImportError:
    import mock
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase


J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
class ExtensionsTest(TestCase):
14

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
15
16
17
    def create_app(self):
        self.app = Flask("test_extensions")
        self.dir = os.path.join(os.path.dirname(__file__), "..")
18
        self.senpy = Senpy(plugin_folder=self.dir, base_plugins=False)
19
20
        self.senpy.init_app(self.app)
        self.senpy.activate_plugin("Dummy", sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21
22
23
24
25
26
        return self.app

    def test_init(self):
        """ Initialising the app with the extension.  """
        assert hasattr(self.app, "senpy")
        tapp = Flask("temp app")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27
        self.senpy.init_app(tapp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
28
29
30
31
        assert hasattr(tapp, "senpy")

    def test_discovery(self):
        """ Discovery of plugins in given folders.  """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32
        # noinspection PyProtectedMember
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
34
        assert self.dir in self.senpy._search_folders
        print self.senpy.plugins
35
        assert "Dummy" in self.senpy.plugins
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36
37
38

    def test_enabling(self):
        """ Enabling a plugin """
39
40
41
        self.senpy.activate_all(sync=True)
        assert len(self.senpy.plugins) == 2
        assert self.senpy.plugins["Sleep"].is_activated
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
42
43
44

    def test_disabling(self):
        """ Disabling a plugin """
45
        self.senpy.deactivate_all(sync=True)
46
47
        assert not self.senpy.plugins["Dummy"].is_activated
        assert not self.senpy.plugins["Sleep"].is_activated
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
48
49
50
51

    def test_default(self):
        """ Default plugin should be set """
        assert self.senpy.default_plugin
52
53
54
55
56
57
58
59
60
61
62
        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)
        resp = self.senpy.analyse(input="tupni")
        logging.debug("Response: {}".format(resp))
        assert resp["status"] == 404
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65

    def test_analyse(self):
        """ Using a plugin """
66
67
68
69
        # I was using mock until plugin started inheriting
        # Leaf (defaultdict with  __setattr__ and __getattr__.
        r1 = self.senpy.analyse(
            algorithm="Dummy", input="tupni", output="tuptuo")
70
71
72
        r2 = self.senpy.analyse(input="tupni", output="tuptuo")
        assert r1.analysis[0].id[:5] == "Dummy"
        assert r2.analysis[0].id[:5] == "Dummy"
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
73
        for plug in self.senpy.plugins:
74
            self.senpy.deactivate_plugin(plug, sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
75
76
        resp = self.senpy.analyse(input="tupni")
        logging.debug("Response: {}".format(resp))
77
        assert resp["status"] == 404
78

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
80
81

    def test_filtering(self):
        """ Filtering plugins """
82
        assert len(self.senpy.filter_plugins(name="Dummy")) > 0
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
83
        assert not len(self.senpy.filter_plugins(name="notdummy"))
84
85
        assert self.senpy.filter_plugins(name="Dummy", is_activated=True)
        self.senpy.deactivate_plugin("Dummy", sync=True)
86
87
        assert not len(
            self.senpy.filter_plugins(name="Dummy", is_activated=True))