test_plugins.py 7.33 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
9
from senpy.models import Results, Entry, EmotionSet, Emotion
10
from senpy import plugins
11
from senpy.plugins.conversion.emotion.centroids import CentroidConversion
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
13


14
class ShelfDummyPlugin(plugins.SentimentPlugin, plugins.ShelfMixin):
15
16
17
18
    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
19

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

23
    def analyse(self, *args, **kwargs):
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
24
        self.sh['counter'] = self.sh['counter'] + 1
25
26
27
28
29
        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
30
31


J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
32
class PluginsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
    def tearDown(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
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
        a = ShelfDummyPlugin(
            info={'name': 'default_shelve_file',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
                  'description': 'Dummy plugin for tests',
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
47
                  'version': 'test'})
48
49
50
51
        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
52
    def test_shelf(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53
        ''' A shelf is created and the value is stored '''
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54
        newfile = self.shelf_file + "new"
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
55
56
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
57
            'description': 'Shelf plugin for tests',
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
58
            'version': 'test',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
            'shelf_file': newfile
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
60
        })
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
61
        assert a.sh == {}
62
63
        a.activate()
        assert a.sh == {'counter': 0}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
        assert a.shelf_file == newfile
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
65
66

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

69
70
        a.save()

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
71
        sh = pickle.load(open(newfile, 'rb'))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
72

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

75
    def test_dummy_shelf(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
76
77
        with open(self.shelf_file, 'wb') as f:
            pickle.dump({'counter': 99}, f)
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
78
79
        a = ShelfDummyPlugin(info={
            'name': 'DummyShelf',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
80
            'description': 'Dummy plugin for tests',
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
81
82
83
            'shelf_file': self.shelf_file,
            'version': 'test'
        })
84
85
        a.activate()

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
86
        assert a.shelf_file == self.shelf_file
87
        res1 = a.analyse(input=1)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
88
89
90
91
92
93
94
        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
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
95

96
97
98
99
100
101
102
103
    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')

104
105
        files = {emptyfile: ['empty file', (EOFError, IndexError)],
                 invalidfile: ['invalid file', (pickle.UnpicklingError, IndexError)]}
106
107
108
109
110

        for fn in files:
            with open(fn, 'rb') as f:
                msg, error = files[fn]
                a = ShelfDummyPlugin(info={
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
111
112
                    'name': 'test_corrupt_shelf_{}'.format(msg),
                    'description': 'Dummy plugin for tests',
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                    '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):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
130
        ''' Reusing the values of a previous shelf '''
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
131
132
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
133
            'description': 'Dummy plugin for tests',
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
134
135
136
            'version': 'test',
            'shelf_file': self.shelf_file
        })
137
        a.activate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
138
139
        print('Shelf file: %s' % a.shelf_file)
        a.sh['a'] = 'fromA'
140
        a.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
141

J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
142
143
        b = ShelfDummyPlugin(info={
            'name': 'shelve',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
144
            'description': 'Dummy plugin for tests',
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
145
146
147
            'version': 'test',
            'shelf_file': self.shelf_file
        })
148
149
        b.activate()
        assert b.sh['a'] == 'fromA'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
150
151
        b.sh['a'] = 'fromB'
        assert b.sh['a'] == 'fromB'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
152
153
154
155
156

    def test_extra_params(self):
        ''' Should be able to set extra parameters'''
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
157
            'description': 'Dummy shelf plugin for tests',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
158
159
160
161
162
163
164
165
166
167
168
            'version': 'test',
            'shelf_file': self.shelf_file,
            'extra_params': {
                'example': {
                    'aliases': ['example', 'ex'],
                    'required': True,
                    'default': 'nonsense'
                }
            }
        })
        assert 'example' in a.extra_params
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

    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)
191
        print(c.serialize())
192
193
194
195
196
197
198
199

        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
200
        print(res)
201
202
203
204
205
206
207

        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
208
        print(res)
209
210
211
212
213
214

        e = Emotion()
        e["X-dimension"] = -0.2
        e["Y-dimension"] = -0.3
        res = c._backwards_conversion(e)
        assert res["onyx:hasEmotionCategory"] == "c3"
215
        print(res)
216
217
218
219
220
221

        e = Emotion()
        e["X-dimension"] = -0.2
        e["Y-dimension"] = 0.3
        res = c._backwards_conversion(e)
        assert res["onyx:hasEmotionCategory"] == "c2"
222
223


224
def make_mini_test(plugin_info):
225
    def mini_test(self):
226
        plugin = plugins.load_plugin_from_info(plugin_info, install=True)
227
228
229
230
        plugin.test()
    return mini_test


231
def _add_tests():
232
233
234
    root = os.path.join(os.path.dirname(__file__), '..')
    print(root)
    plugs = plugins.load_plugins([root, ], loader=plugins.parse_plugin_info)
235
    for k, v in plugs.items():
236
        pass
237
238
239
240
241
242
        t_method = make_mini_test(v)
        t_method.__name__ = 'test_plugin_{}'.format(k)
        setattr(PluginsTest, t_method.__name__, t_method)
        del t_method


243
_add_tests()