mirror of
https://github.com/gsi-upm/senpy
synced 2024-11-24 17:12:29 +00:00
b8339e397b
Also: * Shelve -> Pickle to avoid weird db problems * Serving schemas and contexts
73 lines
1.9 KiB
Python
73 lines
1.9 KiB
Python
#!/bin/env python
|
|
|
|
import os
|
|
import logging
|
|
import pickle
|
|
import shutil
|
|
import tempfile
|
|
|
|
import json
|
|
import os
|
|
from unittest import TestCase
|
|
from senpy.models import Results, Entry
|
|
from senpy.plugins import SenpyPlugin, ShelfMixin
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
class ModelsTest(TestCase):
|
|
|
|
|
|
def tearDown(self):
|
|
if os.path.exists(self.shelf_dir):
|
|
shutil.rmtree(self.shelf_dir)
|
|
|
|
if os.path.isfile(self.shelf_file):
|
|
os.remove(self.shelf_file)
|
|
|
|
def setUp(self):
|
|
self.shelf_dir = tempfile.mkdtemp()
|
|
self.shelf_file = os.path.join(self.shelf_dir, "shelf")
|
|
|
|
def test_shelf(self):
|
|
''' 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')
|
|
|
|
a.save()
|
|
|
|
sh = pickle.load(open(self.shelf_file, 'rb'))
|
|
|
|
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'
|
|
a.save()
|
|
|
|
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'
|