mirror of
https://github.com/gsi-upm/senpy
synced 2025-09-16 11:32:21 +00:00
Compare commits
2 Commits
mock-reque
...
0.11.1
Author | SHA1 | Date | |
---|---|---|---|
|
c939b095de | ||
|
6dd4a44924 |
@@ -69,7 +69,7 @@ def encoded_url(url=None, base=None):
|
||||
if request.method == 'GET':
|
||||
url = request.full_path[1:] # Remove the first slash
|
||||
else:
|
||||
hash(frozenset(request.form.params().items()))
|
||||
hash(frozenset(request.parameters.items()))
|
||||
code = 'hash:{}'.format(hash)
|
||||
|
||||
code = code or base64.urlsafe_b64encode(url.encode()).decode()
|
||||
@@ -184,14 +184,19 @@ def basic_api(f):
|
||||
return decorated_function
|
||||
|
||||
|
||||
@api_blueprint.route('/', methods=['POST', 'GET'])
|
||||
@api_blueprint.route('/', defaults={'plugin': None}, methods=['POST', 'GET'])
|
||||
@api_blueprint.route('/<path:plugin>', methods=['POST', 'GET'])
|
||||
@basic_api
|
||||
def api_root():
|
||||
def api_root(plugin):
|
||||
if request.parameters['help']:
|
||||
dic = dict(api.API_PARAMS, **api.NIF_PARAMS)
|
||||
response = Help(valid_parameters=dic)
|
||||
return response
|
||||
req = api.parse_call(request.parameters)
|
||||
if plugin:
|
||||
plugin = plugin.replace('+', '/')
|
||||
plugin = plugin.split('/')
|
||||
req.parameters['algorithm'] = plugin
|
||||
return current_app.senpy.analyse(req)
|
||||
|
||||
|
||||
@@ -221,7 +226,7 @@ def plugins():
|
||||
|
||||
@api_blueprint.route('/plugins/<plugin>/', methods=['POST', 'GET'])
|
||||
@basic_api
|
||||
def plugin(plugin=None):
|
||||
def plugin(plugin):
|
||||
sp = current_app.senpy
|
||||
return sp.get_plugin(plugin)
|
||||
|
||||
|
@@ -153,7 +153,6 @@ class Senpy(object):
|
||||
yield i
|
||||
return
|
||||
plugin = plugins[0]
|
||||
self._activate(plugin) # Make sure the plugin is activated
|
||||
specific_params = api.parse_extra_params(req, plugin)
|
||||
req.analysis.append({'plugin': plugin,
|
||||
'parameters': specific_params})
|
||||
@@ -352,7 +351,7 @@ class Senpy(object):
|
||||
|
||||
logger.info("Activating plugin: {}".format(plugin.name))
|
||||
|
||||
if sync or 'async' in plugin and not plugin.async:
|
||||
if sync or not getattr(plugin, 'async', True):
|
||||
return self._activate(plugin)
|
||||
else:
|
||||
th = Thread(target=partial(self._activate, plugin))
|
||||
@@ -375,7 +374,7 @@ class Senpy(object):
|
||||
|
||||
self._set_active(plugin, False)
|
||||
|
||||
if sync or 'async' in plugin and not plugin.async:
|
||||
if sync or not getattr(plugin, 'async', True):
|
||||
self._deactivate(plugin)
|
||||
else:
|
||||
th = Thread(target=partial(self._deactivate, plugin))
|
||||
|
@@ -15,9 +15,12 @@ try:
|
||||
from gsitk.datasets.datasets import DatasetManager
|
||||
from gsitk.evaluation.evaluation import Evaluation as Eval
|
||||
from sklearn.pipeline import Pipeline
|
||||
GSITK_AVAILABLE = True
|
||||
import pkg_resources
|
||||
GSITK_VERSION = pkg_resources.get_distribution("gsitk").version.split()
|
||||
GSITK_AVAILABLE = GSITK_VERSION > (0, 1, 9, 1) # Earlier versions have a bug
|
||||
modules = locals()
|
||||
except ImportError:
|
||||
logger.warn(IMPORTMSG)
|
||||
logger.warning(IMPORTMSG)
|
||||
GSITK_AVAILABLE = False
|
||||
GSITK_VERSION = ()
|
||||
DatasetManager = Eval = Pipeline = raise_exception
|
||||
|
@@ -67,10 +67,24 @@ class BlueprintsTest(TestCase):
|
||||
logging.debug("Got response: %s", js)
|
||||
assert "@context" in js
|
||||
assert "entries" in js
|
||||
assert len(js['analysis']) == 1
|
||||
|
||||
def test_analysis_post(self):
|
||||
"""
|
||||
The results for a POST request should be the same as for a GET request.
|
||||
"""
|
||||
resp = self.client.post("/api/", data={'i': 'My aloha mohame',
|
||||
'with_parameters': True})
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
logging.debug("Got response: %s", js)
|
||||
assert "@context" in js
|
||||
assert "entries" in js
|
||||
assert len(js['analysis']) == 1
|
||||
|
||||
def test_analysis_extra(self):
|
||||
"""
|
||||
Extra params that have a default should
|
||||
Extra params that have a default should use it
|
||||
"""
|
||||
resp = self.client.get("/api/?i=My aloha mohame&algo=Dummy&with_parameters=true")
|
||||
self.assertCode(resp, 200)
|
||||
@@ -95,6 +109,44 @@ class BlueprintsTest(TestCase):
|
||||
resp = self.client.get("/api/?i=My aloha mohame&algo=DummyRequired&example=a")
|
||||
self.assertCode(resp, 200)
|
||||
|
||||
def test_analysis_url(self):
|
||||
"""
|
||||
The algorithm can also be specified as part of the URL
|
||||
"""
|
||||
self.app.config['TESTING'] = False # Errors are expected in this case
|
||||
resp = self.client.get("/api/DummyRequired?i=My aloha mohame")
|
||||
self.assertCode(resp, 400)
|
||||
js = parse_resp(resp)
|
||||
logging.debug("Got response: %s", js)
|
||||
assert isinstance(js, models.Error)
|
||||
resp = self.client.get("/api/DummyRequired?i=My aloha mohame&example=notvalid")
|
||||
self.assertCode(resp, 400)
|
||||
resp = self.client.get("/api/DummyRequired?i=My aloha mohame&example=a")
|
||||
self.assertCode(resp, 200)
|
||||
|
||||
def test_analysis_chain(self):
|
||||
"""
|
||||
More than one algorithm can be specified. Plugins will then be chained
|
||||
"""
|
||||
resp = self.client.get("/api/Dummy?i=My aloha mohame")
|
||||
js = parse_resp(resp)
|
||||
assert len(js['analysis']) == 1
|
||||
assert js['entries'][0]['nif:isString'] == 'My aloha mohame'[::-1]
|
||||
|
||||
resp = self.client.get("/api/Dummy/Dummy?i=My aloha mohame")
|
||||
# Calling dummy twice, should return the same string
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
assert len(js['analysis']) == 2
|
||||
assert js['entries'][0]['nif:isString'] == 'My aloha mohame'
|
||||
|
||||
resp = self.client.get("/api/Dummy+Dummy?i=My aloha mohame")
|
||||
# Same with pluses instead of slashes
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
assert len(js['analysis']) == 2
|
||||
assert js['entries'][0]['nif:isString'] == 'My aloha mohame'
|
||||
|
||||
def test_error(self):
|
||||
"""
|
||||
The dummy plugin returns an empty response,\
|
||||
|
@@ -24,6 +24,19 @@ class ModelsTest(TestCase):
|
||||
except Error:
|
||||
pass
|
||||
|
||||
def test_client_post(self):
|
||||
endpoint = 'http://dummy/'
|
||||
client = Client(endpoint)
|
||||
with patch_requests('http://dummy/', Results()):
|
||||
resp = client.analyse('hello')
|
||||
assert isinstance(resp, Results)
|
||||
with patch_requests('http://dummy/', Error('Nothing'), method='POST'):
|
||||
try:
|
||||
client.analyse(input='hello', method='POST', algorithm='NONEXISTENT')
|
||||
raise Exception('Exceptions should be raised. This is not golang')
|
||||
except Error:
|
||||
pass
|
||||
|
||||
def test_plugins(self):
|
||||
endpoint = 'http://dummy/'
|
||||
client = Client(endpoint)
|
||||
|
@@ -1,15 +1,15 @@
|
||||
#!/bin/env python
|
||||
|
||||
import os
|
||||
import sys
|
||||
import pickle
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
from unittest import TestCase
|
||||
from unittest import TestCase, skipIf
|
||||
from senpy.models import Results, Entry, EmotionSet, Emotion, Plugins
|
||||
from senpy import plugins
|
||||
from senpy.plugins.conversion.emotion.centroids import CentroidConversion
|
||||
from senpy.gsitk_compat import GSITK_AVAILABLE
|
||||
|
||||
import pandas as pd
|
||||
|
||||
@@ -346,13 +346,15 @@ class PluginsTest(TestCase):
|
||||
smart_metrics = results[0].metrics[0]
|
||||
assert abs(smart_metrics['accuracy'] - 1) < 0.01
|
||||
|
||||
@skipIf(not GSITK_AVAILABLE, "GSITK is not available")
|
||||
def test_evaluation(self):
|
||||
if sys.version_info < (3, 0):
|
||||
with self.assertRaises(Exception) as context:
|
||||
self._test_evaluation()
|
||||
self.assertTrue('GSITK ' in str(context.exception))
|
||||
else:
|
||||
self._test_evaluation()
|
||||
|
||||
@skipIf(GSITK_AVAILABLE, "GSITK is available")
|
||||
def test_evaluation_unavailable(self):
|
||||
with self.assertRaises(Exception) as context:
|
||||
self._test_evaluation()
|
||||
self.assertTrue('GSITK ' in str(context.exception))
|
||||
|
||||
|
||||
def make_mini_test(fpath):
|
||||
|
Reference in New Issue
Block a user