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-07-12 15:41:14 +00:00
|
|
|
from senpy import plugins
|
2017-03-13 20:06:19 +00:00
|
|
|
from senpy.models import Error, Results, Entry, EmotionSet, Emotion, Plugin
|
2017-06-21 17:58:18 +00:00
|
|
|
from senpy import api
|
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
|
|
|
|
|
|
|
|
2017-06-21 17:58:18 +00:00
|
|
|
def analyse(instance, **kwargs):
|
|
|
|
request = api.parse_call(kwargs)
|
|
|
|
return instance.analyse(request)
|
|
|
|
|
|
|
|
|
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')
|
2017-12-30 17:59:58 +00:00
|
|
|
self.examples_dir = os.path.join(os.path.dirname(__file__), '..', 'example-plugins')
|
|
|
|
self.senpy = Senpy(plugin_folder=self.examples_dir,
|
2017-02-17 00:53:02 +00:00
|
|
|
app=self.app,
|
|
|
|
default_plugins=False)
|
2018-01-03 08:39:30 +00:00
|
|
|
self.senpy.deactivate_all()
|
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
|
2018-01-03 08:39:30 +00:00
|
|
|
print(self.senpy.plugins())
|
|
|
|
assert self.senpy.get_plugin("dummy")
|
|
|
|
|
|
|
|
def test_add_delete(self):
|
|
|
|
'''Should be able to add and delete new plugins. '''
|
|
|
|
new = plugins.Plugin(name='new', description='new', version=0)
|
|
|
|
self.senpy.add_plugin(new)
|
|
|
|
assert new in self.senpy.plugins()
|
|
|
|
self.senpy.delete_plugin(new)
|
|
|
|
assert new not in self.senpy.plugins()
|
|
|
|
|
|
|
|
def test_adding_folder(self):
|
|
|
|
""" It should be possible for senpy to look for plugins in more folders. """
|
|
|
|
senpy = Senpy(plugin_folder=None,
|
|
|
|
app=self.app,
|
|
|
|
default_plugins=False)
|
|
|
|
assert not senpy.analysis_plugins
|
|
|
|
senpy.add_folder(self.examples_dir)
|
|
|
|
assert senpy.analysis_plugins
|
|
|
|
self.assertRaises(AttributeError, senpy.add_folder, 'DOES NOT EXIST')
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2017-07-12 15:41:14 +00:00
|
|
|
def test_installing(self):
|
|
|
|
""" Installing a plugin """
|
2016-09-21 16:59:28 +00:00
|
|
|
info = {
|
|
|
|
'name': 'TestPip',
|
2018-01-03 08:39:30 +00:00
|
|
|
'module': 'mynoop',
|
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
|
|
|
}
|
2018-01-03 08:39:30 +00:00
|
|
|
module = plugins.from_info(info, root=self.examples_dir, install=True)
|
2017-08-27 16:43:40 +00:00
|
|
|
assert module.name == 'TestPip'
|
2016-09-21 16:59:28 +00:00
|
|
|
assert module
|
|
|
|
import noop
|
2017-01-10 10:10:10 +00:00
|
|
|
dir(noop)
|
2016-09-21 16:59:28 +00:00
|
|
|
|
2017-07-12 15:41:14 +00:00
|
|
|
def test_enabling(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)
|
2018-01-03 08:39:30 +00:00
|
|
|
assert len(self.senpy.plugins()) >= 3
|
|
|
|
assert self.senpy.get_plugin("Sleep").is_activated
|
2014-11-07 18:12:21 +00:00
|
|
|
|
2017-03-30 15:38:17 +00:00
|
|
|
def test_installing_nonexistent(self):
|
|
|
|
""" Fail if the dependencies cannot be met """
|
|
|
|
info = {
|
|
|
|
'name': 'TestPipFail',
|
|
|
|
'module': 'dummy',
|
|
|
|
'description': None,
|
|
|
|
'requirements': ['IAmMakingThisPackageNameUpToFail'],
|
|
|
|
'version': 0
|
|
|
|
}
|
|
|
|
with self.assertRaises(Error):
|
2017-08-27 16:43:40 +00:00
|
|
|
plugins.install_deps(info)
|
2017-03-30 15:38:17 +00:00
|
|
|
|
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)
|
2018-01-03 08:39:30 +00:00
|
|
|
assert not self.senpy.get_plugin("dummy").is_activated
|
|
|
|
assert not self.senpy.get_plugin("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-06-21 17:58:18 +00:00
|
|
|
self.assertRaises(Error, partial(analyse, self.senpy, input="tupni"))
|
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__.
|
2017-06-21 17:58:18 +00:00
|
|
|
r1 = analyse(self.senpy, algorithm="Dummy", input="tupni", output="tuptuo")
|
|
|
|
r2 = analyse(self.senpy, 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"
|
2018-01-01 12:13:17 +00:00
|
|
|
assert r1.entries[0]['nif:isString'] == 'input'
|
2017-03-14 18:54:33 +00:00
|
|
|
|
2018-01-03 08:39:30 +00:00
|
|
|
def test_analyse_empty(self):
|
|
|
|
""" Trying to analyse when no plugins are installed should raise an error."""
|
|
|
|
senpy = Senpy(plugin_folder=None,
|
|
|
|
app=self.app,
|
|
|
|
default_plugins=False)
|
|
|
|
self.assertRaises(Error, senpy.analyse, Results())
|
|
|
|
|
|
|
|
def test_analyse_wrong(self):
|
|
|
|
""" Trying to analyse with a non-existent plugin should raise an error."""
|
|
|
|
self.assertRaises(Error, analyse, self.senpy, algorithm='DOES NOT EXIST', input='test')
|
|
|
|
|
2017-03-14 18:54:33 +00:00
|
|
|
def test_analyse_jsonld(self):
|
|
|
|
""" Using a plugin with JSON-LD input"""
|
|
|
|
js_input = '''{
|
|
|
|
"@id": "prueba",
|
|
|
|
"@type": "results",
|
|
|
|
"entries": [
|
|
|
|
{"@id": "entry1",
|
2017-06-21 17:58:18 +00:00
|
|
|
"nif:isString": "tupni",
|
2017-03-14 18:54:33 +00:00
|
|
|
"@type": "entry"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}'''
|
2017-06-21 17:58:18 +00:00
|
|
|
r1 = analyse(self.senpy,
|
|
|
|
algorithm="Dummy",
|
|
|
|
input=js_input,
|
|
|
|
informat="json-ld",
|
|
|
|
output="tuptuo")
|
|
|
|
r2 = analyse(self.senpy,
|
|
|
|
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"
|
2018-01-01 12:13:17 +00:00
|
|
|
assert r1.entries[0]['nif:isString'] == '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'
|
2018-01-03 08:39:30 +00:00
|
|
|
mm.name = 'mock'
|
2017-08-27 16:43:40 +00:00
|
|
|
mm.is_activated = True
|
2017-06-21 17:58:18 +00:00
|
|
|
mm.analyse_entries.side_effect = Error('error in analysis', status=500)
|
2018-01-03 08:39:30 +00:00
|
|
|
self.senpy.add_plugin(mm)
|
2017-03-13 20:06:19 +00:00
|
|
|
try:
|
2017-06-21 17:58:18 +00:00
|
|
|
analyse(self.senpy, input='nothing', algorithm='MOCK')
|
2017-03-13 20:06:19 +00:00
|
|
|
assert False
|
|
|
|
except Error as ex:
|
2017-06-21 17:58:18 +00:00
|
|
|
assert 'error in analysis' in ex['message']
|
2017-03-13 20:06:19 +00:00
|
|
|
assert ex['status'] == 500
|
|
|
|
|
2018-01-03 08:39:30 +00:00
|
|
|
ex = Exception('generic exception on analysis')
|
|
|
|
mm.analyse.side_effect = ex
|
|
|
|
mm.analyse_entries.side_effect = ex
|
2017-03-13 20:06:19 +00:00
|
|
|
|
|
|
|
try:
|
2017-06-21 17:58:18 +00:00
|
|
|
analyse(self.senpy, input='nothing', algorithm='MOCK')
|
2017-03-13 20:06:19 +00:00
|
|
|
assert False
|
2018-01-03 08:39:30 +00:00
|
|
|
except Exception as ex:
|
2017-06-21 17:58:18 +00:00
|
|
|
assert 'generic exception on analysis' in ex['message']
|
2017-03-13 20:06:19 +00:00
|
|
|
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 """
|
2018-01-03 08:39:30 +00:00
|
|
|
assert len(self.senpy.plugins(name="Dummy")) > 0
|
|
|
|
assert not len(self.senpy.plugins(name="NotDummy"))
|
|
|
|
assert self.senpy.plugins(name="Dummy", is_activated=True)
|
2014-12-01 17:27:20 +00:00
|
|
|
self.senpy.deactivate_plugin("Dummy", sync=True)
|
2018-01-03 08:39:30 +00:00
|
|
|
assert not len(self.senpy.plugins(name="Dummy",
|
|
|
|
is_activated=True))
|
2017-02-17 00:53:02 +00:00
|
|
|
|
|
|
|
def test_load_default_plugins(self):
|
2017-12-30 17:59:58 +00:00
|
|
|
senpy = Senpy(plugin_folder=self.examples_dir, default_plugins=True)
|
2018-01-03 08:39:30 +00:00
|
|
|
assert len(senpy.plugins()) > 1
|
2017-02-28 03:01:05 +00:00
|
|
|
|
|
|
|
def test_convert_emotions(self):
|
2017-04-10 14:36:43 +00:00
|
|
|
self.senpy.activate_all(sync=True)
|
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-07-12 15:41:14 +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({
|
2017-06-21 17:58:18 +00:00
|
|
|
'analysis': [{'plugin': plugin}],
|
2017-02-28 03:01:05 +00:00
|
|
|
'entries': [Entry({
|
2018-01-01 12:13:17 +00:00
|
|
|
'nif:isString': 'much ado about nothing',
|
2017-02-28 03:01:05 +00:00
|
|
|
'emotions': [eSet1]
|
|
|
|
})]
|
|
|
|
})
|
|
|
|
params = {'emotionModel': 'emoml:big6',
|
|
|
|
'conversion': 'full'}
|
|
|
|
r1 = deepcopy(response)
|
2017-06-21 17:58:18 +00:00
|
|
|
r1.parameters = params
|
|
|
|
self.senpy.convert_emotions(r1)
|
2017-02-28 03:01:05 +00:00
|
|
|
assert len(r1.entries[0].emotions) == 2
|
|
|
|
params['conversion'] = 'nested'
|
|
|
|
r2 = deepcopy(response)
|
2017-06-21 17:58:18 +00:00
|
|
|
r2.parameters = params
|
|
|
|
self.senpy.convert_emotions(r2)
|
2017-02-28 03:01:05 +00:00
|
|
|
assert len(r2.entries[0].emotions) == 1
|
|
|
|
assert r2.entries[0].emotions[0]['prov:wasDerivedFrom'] == eSet1
|
|
|
|
params['conversion'] = 'filtered'
|
|
|
|
r3 = deepcopy(response)
|
2017-06-21 17:58:18 +00:00
|
|
|
r3.parameters = params
|
|
|
|
self.senpy.convert_emotions(r3)
|
2017-02-28 03:01:05 +00:00
|
|
|
assert len(r3.entries[0].emotions) == 1
|
2017-05-05 15:05:17 +00:00
|
|
|
r3.jsonld()
|