From d3e587a756541f0b88d18d1b71cfb2fbc858cca7 Mon Sep 17 00:00:00 2001 From: Arturo Hernandez Date: Mon, 13 Aug 2018 11:04:17 -0400 Subject: [PATCH] fixed unittest for python3 --- test.py | 61 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/test.py b/test.py index 4b03a8b..637b87f 100644 --- a/test.py +++ b/test.py @@ -11,6 +11,12 @@ from zk.user import User from zk.finger import Finger from zk.attendance import Attendance 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): spacing = ' ' if type(obj) == dict: @@ -41,36 +47,40 @@ class PYZKTest(unittest.TestCase): def tearDown(self): pass - + + @patch('zk.base.socket') @patch('zk.base.ZK_helper') - def test_no_ping(self,helper): + def test_no_ping(self,helper, socket): """ what if ping doesn't response """ helper.return_value.test_ping.return_value = False #no ping simulated #begin zk = ZK('192.168.1.201') 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') - def test_correct_ping(self,helper): + def test_correct_ping(self,helper, socket): """ what if ping is ok """ 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 zk = ZK('192.168.1.201') 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.ZK_helper') 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_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 zk = ZK('192.168.1.201') 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.ZK_helper') @@ -117,7 +127,7 @@ class PYZKTest(unittest.TestCase): @patch('zk.base.socket') @patch('zk.base.ZK_helper') 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_tcp.return_value = 0 # helper tcp ok socket.return_value.recv.side_effect = [ @@ -126,7 +136,7 @@ class PYZKTest(unittest.TestCase): ] #begin 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 @patch('zk.base.socket') @@ -139,7 +149,7 @@ class PYZKTest(unittest.TestCase): codecs.decode('5050827d08000000d5075bb2cf450000', 'hex'), # tcp CMD_UNAUTH codecs.decode('5050827d08000000d0075fb2cf450100', 'hex'), # tcp CMD_ACK_OK codecs.decode('5050827d08000000d00745b2cf451b00', 'hex') # tcp random CMD_ACK_OK TODO: generate proper sequenced response - + ] #begin zk = ZK('192.168.1.201', password=45) @@ -196,12 +206,15 @@ class PYZKTest(unittest.TestCase): #assert one user usu = users[3] self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid)) - self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id)) + 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.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.name, "NN-831", "incorrect uid %s" % usu.name) # generated conn.disconnect() - + @patch('zk.base.socket') @patch('zk.base.ZK_helper') def test_tcp_get_users_broken_tcp(self, helper, socket): @@ -218,19 +231,19 @@ class PYZKTest(unittest.TestCase): codecs.decode('0000000004000e00000000000000000000000000000000000000000000000000000000000000000000000001000000000000000032333338323035000000000000000000000000000000000005000e000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000333632363439300000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000313838343633340000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000000000000', 'hex'), #raw data 256 codecs.decode('00000000000000000000000000000000000000000000000000000000000000000000000100000000000000003131313336333200000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003130353233383900000000000000000000000000000000000d00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003135333538333600000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000100000000', 'hex'), #raw data 256 codecs.decode('000000003933313637300000000000000000000000000000', 'hex'), #raw data 24 - + codecs.decode('5050827df8030000dd0520b601001600000000000f00003334323931343800000000000000000000000000000000000000000000000000000000000100000000000000003334323931343800000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003139303636393700000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003139333831333500000000000000000000000000', 'hex'), # DATA 1016 -8 (util216 codecs.decode('00000000120000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000393231303537000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000333634383739340000000000000000000000000000000000140000323831353732000000000000000000000000000000000000000000000000000000000000010000000000000000323831353732000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000000000000', 'hex'), #raw data 256 codecs.decode('00000001000000000000000031383133323236000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000035393037353800000000000000000000000000000000000017000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000031363933373232000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000033363430323131000000000000000000000000000000000019000000', 'hex'), #raw data 256 codecs.decode('00000000000000000000000000000000000000000000000000000000000000000000000100000000000000003331303733390000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003433353430393400000000000000000000000000000000001b00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003338303736333200000000000000000000000000000000001c00000000000000000000000000000000000000000000000000000000000000000000000000000100000000', 'hex'), #raw data 256 codecs.decode('000000003231333938313700000000000000000000000000', 'hex'), #raw data 24 - + codecs.decode('5050827df8030000dd059a2102001600000000001d00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003333383738313900000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003439353634363800000000000000000000000000000000001f00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003832343030300000000000000000000000000000', 'hex'), #DATA 1016 -8 (util 216) codecs.decode('00000000200000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000333937373437370000000000000000000000000000000000210000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000343435383038340000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000343430353130390000000000000000000000000000000000230000000000000000000000000000000000000000000000000000000000000000000000', 'hex'), #raw data 256 codecs.decode('00000001000000000000000033353732363931000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000033363336333832000000000000000000000000000000000025000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000033333232353432000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000039393437303800000000000000000000000000000000000027000000', 'hex'), #raw data 256 codecs.decode('00000000000000000000000000000000000000000000000000000000000000000000000100000000000000003836333539380000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003338383736383000000000000000000000000000000000002900000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003739393434350000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000000000000000000000100000000', 'hex'), # raw data 256 codecs.decode('000000003532313136340000000000000000000000000000', 'hex'), # raw data 24 - + codecs.decode('5050827df8030000dd053da903001600000000002b00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003439373033323400000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000000000000', 'hex'), # DATA 1016 -8 (util 112) codecs.decode('0000000100000000000000003134363732353100000000000000000000000000000000002d000e32363635373336006d61726368756b0000000000000000000000000000000000000000000100000000000000003236363537333600000000000000000000000000', 'hex'), # raw data 104 codecs.decode('000000002e00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003136383133353200000000000000000000000000000000002f000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000034393633363732000000000000000000000000000000000030000000', 'hex'), # raw data 152 @@ -238,16 +251,16 @@ class PYZKTest(unittest.TestCase): codecs.decode('0000000033373336323437000000000000000000000000000000000034000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000031323930313635000000000000000000000000000000000035000000000000000000000000000000000000000000000000000000', 'hex'), # raw data 128 codecs.decode('0000000000000000000000010000000000000000333236333636330000000000000000000000000000000000360000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000393031353036000000000000000000000000000000000000370000000000000000000000', 'hex'), # raw data 128 codecs.decode('0000000000000000000000000000000000000000000000000000000100000000000000003238313732393300000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003437303630333800000000000000000000000000', 'hex'), # raw data 128 - + codecs.decode('5050827df8030000dd05037d04001600000000003900000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003136343731353600000000000000000000000000000000003a0000000000000000000000000000000000000000000000000000000000000000000000', 'hex'), # DATA 1016 -8 (util 112) codecs.decode('0000000100000000000000003530313435310000000000000000000000000000000000003b00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003534363236373300000000000000000000000000000000003c00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003533363730310000000000000000000000000000000000003d00000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003435383033303700000000000000000000000000000000003e000000', 'hex'), # raw data 256 codecs.decode('00000000000000000000000000000000000000000000000000000000000000000000000100000000000000003136333835333200000000000000000000000000000000003f000e3336323634313900000000000000000000000000000000000000000000000000000000000100000000000000003336323634313900000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003233323331383500000000000000000000000000000000004100000000000000000000000000000000000000000000000000000000000000000000000000000100000000', 'hex'), # raw data 256 codecs.decode('0000000035323930373337000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000033393839303636000000000000000000000000000000000043000000000000000000000000000000000000000000000000000000', 'hex'), # raw data 128 codecs.decode('0000000000000000000000010000000000000000343033323930390000000000000000000000000000000000440000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000323034363338380000000000000000000000000000000000450000000000000000000000', 'hex'), # raw data 128 codecs.decode('0000000000000000000000000000000000000000000000000000000100000000000000003733383730330000000000000000000000000000000000004600000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000003239313836333600000000000000000000000000', 'hex'), # raw data 128 - + codecs.decode('5050827d0c000000dd0507fa0500160000000000', 'hex'), # DATA 12-8 (util 4 ok) and ACK OK!!! - + codecs.decode('5050827d08000000d00745b2cf451b00', 'hex'), # CMD_ACK_OK for get_users TODO: generate proper sequenced response codecs.decode('5050827d08000000d00745b2cf451b00', 'hex'), # CMD_ACK_OK for free_data TODO: generate proper sequenced response codecs.decode('5050827d08000000d00745b2cf451b00', 'hex'), # CMD_ACK_OK for exit TODO: generate proper sequenced response @@ -263,7 +276,10 @@ class PYZKTest(unittest.TestCase): #assert one user usu = users[1] self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid)) - self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id)) + 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.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.name, "NievesLopez", "incorrect uid %s" % usu.name) # check test case @@ -291,7 +307,10 @@ class PYZKTest(unittest.TestCase): #assert one user usu = users[3] self.assertIsInstance(usu.uid, int, "uid should be int() %s" % type(usu.uid)) - self.assertIsInstance(usu.user_id, (str, unicode), "user_id should be str() or unicode() %s" % type(usu.user_id)) + 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.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.name, "NN-831", "incorrect uid %s" % usu.name) # generated