| 1 |
import unittest |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
try: |
| 6 |
from unittest.mock import call, patch |
| 7 |
except ImportError: |
| 8 |
# Python 2 |
| 9 |
from mock import call, patch |
| 10 |
|
| 11 |
from pylibdmtx import dmtx_library |
| 12 |
|
| 13 |
|
| 14 |
class TestLoad(unittest.TestCase): |
| 15 |
def setUp(self): |
| 16 |
self.addCleanup(patch.stopall) |
| 17 |
self.cdll = patch( |
| 18 |
'pylibdmtx.dmtx_library.cdll', autospec=True |
| 19 |
).start() |
| 20 |
self.find_library = patch( |
| 21 |
'pylibdmtx.dmtx_library.find_library', autospec=True |
| 22 |
).start() |
| 23 |
self.platform = patch( |
| 24 |
'pylibdmtx.dmtx_library.platform', autospec=True |
| 25 |
).start() |
| 26 |
self.windows_fname = patch( |
| 27 |
'pylibdmtx.dmtx_library._windows_fname', autospec=True, |
| 28 |
return_value='dll fname' |
| 29 |
).start() |
| 30 |
|
| 31 |
def test_found_non_windows(self): |
| 32 |
"libdmtx loaded ok on non-Windows platform" |
| 33 |
self.platform.system.return_value = 'Not windows' |
| 34 |
|
| 35 |
res = dmtx_library.load() |
| 36 |
|
| 37 |
self.platform.system.assert_called_once_with() |
| 38 |
self.find_library.assert_called_once_with('dmtx') |
| 39 |
self.cdll.LoadLibrary.assert_called_once_with( |
| 40 |
self.find_library.return_value |
| 41 |
) |
| 42 |
|
| 43 |
self.assertEqual(self.cdll.LoadLibrary.return_value, res) |
| 44 |
self.assertEqual(0, self.windows_fname.call_count) |
| 45 |
|
| 46 |
def test_not_found_non_windows(self): |
| 47 |
"libdmtx not found on non-Windows platform" |
| 48 |
self.platform.system.return_value = 'Not windows' |
| 49 |
self.find_library.return_value = None |
| 50 |
|
| 51 |
self.assertRaises(ImportError, dmtx_library.load) |
| 52 |
|
| 53 |
self.platform.system.assert_called_once_with() |
| 54 |
self.find_library.assert_called_once_with('dmtx') |
| 55 |
|
| 56 |
def test_found_windows(self): |
| 57 |
"libdmtx found on Windows" |
| 58 |
self.platform.system.return_value = 'Windows' |
| 59 |
|
| 60 |
res = dmtx_library.load() |
| 61 |
|
| 62 |
self.platform.system.assert_called_once_with() |
| 63 |
self.cdll.LoadLibrary.assert_called_once_with( |
| 64 |
self.windows_fname.return_value |
| 65 |
) |
| 66 |
self.assertEqual(self.cdll.LoadLibrary.return_value, res) |
| 67 |
|
| 68 |
def test_found_second_attempt_windows(self): |
| 69 |
"libdmtx found on the second attempt on Windows" |
| 70 |
self.platform.system.return_value = 'Windows' |
| 71 |
self.cdll.LoadLibrary.side_effect = [ |
| 72 |
OSError, # First call does not load DLL |
| 73 |
'loaded library', # Second call loads DLL |
| 74 |
] |
| 75 |
|
| 76 |
res = dmtx_library.load() |
| 77 |
|
| 78 |
self.platform.system.assert_called_once_with() |
| 79 |
self.cdll.LoadLibrary.assert_has_calls([ |
| 80 |
call(self.windows_fname.return_value), |
| 81 |
call(str(Path(dmtx_library.__file__).parent.joinpath( |
| 82 |
self.windows_fname.return_value |
| 83 |
))), |
| 84 |
]) |
| 85 |
|
| 86 |
self.assertEqual('loaded library', res) |
| 87 |
|
| 88 |
def test_not_found_windows(self): |
| 89 |
"libdmtx not found on Windows" |
| 90 |
self.platform.system.return_value = 'Windows' |
| 91 |
self.cdll.LoadLibrary.side_effect = OSError |
| 92 |
|
| 93 |
self.assertRaises(OSError, dmtx_library.load) |
| 94 |
|
| 95 |
self.platform.system.assert_called_once_with() |
| 96 |
# Two attempts at loading |
| 97 |
self.cdll.LoadLibrary.assert_has_calls([ |
| 98 |
call(self.windows_fname.return_value), |
| 99 |
call(str(Path(dmtx_library.__file__).parent.joinpath( |
| 100 |
self.windows_fname.return_value |
| 101 |
))), |
| 102 |
]) |
| 103 |
|
| 104 |
|
| 105 |
class TestWindowsFname(unittest.TestCase): |
| 106 |
def setUp(self): |
| 107 |
self.addCleanup(patch.stopall) |
| 108 |
self.sys = patch('pylibdmtx.dmtx_library.sys', autospec=True).start() |
| 109 |
|
| 110 |
def test_32bit(self): |
| 111 |
self.sys.maxsize = 2**32 |
| 112 |
self.assertEqual('libdmtx-32.dll', dmtx_library._windows_fname()) |
| 113 |
|
| 114 |
def test_64bit(self): |
| 115 |
# This is a 'long' on a 32-bit interpreter |
| 116 |
self.sys.maxsize = 2**32 + 1 |
| 117 |
self.assertEqual('libdmtx-64.dll', dmtx_library._windows_fname()) |
| 118 |
|
| 119 |
|
| 120 |
if __name__ == '__main__': |
| 121 |
unittest.main() |