test_plugins.py 2.74 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
2
#!/bin/env python

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
3
import os
4
import pickle
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
5
6
import shutil
import tempfile
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
8

from unittest import TestCase
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
9
from senpy.models import Results, Entry
10
from senpy.plugins import SentimentPlugin, ShelfMixin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
11
12


J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
13
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
14
15
16
17
    def activate(self, *args, **kwargs):
        if 'counter' not in self.sh:
            self.sh['counter'] = 0
        self.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
18

19
20
    def deactivate(self, *args, **kwargs):
        self.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21

22
    def analyse(self, *args, **kwargs):
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
23
        self.sh['counter'] = self.sh['counter'] + 1
24
25
26
27
28
        e = Entry()
        e.nif__isString = self.sh['counter']
        r = Results()
        r.entries.append(e)
        return r
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
29
30


J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
31
class PluginsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
32
    def tearDown(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
34
35
        if os.path.exists(self.shelf_dir):
            shutil.rmtree(self.shelf_dir)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36
37
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
38

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
39
40
41
    def setUp(self):
        self.shelf_dir = tempfile.mkdtemp()
        self.shelf_file = os.path.join(self.shelf_dir, "shelf")
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
42

43
    def test_shelf_file(self):
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
44
45
46
        a = ShelfDummyPlugin(
            info={'name': 'default_shelve_file',
                  'version': 'test'})
47
48
49
50
        a.activate()
        assert os.path.isfile(a.shelf_file)
        os.remove(a.shelf_file)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
51
    def test_shelf(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
52
        ''' A shelf is created and the value is stored '''
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
53
54
55
56
57
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
58
        assert a.sh == {}
59
60
        a.activate()
        assert a.sh == {'counter': 0}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
62
63
        assert a.shelf_file == self.shelf_file

        a.sh['a'] = 'fromA'
64
        assert a.sh['a'] == 'fromA'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
65

66
67
68
        a.save()

        sh = pickle.load(open(self.shelf_file, 'rb'))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
69

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
70
71
        assert sh['a'] == 'fromA'

72
    def test_dummy_shelf(self):
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
73
74
75
76
77
        a = ShelfDummyPlugin(info={
            'name': 'DummyShelf',
            'shelf_file': self.shelf_file,
            'version': 'test'
        })
78
79
80
81
82
83
        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
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
84
85
86

    def test_two(self):
        ''' Reusing the values of a previous shelf '''
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
87
88
89
90
91
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
92
        a.activate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
93
94
        print('Shelf file: %s' % a.shelf_file)
        a.sh['a'] = 'fromA'
95
        a.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
96

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
97
98
99
100
101
        b = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
102
103
        b.activate()
        assert b.sh['a'] == 'fromA'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
104
105
        b.sh['a'] = 'fromB'
        assert b.sh['a'] == 'fromB'