diff --git a/devsupApp/src/Makefile b/devsupApp/src/Makefile index 2d01d17..3a26547 100644 --- a/devsupApp/src/Makefile +++ b/devsupApp/src/Makefile @@ -40,6 +40,7 @@ PY += devsup/disect.py PY += devsup/ptable.py PY += devsup/test/__init__.py +PY += devsup/test/util.py PY += devsup/test/test_db.py #=========================== diff --git a/devsupApp/src/devsup/test/test_db.py b/devsupApp/src/devsup/test/test_db.py index 0ab6247..2dc33e0 100644 --- a/devsupApp/src/devsup/test/test_db.py +++ b/devsupApp/src/devsup/test/test_db.py @@ -10,43 +10,10 @@ from ..db import getRecord from .. import _dbapi from .. import _init -# short-circuit warning from _dbapi._init() -os.environ['TOP'] = _dbapi.XPYDEV_BASE +from .util import IOCHelper -class IOCHelper(unittest.TestCase): - db = None - 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 +# short-circuit warning from base_registerRecordDeviceDriver() +os.environ['TOP'] = _dbapi.XPYDEV_BASE # external code use devsup.XPYDEV_BASE class TestScan(IOCHelper): db = """ @@ -90,7 +57,6 @@ class TestField(IOCHelper): field(NELM, "10") } """ - autostart = True def test_ai(self): rec = getRecord("rec:ai") @@ -171,7 +137,6 @@ class TestDset(IOCHelper): field(INP , "@devsup.test.test_db|TestDset foo bar") } """ - autostart = True class Increment(object): def process(self, rec, reason): diff --git a/devsupApp/src/devsup/test/util.py b/devsupApp/src/devsup/test/util.py new file mode 100644 index 0000000..6177a4b --- /dev/null +++ b/devsupApp/src/devsup/test/util.py @@ -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