__init__.py 3.16 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
from __future__ import print_function
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
2
3
import os
import logging
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
6
7
8
9
10
11
12
13
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
14
class ExtensionsTest(TestCase):
15

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
18
    def create_app(self):
        self.app = Flask("test_extensions")
        self.dir = os.path.join(os.path.dirname(__file__), "..")
19
        self.senpy = Senpy(plugin_folder=self.dir, default_plugins=False)
20
21
        self.senpy.init_app(self.app)
        self.senpy.activate_plugin("Dummy", sync=True)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
22
23
24
25
26
27
        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
28
        self.senpy.init_app(tapp)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
29
30
31
32
        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
33
        # noinspection PyProtectedMember
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
34
        assert self.dir in self.senpy._search_folders
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35
        print(self.senpy.plugins)
36
        assert "Dummy" in self.senpy.plugins
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38
39

    def test_enabling(self):
        """ Enabling a plugin """
40
41
42
        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
43
44
45

    def test_disabling(self):
        """ Disabling a plugin """
46
        self.senpy.deactivate_all(sync=True)
47
48
        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
49
50
51
52

    def test_default(self):
        """ Default plugin should be set """
        assert self.senpy.default_plugin
53
54
55
56
57
58
59
60
61
62
63
        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
64
65
66

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

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

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