How to use forced_value method in hypothesis

Best Python code snippet using hypothesis

modbusclient_test.py

Source:modbusclient_test.py Github

copy

Full Screen

1# Imports2import unittest, os3from unittest.mock import MagicMock4import pandas as pd5# Self-defined imports6from src.modbusclient import FactoryIOModbusClient7class FactoryIOModbusClientTest(unittest.TestCase):8 # Setup mock FactoryIO tags9 mock_tags_dict= {10 "Name": ["S_AL1_B", "AL2_ST_GRAB", "Machining Center 3 (Reset)",11 "AL2_ST_Z_POS", "AL1_Z_SET"],12 "Type": ["Input", "Output", "Output", "Input", "Output"],13 "Data Type": ["Bool", "Bool", "Bool", "Real", "Real"],14 "Address": ["Input 0", "Coil 54", "Coil 187", "Input Reg 11", 15 "Holding Reg 2"]16 }17 MOCK_TAGS_PATH= "./test/integration/mock_tags.csv"18 19 # SETUP20 # ===================================================================================21 @classmethod22 def setUpClass(cls):23 # Create mock tags dataframe24 mock_tags_df= pd.DataFrame.from_dict(FactoryIOModbusClientTest.mock_tags_dict)25 # Save to file26 mock_tags_df.to_csv(FactoryIOModbusClientTest.MOCK_TAGS_PATH, index=False)27 @classmethod28 def tearDownClass(cls):29 # Delete30 os.remove(FactoryIOModbusClientTest.MOCK_TAGS_PATH)31 def setUp(self):32 # Initialize FactoryIOModbusClient object33 self.fmc= FactoryIOModbusClient("127.0.0.1", 34 filepath= FactoryIOModbusClientTest.MOCK_TAGS_PATH)35 # ===================================================================================36 # TESTING37 # ===================================================================================38 # __init__39 def test_constructor(self):40 self.assertEqual(41 self.fmc.fault_tags,42 { "read": {}, "write": {} }43 )44 # load_tags45 def test_load_tags(self):46 loaded_tags= self.fmc.load_tags(FactoryIOModbusClientTest.MOCK_TAGS_PATH)47 # Assertions48 self.assertEqual(loaded_tags["Name"][0], 49 FactoryIOModbusClientTest.mock_tags_dict["Name"][0])50 self.assertEqual(loaded_tags["Address"][2], 187)51 self.assertEqual(loaded_tags["Type"][4], 52 FactoryIOModbusClientTest.mock_tags_dict["Type"][4])53 self.assertEqual(loaded_tags["Data Type"][1], 54 FactoryIOModbusClientTest.mock_tags_dict["Data Type"][1])55 # ===================================================================================56 # read_tag57 def test_read_tag1(self):58 # Spy on FactoryIOModbusClient object method "read_discrete_inputs"59 self.fmc.read_discrete_inputs= MagicMock()60 # Read an Input Bool tag61 self.fmc.read_tag("S_AL1_B")62 # Assert that "read_discrete_inputs" was called with:63 exp_args= {64 "Address": 0,65 "length": 1,66 "UNIT": 0x167 }68 self.fmc.read_discrete_inputs.assert_called_with(69 exp_args["Address"], exp_args["length"], unit= exp_args["UNIT"]70 )71 def test_read_tag2(self):72 # Spy on FactoryIOModbusClient object method "read_coils"73 self.fmc.read_coils= MagicMock()74 # Read an Output Bool tag75 self.fmc.read_tag("AL2_ST_GRAB")76 # Assert that "read_coils" was called with:77 exp_args= {78 "Address": 54,79 "length": 1,80 "UNIT": 0x181 }82 self.fmc.read_coils.assert_called_with(83 exp_args["Address"], exp_args["length"], unit= exp_args["UNIT"]84 )85 def test_read_tag3(self):86 # Spy on FactoryIOModbusClient object method "read_input_registers"87 self.fmc.read_input_registers= MagicMock()88 # Read an Input Reg Real tag89 self.fmc.read_tag("AL2_ST_Z_POS")90 # Assert that "read_input_registers" was called with:91 exp_args= {92 "Address": 11,93 "length": 1,94 "UNIT": 0x195 }96 self.fmc.read_input_registers.assert_called_with(97 exp_args["Address"], exp_args["length"], unit= exp_args["UNIT"]98 )99 def test_read_tag4(self):100 # Spy on FactoryIOModbusClient object method "read_holding_registers"101 self.fmc.read_holding_registers= MagicMock()102 # Read an Holding Reg Real tag103 self.fmc.read_tag("AL1_Z_SET")104 # Assert that "read_holding_registers" was called with:105 exp_args= {106 "Address": 2,107 "length": 1,108 "UNIT": 0x1109 }110 self.fmc.read_holding_registers.assert_called_with(111 exp_args["Address"], exp_args["length"], unit= exp_args["UNIT"]112 )113 @unittest.expectedFailure114 def test_read_tag_fail1(self):115 # Invalid tag name (Tag not present in supplied tags)116 self.fmc.read_tag("TAG_NOT_PRESENT")117 # ===================================================================================118 # write_tag119 def test_write_tag1(self):120 # Spy on FactoryIOModbusClient object method "write_register"121 self.fmc.write_register= MagicMock()122 # Expected arguments123 exp_args= {124 "Address": 2,125 "value": 5,126 "UNIT": 0x1127 }128 # Write to a Holding Reg Real tag129 self.fmc.write_tag("AL1_Z_SET", exp_args["value"])130 # Assert that "write_register" was called with expected arguments131 self.fmc.write_register.assert_called_with(132 exp_args["Address"], exp_args["value"], unit= exp_args["UNIT"]133 )134 def test_write_tag2(self):135 # Spy on FactoryIOModbusClient object method "write_coil"136 self.fmc.write_coil= MagicMock()137 # Expected arguments138 exp_args= {139 "Address": 54,140 "value": True,141 "UNIT": 0x1142 }143 # Write to an Output Bool tag144 self.fmc.write_tag("AL2_ST_GRAB", exp_args["value"])145 # Assert that "write_coil" was called with expected arguments146 self.fmc.write_coil.assert_called_with(147 exp_args["Address"], exp_args["value"], unit= exp_args["UNIT"]148 )149 @unittest.expectedFailure150 def test_write_tag_fail1(self):151 # Write an integer to an Output Bool tag152 self.fmc.write_tag("AL2_ST_GRAB", 5)153 @unittest.expectedFailure154 def test_write_tag_fail2(self):155 # Write to an Input Bool tag156 self.fmc.write_tag("S_AL1_B", True)157 @unittest.expectedFailure158 def test_write_tag_fail3(self):159 # Write a boolean to an Output Reg Real tag160 self.fmc.write_tag("AL1_Z_SET", False)161 @unittest.expectedFailure162 def test_write_tag_fail4(self):163 # Write a float to an Output Reg Real tag164 self.fmc.write_tag("AL1_Z_SET", 5.2)165 # ===================================================================================166 # read_fault167 def test_read_fault1(self):168 # Spy on FactoryIOModbusClient object method "read_discrete_inputs"169 self.fmc.read_discrete_inputs= MagicMock()170 tag_name= "S_AL1_B"171 forced_value= False172 # assertions173 self.assertTrue(self.fmc.read_fault(tag_name, forced_value))174 self.assertTrue(tag_name in self.fmc.fault_tags["read"])175 self.assertTrue(self.fmc.fault_tags["read"][tag_name] == forced_value)176 self.assertTrue(self.fmc.read_tag(tag_name) == forced_value)177 178 def test_read_fault2(self):179 # Spy on FactoryIOModbusClient object method "read_input_registers"180 self.fmc.read_input_registers= MagicMock()181 tag_name= "AL2_ST_Z_POS"182 forced_value= 500183 # assertions184 self.assertTrue(self.fmc.read_fault(tag_name, forced_value))185 self.assertTrue(tag_name in self.fmc.fault_tags["read"])186 self.assertTrue(self.fmc.fault_tags["read"][tag_name] == forced_value)187 self.assertTrue(self.fmc.read_tag(tag_name) == forced_value)188 def test_read_fault3(self):189 # Spy on FactoryIOModbusClient object method "read_coils"190 self.fmc.read_coils= MagicMock()191 tag_name= "AL2_ST_GRAB"192 forced_value= False193 # assertions194 self.assertTrue(self.fmc.read_fault(tag_name, forced_value))195 self.assertTrue(tag_name in self.fmc.fault_tags["read"])196 self.assertTrue(self.fmc.fault_tags["read"][tag_name] == forced_value)197 self.assertTrue(self.fmc.read_tag(tag_name) == forced_value)198 def test_read_fault4(self):199 # Spy on FactoryIOModbusClient object method "read_holding_registers"200 self.fmc.read_holding_registers= MagicMock()201 tag_name= "AL1_Z_SET"202 forced_value= 400203 # assertions204 self.assertTrue(self.fmc.read_fault(tag_name, forced_value))205 self.assertTrue(tag_name in self.fmc.fault_tags["read"])206 self.assertTrue(self.fmc.fault_tags["read"][tag_name] == forced_value)207 self.assertTrue(self.fmc.read_tag(tag_name) == forced_value)208 @unittest.expectedFailure209 def test_read_fault_fail1(self):210 tag_name= "AL2_ST_Z_POS"211 forced_value= False212 # assertions213 self.fmc.read_fault(tag_name, forced_value)214 @unittest.expectedFailure215 def test_read_fault_fail2(self):216 tag_name= "S_AL1_B"217 forced_value= 500218 # assertions219 self.fmc.read_fault(tag_name, forced_value)220 @unittest.expectedFailure221 def test_read_fault_fail3(self):222 tag_name= "AL2_ST_GRAB"223 forced_value= 500224 # assertions225 self.fmc.read_fault(tag_name, forced_value)226 @unittest.expectedFailure227 def test_read_fault_fail4(self):228 tag_name= "AL1_Z_SET"229 forced_value= False230 # assertions231 self.fmc.read_fault(tag_name, forced_value)232 # ===================================================================================233 # write_fault234 def test_write_fault1(self):235 # Spy on FactoryIOModbusClient object method "write_coil"236 self.fmc.write_coil= MagicMock()237 tag_name= "AL2_ST_GRAB"238 # Expected arguments239 exp_args= {240 "Address": 54,241 "value": True,242 "UNIT": 0x1243 }244 forced_value= False245 # assertions246 self.assertTrue(self.fmc.write_fault(tag_name, forced_value))247 self.assertTrue(tag_name in self.fmc.fault_tags["write"])248 self.assertTrue(self.fmc.fault_tags["write"][tag_name] == forced_value)249 self.fmc.write_tag(tag_name, exp_args["value"])250 self.fmc.write_coil.assert_called_with(251 exp_args["Address"],252 forced_value, unit=exp_args["UNIT"]253 )254 def test_write_fault2(self):255 # Spy on FactoryIOModbusClient object method "write_register"256 self.fmc.write_register= MagicMock()257 tag_name= "AL1_Z_SET"258 # Expected arguments259 exp_args= {260 "Address": 2,261 "value": 700,262 "UNIT": 0x1263 }264 forced_value= 400265 # assertions266 self.assertTrue(self.fmc.write_fault(tag_name, forced_value))267 self.assertTrue(tag_name in self.fmc.fault_tags["write"])268 self.assertTrue(self.fmc.fault_tags["write"][tag_name] == forced_value)269 self.fmc.write_tag(tag_name, exp_args["value"])270 self.fmc.write_register.assert_called_with(271 exp_args["Address"],272 forced_value, unit=exp_args["UNIT"]273 )274 @unittest.expectedFailure275 def test_write_fault_fail1(self):276 tag_name= "AL2_ST_GRAB"277 forced_value= 500278 # assertions279 self.fmc.write_fault(tag_name, forced_value)280 @unittest.expectedFailure281 def test_write_fault_fail2(self):282 tag_name= "AL1_Z_SET"283 forced_value= False284 # assertions...

Full Screen

Full Screen

settings.py

Source:settings.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2from __future__ import absolute_import, division, unicode_literals3from resources.lib.database.providerCache import ProviderCache4from resources.lib.modules.globals import g5class SettingsManager(ProviderCache):6 settings_template = {7 'string': {8 'default': '',9 'cast': str,10 },11 'int': {12 'default': 0,13 'cast': int14 }15 }16 def __init__(self):17 self._setting_store = {}18 super(SettingsManager, self).__init__()19 def create_settings(self, package_name, settings):20 self.execute_sql(self.package_setting_insert_query, [self._parse_new_setting(package_name, setting)21 for setting in settings])22 def _parse_new_setting(self, package_name, setting):23 try:24 setting_id = setting['id']25 setting_type = setting['type']26 except KeyError as e:27 g.log('Failed to create provider setting - {}'.format(setting))28 raise e29 visible = 1 if setting.get('visible', False) else 030 label = setting.get('label', '')31 value = setting.get('default', self.settings_template[setting_type]['default'])32 return package_name, setting_id, setting_type, visible, value, label, setting33 def remove_package_settings(self, package_name):34 self.execute_sql('DELETE FROM package_settings WHERE package=?', (package_name,))35 def _cast_setting(self, setting, forced_value=None):36 return self.settings_template[setting['type']]['cast'](setting['value'] if not forced_value else forced_value)37 def get_setting(self, package_name, setting_id):38 setting = self._get_package_setting(package_name, setting_id)39 return self._cast_setting(setting)40 def get_all_package_settings(self, package):41 return self.fetchall("SELECT * FROM package_settings WHERE package=?", (package,))42 def get_all_visible_package_settings(self, package):43 return self.fetchall("SELECT * FROM package_settings WHERE package=? AND visible=1", (package,))44 def set_setting(self, package_name, setting_id, value):45 setting = self._get_package_setting(package_name, setting_id)46 self._cast_setting(setting, forced_value=value)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful