test_plugins.py 6.1 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.plugins import SentimentPlugin, ShelfMixin
11
from senpy.plugins.conversion.emotion.centroids import CentroidConversion
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
13


J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
14
class ShelfDummyPlugin(SentimentPlugin, 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
36
        if os.path.exists(self.shelf_dir):
            shutil.rmtree(self.shelf_dir)

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
41
42
    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
43

44
    def test_shelf_file(self):
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
45
46
47
        a = ShelfDummyPlugin(
            info={'name': 'default_shelve_file',
                  '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
YAPFed    
J. Fernando Sánchez committed
54
55
56
57
58
        a = ShelfDummyPlugin(info={
            'name': 'shelve',
            'version': 'test',
            'shelf_file': self.shelf_file
        })
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
63
64
        assert a.shelf_file == self.shelf_file

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

67
68
69
        a.save()

        sh = pickle.load(open(self.shelf_file, '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
YAPFed    
J. Fernando Sánchez committed
74
75
76
77
78
        a = ShelfDummyPlugin(info={
            'name': 'DummyShelf',
            'shelf_file': self.shelf_file,
            'version': 'test'
        })
79
80
        a.activate()

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

87
88
89
90
91
92
93
94
    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')

95
96
        files = {emptyfile: ['empty file', (EOFError, IndexError)],
                 invalidfile: ['invalid file', (pickle.UnpicklingError, IndexError)]}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

        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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

    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)

        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

        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

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

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