2019-07-17 14:29:30 +00:00
|
|
|
#
|
|
|
|
# Copyright 2014 Grupo de Sistemas Inteligentes (GSI) DIT, UPM
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
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
|
|
|
|
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
|
2023-09-26 17:47:23 +00:00
|
|
|
from senpy import plugins, config, api
|
2024-12-12 23:01:27 +00:00
|
|
|
from senpy.models import Results, Entry, EmotionSet, Emotion, Plugin
|
|
|
|
from senpy.errors import Error
|
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):
|
2018-12-07 17:30:05 +00:00
|
|
|
basic = api.parse_params(kwargs, api.API_PARAMS)
|
|
|
|
request = api.parse_call(basic)
|
2017-06-21 17:58:18 +00:00
|
|
|
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)
|
2024-12-12 23:01:27 +00:00
|
|
|
self.senpy.default_plugin = "Dummy"
|
2019-01-07 11:08:19 +00:00
|
|
|
self.app.config['TESTING'] = True # Tell Flask not to catch Exceptions
|
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. '''
|
2019-01-07 11:08:19 +00:00
|
|
|
new = plugins.Analyser(name='new', description='new', version=0)
|
2018-01-03 08:39:30 +00:00
|
|
|
self.senpy.add_plugin(new)
|
2024-12-12 23:01:27 +00:00
|
|
|
assert new in self.senpy.plugins()
|
2018-01-03 08:39:30 +00:00
|
|
|
self.senpy.delete_plugin(new)
|
2024-12-12 23:01:27 +00:00
|
|
|
assert new not in self.senpy.plugins()
|
2018-01-03 08:39:30 +00:00
|
|
|
|
|
|
|
def test_adding_folder(self):
|
|
|
|
""" It should be possible for senpy to look for plugins in more folders. """
|
2022-05-20 10:51:01 +00:00
|
|
|
app = Flask('test_adding_folder')
|
2018-01-03 08:39:30 +00:00
|
|
|
senpy = Senpy(plugin_folder=None,
|
2022-05-20 10:51:01 +00:00
|
|
|
app=app,
|
2018-01-03 08:39:30 +00:00
|
|
|
default_plugins=False)
|
2019-01-07 11:08:19 +00:00
|
|
|
assert not senpy.analysis_plugins()
|
2018-01-03 08:39:30 +00:00
|
|
|
senpy.add_folder(self.examples_dir)
|
2024-12-12 23:01:27 +00:00
|
|
|
assert senpy.plugins(plugin_type=plugins.Analyser)
|
2018-01-03 08:39:30 +00:00
|
|
|
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 """
|
2018-01-03 08:39:30 +00:00
|
|
|
assert len(self.senpy.plugins()) >= 3
|
2024-12-12 23:01:27 +00:00
|
|
|
assert self.senpy.get_plugin("Sleep")
|
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_default(self):
|
|
|
|
""" Default plugin should be set """
|
|
|
|
assert self.senpy.default_plugin
|
2019-01-07 11:08:19 +00:00
|
|
|
assert self.senpy.default_plugin.name == "dummy"
|
2015-02-24 06:15:25 +00:00
|
|
|
|
|
|
|
def test_noplugin(self):
|
|
|
|
""" Don't analyse if there isn't any plugin installed """
|
2024-12-12 23:01:27 +00:00
|
|
|
nosenpy = Senpy(default_plugins=False, plugin_folders=[])
|
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")
|
2019-01-07 11:08:19 +00:00
|
|
|
assert r1.activities[0].algorithm == "endpoint:plugins/dummy_0.1"
|
|
|
|
assert r2.activities[0].algorithm == "endpoint: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."""
|
2022-05-20 10:51:01 +00:00
|
|
|
app = Flask('test_adding_folder')
|
2018-01-03 08:39:30 +00:00
|
|
|
senpy = Senpy(plugin_folder=None,
|
2022-05-20 10:51:01 +00:00
|
|
|
app=app,
|
2018-01-03 08:39:30 +00:00
|
|
|
default_plugins=False)
|
2018-12-07 17:30:05 +00:00
|
|
|
self.assertRaises(Error, senpy.analyse, Results(), [])
|
2018-01-03 08:39:30 +00:00
|
|
|
|
|
|
|
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")
|
2019-01-07 11:08:19 +00:00
|
|
|
assert r1.activities[0].algorithm == "endpoint:plugins/dummy_0.1"
|
|
|
|
assert r2.activities[0].algorithm == "endpoint: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):
|
2019-01-07 11:08:19 +00:00
|
|
|
class ErrorPlugin(plugins.Analyser):
|
2024-12-12 23:01:27 +00:00
|
|
|
author: str = 'nobody'
|
2018-12-07 17:30:05 +00:00
|
|
|
version = 0
|
|
|
|
ex = Error()
|
|
|
|
|
|
|
|
def process(self, *args, **kwargs):
|
|
|
|
raise self.ex
|
|
|
|
|
|
|
|
m = ErrorPlugin(ex=Error('error in analysis', status=500))
|
|
|
|
self.senpy.add_plugin(m)
|
2017-03-13 20:06:19 +00:00
|
|
|
try:
|
2018-12-07 17:30:05 +00:00
|
|
|
analyse(self.senpy, input='nothing', algorithm='ErrorPlugin')
|
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-12-07 17:30:05 +00:00
|
|
|
m.ex = Exception('generic exception on analysis')
|
2017-03-13 20:06:19 +00:00
|
|
|
|
|
|
|
try:
|
2018-12-07 17:30:05 +00:00
|
|
|
analyse(self.senpy, input='nothing', algorithm='ErrorPlugin')
|
2017-03-13 20:06:19 +00:00
|
|
|
assert False
|
2018-01-03 08:39:30 +00:00
|
|
|
except Exception as ex:
|
2018-01-18 12:25:20 +00:00
|
|
|
assert 'generic exception on analysis' in str(ex)
|
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"))
|
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)
|
2024-12-12 23:01:27 +00:00
|
|
|
assert len(senpy.plugins()) > 1
|
2017-02-28 03:01:05 +00:00
|
|
|
|
|
|
|
def test_convert_emotions(self):
|
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()
|
2019-01-07 11:08:19 +00:00
|
|
|
activity = plugin.activity()
|
|
|
|
eSet1.prov(activity)
|
2017-02-28 03:01:05 +00:00
|
|
|
eSet1['onyx:hasEmotion'].append(Emotion({
|
|
|
|
'emoml:arousal': 1,
|
|
|
|
'emoml:potency': 0,
|
|
|
|
'emoml:valence': 0
|
|
|
|
}))
|
|
|
|
response = Results({
|
2019-01-07 11:08:19 +00:00
|
|
|
'activities': [activity],
|
2017-02-28 03:01:05 +00:00
|
|
|
'entries': [Entry({
|
2018-01-01 12:13:17 +00:00
|
|
|
'nif:isString': 'much ado about nothing',
|
2019-01-07 11:08:19 +00:00
|
|
|
'onyx:hasEmotionSet': [eSet1]
|
2017-02-28 03:01:05 +00:00
|
|
|
})]
|
|
|
|
})
|
2019-01-07 11:08:19 +00:00
|
|
|
params = {'emotion-model': 'emoml:big6',
|
2018-11-22 16:27:43 +00:00
|
|
|
'algorithm': ['conversion'],
|
2017-02-28 03:01:05 +00:00
|
|
|
'conversion': 'full'}
|
|
|
|
r1 = deepcopy(response)
|
2017-06-21 17:58:18 +00:00
|
|
|
r1.parameters = params
|
2018-11-22 16:27:43 +00:00
|
|
|
self.senpy.analyse(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
|
2018-11-22 16:27:43 +00:00
|
|
|
self.senpy.analyse(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
|
2018-11-22 16:27:43 +00:00
|
|
|
self.senpy.analyse(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()
|
2023-09-22 21:28:19 +00:00
|
|
|
|
|
|
|
def testDefaultPlugins(self):
|
|
|
|
'''The default set of plugins should all load'''
|
|
|
|
self.app = Flask('test_extensions')
|
|
|
|
self.examples_dir = os.path.join(os.path.dirname(__file__), '..', 'example-plugins')
|
2023-09-26 17:47:23 +00:00
|
|
|
self.senpy = Senpy(app=self.app, default_plugins=False)
|