test_plugins.py 4.51 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
        a.activate()

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
80
        assert a.shelf_file == self.shelf_file
81
82
83
84
        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
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    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')

        files = {emptyfile: ['empty file', EOFError],
                 invalidfile: ['invalid file', pickle.UnpicklingError]}

        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):
                    # By default, raise an 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):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
120
        ''' Reusing the values of a previous shelf '''
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
121
122
123
124
125
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
126
        a.activate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
127
128
        print('Shelf file: %s' % a.shelf_file)
        a.sh['a'] = 'fromA'
129
        a.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
130

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
131
132
133
134
135
        b = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
136
137
        b.activate()
        assert b.sh['a'] == 'fromA'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
138
139
        b.sh['a'] = 'fromB'
        assert b.sh['a'] == 'fromB'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

    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