mirror of
https://github.com/gsi-upm/senpy
synced 2025-08-23 18:12:20 +00:00
Several fixes
* Refactored BaseModel for efficiency * Added plugin metaclass to keep track of plugin types * Moved plugins to examples dir (in a previous commit) * Simplified validation in parse_params * Added convenience methods to mock requests in tests * Changed help schema to use `.valid_parameters` instead of `.parameters`, which was used in results to show parameters provided by the user. * Improved UI * Added basic parameters * Fixed bugs in parameter handling * Refactored and cleaned code
This commit is contained in:
@@ -162,5 +162,5 @@ class BlueprintsTest(TestCase):
|
||||
resp = self.client.get("/api/?help=true")
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
assert "parameters" in js
|
||||
assert "help" in js["parameters"]
|
||||
assert "valid_parameters" in js
|
||||
assert "help" in js["valid_parameters"]
|
||||
|
@@ -1,24 +1,9 @@
|
||||
from unittest import TestCase
|
||||
try:
|
||||
from unittest.mock import patch
|
||||
except ImportError:
|
||||
from mock import patch
|
||||
|
||||
import json
|
||||
|
||||
from senpy.test import patch_requests
|
||||
from senpy.client import Client
|
||||
from senpy.models import Results, Plugins, Error
|
||||
from senpy.plugins import AnalysisPlugin, default_plugin_type
|
||||
|
||||
|
||||
class Call(dict):
|
||||
def __init__(self, obj):
|
||||
self.obj = obj.serialize()
|
||||
self.status_code = 200
|
||||
self.content = self.json()
|
||||
|
||||
def json(self):
|
||||
return json.loads(self.obj)
|
||||
from senpy.plugins import AnalysisPlugin
|
||||
|
||||
|
||||
class ModelsTest(TestCase):
|
||||
@@ -29,20 +14,18 @@ class ModelsTest(TestCase):
|
||||
def test_client(self):
|
||||
endpoint = 'http://dummy/'
|
||||
client = Client(endpoint)
|
||||
success = Call(Results())
|
||||
with patch('requests.request', return_value=success) as patched:
|
||||
with patch_requests(Results()) as (request, response):
|
||||
resp = client.analyse('hello')
|
||||
assert isinstance(resp, Results)
|
||||
patched.assert_called_with(
|
||||
request.assert_called_with(
|
||||
url=endpoint + '/', method='GET', params={'input': 'hello'})
|
||||
error = Call(Error('Nothing'))
|
||||
with patch('requests.request', return_value=error) as patched:
|
||||
with patch_requests(Error('Nothing')) as (request, response):
|
||||
try:
|
||||
client.analyse(input='hello', algorithm='NONEXISTENT')
|
||||
raise Exception('Exceptions should be raised. This is not golang')
|
||||
except Error:
|
||||
pass
|
||||
patched.assert_called_with(
|
||||
request.assert_called_with(
|
||||
url=endpoint + '/',
|
||||
method='GET',
|
||||
params={'input': 'hello',
|
||||
@@ -54,12 +37,11 @@ class ModelsTest(TestCase):
|
||||
plugins = Plugins()
|
||||
p1 = AnalysisPlugin({'name': 'AnalysisP1', 'version': 0, 'description': 'No'})
|
||||
plugins.plugins = [p1, ]
|
||||
success = Call(plugins)
|
||||
with patch('requests.request', return_value=success) as patched:
|
||||
with patch_requests(plugins) as (request, response):
|
||||
response = client.plugins()
|
||||
assert isinstance(response, dict)
|
||||
assert len(response) == 1
|
||||
assert "AnalysisP1" in response
|
||||
patched.assert_called_with(
|
||||
request.assert_called_with(
|
||||
url=endpoint + '/plugins', method='GET',
|
||||
params={'plugin_type': default_plugin_type})
|
||||
params={})
|
||||
|
@@ -106,7 +106,7 @@ class ExtensionsTest(TestCase):
|
||||
r2 = analyse(self.senpy, input="tupni", output="tuptuo")
|
||||
assert r1.analysis[0] == "plugins/Dummy_0.1"
|
||||
assert r2.analysis[0] == "plugins/Dummy_0.1"
|
||||
assert r1.entries[0]['nif:iString'] == 'input'
|
||||
assert r1.entries[0]['nif:isString'] == 'input'
|
||||
|
||||
def test_analyse_jsonld(self):
|
||||
""" Using a plugin with JSON-LD input"""
|
||||
@@ -130,7 +130,7 @@ class ExtensionsTest(TestCase):
|
||||
output="tuptuo")
|
||||
assert r1.analysis[0] == "plugins/Dummy_0.1"
|
||||
assert r2.analysis[0] == "plugins/Dummy_0.1"
|
||||
assert r1.entries[0]['nif:iString'] == 'input'
|
||||
assert r1.entries[0]['nif:isString'] == 'input'
|
||||
|
||||
def test_analyse_error(self):
|
||||
mm = mock.MagicMock()
|
||||
@@ -185,7 +185,7 @@ class ExtensionsTest(TestCase):
|
||||
response = Results({
|
||||
'analysis': [{'plugin': plugin}],
|
||||
'entries': [Entry({
|
||||
'nif:iString': 'much ado about nothing',
|
||||
'nif:isString': 'much ado about nothing',
|
||||
'emotions': [eSet1]
|
||||
})]
|
||||
})
|
||||
|
@@ -12,8 +12,8 @@ from senpy.models import (Emotion,
|
||||
Error,
|
||||
Results,
|
||||
Sentiment,
|
||||
SentimentPlugin,
|
||||
Plugins,
|
||||
Plugin,
|
||||
from_string,
|
||||
from_dict)
|
||||
from senpy import plugins
|
||||
@@ -99,19 +99,19 @@ class ModelsTest(TestCase):
|
||||
|
||||
def test_plugins(self):
|
||||
self.assertRaises(Error, plugins.Plugin)
|
||||
p = plugins.Plugin({"name": "dummy",
|
||||
"description": "I do nothing",
|
||||
"version": 0,
|
||||
"extra_params": {
|
||||
"none": {
|
||||
"options": ["es", ],
|
||||
"required": False,
|
||||
"default": "0"
|
||||
}
|
||||
}})
|
||||
p = plugins.SentimentPlugin({"name": "dummy",
|
||||
"description": "I do nothing",
|
||||
"version": 0,
|
||||
"extra_params": {
|
||||
"none": {
|
||||
"options": ["es", ],
|
||||
"required": False,
|
||||
"default": "0"
|
||||
}
|
||||
}})
|
||||
c = p.jsonld()
|
||||
assert '@type' in c
|
||||
assert c['@type'] == 'plugin'
|
||||
assert c['@type'] == 'sentimentPlugin'
|
||||
assert 'info' not in c
|
||||
assert 'repo' not in c
|
||||
assert 'extra_params' in c
|
||||
@@ -173,13 +173,14 @@ class ModelsTest(TestCase):
|
||||
def test_single_plugin(self):
|
||||
"""A response with a single plugin should still return a list"""
|
||||
plugs = Plugins()
|
||||
p = Plugin({'id': str(1),
|
||||
'version': 0,
|
||||
'description': 'dummy'})
|
||||
p = SentimentPlugin({'id': str(1),
|
||||
'version': 0,
|
||||
'description': 'dummy'})
|
||||
plugs.plugins.append(p)
|
||||
assert isinstance(plugs.plugins, list)
|
||||
js = plugs.jsonld()
|
||||
assert isinstance(js['plugins'], list)
|
||||
assert js['plugins'][0]['@type'] == 'sentimentPlugin'
|
||||
|
||||
def test_from_string(self):
|
||||
results = {
|
||||
@@ -192,6 +193,7 @@ class ModelsTest(TestCase):
|
||||
}]
|
||||
}
|
||||
recovered = from_dict(results)
|
||||
assert recovered.id == results['@id']
|
||||
assert isinstance(recovered, Results)
|
||||
assert isinstance(recovered.entries[0], Entry)
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import shutil
|
||||
import tempfile
|
||||
|
||||
from unittest import TestCase
|
||||
from senpy.models import Results, Entry, EmotionSet, Emotion
|
||||
from senpy.models import Results, Entry, EmotionSet, Emotion, Plugins
|
||||
from senpy import plugins
|
||||
from senpy.plugins.conversion.emotion.centroids import CentroidConversion
|
||||
|
||||
@@ -49,6 +49,25 @@ class PluginsTest(TestCase):
|
||||
assert os.path.isfile(a.shelf_file)
|
||||
os.remove(a.shelf_file)
|
||||
|
||||
def test_plugin_filter(self):
|
||||
ps = Plugins()
|
||||
for i in (plugins.SentimentPlugin,
|
||||
plugins.EmotionPlugin,
|
||||
plugins.AnalysisPlugin):
|
||||
p = i(name='Plugin_{}'.format(i.__name__),
|
||||
description='TEST',
|
||||
version=0,
|
||||
author='NOBODY')
|
||||
ps.plugins.append(p)
|
||||
assert len(ps.plugins) == 3
|
||||
cases = [('AnalysisPlugin', 3),
|
||||
('SentimentPlugin', 1),
|
||||
('EmotionPlugin', 1)]
|
||||
|
||||
for name, num in cases:
|
||||
res = plugins.pfilter(ps.plugins, plugin_type=name)
|
||||
assert len(res) == num
|
||||
|
||||
def test_shelf(self):
|
||||
''' A shelf is created and the value is stored '''
|
||||
newfile = self.shelf_file + "new"
|
||||
|
Reference in New Issue
Block a user