1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-08-23 10:02:21 +00:00

Python 3 compatible

There are also some slight changes to the JSON schemas and the use of
JSON-LD.
This commit is contained in:
J. Fernando Sánchez
2016-02-19 19:24:09 +01:00
parent a79df7a3da
commit 14c9f61864
32 changed files with 621 additions and 349 deletions

View File

@@ -1,40 +0,0 @@
{
"dc": "http://purl.org/dc/terms/",
"dc:subject": {
"@type": "@id"
},
"xsd": "http://www.w3.org/2001/XMLSchema#",
"marl": "http://www.gsi.dit.upm.es/ontologies/marl/ns#",
"nif": "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#",
"onyx": "http://www.gsi.dit.upm.es/ontologies/onyx/ns#",
"emotions": {
"@container": "@set",
"@id": "onyx:hasEmotionSet"
},
"opinions": {
"@container": "@set",
"@id": "marl:hasOpinion"
},
"prov": "http://www.w3.org/ns/prov#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"analysis": {
"@container": "@set",
"@id": "prov:wasInformedBy"
},
"entries": {
"@container": "@set",
"@id": "prov:generated"
},
"strings": {
"@container": "@set",
"@reverse": "nif:hasContext"
},
"date":
{
"@id": "dc:date",
"@type": "xsd:dateTime"
},
"text": { "@id": "nif:isString" },
"wnaffect": "http://www.gsi.dit.upm.es/ontologies/wnaffect#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
}

View File

@@ -1,8 +1,8 @@
from senpy.plugins import SentimentPlugin
from senpy.models import Response
from senpy.models import Results
class DummyPlugin(SentimentPlugin):
def analyse(self, *args, **kwargs):
return Response()
return Results()

View File

@@ -1,81 +0,0 @@
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
import json
import os
from unittest import TestCase
from senpy.models import Response, Entry
from senpy.plugins import SenpyPlugin
class ModelsTest(TestCase):
def test_response(self):
r = Response(context=os.path.normpath(
os.path.join(__file__, "..", "..", "context.jsonld")))
assert("@context" in r)
assert(r._frame)
logging.debug("Default frame: %s", r._frame)
assert("marl" in r.context)
assert("entries" in r.context)
r2 = Response(context=json.loads('{"test": "roger"}'))
assert("test" in r2.context)
r3 = Response(context=None)
del r3.context
assert("@context" not in r3)
assert("entries" in r3)
assert("analysis" in r3)
r4 = Response()
assert("@context" in r4)
assert("entries" in r4)
assert("analysis" in r4)
dummy = SenpyPlugin({"name": "dummy", "version": 0})
r5 = Response({"dummy": dummy}, context=None, frame=None)
logging.debug("Response 5: %s", r5)
assert("dummy" in r5)
assert(r5["dummy"].name == "dummy")
js = r5.jsonld(context={}, frame={})
logging.debug("jsonld 5: %s", js)
assert("dummy" in js)
assert(js["dummy"].name == "dummy")
r6 = Response()
r6.entries.append(Entry(text="Just testing"))
logging.debug("Reponse 6: %s", r6)
assert("@context" in r6)
assert("marl" in r6.context)
assert("entries" in r6.context)
js = r6.jsonld()
logging.debug("jsonld: %s", js)
assert("entries" in js)
assert("entries" in js)
assert("analysis" in js)
resp = r6.flask()
received = json.loads(resp.data.decode())
logging.debug("Response: %s", js)
assert(received["entries"])
assert(received["entries"][0]["text"] == "Just testing")
assert(received["entries"][0]["text"] != "Not testing")
def test_opinions(self):
pass
def test_plugins(self):
p = SenpyPlugin({"name": "dummy", "version": 0})
c = p.jsonld()
assert "info" not in c
assert "repo" not in c
assert "params" not in c
logging.debug("Framed: %s", c)
assert "extra_params" in c
def test_frame_response(self):
pass

View File

@@ -1,5 +1,5 @@
from senpy.plugins import SenpyPlugin
from senpy.models import Response
from senpy.models import Results
from time import sleep
@@ -14,4 +14,4 @@ class SleepPlugin(SenpyPlugin):
def analyse(self, *args, **kwargs):
sleep(float(kwargs.get("timeout", self.timeout)))
return Response()
return Results()

View File

@@ -1,10 +1,6 @@
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase
@@ -31,7 +27,7 @@ class BlueprintsTest(TestCase):
"""
Calling with no arguments should ask the user for more arguments
"""
resp = self.client.get("/api")
resp = self.client.get("/api/")
self.assert404(resp)
logging.debug(resp.json)
assert resp.json["status"] == 404
@@ -46,7 +42,7 @@ class BlueprintsTest(TestCase):
The dummy plugin returns an empty response,\
it should contain the context
"""
resp = self.client.get("/api?i=My aloha mohame")
resp = self.client.get("/api/?i=My aloha mohame")
self.assert200(resp)
logging.debug("Got response: %s", resp.json)
assert "@context" in resp.json
@@ -64,7 +60,7 @@ class BlueprintsTest(TestCase):
assert "@context" in resp.json
def test_headers(self):
for i, j in product(["/api/plugins/?nothing=", "/api?i=test&"],
for i, j in product(["/api/plugins/?nothing=", "/api/?i=test&"],
["headers", "inHeaders"]):
resp = self.client.get("%s" % (i))
assert "@context" in resp.json
@@ -77,7 +73,7 @@ class BlueprintsTest(TestCase):
def test_detail(self):
""" Show only one plugin"""
resp = self.client.get("/api/plugins/Dummy")
resp = self.client.get("/api/plugins/Dummy/")
self.assert200(resp)
logging.debug(resp.json)
assert "@id" in resp.json
@@ -88,14 +84,14 @@ class BlueprintsTest(TestCase):
resp = self.client.get("/api/plugins/Dummy/deactivate")
self.assert200(resp)
sleep(0.5)
resp = self.client.get("/api/plugins/Dummy")
resp = self.client.get("/api/plugins/Dummy/")
self.assert200(resp)
assert "is_activated" in resp.json
assert resp.json["is_activated"] == False
resp = self.client.get("/api/plugins/Dummy/activate")
self.assert200(resp)
sleep(0.5)
resp = self.client.get("/api/plugins/Dummy")
resp = self.client.get("/api/plugins/Dummy/")
self.assert200(resp)
assert "is_activated" in resp.json
assert resp.json["is_activated"] == True

View File

@@ -2,10 +2,6 @@ from __future__ import print_function
import os
import logging
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
from flask.ext.testing import TestCase
@@ -15,7 +11,7 @@ class ExtensionsTest(TestCase):
def create_app(self):
self.app = Flask("test_extensions")
self.dir = os.path.join(os.path.dirname(__file__), "..")
self.dir = os.path.join(os.path.dirname(__file__))
self.senpy = Senpy(plugin_folder=self.dir, default_plugins=False)
self.senpy.init_app(self.app)
self.senpy.activate_plugin("Dummy", sync=True)
@@ -60,7 +56,7 @@ class ExtensionsTest(TestCase):
self.senpy.deactivate_all(sync=True)
resp = self.senpy.analyse(input="tupni")
logging.debug("Response: {}".format(resp))
assert resp["status"] == 404
assert resp.status == 404
def test_analyse(self):
""" Using a plugin """
@@ -75,7 +71,7 @@ class ExtensionsTest(TestCase):
self.senpy.deactivate_plugin(plug, sync=True)
resp = self.senpy.analyse(input="tupni")
logging.debug("Response: {}".format(resp))
assert resp["status"] == 404
assert resp.status == 404
def test_filtering(self):

97
tests/test_models.py Normal file
View File

@@ -0,0 +1,97 @@
import os
import logging
import jsonschema
import json
import os
from unittest import TestCase
from senpy.models import Response, Entry, Results, Sentiment, EmotionSet, Error
from senpy.plugins import SenpyPlugin
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": []}
r = Results(**prueba)
print("Response's context: ")
pprint(r.context)
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"])
r6 = Results(**prueba)
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)
j6 = r6.jsonld(with_context=True)
logging.debug("jsonld: %s", 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")
def test_entries(self):
e = Entry()
self.assertRaises(jsonschema.ValidationError, e.validate)
e.nif__isString = "this is a test"
e.nif__beginIndex = 0
e.nif__endIndex = 10
e.validate()
def test_sentiment(self):
s = Sentiment()
self.assertRaises(jsonschema.ValidationError, s.validate)
s.nif__anchorOf = "so much testing"
s.prov__wasGeneratedBy = ""
s.validate()
def test_emotion_set(self):
e = EmotionSet()
self.assertRaises(jsonschema.ValidationError, e.validate)
e.nif__anchorOf = "so much testing"
e.prov__wasGeneratedBy = ""
self.assertRaises(jsonschema.ValidationError, e.validate)
e.onyx__hasEmotion = {}
e.validate()
def test_results(self):
r = Results()
e = Entry()
e.nif__isString = "Results test"
r.entries.append(e)
r.id = ":test_results"
r.validate()
def test_sentiments(self):
pass
def test_plugins(self):
self.assertRaises(Error, SenpyPlugin)
p = SenpyPlugin({"name": "dummy", "version": 0})
c = p.jsonld()
assert "info" not in c
assert "repo" not in c
assert "params" in c
logging.debug("Framed:")
logging.debug(c)
p.validate()
def test_frame_response(self):
pass

View File

@@ -1,17 +1,15 @@
#!/bin/env python2
# -*- py-which-shell: "python2"; -*-
#!/bin/env python
import os
import logging
import shelve
import shutil
import tempfile
try:
import unittest.mock as mock
except ImportError:
import mock
import json
import os
from unittest import TestCase
from senpy.models import Response, Entry
from senpy.models import Results, Entry
from senpy.plugins import SenpyPlugin, ShelfMixin
@@ -27,14 +25,18 @@ class ShelfTest(ShelfMixin, SenpyPlugin):
class ModelsTest(TestCase):
shelf_file = 'shelf_test.db'
def tearDown(self):
if os.path.exists(self.shelf_dir):
shutil.rmtree(self.shelf_dir)
if os.path.isfile(self.shelf_file):
os.remove(self.shelf_file)
setUp = tearDown
def setUp(self):
self.shelf_dir = tempfile.mkdtemp()
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
def test_shelf(self):
''' A shelf is created and the value is stored '''
@@ -45,11 +47,10 @@ class ModelsTest(TestCase):
assert a.shelf_file == self.shelf_file
a.sh['a'] = 'fromA'
a.test(key='a', value='fromA')
del(a)
assert os.path.isfile(self.shelf_file)
sh = shelve.open(self.shelf_file)
assert sh['a'] == 'fromA'