1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-24 09:02:28 +00:00
senpy/tests/test_plugins.py

123 lines
3.3 KiB
Python
Raw Permalink Normal View History

#!/bin/env python
2015-11-05 17:50:37 +00:00
import os
import pickle
import shutil
import tempfile
2015-11-05 17:50:37 +00:00
from unittest import TestCase
from senpy.models import Results, Entry
from senpy.plugins import SentimentPlugin, ShelfMixin
2015-11-05 17:50:37 +00:00
2017-01-10 09:16:45 +00:00
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
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
def deactivate(self, *args, **kwargs):
self.save()
2015-12-11 13:53:30 +00:00
def analyse(self, *args, **kwargs):
2017-01-10 09:16:45 +00:00
self.sh['counter'] = self.sh['counter'] + 1
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):
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
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
def test_shelf_file(self):
2017-01-10 09:16:45 +00:00
a = ShelfDummyPlugin(
info={'name': 'default_shelve_file',
'version': 'test'})
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-01-10 09:16:45 +00:00
a = ShelfDummyPlugin(info={
'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file
})
2015-12-11 13:53:30 +00:00
assert a.sh == {}
a.activate()
assert a.sh == {'counter': 0}
2015-12-11 13:53:30 +00:00
assert a.shelf_file == self.shelf_file
a.sh['a'] = 'fromA'
assert a.sh['a'] == 'fromA'
a.save()
sh = pickle.load(open(self.shelf_file, 'rb'))
2015-12-11 13:53:30 +00:00
assert sh['a'] == 'fromA'
def test_dummy_shelf(self):
2017-01-10 09:16:45 +00:00
a = ShelfDummyPlugin(info={
'name': 'DummyShelf',
'shelf_file': self.shelf_file,
'version': 'test'
})
a.activate()
assert a.shelf_file == self.shelf_file
res1 = a.analyse(input=1)
assert res1.entries[0].nif__isString == 1
res2 = a.analyse(input=1)
assert res2.entries[0].nif__isString == 2
2015-12-11 13:53:30 +00:00
def test_two(self):
''' 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
})
a.activate()
2015-12-11 13:53:30 +00:00
print('Shelf file: %s' % a.shelf_file)
a.sh['a'] = 'fromA'
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
})
b.activate()
assert b.sh['a'] == 'fromA'
2015-12-11 13:53:30 +00:00
b.sh['a'] = 'fromB'
assert b.sh['a'] == 'fromB'
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