test_extensions.py 2.86 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

5
from functools import partial 
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
6
from senpy.extensions import Senpy
7
from senpy.models import Error
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
8
9
10
11
from flask import Flask
from flask.ext.testing import TestCase


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

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

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

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

    def test_default(self):
        """ Default plugin should be set """
        assert self.senpy.default_plugin
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)
59
        self.assertRaises(Error, partial(self.senpy.analyse, input="tupni"))
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
71
72

    def test_filtering(self):
        """ Filtering plugins """
73
        assert len(self.senpy.filter_plugins(name="Dummy")) > 0
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
74
        assert not len(self.senpy.filter_plugins(name="notdummy"))
75
76
        assert self.senpy.filter_plugins(name="Dummy", is_activated=True)
        self.senpy.deactivate_plugin("Dummy", sync=True)
77
78
        assert not len(
            self.senpy.filter_plugins(name="Dummy", is_activated=True))