2015-12-17 18:47:11 +00:00
|
|
|
from __future__ import print_function
|
2014-11-07 18:12:21 +00:00
|
|
|
import os
|
2017-02-28 03:01:05 +00:00
|
|
|
from copy import deepcopy
|
2014-11-07 18:12:21 +00:00
|
|
|
import logging
|
2014-11-20 18:29:49 +00:00
|
|
|
|
2017-02-17 00:53:02 +00:00
|
|
|
try:
|
|
|
|
from unittest import mock
|
|
|
|
except ImportError:
|
|
|
|
import mock
|
|
|
|
|
2017-01-10 09:16:45 +00:00
|
|
|
from functools import partial
|
2014-11-07 18:12:21 +00:00
|
|
|
from senpy.extensions import Senpy
|
2017-03-13 20:06:19 +00:00
|
|
|
from senpy.models import Error, Results, Entry, EmotionSet, Emotion, Plugin
|
2014-11-07 18:12:21 +00:00
|
|
|
from flask import Flask
|
2016-09-21 16:59:28 +00:00
|
|
|
from unittest import TestCase
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
|
2014-11-20 18:29:49 +00:00
|
|
|
class ExtensionsTest(TestCase):
|
2016-09-21 16:59:28 +00:00
|
|
|
def setUp(self):
|
2017-02-27 10:37:43 +00:00
|
|
|
self.app = Flask('test_extensions')
|
2016-02-19 18:24:09 +00:00
|
|
|
self.dir = os.path.join(os.path.dirname(__file__))
|
2017-02-17 00:53:02 +00:00
|
|
|
self.senpy = Senpy(plugin_folder=self.dir,
|
|
|
|
app=self.app,
|
|
|
|
default_plugins=False)
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.activate_plugin("Dummy", sync=True)
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
""" Initialising the app with the extension. """
|
|
|
|
assert hasattr(self.app, "senpy")
|
|
|
|
tapp = Flask("temp app")
|
2014-11-20 18:29:49 +00:00
|
|
|
self.senpy.init_app(tapp)
|
2014-11-07 18:12:21 +00:00
|
|
|
assert hasattr(tapp, "senpy")
|
|
|
|
|
|
|
|
def test_discovery(self):
|
|
|
|
""" Discovery of plugins in given folders. """
|
2014-11-20 18:29:49 +00:00
|
|
|
# noinspection PyProtectedMember
|
2014-11-07 18:12:21 +00:00
|
|
|
assert self.dir in self.senpy._search_folders
|
2015-12-17 18:47:11 +00:00
|
|
|
print(self.senpy.plugins)
|
2014-12-01 17:27:20 +00:00
|
|
|
assert "Dummy" in self.senpy.plugins
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_enabling(self):
|
2016-09-21 16:59:28 +00:00
|
|
|
""" Enabling a plugin """
|
|
|
|
info = {
|
|
|
|
'name': 'TestPip',
|
|
|
|
'module': 'dummy',
|
2017-02-27 11:14:39 +00:00
|
|
|
'description': None,
|
2016-09-21 16:59:28 +00:00
|
|
|
'requirements': ['noop'],
|
|
|
|
'version': 0
|
2017-01-10 09:16:45 +00:00
|
|
|
}
|
2017-02-27 10:37:43 +00:00
|
|
|
root = os.path.join(self.dir, 'plugins', 'dummy_plugin')
|
2016-09-21 16:59:28 +00:00
|
|
|
name, module = self.senpy._load_plugin_from_info(info, root=root)
|
|
|
|
assert name == 'TestPip'
|
|
|
|
assert module
|
|
|
|
import noop
|
2017-01-10 10:10:10 +00:00
|
|
|
dir(noop)
|
2017-02-28 03:01:05 +00:00
|
|
|
self.senpy.install_deps()
|
2016-09-21 16:59:28 +00:00
|
|
|
|
|
|
|
def test_installing(self):
|
2014-11-07 18:12:21 +00:00
|
|
|
""" Enabling a plugin """
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.activate_all(sync=True)
|
2017-02-27 10:37:43 +00:00
|
|
|
assert len(self.senpy.plugins) >= 3
|
2014-12-01 17:27:20 +00:00
|
|
|
assert self.senpy.plugins["Sleep"].is_activated
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_disabling(self):
|
|
|
|
""" Disabling a plugin """
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.deactivate_all(sync=True)
|
2015-02-24 06:15:25 +00:00
|
|
|
assert not self.senpy.plugins["Dummy"].is_activated
|
|
|
|
assert not self.senpy.plugins["Sleep"].is_activated
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_default(self):
|
|
|
|
""" Default plugin should be set """
|
|
|
|
assert self.senpy.default_plugin
|
2015-02-24 06:15:25 +00:00
|
|
|
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)
|
2017-02-27 10:37:43 +00:00
|
|
|
self.assertRaises(Error, partial(self.senpy.analyse, input="tupni"))
|
|
|
|
self.assertRaises(Error,
|
|
|
|
partial(
|
|
|
|
self.senpy.analyse,
|
|
|
|
input="tupni",
|
|
|
|
algorithm='Dummy'))
|
2014-11-07 18:12:21 +00:00
|
|
|
|
|
|
|
def test_analyse(self):
|
|
|
|
""" Using a plugin """
|
2015-02-24 06:15:25 +00:00
|
|
|
# I was using mock until plugin started inheriting
|
|
|
|
# Leaf (defaultdict with __setattr__ and __getattr__.
|
|
|
|
r1 = self.senpy.analyse(
|
|
|
|
algorithm="Dummy", input="tupni", output="tuptuo")
|
2015-02-23 01:13:31 +00:00
|
|
|
r2 = self.senpy.analyse(input="tupni", output="tuptuo")
|
2017-03-14 18:54:33 +00:00
|
|
|
assert r1.analysis[0] == "plugins/Dummy_0.1"
|
|
|
|
assert r2.analysis[0] == "plugins/Dummy_0.1"
|
|
|
|
assert r1.entries[0].text == 'input'
|
|
|
|
|
|
|
|
def test_analyse_jsonld(self):
|
|
|
|
""" Using a plugin with JSON-LD input"""
|
|
|
|
js_input = '''{
|
|
|
|
"@id": "prueba",
|
|
|
|
"@type": "results",
|
|
|
|
"entries": [
|
|
|
|
{"@id": "entry1",
|
|
|
|
"text": "tupni",
|
|
|
|
"@type": "entry"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}'''
|
|
|
|
r1 = self.senpy.analyse(algorithm="Dummy",
|
|
|
|
input=js_input,
|
|
|
|
informat="json-ld",
|
|
|
|
output="tuptuo")
|
|
|
|
r2 = self.senpy.analyse(input="tupni", output="tuptuo")
|
2017-02-27 10:37:43 +00:00
|
|
|
assert r1.analysis[0] == "plugins/Dummy_0.1"
|
|
|
|
assert r2.analysis[0] == "plugins/Dummy_0.1"
|
|
|
|
assert r1.entries[0].text == 'input'
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2017-02-17 00:53:02 +00:00
|
|
|
def test_analyse_error(self):
|
|
|
|
mm = mock.MagicMock()
|
2017-03-13 20:06:19 +00:00
|
|
|
mm.id = 'magic_mock'
|
|
|
|
mm.analyse_entries.side_effect = Error('error on analysis', status=500)
|
2017-02-17 00:53:02 +00:00
|
|
|
self.senpy.plugins['MOCK'] = mm
|
2017-03-13 20:06:19 +00:00
|
|
|
try:
|
|
|
|
self.senpy.analyse(input='nothing', algorithm='MOCK')
|
|
|
|
assert False
|
|
|
|
except Error as ex:
|
|
|
|
assert ex['message'] == 'error on analysis'
|
|
|
|
assert ex['status'] == 500
|
|
|
|
|
2017-02-17 00:53:02 +00:00
|
|
|
mm.analyse.side_effect = Exception('generic exception on analysis')
|
2017-03-13 20:06:19 +00:00
|
|
|
mm.analyse_entries.side_effect = Exception(
|
2017-02-27 10:37:43 +00:00
|
|
|
'generic exception on analysis')
|
2017-03-13 20:06:19 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.senpy.analyse(input='nothing', algorithm='MOCK')
|
|
|
|
assert False
|
|
|
|
except Error as ex:
|
|
|
|
assert ex['message'] == 'generic exception on analysis'
|
|
|
|
assert ex['status'] == 500
|
2017-02-17 00:53:02 +00:00
|
|
|
|
2014-11-07 18:12:21 +00:00
|
|
|
def test_filtering(self):
|
|
|
|
""" Filtering plugins """
|
2014-12-01 17:27:20 +00:00
|
|
|
assert len(self.senpy.filter_plugins(name="Dummy")) > 0
|
2014-11-07 18:12:21 +00:00
|
|
|
assert not len(self.senpy.filter_plugins(name="notdummy"))
|
2014-12-01 17:27:20 +00:00
|
|
|
assert self.senpy.filter_plugins(name="Dummy", is_activated=True)
|
|
|
|
self.senpy.deactivate_plugin("Dummy", sync=True)
|
2015-02-24 06:15:25 +00:00
|
|
|
assert not len(
|
2017-02-27 10:37:43 +00:00
|
|
|
self.senpy.filter_plugins(name="Dummy", is_activated=True))
|
2017-02-17 00:53:02 +00:00
|
|
|
|
|
|
|
def test_load_default_plugins(self):
|
|
|
|
senpy = Senpy(plugin_folder=self.dir, default_plugins=True)
|
|
|
|
assert len(senpy.plugins) > 1
|
2017-02-28 03:01:05 +00:00
|
|
|
|
|
|
|
def test_convert_emotions(self):
|
|
|
|
self.senpy.activate_all()
|
2017-03-13 20:06:19 +00:00
|
|
|
plugin = Plugin({
|
2017-02-28 03:01:05 +00:00
|
|
|
'id': 'imaginary',
|
|
|
|
'onyx:usesEmotionModel': 'emoml:fsre-dimensions'
|
2017-03-13 20:06:19 +00:00
|
|
|
})
|
2017-02-28 03:01:05 +00:00
|
|
|
eSet1 = EmotionSet()
|
2017-03-13 20:06:19 +00:00
|
|
|
eSet1.prov__wasGeneratedBy = plugin['id']
|
2017-02-28 03:01:05 +00:00
|
|
|
eSet1['onyx:hasEmotion'].append(Emotion({
|
|
|
|
'emoml:arousal': 1,
|
|
|
|
'emoml:potency': 0,
|
|
|
|
'emoml:valence': 0
|
|
|
|
}))
|
|
|
|
response = Results({
|
|
|
|
'entries': [Entry({
|
|
|
|
'text': 'much ado about nothing',
|
|
|
|
'emotions': [eSet1]
|
|
|
|
})]
|
|
|
|
})
|
|
|
|
params = {'emotionModel': 'emoml:big6',
|
|
|
|
'conversion': 'full'}
|
|
|
|
r1 = deepcopy(response)
|
|
|
|
self.senpy.convert_emotions(r1,
|
2017-03-13 20:06:19 +00:00
|
|
|
[plugin, ],
|
2017-02-28 03:01:05 +00:00
|
|
|
params)
|
|
|
|
assert len(r1.entries[0].emotions) == 2
|
|
|
|
params['conversion'] = 'nested'
|
|
|
|
r2 = deepcopy(response)
|
|
|
|
self.senpy.convert_emotions(r2,
|
2017-03-13 20:06:19 +00:00
|
|
|
[plugin, ],
|
2017-02-28 03:01:05 +00:00
|
|
|
params)
|
|
|
|
assert len(r2.entries[0].emotions) == 1
|
|
|
|
assert r2.entries[0].emotions[0]['prov:wasDerivedFrom'] == eSet1
|
|
|
|
params['conversion'] = 'filtered'
|
|
|
|
r3 = deepcopy(response)
|
|
|
|
self.senpy.convert_emotions(r3,
|
2017-03-13 20:06:19 +00:00
|
|
|
[plugin, ],
|
2017-02-28 03:01:05 +00:00
|
|
|
params)
|
|
|
|
assert len(r3.entries[0].emotions) == 1
|