test_plugins.py 1.92 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
5
import os
import logging
import shelve
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
26
class ShelfTest(ShelfMixin, SenpyPlugin):

    def test(self, key=None, value=None):
        assert isinstance(self.sh, shelve.Shelf)
        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
27
class ModelsTest(TestCase):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
28
29
30


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

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

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38
39
    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
40
41
        
    def test_shelf(self):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
42
43
44
45
46
47
48
49
50
        ''' 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
51

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
52
        sh = shelve.open(self.shelf_file)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
53

J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54
55
56
57
58
59
60
61
62
63
        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'
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
        a.close()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
65
66
67
68
69
70
71

        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'