1
0
mirror of https://github.com/gsi-upm/senpy synced 2025-08-24 02:22:20 +00:00

New shelf location and better shelf tests

This commit is contained in:
J. Fernando Sánchez
2016-12-19 18:18:47 +01:00
parent 40b67503ce
commit d72a995fa9
11 changed files with 66 additions and 29 deletions

View File

@@ -9,22 +9,29 @@ import tempfile
import json
import os
from unittest import TestCase
from flask import Flask
from senpy.models import Results, Entry
from senpy.plugins import SenpyPlugin, ShelfMixin
from senpy.plugins import SentimentPlugin, ShelfMixin
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
class ShelfTest(ShelfMixin, SenpyPlugin):
def activate(self, *args, **kwargs):
if 'counter' not in self.sh:
self.sh['counter'] = 0
self.save()
def test(self, key=None, value=None):
assert key in self.sh
print('Checking: sh[{}] == {}'.format(key, value))
print('SH[{}]: {}'.format(key, self.sh[key]))
assert self.sh[key] == value
def deactivate(self, *args, **kwargs):
self.save()
def analyse(self, *args, **kwargs):
self.sh['counter'] = self.sh['counter']+1
e = Entry()
e.nif__isString = self.sh['counter']
r = Results()
r.entries.append(e)
return r
class ModelsTest(TestCase):
class PluginsTest(TestCase):
def tearDown(self):
if os.path.exists(self.shelf_dir):
@@ -37,16 +44,26 @@ class ModelsTest(TestCase):
self.shelf_dir = tempfile.mkdtemp()
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
def test_shelf_file(self):
a = ShelfDummyPlugin(info={'name': 'default_shelve_file',
'version': 'test'})
assert os.path.dirname(a.shelf_file) == tempfile.gettempdir()
a.activate()
assert os.path.isfile(a.shelf_file)
os.remove(a.shelf_file)
def test_shelf(self):
''' A shelf is created and the value is stored '''
a = ShelfTest(info={'name': 'shelve',
a = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file})
assert a.sh == {}
a.activate()
assert a.sh == {'counter': 0}
assert a.shelf_file == self.shelf_file
a.sh['a'] = 'fromA'
a.test(key='a', value='fromA')
assert a.sh['a'] == 'fromA'
a.save()
@@ -54,19 +71,31 @@ class ModelsTest(TestCase):
assert sh['a'] == 'fromA'
def test_dummy_shelf(self):
a = ShelfDummyPlugin(info={'name': 'DummyShelf',
'shelf_file': self.shelf_file,
'version': 'test'})
a.activate()
res1 = a.analyse(input=1)
assert res1.entries[0].nif__isString == 1
res2 = a.analyse(input=1)
assert res2.entries[0].nif__isString == 2
def test_two(self):
''' Reusing the values of a previous shelf '''
a = ShelfTest(info={'name': 'shelve',
a = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file})
a.activate()
print('Shelf file: %s' % a.shelf_file)
a.sh['a'] = 'fromA'
a.save()
b = ShelfTest(info={'name': 'shelve',
b = ShelfDummyPlugin(info={'name': 'shelve',
'version': 'test',
'shelf_file': self.shelf_file})
b.test(key='a', value='fromA')
b.activate()
assert b.sh['a'] == 'fromA'
b.sh['a'] = 'fromB'
assert b.sh['a'] == 'fromB'