test_extensions.py 3.08 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
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
10
class ExtensionsTest(TestCase):
11

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

    def test_enabling(self):
        """ Enabling a plugin """
36
37
38
        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
39
40
41

    def test_disabling(self):
        """ Disabling a plugin """
42
        self.senpy.deactivate_all(sync=True)
43
44
        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
45
46
47
48

    def test_default(self):
        """ Default plugin should be set """
        assert self.senpy.default_plugin
49
50
51
52
53
54
55
56
57
58
        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))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
        assert resp.status == 404
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
60
61
62

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
76
77
78

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