testing cleanup
This commit is contained in:
@ -40,6 +40,7 @@ PY += devsup/disect.py
|
|||||||
PY += devsup/ptable.py
|
PY += devsup/ptable.py
|
||||||
|
|
||||||
PY += devsup/test/__init__.py
|
PY += devsup/test/__init__.py
|
||||||
|
PY += devsup/test/util.py
|
||||||
PY += devsup/test/test_db.py
|
PY += devsup/test/test_db.py
|
||||||
|
|
||||||
#===========================
|
#===========================
|
||||||
|
@ -10,43 +10,10 @@ from ..db import getRecord
|
|||||||
from .. import _dbapi
|
from .. import _dbapi
|
||||||
from .. import _init
|
from .. import _init
|
||||||
|
|
||||||
# short-circuit warning from _dbapi._init()
|
from .util import IOCHelper
|
||||||
os.environ['TOP'] = _dbapi.XPYDEV_BASE
|
|
||||||
|
|
||||||
class IOCHelper(unittest.TestCase):
|
# short-circuit warning from base_registerRecordDeviceDriver()
|
||||||
db = None
|
os.environ['TOP'] = _dbapi.XPYDEV_BASE # external code use devsup.XPYDEV_BASE
|
||||||
autostart = running = False
|
|
||||||
def setUp(self):
|
|
||||||
print("testdbPrepare()")
|
|
||||||
_dbapi._UTest.testdbPrepare()
|
|
||||||
_init(iocMain=False) # load base.dbd
|
|
||||||
|
|
||||||
if self.db is not None:
|
|
||||||
with tempfile.NamedTemporaryFile() as F:
|
|
||||||
F.write(self.db.encode('ascii'))
|
|
||||||
F.flush()
|
|
||||||
_dbapi.dbReadDatabase(F.name)
|
|
||||||
|
|
||||||
if self.autostart:
|
|
||||||
self.iocInit()
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
self.iocShutdown();
|
|
||||||
print("testdbCleanup()")
|
|
||||||
_dbapi.initHookAnnounce(9999) # our magic/fake AtExit hook
|
|
||||||
_dbapi._UTest.testdbCleanup()
|
|
||||||
|
|
||||||
def iocInit(self):
|
|
||||||
if not self.running:
|
|
||||||
print("testIocInitOk")
|
|
||||||
_dbapi._UTest.testIocInitOk()
|
|
||||||
self.running = True
|
|
||||||
|
|
||||||
def iocShutdown(self):
|
|
||||||
if self.running:
|
|
||||||
print("testIocShutdownOk")
|
|
||||||
_dbapi._UTest.testIocShutdownOk()
|
|
||||||
self.running = False
|
|
||||||
|
|
||||||
class TestScan(IOCHelper):
|
class TestScan(IOCHelper):
|
||||||
db = """
|
db = """
|
||||||
@ -90,7 +57,6 @@ class TestField(IOCHelper):
|
|||||||
field(NELM, "10")
|
field(NELM, "10")
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
autostart = True
|
|
||||||
|
|
||||||
def test_ai(self):
|
def test_ai(self):
|
||||||
rec = getRecord("rec:ai")
|
rec = getRecord("rec:ai")
|
||||||
@ -171,7 +137,6 @@ class TestDset(IOCHelper):
|
|||||||
field(INP , "@devsup.test.test_db|TestDset foo bar")
|
field(INP , "@devsup.test.test_db|TestDset foo bar")
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
autostart = True
|
|
||||||
|
|
||||||
class Increment(object):
|
class Increment(object):
|
||||||
def process(self, rec, reason):
|
def process(self, rec, reason):
|
||||||
|
74
devsupApp/src/devsup/test/util.py
Normal file
74
devsupApp/src/devsup/test/util.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import numpy
|
||||||
|
from numpy.testing import assert_array_almost_equal, assert_array_equal
|
||||||
|
|
||||||
|
from ..db import getRecord
|
||||||
|
from .. import _dbapi
|
||||||
|
from .. import _init
|
||||||
|
|
||||||
|
__all__ = (
|
||||||
|
'IOCHelper',
|
||||||
|
)
|
||||||
|
|
||||||
|
class IOCHelper(unittest.TestCase):
|
||||||
|
"""Test case run in an IOC. ::
|
||||||
|
|
||||||
|
from devsup.db import getRecord
|
||||||
|
from devsup.test.util impmort IOCHelper
|
||||||
|
class TestScan(IOCHelper): # sub-class of unittest.TestCase
|
||||||
|
db = \"\"\"
|
||||||
|
record(longout, foo) {}
|
||||||
|
\"\"\"
|
||||||
|
autostart = True
|
||||||
|
|
||||||
|
def test_link(self):
|
||||||
|
rec = getRecord('foo')
|
||||||
|
with rec: # dbScanLock()
|
||||||
|
self.assertEqual(rec.VAL, 0)
|
||||||
|
"""
|
||||||
|
# DB definition to be used. May include eg. 'record(ai, "blah") {}'
|
||||||
|
db = None
|
||||||
|
# Whether to automatically run iocInit() before test methods
|
||||||
|
# whether iocInit() has been called
|
||||||
|
autostart = True
|
||||||
|
running = False
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
print("testdbPrepare()")
|
||||||
|
_dbapi._UTest.testdbPrepare()
|
||||||
|
_init(iocMain=False) # load base.dbd
|
||||||
|
|
||||||
|
if self.db is not None:
|
||||||
|
with tempfile.NamedTemporaryFile() as F:
|
||||||
|
F.write(self.db.encode('ascii'))
|
||||||
|
F.flush()
|
||||||
|
_dbapi.dbReadDatabase(F.name)
|
||||||
|
|
||||||
|
if self.autostart:
|
||||||
|
self.iocInit()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.iocShutdown();
|
||||||
|
print("testdbCleanup()")
|
||||||
|
_dbapi.initHookAnnounce(9999) # our magic/fake AtExit hook
|
||||||
|
_dbapi._UTest.testdbCleanup()
|
||||||
|
|
||||||
|
def iocInit(self):
|
||||||
|
"""If not autostart, then this must be called before runtime database access is possible
|
||||||
|
"""
|
||||||
|
if not self.running:
|
||||||
|
print("testIocInitOk")
|
||||||
|
_dbapi._UTest.testIocInitOk()
|
||||||
|
self.running = True
|
||||||
|
|
||||||
|
def iocShutdown(self):
|
||||||
|
"""Call to stop IOC scanning processes. Happens automatically during test tearDown
|
||||||
|
"""
|
||||||
|
if self.running:
|
||||||
|
print("testIocShutdownOk")
|
||||||
|
_dbapi._UTest.testIocShutdownOk()
|
||||||
|
self.running = False
|
Reference in New Issue
Block a user