fixed unittest for python3

This commit is contained in:
Arturo Hernandez 2018-08-13 11:04:17 -04:00
parent 005d1a7221
commit d3e587a756

37
test.py
View File

@ -11,6 +11,12 @@ from zk.user import User
from zk.finger import Finger from zk.finger import Finger
from zk.attendance import Attendance from zk.attendance import Attendance
from zk.exception import ZKErrorResponse, ZKNetworkError from zk.exception import ZKErrorResponse, ZKNetworkError
try:
unittest.TestCase.assertRaisesRegex
except AttributeError:
unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
def dump(obj, nested_level=0, output=sys.stdout): def dump(obj, nested_level=0, output=sys.stdout):
spacing = ' ' spacing = ' '
if type(obj) == dict: if type(obj) == dict:
@ -42,35 +48,39 @@ class PYZKTest(unittest.TestCase):
def tearDown(self): def tearDown(self):
pass pass
@patch('zk.base.socket')
@patch('zk.base.ZK_helper') @patch('zk.base.ZK_helper')
def test_no_ping(self,helper): def test_no_ping(self,helper, socket):
""" what if ping doesn't response """ """ what if ping doesn't response """
helper.return_value.test_ping.return_value = False #no ping simulated helper.return_value.test_ping.return_value = False #no ping simulated
#begin #begin
zk = ZK('192.168.1.201') zk = ZK('192.168.1.201')
helper.assert_called_with('192.168.1.201', 4370) # called correctly helper.assert_called_with('192.168.1.201', 4370) # called correctly
self.assertRaisesRegexp(ZKNetworkError, "can't reach device", zk.connect) self.assertRaisesRegex(ZKNetworkError, "can't reach device", zk.connect)
@patch('zk.base.socket')
@patch('zk.base.ZK_helper') @patch('zk.base.ZK_helper')
def test_correct_ping(self,helper): def test_correct_ping(self,helper, socket):
""" what if ping is ok """ """ what if ping is ok """
helper.return_value.test_ping.return_value = True # ping simulated helper.return_value.test_ping.return_value = True # ping simulated
helper.return_value.test_tcp.return_value = 1 # helper tcp ok
socket.return_value.recv.return_value = b''
#begin #begin
zk = ZK('192.168.1.201') zk = ZK('192.168.1.201')
helper.assert_called_with('192.168.1.201', 4370) # called correctly helper.assert_called_with('192.168.1.201', 4370) # called correctly
self.assertRaisesRegexp(ZKNetworkError, "unpack requires", zk.connect) # no data...? self.assertRaisesRegex(ZKNetworkError, "unpack requires", zk.connect) # no data...?
@patch('zk.base.socket') @patch('zk.base.socket')
@patch('zk.base.ZK_helper') @patch('zk.base.ZK_helper')
def test_tcp_invalid(self, helper, socket): def test_tcp_invalid(self, helper, socket):
""" Basic connection test """ """ Basic tcp invalid """
helper.return_value.test_ping.return_value = True # ping simulated helper.return_value.test_ping.return_value = True # ping simulated
helper.return_value.test_tcp.return_value = 0 # helper tcp ok helper.return_value.test_tcp.return_value = 0 # helper tcp ok
socket.return_value.recv.return_value = 'Invalid tcp data' socket.return_value.recv.return_value = b'Invalid tcp data'
#begin #begin
zk = ZK('192.168.1.201') zk = ZK('192.168.1.201')
helper.assert_called_with('192.168.1.201', 4370) # called correctly helper.assert_called_with('192.168.1.201', 4370) # called correctly
self.assertRaisesRegexp(ZKNetworkError, "TCP packet invalid", zk.connect) self.assertRaisesRegex(ZKNetworkError, "TCP packet invalid", zk.connect)
@patch('zk.base.socket') @patch('zk.base.socket')
@patch('zk.base.ZK_helper') @patch('zk.base.ZK_helper')
@ -117,7 +127,7 @@ class PYZKTest(unittest.TestCase):
@patch('zk.base.socket') @patch('zk.base.socket')
@patch('zk.base.ZK_helper') @patch('zk.base.ZK_helper')
def test_tcp_unauth(self, helper, socket): def test_tcp_unauth(self, helper, socket):
""" Basic auth test """ """ Basic unauth test """
helper.return_value.test_ping.return_value = True # ping simulated helper.return_value.test_ping.return_value = True # ping simulated
helper.return_value.test_tcp.return_value = 0 # helper tcp ok helper.return_value.test_tcp.return_value = 0 # helper tcp ok
socket.return_value.recv.side_effect = [ socket.return_value.recv.side_effect = [
@ -126,7 +136,7 @@ class PYZKTest(unittest.TestCase):
] ]
#begin #begin
zk = ZK('192.168.1.201', password=12) zk = ZK('192.168.1.201', password=12)
self.assertRaisesRegexp(ZKErrorResponse, "Unauthenticated", zk.connect) self.assertRaisesRegex(ZKErrorResponse, "Unauthenticated", zk.connect)
socket.return_value.send.assert_called_with(codecs.decode('5050827d0c0000004e044e2ccf450100614d323c', 'hex')) # try with password 12 socket.return_value.send.assert_called_with(codecs.decode('5050827d0c0000004e044e2ccf450100614d323c', 'hex')) # try with password 12
@patch('zk.base.socket') @patch('zk.base.socket')
@ -196,6 +206,9 @@ class PYZKTest(unittest.TestCase):
#assert one user #assert one user
usu = users[3] usu = users[3]
self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid)) self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid))
if sys.version_info >= (3, 0):
self.assertIsInstance(usu.user_id, (str, bytes), "user_id should be str() or bytes() %s" % type(usu.user_id))
else:
self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id)) self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id))
self.assertEqual(usu.uid, 4, "incorrect uid %s" % usu.uid) self.assertEqual(usu.uid, 4, "incorrect uid %s" % usu.uid)
self.assertEqual(usu.user_id, "831", "incorrect user_id %s" % usu.user_id) self.assertEqual(usu.user_id, "831", "incorrect user_id %s" % usu.user_id)
@ -263,6 +276,9 @@ class PYZKTest(unittest.TestCase):
#assert one user #assert one user
usu = users[1] usu = users[1]
self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid)) self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid))
if sys.version_info >= (3, 0):
self.assertIsInstance(usu.user_id, (str, bytes), "user_id should be str() or bytes() %s" % type(usu.user_id))
else:
self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id)) self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id))
self.assertEqual(usu.uid, 2, "incorrect uid %s" % usu.uid) self.assertEqual(usu.uid, 2, "incorrect uid %s" % usu.uid)
self.assertEqual(usu.user_id, "3494866", "incorrect user_id %s" % usu.user_id) self.assertEqual(usu.user_id, "3494866", "incorrect user_id %s" % usu.user_id)
@ -291,6 +307,9 @@ class PYZKTest(unittest.TestCase):
#assert one user #assert one user
usu = users[3] usu = users[3]
self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid)) self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid))
if sys.version_info >= (3, 0):
self.assertIsInstance(usu.user_id, (str, bytes), "user_id should be str() or bytes() %s" % type(usu.user_id))
else:
self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id)) self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id))
self.assertEqual(usu.uid, 4, "incorrect uid %s" % usu.uid) self.assertEqual(usu.uid, 4, "incorrect uid %s" % usu.uid)
self.assertEqual(usu.user_id, "831", "incorrect user_id %s" % usu.user_id) self.assertEqual(usu.user_id, "831", "incorrect user_id %s" % usu.user_id)