2016-02-19 18:24:09 +00:00
|
|
|
#!/bin/env python
|
|
|
|
|
2015-11-05 17:50:37 +00:00
|
|
|
import os
|
|
|
|
import logging
|
2016-02-21 18:36:24 +00:00
|
|
|
import pickle
|
2016-02-19 18:24:09 +00:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2015-11-05 17:50:37 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from unittest import TestCase
|
2016-02-19 18:24:09 +00:00
|
|
|
from senpy.models import Results, Entry
|
2015-11-05 17:50:37 +00:00
|
|
|
from senpy.plugins import SenpyPlugin, ShelfMixin
|
|
|
|
|
|
|
|
|
2015-12-11 13:53:30 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-05 17:50:37 +00:00
|
|
|
class ModelsTest(TestCase):
|
2015-12-11 13:53:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
2016-02-19 18:24:09 +00:00
|
|
|
if os.path.exists(self.shelf_dir):
|
|
|
|
shutil.rmtree(self.shelf_dir)
|
|
|
|
|
2015-12-11 13:53:30 +00:00
|
|
|
if os.path.isfile(self.shelf_file):
|
|
|
|
os.remove(self.shelf_file)
|
2015-11-05 17:50:37 +00:00
|
|
|
|
2016-02-19 18:24:09 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.shelf_dir = tempfile.mkdtemp()
|
|
|
|
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
|
2015-11-05 17:50:37 +00:00
|
|
|
|
|
|
|
def test_shelf(self):
|
2015-12-11 13:53:30 +00:00
|
|
|
''' 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')
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2016-02-21 18:36:24 +00:00
|
|
|
a.save()
|
|
|
|
|
|
|
|
sh = pickle.load(open(self.shelf_file, 'rb'))
|
2016-02-19 18:24:09 +00:00
|
|
|
|
2015-12-11 13:53:30 +00:00
|
|
|
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'
|
2016-02-21 18:36:24 +00:00
|
|
|
a.save()
|
2015-12-11 13:53:30 +00:00
|
|
|
|
|
|
|
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'
|