2016-02-19 18:24:09 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import jsonschema
|
|
|
|
|
|
|
|
import json
|
2017-02-27 10:37:43 +00:00
|
|
|
import rdflib
|
2016-02-19 18:24:09 +00:00
|
|
|
from unittest import TestCase
|
2017-02-27 10:37:43 +00:00
|
|
|
from senpy.models import (Emotion,
|
|
|
|
EmotionAnalysis,
|
|
|
|
EmotionSet,
|
|
|
|
Entry,
|
|
|
|
Error,
|
|
|
|
Results,
|
|
|
|
Sentiment)
|
2016-02-19 18:24:09 +00:00
|
|
|
from senpy.plugins import SenpyPlugin
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
|
|
|
|
class ModelsTest(TestCase):
|
|
|
|
def test_jsonld(self):
|
2017-02-27 10:37:43 +00:00
|
|
|
prueba = {"id": "test", "analysis": [], "entries": []}
|
2016-02-19 18:24:09 +00:00
|
|
|
r = Results(**prueba)
|
|
|
|
print("Response's context: ")
|
2017-02-27 10:37:43 +00:00
|
|
|
pprint(r._context)
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2016-02-21 01:53:39 +00:00
|
|
|
assert r.id == "test"
|
|
|
|
|
2016-02-19 18:24:09 +00:00
|
|
|
j = r.jsonld(with_context=True)
|
|
|
|
print("As JSON:")
|
|
|
|
pprint(j)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert ("@context" in j)
|
|
|
|
assert ("marl" in j["@context"])
|
|
|
|
assert ("entries" in j["@context"])
|
|
|
|
assert (j["@id"] == "test")
|
2016-02-21 01:53:39 +00:00
|
|
|
assert "id" not in j
|
2016-02-19 18:24:09 +00:00
|
|
|
|
|
|
|
r6 = Results(**prueba)
|
2017-02-27 10:37:43 +00:00
|
|
|
e = Entry({"@id": "ohno", "nif:isString": "Just testing"})
|
2017-02-08 20:55:17 +00:00
|
|
|
r6.entries.append(e)
|
2016-02-19 18:24:09 +00:00
|
|
|
logging.debug("Reponse 6: %s", r6)
|
2017-02-27 10:37:43 +00:00
|
|
|
assert ("marl" in r6._context)
|
|
|
|
assert ("entries" in r6._context)
|
2016-02-19 18:24:09 +00:00
|
|
|
j6 = r6.jsonld(with_context=True)
|
|
|
|
logging.debug("jsonld: %s", j6)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert ("@context" in j6)
|
|
|
|
assert ("entries" in j6)
|
|
|
|
assert ("analysis" in j6)
|
2016-02-19 18:24:09 +00:00
|
|
|
resp = r6.flask()
|
|
|
|
received = json.loads(resp.data.decode())
|
|
|
|
logging.debug("Response: %s", j6)
|
2017-01-10 09:16:45 +00:00
|
|
|
assert (received["entries"])
|
|
|
|
assert (received["entries"][0]["nif:isString"] == "Just testing")
|
|
|
|
assert (received["entries"][0]["nif:isString"] != "Not testing")
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2016-02-21 01:53:39 +00:00
|
|
|
def test_id(self):
|
|
|
|
''' Adding the id after creation should overwrite the automatic ID
|
|
|
|
'''
|
|
|
|
r = Entry()
|
|
|
|
j = r.jsonld()
|
|
|
|
assert '@id' in j
|
|
|
|
r.id = "test"
|
|
|
|
j2 = r.jsonld()
|
|
|
|
assert j2['@id'] == 'test'
|
|
|
|
assert 'id' not in j2
|
|
|
|
|
2016-02-19 18:24:09 +00:00
|
|
|
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 = ""
|
|
|
|
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_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
|
2016-02-21 18:36:24 +00:00
|
|
|
assert "extra_params" in c
|
2016-02-19 18:24:09 +00:00
|
|
|
logging.debug("Framed:")
|
|
|
|
logging.debug(c)
|
|
|
|
p.validate()
|
|
|
|
|
2017-02-02 03:46:35 +00:00
|
|
|
def test_str(self):
|
|
|
|
"""The string representation shouldn't include private variables"""
|
|
|
|
r = Results()
|
|
|
|
p = SenpyPlugin({"name": "STR test", "version": 0})
|
|
|
|
p._testing = 0
|
|
|
|
s = str(p)
|
|
|
|
assert "_testing" not in s
|
|
|
|
r.analysis.append(p)
|
|
|
|
s = str(r)
|
|
|
|
assert "_testing" not in s
|
|
|
|
|
2017-02-27 10:37:43 +00:00
|
|
|
def test_turtle(self):
|
|
|
|
"""Any model should be serializable as a turtle file"""
|
|
|
|
ana = EmotionAnalysis()
|
|
|
|
res = Results()
|
|
|
|
res.analysis.append(ana)
|
|
|
|
entry = Entry(text='Just testing')
|
|
|
|
eSet = EmotionSet()
|
|
|
|
emotion = Emotion()
|
|
|
|
entry.emotions.append(eSet)
|
|
|
|
res.entries.append(entry)
|
|
|
|
eSet.onyx__hasEmotion.append(emotion)
|
|
|
|
eSet.prov__wasGeneratedBy = ana.id
|
|
|
|
triples = ('ana a :Analysis',
|
|
|
|
'entry a :entry',
|
|
|
|
' nif:isString "Just testing"',
|
|
|
|
' onyx:hasEmotionSet eSet',
|
|
|
|
'eSet a onyx:EmotionSet',
|
|
|
|
' prov:wasGeneratedBy ana',
|
|
|
|
' onyx:hasEmotion emotion',
|
|
|
|
'emotion a onyx:Emotion',
|
|
|
|
'res a :results',
|
|
|
|
' me:AnalysisInvoloved ana',
|
|
|
|
' prov:used entry')
|
|
|
|
|
|
|
|
t = res.serialize(format='turtle')
|
|
|
|
print(t)
|
|
|
|
g = rdflib.Graph().parse(data=t, format='turtle')
|
|
|
|
assert len(g) == len(triples)
|
|
|
|
|
|
|
|
def test_convert_emotions(self):
|
|
|
|
"""It should be possible to convert between different emotion models"""
|
2016-02-19 18:24:09 +00:00
|
|
|
pass
|