mirror of
https://github.com/gsi-upm/senpy
synced 2025-08-23 18:12:20 +00:00
YAPFed
This commit is contained in:
@@ -3,6 +3,5 @@ from senpy.models import Results
|
||||
|
||||
|
||||
class DummyPlugin(SentimentPlugin):
|
||||
|
||||
def analyse(self, *args, **kwargs):
|
||||
return Results()
|
||||
|
@@ -4,7 +4,6 @@ from time import sleep
|
||||
|
||||
|
||||
class SleepPlugin(SenpyPlugin):
|
||||
|
||||
def activate(self, *args, **kwargs):
|
||||
sleep(self.timeout)
|
||||
|
||||
|
@@ -12,12 +12,12 @@ from itertools import product
|
||||
def check_dict(indic, template):
|
||||
return all(item in indic.items() for item in template.items())
|
||||
|
||||
|
||||
def parse_resp(resp):
|
||||
return json.loads(resp.data.decode('utf-8'))
|
||||
|
||||
|
||||
class BlueprintsTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.app = Flask("test_extensions")
|
||||
self.client = self.app.test_client()
|
||||
@@ -29,7 +29,7 @@ class BlueprintsTest(TestCase):
|
||||
|
||||
def assertCode(self, resp, code):
|
||||
self.assertEqual(resp.status_code, code)
|
||||
|
||||
|
||||
def test_home(self):
|
||||
"""
|
||||
Calling with no arguments should ask the user for more arguments
|
||||
@@ -55,7 +55,7 @@ class BlueprintsTest(TestCase):
|
||||
js = parse_resp(resp)
|
||||
logging.debug("Got response: %s", js)
|
||||
assert "@context" in js
|
||||
assert "entries" in js
|
||||
assert "entries" in js
|
||||
|
||||
def test_list(self):
|
||||
""" List the plugins """
|
||||
@@ -77,13 +77,13 @@ class BlueprintsTest(TestCase):
|
||||
assert "@context" in js
|
||||
resp = self.client.get("%s&%s=0" % (i, j))
|
||||
js = parse_resp(resp)
|
||||
assert "@context" in js
|
||||
assert "@context" in js
|
||||
resp = self.client.get("%s&%s=1" % (i, j))
|
||||
js = parse_resp(resp)
|
||||
assert "@context" not in js
|
||||
assert "@context" not in js
|
||||
resp = self.client.get("%s&%s=true" % (i, j))
|
||||
js = parse_resp(resp)
|
||||
assert "@context" not in js
|
||||
assert "@context" not in js
|
||||
|
||||
def test_detail(self):
|
||||
""" Show only one plugin"""
|
||||
@@ -110,7 +110,7 @@ class BlueprintsTest(TestCase):
|
||||
resp = self.client.get("/api/plugins/Dummy/")
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
assert "is_activated" in js
|
||||
assert "is_activated" in js
|
||||
assert js["is_activated"] == True
|
||||
|
||||
def test_default(self):
|
||||
@@ -119,7 +119,7 @@ class BlueprintsTest(TestCase):
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
logging.debug(js)
|
||||
assert "@id" in js
|
||||
assert "@id" in js
|
||||
assert js["@id"] == "Dummy_0.1"
|
||||
resp = self.client.get("/api/plugins/Dummy/deactivate")
|
||||
self.assertCode(resp, 200)
|
||||
@@ -140,4 +140,4 @@ class BlueprintsTest(TestCase):
|
||||
resp = self.client.get("/api/schemas/definitions.json")
|
||||
self.assertCode(resp, 200)
|
||||
js = parse_resp(resp)
|
||||
assert "$schema" in js
|
||||
assert "$schema" in js
|
||||
|
@@ -10,7 +10,6 @@ from senpy.models import Error
|
||||
|
||||
|
||||
class CLITest(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
self.assertRaises(Error, partial(main_function, []))
|
||||
res = main_function(['--input', 'test'])
|
||||
|
@@ -2,7 +2,7 @@ from __future__ import print_function
|
||||
import os
|
||||
import logging
|
||||
|
||||
from functools import partial
|
||||
from functools import partial
|
||||
from senpy.extensions import Senpy
|
||||
from senpy.models import Error
|
||||
from flask import Flask
|
||||
@@ -10,7 +10,6 @@ from unittest import TestCase
|
||||
|
||||
|
||||
class ExtensionsTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.app = Flask("test_extensions")
|
||||
self.dir = os.path.join(os.path.dirname(__file__))
|
||||
@@ -39,7 +38,7 @@ class ExtensionsTest(TestCase):
|
||||
'module': 'dummy',
|
||||
'requirements': ['noop'],
|
||||
'version': 0
|
||||
}
|
||||
}
|
||||
root = os.path.join(self.dir, 'dummy_plugin')
|
||||
name, module = self.senpy._load_plugin_from_info(info, root=root)
|
||||
assert name == 'TestPip'
|
||||
@@ -88,4 +87,5 @@ class ExtensionsTest(TestCase):
|
||||
assert self.senpy.filter_plugins(name="Dummy", is_activated=True)
|
||||
self.senpy.deactivate_plugin("Dummy", sync=True)
|
||||
assert not len(
|
||||
self.senpy.filter_plugins(name="Dummy", is_activated=True))
|
||||
self.senpy.filter_plugins(
|
||||
name="Dummy", is_activated=True))
|
||||
|
@@ -12,12 +12,11 @@ from pprint import pprint
|
||||
|
||||
|
||||
class ModelsTest(TestCase):
|
||||
|
||||
def test_jsonld(self):
|
||||
ctx = os.path.normpath(os.path.join(__file__, "..", "..", "..", "senpy", "schemas", "context.jsonld"))
|
||||
prueba = {"id": "test",
|
||||
"analysis": [],
|
||||
"entries": []}
|
||||
ctx = os.path.normpath(
|
||||
os.path.join(__file__, "..", "..", "..", "senpy", "schemas",
|
||||
"context.jsonld"))
|
||||
prueba = {"id": "test", "analysis": [], "entries": []}
|
||||
r = Results(**prueba)
|
||||
print("Response's context: ")
|
||||
pprint(r.context)
|
||||
@@ -27,28 +26,32 @@ class ModelsTest(TestCase):
|
||||
j = r.jsonld(with_context=True)
|
||||
print("As JSON:")
|
||||
pprint(j)
|
||||
assert("@context" in j)
|
||||
assert("marl" in j["@context"])
|
||||
assert("entries" in j["@context"])
|
||||
assert(j["@id"] == "test")
|
||||
assert ("@context" in j)
|
||||
assert ("marl" in j["@context"])
|
||||
assert ("entries" in j["@context"])
|
||||
assert (j["@id"] == "test")
|
||||
assert "id" not in j
|
||||
|
||||
r6 = Results(**prueba)
|
||||
r6.entries.append(Entry({"@id":"ohno", "nif:isString":"Just testing"}))
|
||||
r6.entries.append(
|
||||
Entry({
|
||||
"@id": "ohno",
|
||||
"nif:isString": "Just testing"
|
||||
}))
|
||||
logging.debug("Reponse 6: %s", r6)
|
||||
assert("marl" in r6.context)
|
||||
assert("entries" in r6.context)
|
||||
assert ("marl" in r6.context)
|
||||
assert ("entries" in r6.context)
|
||||
j6 = r6.jsonld(with_context=True)
|
||||
logging.debug("jsonld: %s", j6)
|
||||
assert("@context" in j6)
|
||||
assert("entries" in j6)
|
||||
assert("analysis" in j6)
|
||||
assert ("@context" in j6)
|
||||
assert ("entries" in j6)
|
||||
assert ("analysis" in j6)
|
||||
resp = r6.flask()
|
||||
received = json.loads(resp.data.decode())
|
||||
logging.debug("Response: %s", j6)
|
||||
assert(received["entries"])
|
||||
assert(received["entries"][0]["nif:isString"] == "Just testing")
|
||||
assert(received["entries"][0]["nif:isString"] != "Not testing")
|
||||
assert (received["entries"])
|
||||
assert (received["entries"][0]["nif:isString"] == "Just testing")
|
||||
assert (received["entries"][0]["nif:isString"] != "Not testing")
|
||||
|
||||
def test_id(self):
|
||||
''' Adding the id after creation should overwrite the automatic ID
|
||||
@@ -61,7 +64,6 @@ class ModelsTest(TestCase):
|
||||
assert j2['@id'] == 'test'
|
||||
assert 'id' not in j2
|
||||
|
||||
|
||||
def test_entries(self):
|
||||
e = Entry()
|
||||
self.assertRaises(jsonschema.ValidationError, e.validate)
|
||||
|
@@ -13,8 +13,8 @@ from flask import Flask
|
||||
from senpy.models import Results, Entry
|
||||
from senpy.plugins import SentimentPlugin, ShelfMixin
|
||||
|
||||
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
|
||||
|
||||
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
|
||||
def activate(self, *args, **kwargs):
|
||||
if 'counter' not in self.sh:
|
||||
self.sh['counter'] = 0
|
||||
@@ -24,15 +24,15 @@ class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
|
||||
self.save()
|
||||
|
||||
def analyse(self, *args, **kwargs):
|
||||
self.sh['counter'] = self.sh['counter']+1
|
||||
self.sh['counter'] = self.sh['counter'] + 1
|
||||
e = Entry()
|
||||
e.nif__isString = self.sh['counter']
|
||||
r = Results()
|
||||
r.entries.append(e)
|
||||
return r
|
||||
|
||||
class PluginsTest(TestCase):
|
||||
|
||||
class PluginsTest(TestCase):
|
||||
def tearDown(self):
|
||||
if os.path.exists(self.shelf_dir):
|
||||
shutil.rmtree(self.shelf_dir)
|
||||
@@ -43,10 +43,11 @@ class PluginsTest(TestCase):
|
||||
def setUp(self):
|
||||
self.shelf_dir = tempfile.mkdtemp()
|
||||
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
|
||||
|
||||
|
||||
def test_shelf_file(self):
|
||||
a = ShelfDummyPlugin(info={'name': 'default_shelve_file',
|
||||
'version': 'test'})
|
||||
a = ShelfDummyPlugin(
|
||||
info={'name': 'default_shelve_file',
|
||||
'version': 'test'})
|
||||
assert os.path.dirname(a.shelf_file) == tempfile.gettempdir()
|
||||
a.activate()
|
||||
assert os.path.isfile(a.shelf_file)
|
||||
@@ -54,9 +55,11 @@ class PluginsTest(TestCase):
|
||||
|
||||
def test_shelf(self):
|
||||
''' A shelf is created and the value is stored '''
|
||||
a = ShelfDummyPlugin(info={'name': 'shelve',
|
||||
'version': 'test',
|
||||
'shelf_file': self.shelf_file})
|
||||
a = ShelfDummyPlugin(info={
|
||||
'name': 'shelve',
|
||||
'version': 'test',
|
||||
'shelf_file': self.shelf_file
|
||||
})
|
||||
assert a.sh == {}
|
||||
a.activate()
|
||||
assert a.sh == {'counter': 0}
|
||||
@@ -72,9 +75,11 @@ class PluginsTest(TestCase):
|
||||
assert sh['a'] == 'fromA'
|
||||
|
||||
def test_dummy_shelf(self):
|
||||
a = ShelfDummyPlugin(info={'name': 'DummyShelf',
|
||||
'shelf_file': self.shelf_file,
|
||||
'version': 'test'})
|
||||
a = ShelfDummyPlugin(info={
|
||||
'name': 'DummyShelf',
|
||||
'shelf_file': self.shelf_file,
|
||||
'version': 'test'
|
||||
})
|
||||
a.activate()
|
||||
|
||||
res1 = a.analyse(input=1)
|
||||
@@ -84,17 +89,21 @@ class PluginsTest(TestCase):
|
||||
|
||||
def test_two(self):
|
||||
''' Reusing the values of a previous shelf '''
|
||||
a = ShelfDummyPlugin(info={'name': 'shelve',
|
||||
'version': 'test',
|
||||
'shelf_file': self.shelf_file})
|
||||
a = ShelfDummyPlugin(info={
|
||||
'name': 'shelve',
|
||||
'version': 'test',
|
||||
'shelf_file': self.shelf_file
|
||||
})
|
||||
a.activate()
|
||||
print('Shelf file: %s' % a.shelf_file)
|
||||
a.sh['a'] = 'fromA'
|
||||
a.save()
|
||||
|
||||
b = ShelfDummyPlugin(info={'name': 'shelve',
|
||||
'version': 'test',
|
||||
'shelf_file': self.shelf_file})
|
||||
b = ShelfDummyPlugin(info={
|
||||
'name': 'shelve',
|
||||
'version': 'test',
|
||||
'shelf_file': self.shelf_file
|
||||
})
|
||||
b.activate()
|
||||
assert b.sh['a'] == 'fromA'
|
||||
b.sh['a'] = 'fromB'
|
||||
|
@@ -14,9 +14,11 @@ schema_folder = path.join(root_path, 'senpy', 'schemas')
|
||||
examples_path = path.join(root_path, 'docs', 'examples')
|
||||
bad_examples_path = path.join(root_path, 'docs', 'bad-examples')
|
||||
|
||||
|
||||
class JSONSchemaTests(unittest.TestCase):
|
||||
pass
|
||||
|
||||
|
||||
def do_create_(jsfile, success):
|
||||
def do_expected(self):
|
||||
with open(jsfile) as f:
|
||||
@@ -24,7 +26,8 @@ def do_create_(jsfile, success):
|
||||
try:
|
||||
assert '@type' in js
|
||||
schema_name = js['@type']
|
||||
with open(os.path.join(schema_folder, schema_name+".json")) as file_object:
|
||||
with open(os.path.join(schema_folder, schema_name +
|
||||
".json")) as file_object:
|
||||
schema = json.load(file_object)
|
||||
resolver = RefResolver('file://' + schema_folder + '/', schema)
|
||||
validator = Draft4Validator(schema, resolver=resolver)
|
||||
@@ -32,19 +35,25 @@ def do_create_(jsfile, success):
|
||||
except (AssertionError, ValidationError, KeyError) as ex:
|
||||
if success:
|
||||
raise
|
||||
|
||||
return do_expected
|
||||
|
||||
|
||||
def add_examples(dirname, success):
|
||||
for dirpath, dirnames, filenames in os.walk(dirname):
|
||||
for i in filenames:
|
||||
if fnmatch(i, '*.json'):
|
||||
filename = path.join(dirpath, i)
|
||||
test_method = do_create_(filename, success)
|
||||
test_method.__name__ = 'test_file_%s_success_%s' % (filename, success)
|
||||
test_method.__doc__ = '%s should %svalidate' % (filename, '' if success else 'not' )
|
||||
test_method.__name__ = 'test_file_%s_success_%s' % (filename,
|
||||
success)
|
||||
test_method.__doc__ = '%s should %svalidate' % (filename, ''
|
||||
if success else
|
||||
'not')
|
||||
setattr(JSONSchemaTests, test_method.__name__, test_method)
|
||||
del test_method
|
||||
|
||||
|
||||
add_examples(examples_path, True)
|
||||
add_examples(bad_examples_path, False)
|
||||
|
||||
|
Reference in New Issue
Block a user