Monkey-patching replaced with unittest.patch. Somewhat saner. closes #93
This commit is contained in:
parent
fc147a29e5
commit
687d94d415
1 changed files with 34 additions and 46 deletions
|
|
@ -6,16 +6,18 @@ import sys
|
|||
import tempfile
|
||||
import warnings
|
||||
|
||||
if sys.hexversion < 0x03000000:
|
||||
from StringIO import StringIO
|
||||
else:
|
||||
from io import StringIO
|
||||
|
||||
if sys.hexversion < 0x02070000:
|
||||
import unittest2 as unittest
|
||||
else:
|
||||
import unittest
|
||||
|
||||
if sys.hexversion <= 0x03030000:
|
||||
from mock import patch
|
||||
from StringIO import StringIO
|
||||
else:
|
||||
from unittest.mock import patch
|
||||
from io import StringIO
|
||||
|
||||
import glymur
|
||||
|
||||
|
||||
|
|
@ -24,15 +26,11 @@ import glymur
|
|||
class TestCallbacks(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
# Save sys.stdout.
|
||||
self.stdout = sys.stdout
|
||||
sys.stdout = StringIO()
|
||||
self.jp2file = glymur.data.nemo()
|
||||
self.j2kfile = glymur.data.goodstuff()
|
||||
|
||||
def tearDown(self):
|
||||
# Restore stdout.
|
||||
sys.stdout = self.stdout
|
||||
pass
|
||||
|
||||
@unittest.skipIf(os.name == "nt", "Temporary file issue on window.")
|
||||
def test_info_callback_on_write(self):
|
||||
|
|
@ -43,8 +41,9 @@ class TestCallbacks(unittest.TestCase):
|
|||
tiledata = j.read(tile=0)
|
||||
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
|
||||
j = glymur.Jp2k(tfile.name, 'wb')
|
||||
j.write(tiledata, verbose=True)
|
||||
actual = sys.stdout.getvalue().strip()
|
||||
with patch('sys.stdout', new=StringIO()) as fake_out:
|
||||
j.write(tiledata, verbose=True)
|
||||
actual = fake_out.getvalue().strip()
|
||||
expected = '[INFO] tile number 1 / 1'
|
||||
self.assertEqual(actual, expected)
|
||||
|
||||
|
|
@ -52,8 +51,9 @@ class TestCallbacks(unittest.TestCase):
|
|||
# Verify that we get the expected stdio output when our internal info
|
||||
# callback handler is enabled.
|
||||
j = glymur.Jp2k(self.j2kfile)
|
||||
d = j.read(rlevel=1, verbose=True, area=(0, 0, 200, 150))
|
||||
actual = sys.stdout.getvalue().strip()
|
||||
with patch('sys.stdout', new=StringIO()) as fake_out:
|
||||
d = j.read(rlevel=1, verbose=True, area=(0, 0, 200, 150))
|
||||
actual = fake_out.getvalue().strip()
|
||||
|
||||
lines = ['[INFO] Start to read j2k main header (0).',
|
||||
'[INFO] Main header has been correctly decoded.',
|
||||
|
|
@ -72,47 +72,35 @@ class TestCallbacks15(unittest.TestCase):
|
|||
"""This test suite is for OpenJPEG 1.5.1 properties.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
# Monkey patch the package so as to use OPENJPEG instead of OPENJP2
|
||||
cls.openjp2 = glymur.lib.openjp2.OPENJP2
|
||||
glymur.lib.openjp2.OPENJP2 = None
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
# Restore OPENJP2
|
||||
glymur.lib.openjp2.OPENJP2 = cls.openjp2
|
||||
|
||||
def setUp(self):
|
||||
# Save sys.stdout.
|
||||
self.stdout = sys.stdout
|
||||
sys.stdout = StringIO()
|
||||
self.jp2file = glymur.data.nemo()
|
||||
self.j2kfile = glymur.data.goodstuff()
|
||||
|
||||
def tearDown(self):
|
||||
# Restore stdout.
|
||||
sys.stdout = self.stdout
|
||||
pass
|
||||
|
||||
def test_info_callbacks_on_read(self):
|
||||
# Verify that we get the expected stdio output when our internal info
|
||||
# callback handler is enabled.
|
||||
j = glymur.Jp2k(self.j2kfile)
|
||||
d = j.read(rlevel=1, verbose=True)
|
||||
actual = sys.stdout.getvalue().strip()
|
||||
|
||||
regex = re.compile(r"""\[INFO\]\stile\s1\sof\s1\s+
|
||||
\[INFO\]\s-\stiers-1\stook\s
|
||||
[0-9]+\.[0-9]+\ss\s+
|
||||
\[INFO\]\s-\sdwt\stook\s
|
||||
(-){0,1}[0-9]+\.[0-9]+\ss\s+
|
||||
\[INFO\]\s-\stile\sdecoded\sin\s
|
||||
[0-9]+\.[0-9]+\ss""",
|
||||
re.VERBOSE)
|
||||
if sys.hexversion <= 0x03020000:
|
||||
self.assertRegexpMatches(actual, regex)
|
||||
else:
|
||||
self.assertRegex(actual, regex)
|
||||
with patch('glymur.lib.openjp2.OPENJP2', new=None):
|
||||
# Force to use OPENJPEG instead of OPENJP2.
|
||||
j = glymur.Jp2k(self.j2kfile)
|
||||
with patch('sys.stdout', new=StringIO()) as fake_out:
|
||||
d = j.read(rlevel=1, verbose=True)
|
||||
actual = fake_out.getvalue().strip()
|
||||
|
||||
regex = re.compile(r"""\[INFO\]\stile\s1\sof\s1\s+
|
||||
\[INFO\]\s-\stiers-1\stook\s
|
||||
[0-9]+\.[0-9]+\ss\s+
|
||||
\[INFO\]\s-\sdwt\stook\s
|
||||
(-){0,1}[0-9]+\.[0-9]+\ss\s+
|
||||
\[INFO\]\s-\stile\sdecoded\sin\s
|
||||
[0-9]+\.[0-9]+\ss""",
|
||||
re.VERBOSE)
|
||||
if sys.hexversion <= 0x03020000:
|
||||
self.assertRegexpMatches(actual, regex)
|
||||
else:
|
||||
self.assertRegex(actual, regex)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue