test_plugins.py 1.9 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
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
12
from senpy.models import Results, Entry
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
14
15
from senpy.plugins import SenpyPlugin, ShelfMixin


J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
17
18
19
20
21
22
23
24
25
class ShelfTest(ShelfMixin, SenpyPlugin):

    def test(self, key=None, value=None):
        assert key in self.sh
        print('Checking: sh[{}] == {}'.format(key, value))
        print('SH[{}]: {}'.format(key, self.sh[key]))
        assert self.sh[key] == value
        


J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
26
class ModelsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27
28
29


    def tearDown(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
30
31
32
        if os.path.exists(self.shelf_dir):
            shutil.rmtree(self.shelf_dir)

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
34
        if os.path.isfile(self.shelf_file):
            os.remove(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
35

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
36
37
38
    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
39
40
        
    def test_shelf(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
41
42
43
44
45
46
47
48
49
        ''' A shelf is created and the value is stored '''
        a = ShelfTest(info={'name': 'shelve',
                            'version': 'test',
                            'shelf_file': self.shelf_file})
        assert a.sh == {}
        assert a.shelf_file == self.shelf_file

        a.sh['a'] = 'fromA'
        a.test(key='a', value='fromA')
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
50

51
52
53
        a.save()

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
55
56
57
58
59
60
61
62
63
64
        assert sh['a'] == 'fromA'


    def test_two(self):
        ''' Reusing the values of a previous shelf '''
        a = ShelfTest(info={'name': 'shelve',
                            'version': 'test',
                            'shelf_file': self.shelf_file})
        print('Shelf file: %s' % a.shelf_file)
        a.sh['a'] = 'fromA'
65
        a.save()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
66
67
68
69
70
71
72

        b = ShelfTest(info={'name': 'shelve',
                            'version': 'test',
                            'shelf_file': self.shelf_file})
        b.test(key='a', value='fromA')
        b.sh['a'] = 'fromB'
        assert b.sh['a'] == 'fromB'