test_plugins.py 2.93 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
4
import os
import logging
5
import pickle
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
6
7
import shutil
import tempfile
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
8
9
10
11

import json
import os
from unittest import TestCase
12
from flask import Flask
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
from senpy.models import Results, Entry
14
from senpy.plugins import SentimentPlugin, ShelfMixin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
15

16
class ShelfDummyPlugin(SentimentPlugin, ShelfMixin):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17

18
19
20
21
    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
22

23
24
    def deactivate(self, *args, **kwargs):
        self.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
25

26
27
28
29
30
31
32
    def analyse(self, *args, **kwargs):
        self.sh['counter'] = self.sh['counter']+1
        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
33

34
class PluginsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35
36

    def tearDown(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38
39
        if os.path.exists(self.shelf_dir):
            shutil.rmtree(self.shelf_dir)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
40
41
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
42

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
43
44
45
    def setUp(self):
        self.shelf_dir = tempfile.mkdtemp()
        self.shelf_file = os.path.join(self.shelf_dir, "shelf")
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
46
        
47
48
49
50
51
52
53
54
    def test_shelf_file(self):
        a = ShelfDummyPlugin(info={'name': 'default_shelve_file',
                                   'version': 'test'})
        assert os.path.dirname(a.shelf_file) == tempfile.gettempdir()
        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
55
    def test_shelf(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
56
        ''' A shelf is created and the value is stored '''
57
        a = ShelfDummyPlugin(info={'name': 'shelve',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
58
59
60
                            'version': 'test',
                            'shelf_file': self.shelf_file})
        assert a.sh == {}
61
62
        a.activate()
        assert a.sh == {'counter': 0}
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
63
64
65
        assert a.shelf_file == self.shelf_file

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

68
69
70
        a.save()

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

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

74
75
76
77
78
79
80
81
82
83
    def test_dummy_shelf(self):
        a = ShelfDummyPlugin(info={'name': 'DummyShelf',
                                   'shelf_file': self.shelf_file,
                                   'version': 'test'})
        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 '''
87
        a = ShelfDummyPlugin(info={'name': 'shelve',
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
88
89
                            'version': 'test',
                            'shelf_file': self.shelf_file})
90
        a.activate()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
91
92
        print('Shelf file: %s' % a.shelf_file)
        a.sh['a'] = 'fromA'
93
        a.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
94

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