__init__.py 2.5 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):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
14
15
16
17
18
19
20
21
22
23
24
25
    def create_app(self):
        self.app = Flask("test_extensions")
        self.senpy = Senpy()
        self.senpy.init_app(self.app)
        self.dir = os.path.join(os.path.dirname(__file__), "..")
        self.senpy.add_folder(self.dir)
        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
33
34
35
36
37
38
39
40
41
42
43
44
        assert self.dir in self.senpy._search_folders
        print self.senpy.plugins
        assert "dummy" in self.senpy.plugins

    def test_enabling(self):
        """ Enabling a plugin """
        self.senpy.enable_plugin("dummy")
        assert self.senpy.plugins["dummy"].enabled

    def test_disabling(self):
        """ Disabling a plugin """
        self.senpy.enable_plugin("dummy")
        self.senpy.disable_plugin("dummy")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
45
        assert not self.senpy.plugins["dummy"].enabled
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    def test_default(self):
        """ Default plugin should be set """
        assert self.senpy.default_plugin
        assert self.senpy.default_plugin == "dummy"

    def test_analyse(self):
        """ Using a plugin """
        with mock.patch.object(self.senpy.plugins["dummy"], "analyse") as mocked:
            self.senpy.analyse(algorithm="dummy", input="tupni", output="tuptuo")
            self.senpy.analyse(input="tupni", output="tuptuo")
        mocked.assert_any_call(input="tupni", output="tuptuo", algorithm="dummy")
        mocked.assert_any_call(input="tupni", output="tuptuo")
        for plug in self.senpy.plugins:
            self.senpy.disable_plugin(plug)
        resp = self.senpy.analyse(input="tupni")
        logging.debug("Response: {}".format(resp))
        assert resp["status"] == 400

    def test_filtering(self):
        """ Filtering plugins """
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
67
        assert len(self.senpy.filter_plugins(name="dummy")) > 0
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
68
69
70
71
        assert not len(self.senpy.filter_plugins(name="notdummy"))
        assert self.senpy.filter_plugins(name="dummy", enabled=True)
        self.senpy.disable_plugin("dummy")
        assert not len(self.senpy.filter_plugins(name="dummy", enabled=True))