2015-12-11 13:53:30 +00:00
|
|
|
#!/bin/env python2
|
|
|
|
# -*- py-which-shell: "python2"; -*-
|
2015-11-05 17:50:37 +00:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import shelve
|
|
|
|
|
|
|
|
try:
|
|
|
|
import unittest.mock as mock
|
|
|
|
except ImportError:
|
|
|
|
import mock
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from unittest import TestCase
|
|
|
|
from senpy.models import Response, Entry
|
|
|
|
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 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
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-05 17:50:37 +00:00
|
|
|
class ModelsTest(TestCase):
|
2015-12-11 13:53:30 +00:00
|
|
|
shelf_file = 'shelf_test.db'
|
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if os.path.isfile(self.shelf_file):
|
|
|
|
os.remove(self.shelf_file)
|
2015-11-05 17:50:37 +00:00
|
|
|
|
2015-12-11 13:53:30 +00:00
|
|
|
setUp = tearDown
|
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')
|
|
|
|
del(a)
|
|
|
|
assert os.path.isfile(self.shelf_file)
|
|
|
|
sh = shelve.open(self.shelf_file)
|
|
|
|
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'
|
2015-11-05 17:50:37 +00:00
|
|
|
a.close()
|
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'
|