2016-02-19 18:24:09 +00:00
|
|
|
#!/bin/env python
|
|
|
|
|
2015-11-05 17:50:37 +00:00
|
|
|
import os
|
2016-02-21 18:36:24 +00:00
|
|
|
import pickle
|
2016-02-19 18:24:09 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2015-11-05 17:50:37 +00:00
|
|
|
|
|
|
|
from unittest import TestCase
|
2017-06-12 17:40:26 +00:00
|
|
|
from senpy.models import Results, Entry, EmotionSet, Emotion
|
2017-07-12 15:41:14 +00:00
|
|
|
from senpy import plugins
|
2017-06-12 17:40:26 +00:00
|
|
|
from senpy.plugins.conversion.emotion.centroids import CentroidConversion
|
2015-11-05 17:50:37 +00:00
|
|
|
|
|
|
|
|
2017-07-12 15:41:14 +00:00
|
|
|
class ShelfDummyPlugin(plugins.SentimentPlugin, plugins.ShelfMixin):
|
2016-12-19 17:18:47 +00:00
|
|
|
def activate(self, *args, **kwargs):
|
|
|
|
if 'counter' not in self.sh:
|
|
|
|
self.sh['counter'] = 0
|
|
|
|
self.save()
|
2015-12-11 13:53:30 +00:00
|
|
|
|
2016-12-19 17:18:47 +00:00
|
|
|
def deactivate(self, *args, **kwargs):
|
|
|
|
self.save()
|
2015-12-11 13:53:30 +00:00
|
|
|
|
2016-12-19 17:18:47 +00:00
|
|
|
def analyse(self, *args, **kwargs):
|
2017-01-10 09:16:45 +00:00
|
|
|
self.sh['counter'] = self.sh['counter'] + 1
|
2016-12-19 17:18:47 +00:00
|
|
|
e = Entry()
|
|
|
|
e.nif__isString = self.sh['counter']
|
|
|
|
r = Results()
|
|
|
|
r.entries.append(e)
|
|
|
|
return r
|
2015-12-11 13:53:30 +00:00
|
|
|
|
|
|
|
|
2017-01-10 09:16:45 +00:00
|
|
|
class PluginsTest(TestCase):
|
2015-12-11 13:53:30 +00:00
|
|
|
def tearDown(self):
|
2016-02-19 18:24:09 +00:00
|
|
|
if os.path.exists(self.shelf_dir):
|
|
|
|
shutil.rmtree(self.shelf_dir)
|
2015-12-11 13:53:30 +00:00
|
|
|
if os.path.isfile(self.shelf_file):
|
|
|
|
os.remove(self.shelf_file)
|
2015-11-05 17:50:37 +00:00
|
|
|
|
2016-02-19 18:24:09 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.shelf_dir = tempfile.mkdtemp()
|
|
|
|
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
|
2017-01-10 09:16:45 +00:00
|
|
|
|
2016-12-19 17:18:47 +00:00
|
|
|
def test_shelf_file(self):
|
2017-01-10 09:16:45 +00:00
|
|
|
a = ShelfDummyPlugin(
|
|
|
|
info={'name': 'default_shelve_file',
|
|
|
|
'version': 'test'})
|
2016-12-19 17:18:47 +00:00
|
|
|
a.activate()
|
|
|
|
assert os.path.isfile(a.shelf_file)
|
|
|
|
os.remove(a.shelf_file)
|
|
|
|
|
2015-11-05 17:50:37 +00:00
|
|
|
def test_shelf(self):
|
2015-12-11 13:53:30 +00:00
|
|
|
''' A shelf is created and the value is stored '''
|
2017-08-19 21:53:17 +00:00
|
|
|
newfile = self.shelf_file + "new"
|
2017-01-10 09:16:45 +00:00
|
|
|
a = ShelfDummyPlugin(info={
|
|
|
|
'name': 'shelve',
|
|
|
|
'version': 'test',
|
2017-08-19 21:53:17 +00:00
|
|
|
'shelf_file': newfile
|
2017-01-10 09:16:45 +00:00
|
|
|
})
|
2015-12-11 13:53:30 +00:00
|
|
|
assert a.sh == {}
|
2016-12-19 17:18:47 +00:00
|
|
|
a.activate()
|
|
|
|
assert a.sh == {'counter': 0}
|
2017-08-19 21:53:17 +00:00
|
|
|
assert a.shelf_file == newfile
|
2015-12-11 13:53:30 +00:00
|
|
|
|
|
|
|
a.sh['a'] = 'fromA'
|
2016-12-19 17:18:47 +00:00
|
|
|
assert a.sh['a'] == 'fromA'
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2016-02-21 18:36:24 +00:00
|
|
|
a.save()
|
|
|
|
|
2017-08-19 21:53:17 +00:00
|
|
|
sh = pickle.load(open(newfile, 'rb'))
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2015-12-11 13:53:30 +00:00
|
|
|
assert sh['a'] == 'fromA'
|
|
|
|
|
2016-12-19 17:18:47 +00:00
|
|
|
def test_dummy_shelf(self):
|
2017-08-19 21:53:17 +00:00
|
|
|
with open(self.shelf_file, 'wb') as f:
|
|
|
|
pickle.dump({'counter': 99}, f)
|
2017-01-10 09:16:45 +00:00
|
|
|
a = ShelfDummyPlugin(info={
|
|
|
|
'name': 'DummyShelf',
|
|
|
|
'shelf_file': self.shelf_file,
|
|
|
|
'version': 'test'
|
|
|
|
})
|
2016-12-19 17:18:47 +00:00
|
|
|
a.activate()
|
|
|
|
|
2017-02-27 10:37:43 +00:00
|
|
|
assert a.shelf_file == self.shelf_file
|
2016-12-19 17:18:47 +00:00
|
|
|
res1 = a.analyse(input=1)
|
2017-08-19 21:53:17 +00:00
|
|
|
assert res1.entries[0].nif__isString == 100
|
|
|
|
a.deactivate()
|
|
|
|
del a
|
|
|
|
|
|
|
|
with open(self.shelf_file, 'rb') as f:
|
|
|
|
sh = pickle.load(f)
|
|
|
|
assert sh['counter'] == 100
|
2015-12-11 13:53:30 +00:00
|
|
|
|
2017-05-17 14:25:13 +00:00
|
|
|
def test_corrupt_shelf(self):
|
|
|
|
''' Reusing the values of a previous shelf '''
|
|
|
|
|
|
|
|
emptyfile = os.path.join(self.shelf_dir, "emptyfile")
|
|
|
|
invalidfile = os.path.join(self.shelf_dir, "invalid_file")
|
|
|
|
with open(emptyfile, 'w+b'), open(invalidfile, 'w+b') as inf:
|
|
|
|
inf.write(b'ohno')
|
|
|
|
|
2017-05-17 14:51:01 +00:00
|
|
|
files = {emptyfile: ['empty file', (EOFError, IndexError)],
|
|
|
|
invalidfile: ['invalid file', (pickle.UnpicklingError, IndexError)]}
|
2017-05-17 14:25:13 +00:00
|
|
|
|
|
|
|
for fn in files:
|
|
|
|
with open(fn, 'rb') as f:
|
|
|
|
msg, error = files[fn]
|
|
|
|
a = ShelfDummyPlugin(info={
|
|
|
|
'name': 'shelve',
|
|
|
|
'version': 'test',
|
|
|
|
'shelf_file': f.name
|
|
|
|
})
|
|
|
|
assert os.path.isfile(a.shelf_file)
|
|
|
|
print('Shelf file: %s' % a.shelf_file)
|
|
|
|
with self.assertRaises(error):
|
|
|
|
a.sh['a'] = 'fromA'
|
|
|
|
a.save()
|
|
|
|
del a._sh
|
|
|
|
assert os.path.isfile(a.shelf_file)
|
|
|
|
a.force_shelf = True
|
|
|
|
a.sh['a'] = 'fromA'
|
|
|
|
a.save()
|
|
|
|
b = pickle.load(f)
|
|
|
|
assert b['a'] == 'fromA'
|
|
|
|
|
|
|
|
def test_reuse_shelf(self):
|
2015-12-11 13:53:30 +00:00
|
|
|
''' Reusing the values of a previous shelf '''
|
2017-01-10 09:16:45 +00:00
|
|
|
a = ShelfDummyPlugin(info={
|
|
|
|
'name': 'shelve',
|
|
|
|
'version': 'test',
|
|
|
|
'shelf_file': self.shelf_file
|
|
|
|
})
|
2016-12-19 17:18:47 +00:00
|
|
|
a.activate()
|
2015-12-11 13:53:30 +00:00
|
|
|
print('Shelf file: %s' % a.shelf_file)
|
|
|
|
a.sh['a'] = 'fromA'
|
2016-02-21 18:36:24 +00:00
|
|
|
a.save()
|
2015-12-11 13:53:30 +00:00
|
|
|
|
2017-01-10 09:16:45 +00:00
|
|
|
b = ShelfDummyPlugin(info={
|
|
|
|
'name': 'shelve',
|
|
|
|
'version': 'test',
|
|
|
|
'shelf_file': self.shelf_file
|
|
|
|
})
|
2016-12-19 17:18:47 +00:00
|
|
|
b.activate()
|
|
|
|
assert b.sh['a'] == 'fromA'
|
2015-12-11 13:53:30 +00:00
|
|
|
b.sh['a'] = 'fromB'
|
|
|
|
assert b.sh['a'] == 'fromB'
|
2017-02-27 10:37:43 +00:00
|
|
|
|
|
|
|
def test_extra_params(self):
|
|
|
|
''' Should be able to set extra parameters'''
|
|
|
|
a = ShelfDummyPlugin(info={
|
|
|
|
'name': 'shelve',
|
|
|
|
'version': 'test',
|
|
|
|
'shelf_file': self.shelf_file,
|
|
|
|
'extra_params': {
|
|
|
|
'example': {
|
|
|
|
'aliases': ['example', 'ex'],
|
|
|
|
'required': True,
|
|
|
|
'default': 'nonsense'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert 'example' in a.extra_params
|
2017-06-12 17:40:26 +00:00
|
|
|
|
|
|
|
def test_conversion_centroids(self):
|
|
|
|
info = {
|
|
|
|
"name": "CentroidTest",
|
|
|
|
"description": "Centroid test",
|
|
|
|
"version": 0,
|
|
|
|
"centroids": {
|
|
|
|
"c1": {"V1": 0.5,
|
|
|
|
"V2": 0.5},
|
|
|
|
"c2": {"V1": -0.5,
|
|
|
|
"V2": 0.5},
|
|
|
|
"c3": {"V1": -0.5,
|
|
|
|
"V2": -0.5},
|
|
|
|
"c4": {"V1": 0.5,
|
|
|
|
"V2": -0.5}},
|
|
|
|
"aliases": {
|
|
|
|
"V1": "X-dimension",
|
|
|
|
"V2": "Y-dimension"
|
|
|
|
},
|
|
|
|
"centroids_direction": ["emoml:big6", "emoml:fsre-dimensions"]
|
|
|
|
}
|
|
|
|
c = CentroidConversion(info)
|
2017-06-16 10:44:25 +00:00
|
|
|
print(c.serialize())
|
2017-06-12 17:40:26 +00:00
|
|
|
|
|
|
|
es1 = EmotionSet()
|
|
|
|
e1 = Emotion()
|
|
|
|
e1.onyx__hasEmotionCategory = "c1"
|
|
|
|
es1.onyx__hasEmotion.append(e1)
|
|
|
|
res = c._forward_conversion(es1)
|
|
|
|
assert res["X-dimension"] == 0.5
|
|
|
|
assert res["Y-dimension"] == 0.5
|
2017-06-16 10:44:25 +00:00
|
|
|
print(res)
|
2017-06-12 17:40:26 +00:00
|
|
|
|
|
|
|
e2 = Emotion()
|
|
|
|
e2.onyx__hasEmotionCategory = "c2"
|
|
|
|
es1.onyx__hasEmotion.append(e2)
|
|
|
|
res = c._forward_conversion(es1)
|
|
|
|
assert res["X-dimension"] == 0
|
|
|
|
assert res["Y-dimension"] == 1
|
2017-06-16 10:44:25 +00:00
|
|
|
print(res)
|
2017-06-12 17:40:26 +00:00
|
|
|
|
|
|
|
e = Emotion()
|
|
|
|
e["X-dimension"] = -0.2
|
|
|
|
e["Y-dimension"] = -0.3
|
|
|
|
res = c._backwards_conversion(e)
|
|
|
|
assert res["onyx:hasEmotionCategory"] == "c3"
|
2017-06-16 10:44:25 +00:00
|
|
|
print(res)
|
2017-06-12 17:40:26 +00:00
|
|
|
|
|
|
|
e = Emotion()
|
|
|
|
e["X-dimension"] = -0.2
|
|
|
|
e["Y-dimension"] = 0.3
|
|
|
|
res = c._backwards_conversion(e)
|
|
|
|
assert res["onyx:hasEmotionCategory"] == "c2"
|
2017-07-12 15:41:14 +00:00
|
|
|
|
|
|
|
|
2017-08-27 16:43:40 +00:00
|
|
|
def make_mini_test(plugin_info):
|
2017-07-12 15:41:14 +00:00
|
|
|
def mini_test(self):
|
2017-08-27 16:43:40 +00:00
|
|
|
plugin = plugins.load_plugin_from_info(plugin_info, install=True)
|
2017-07-12 15:41:14 +00:00
|
|
|
plugin.test()
|
|
|
|
return mini_test
|
|
|
|
|
|
|
|
|
2017-08-27 16:43:40 +00:00
|
|
|
def _add_tests():
|
2017-07-12 15:41:14 +00:00
|
|
|
root = os.path.dirname(__file__)
|
2017-08-27 16:43:40 +00:00
|
|
|
plugs = plugins.load_plugins(os.path.join(root, ".."), loader=plugins.parse_plugin_info)
|
2017-07-12 15:41:14 +00:00
|
|
|
for k, v in plugs.items():
|
2017-08-27 16:43:40 +00:00
|
|
|
pass
|
2017-07-12 15:41:14 +00:00
|
|
|
t_method = make_mini_test(v)
|
|
|
|
t_method.__name__ = 'test_plugin_{}'.format(k)
|
|
|
|
setattr(PluginsTest, t_method.__name__, t_method)
|
|
|
|
del t_method
|
|
|
|
|
|
|
|
|
2017-08-27 16:43:40 +00:00
|
|
|
_add_tests()
|