def test_init_invalid_type(self): """Test symbol lookup when type cannot be found There was a read/write check that verifies the plc_typ was not None, but this was removed. """ var = PLCVariable(name="UnknownType", ads_type=0, symbol_type="non_existent_type") var.index_group = 123 var.index_offset = 100 var.plc_type = constants.PLCTYPE_BYTE # Set to something real self.handler.add_variable(var) with self.plc: # Create symbol while providing everything: symbol = AdsSymbol(self.plc, name=var.name) self.assertEqual(var.symbol_type, symbol.symbol_type) self.assertIsNone(symbol.plc_type) # with self.assertRaises(ValueError) as cm: # # Without type specified, it cannot read # symbol.read() # self.assertIn('Cannot read or write', str(cm.exception)) with self.assertRaises(TypeError) as cm: # Error is thrown inside pyads_ex symbol.read() self.assertIn("NoneType", str(cm.exception)) self.assertAdsRequestsCount(1) # Only a WRITE followed by a READ
def test_init_by_name_matrix_style(self): """Test symbol creation when it's an array denoted as matrix This is how an array originating from Simulink could look like. """ var = PLCVariable( "ArrayVar", ads_type=0, symbol_type="matrix_21_int8_T", # Simulink array looks like this ) var.plc_type = constants.PLCTYPE_ARR_SINT(21) # Have to do this # manually var.index_group = 123 var.index_offset = 100 self.handler.add_variable(var) self.plc.open() symbol = AdsSymbol( self.plc, name=var.name, index_group=var.index_group, index_offset=var.index_offset, symbol_type=var.symbol_type, ) # No lookup # Verify looked up info self.assertEqual(var.plc_type, symbol.plc_type) self.assertEqual(var.symbol_type, symbol.symbol_type) self.assertAdsRequestsCount(0) # No requests
def test_init_by_name_array(self): """Test symbol creation when it's an array""" var = PLCVariable( "ArrayVar", ads_type=constants.ADST_INT16, # dataType does not represent # array unfortunately symbol_type="ARRAY [1..5] OF INT", # Array looks like this in PLC ) var.plc_type = constants.PLCTYPE_ARR_INT(5) # Have to do this # manually self.handler.add_variable(var) self.plc.open() symbol = AdsSymbol(self.plc, name=var.name) # Verify looked up info self.assertEqual(var.name, symbol.name) self.assertEqual(var.index_group, symbol.index_group) self.assertEqual(var.index_offset, symbol.index_offset) self.assertEqual(var.plc_type, symbol.plc_type) self.assertEqual(var.symbol_type, symbol.symbol_type) self.assertIsNone(symbol.comment) my_list = symbol.read() self.assertIsInstance(my_list, list) self.assertEqual(5, len(my_list)) my_list[4] = 420 symbol.write(my_list) # Modify array my_list2 = symbol.read() # Read again self.assertEqual(my_list, my_list2) self.assertAdsRequestsCount(4) # A READWRITE (for info), READ,