2019-07-17 14:29:30 +00:00
|
|
|
#
|
|
|
|
# Copyright 2014 Grupo de Sistemas Inteligentes (GSI) DIT, UPM
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
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
|
2018-12-07 17:30:05 +00:00
|
|
|
from senpy.models import (Analysis,
|
|
|
|
Emotion,
|
2017-02-27 10:37:43 +00:00
|
|
|
EmotionAnalysis,
|
|
|
|
EmotionSet,
|
|
|
|
Entry,
|
2024-12-12 23:01:27 +00:00
|
|
|
ErrorResponse,
|
2017-02-27 10:37:43 +00:00
|
|
|
Results,
|
2017-03-14 12:05:52 +00:00
|
|
|
Sentiment,
|
2018-01-01 12:13:17 +00:00
|
|
|
SentimentPlugin,
|
2017-03-14 12:05:52 +00:00
|
|
|
Plugins,
|
2017-03-14 18:54:33 +00:00
|
|
|
from_string,
|
2024-12-12 23:01:27 +00:00
|
|
|
from_dict)
|
|
|
|
from senpy.errors import Error
|
2017-03-14 12:05:52 +00:00
|
|
|
from senpy import plugins
|
2016-02-19 18:24:09 +00:00
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
|
|
|
|
class ModelsTest(TestCase):
|
|
|
|
def test_jsonld(self):
|
2019-01-07 11:08:19 +00:00
|
|
|
prueba = {"id": "test", "activities": [], "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)
|
2019-01-07 11:08:19 +00:00
|
|
|
assert ("activities" 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):
|
2017-03-14 12:05:52 +00:00
|
|
|
""" Adding the id after creation should overwrite the automatic ID
|
|
|
|
"""
|
2018-12-07 17:30:05 +00:00
|
|
|
r = Entry(_auto_id=True)
|
2016-02-21 01:53:39 +00:00
|
|
|
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):
|
2017-03-14 12:05:52 +00:00
|
|
|
self.assertRaises(Error, plugins.Plugin)
|
2024-12-12 23:01:27 +00:00
|
|
|
p = plugins.SentimentPlugin.parse_obj({"name": "dummy",
|
2018-01-01 12:13:17 +00:00
|
|
|
"description": "I do nothing",
|
|
|
|
"version": 0,
|
|
|
|
"extra_params": {
|
|
|
|
"none": {
|
|
|
|
"options": ["es", ],
|
|
|
|
"required": False,
|
|
|
|
"default": "0"
|
|
|
|
}
|
|
|
|
}})
|
2016-02-19 18:24:09 +00:00
|
|
|
c = p.jsonld()
|
2017-04-07 14:24:28 +00:00
|
|
|
assert '@type' in c
|
2019-01-07 11:08:19 +00:00
|
|
|
assert c['@type'] == 'SentimentPlugin'
|
2017-04-07 14:24:28 +00:00
|
|
|
assert 'info' not in c
|
|
|
|
assert 'repo' not in c
|
|
|
|
assert 'extra_params' in c
|
|
|
|
logging.debug('Framed:')
|
2016-02-19 18:24:09 +00:00
|
|
|
logging.debug(c)
|
|
|
|
p.validate()
|
2017-04-07 14:24:28 +00:00
|
|
|
assert 'es' in c['extra_params']['none']['options']
|
2017-03-14 12:17:09 +00:00
|
|
|
assert isinstance(c['extra_params']['none']['options'], list)
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2017-02-02 03:46:35 +00:00
|
|
|
def test_str(self):
|
|
|
|
"""The string representation shouldn't include private variables"""
|
|
|
|
r = Results()
|
2017-12-30 17:59:58 +00:00
|
|
|
p = plugins.Plugin({"name": "STR test",
|
|
|
|
"description": "Test of private variables.",
|
|
|
|
"version": 0})
|
2017-02-02 03:46:35 +00:00
|
|
|
p._testing = 0
|
|
|
|
s = str(p)
|
|
|
|
assert "_testing" not in s
|
2019-01-07 11:08:19 +00:00
|
|
|
r.activities.append(p)
|
2017-02-02 03:46:35 +00:00
|
|
|
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()
|
2019-01-07 11:08:19 +00:00
|
|
|
res.activities.append(ana)
|
2017-02-27 10:37:43 +00:00
|
|
|
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',
|
2019-01-07 11:08:19 +00:00
|
|
|
'ent[]ry a :entry',
|
2017-02-27 10:37:43 +00:00
|
|
|
' nif:isString "Just testing"',
|
|
|
|
' onyx:hasEmotionSet eSet',
|
|
|
|
'eSet a onyx:EmotionSet',
|
|
|
|
' prov:wasGeneratedBy ana',
|
|
|
|
' onyx:hasEmotion emotion',
|
|
|
|
'emotion a onyx:Emotion',
|
|
|
|
'res a :results',
|
2019-01-07 11:08:19 +00:00
|
|
|
' me:AnalysisInvolved ana',
|
2017-02-27 10:37:43 +00:00
|
|
|
' prov:used entry')
|
|
|
|
|
|
|
|
t = res.serialize(format='turtle')
|
|
|
|
print(t)
|
|
|
|
g = rdflib.Graph().parse(data=t, format='turtle')
|
|
|
|
assert len(g) == len(triples)
|
2017-03-14 12:05:52 +00:00
|
|
|
|
2017-04-07 14:24:28 +00:00
|
|
|
def test_plugin_list(self):
|
|
|
|
"""The plugin list should be of type \"plugins\""""
|
|
|
|
plugs = Plugins()
|
|
|
|
c = plugs.jsonld()
|
|
|
|
assert '@type' in c
|
2019-01-07 11:08:19 +00:00
|
|
|
assert c['@type'] == 'Plugins'
|
2017-04-07 14:24:28 +00:00
|
|
|
|
2017-03-14 12:05:52 +00:00
|
|
|
def test_single_plugin(self):
|
|
|
|
"""A response with a single plugin should still return a list"""
|
|
|
|
plugs = Plugins()
|
2018-01-01 12:13:17 +00:00
|
|
|
p = SentimentPlugin({'id': str(1),
|
|
|
|
'version': 0,
|
|
|
|
'description': 'dummy'})
|
2017-04-10 15:24:39 +00:00
|
|
|
plugs.plugins.append(p)
|
2017-03-14 12:05:52 +00:00
|
|
|
assert isinstance(plugs.plugins, list)
|
|
|
|
js = plugs.jsonld()
|
|
|
|
assert isinstance(js['plugins'], list)
|
2019-01-07 11:08:19 +00:00
|
|
|
assert js['plugins'][0]['@type'] == 'SentimentPlugin'
|
2017-03-14 18:54:33 +00:00
|
|
|
|
2018-12-07 17:30:05 +00:00
|
|
|
def test_parameters(self):
|
|
|
|
'''An Analysis should contain the algorithm and the list of parameters to be used'''
|
|
|
|
a = Analysis()
|
|
|
|
a.params = {'param1': 1, 'param2': 2}
|
|
|
|
assert len(a.parameters) == 2
|
|
|
|
for param in a.parameters:
|
|
|
|
if param.name == 'param1':
|
|
|
|
assert param.value == 1
|
|
|
|
elif param.name == 'param2':
|
|
|
|
assert param.value == 2
|
|
|
|
else:
|
|
|
|
raise Exception('Unknown value %s' % param)
|
|
|
|
|
2017-03-14 18:54:33 +00:00
|
|
|
def test_from_string(self):
|
|
|
|
results = {
|
2019-01-07 11:08:19 +00:00
|
|
|
'@type': 'Results',
|
2017-03-14 18:54:33 +00:00
|
|
|
'@id': 'prueba',
|
|
|
|
'entries': [{
|
|
|
|
'@id': 'entry1',
|
2019-01-07 11:08:19 +00:00
|
|
|
'@type': 'Entry',
|
2017-06-21 17:58:18 +00:00
|
|
|
'nif:isString': 'TEST'
|
2017-03-14 18:54:33 +00:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
recovered = from_dict(results)
|
2018-01-01 12:13:17 +00:00
|
|
|
assert recovered.id == results['@id']
|
2017-03-14 18:54:33 +00:00
|
|
|
assert isinstance(recovered, Results)
|
|
|
|
assert isinstance(recovered.entries[0], Entry)
|
|
|
|
|
|
|
|
string = json.dumps(results)
|
|
|
|
recovered = from_string(string)
|
|
|
|
assert isinstance(recovered, Results)
|
|
|
|
assert isinstance(recovered.entries[0], Entry)
|
2018-01-18 14:50:46 +00:00
|
|
|
|
|
|
|
def test_serializable(self):
|
|
|
|
r = Results()
|
|
|
|
e = Entry()
|
|
|
|
r.entries.append(e)
|
|
|
|
d = r.serializable()
|
|
|
|
assert d
|
|
|
|
assert d['entries']
|
2019-01-07 11:08:19 +00:00
|
|
|
|
|
|
|
def test_template(self):
|
|
|
|
r = Results()
|
|
|
|
e = Entry()
|
|
|
|
e.nif__isString = 'testing the template'
|
|
|
|
sent = Sentiment()
|
|
|
|
sent.polarity = 'marl:Positive'
|
|
|
|
r.entries.append(e)
|
|
|
|
e.sentiments.append(sent)
|
|
|
|
template = ('{% for entry in entries %}'
|
|
|
|
'{{ entry["nif:isString"] | upper }}'
|
|
|
|
',{{entry.sentiments[0]["marl:hasPolarity"].split(":")[1]}}'
|
|
|
|
'{% endfor %}')
|
|
|
|
res = r.serialize(template=template)
|
|
|
|
assert res == 'TESTING THE TEMPLATE,Positive'
|