test_plugins.py 6.77 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
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
J. Fernando Sánchez committed
53
        newfile = self.shelf_file + "new"
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
54
55
56
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
57
            'shelf_file': newfile
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
58
        })
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
59
        assert a.sh == {}
60
61
        a.activate()
        assert a.sh == {'counter': 0}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
62
        assert a.shelf_file == newfile
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64

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

67
68
        a.save()

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

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

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

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

93
94
95
96
97
98
99
100
    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')

101
102
        files = {emptyfile: ['empty file', (EOFError, IndexError)],
                 invalidfile: ['invalid file', (pickle.UnpicklingError, IndexError)]}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

        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):
                    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
126
        ''' Reusing the values of a previous shelf '''
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
127
128
129
130
131
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
132
        a.activate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
133
134
        print('Shelf file: %s' % a.shelf_file)
        a.sh['a'] = 'fromA'
135
        a.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
136

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

    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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

    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)
184
        print(c.serialize())
185
186
187
188
189
190
191
192

        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
193
        print(res)
194
195
196
197
198
199
200

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

        e = Emotion()
        e["X-dimension"] = -0.2
        e["Y-dimension"] = -0.3
        res = c._backwards_conversion(e)
        assert res["onyx:hasEmotionCategory"] == "c3"
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"] == "c2"
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233


def make_mini_test(plugin):
    def mini_test(self):
        plugin.test()
    return mini_test


def add_tests():
    root = os.path.dirname(__file__)
    plugs = plugins.load_plugins(os.path.join(root, ".."))
    for k, v in plugs.items():
        t_method = make_mini_test(v)
        t_method.__name__ = 'test_plugin_{}'.format(k)
        setattr(PluginsTest, t_method.__name__, t_method)
        del t_method


add_tests()