def test_decider_current_action_pumpoff_height_in_target_and_margin_range( self): decider = Decider(target_height=5000, margin=1000) pump = Pump('127.0.0.1', 8000) sensor = Sensor('127.0.0.1', 8000) controller = Controller(sensor, pump, decider) #equal to target + margin case result = decider.decide(current_height=4000, current_action=Pump.PUMP_OFF, actions=controller.actions) self.assertEqual(result, Pump.PUMP_OFF) #equal to target + margin case result = decider.decide(current_height=4000, current_action=Pump.PUMP_OFF, actions=controller.actions) self.assertEqual(result, Pump.PUMP_OFF) #in the range of (target - margin to target + margin) result = decider.decide(current_height=5800, current_action=Pump.PUMP_OFF, actions=controller.actions) self.assertEqual(result, Pump.PUMP_OFF) #in the range of (target - margin to target + margin) result = decider.decide(current_height=4200, current_action=Pump.PUMP_OFF, actions=controller.actions) self.assertEqual(result, Pump.PUMP_OFF)
def test_decide(self): """ Test decide method of Decider class: P1: If PUMP_OFF, and levels are below margin, decider.decide should choose to PUMP_IN P2: If PUMP_OFF, and levels are higher than margin, decider.decide should choose to PUMP_OUT P3: If PUMP_OFF and levels are within margin, decider.decide should keep pump at PUMP_OFF P4a: If pump is set to PUMP_IN and levels exceed margin tolerance, decider.decide should set pump to PUMP_OFF P4b: If pump is set to PUMP_IN and levels do not exceed margin tolerance, decider.decide should not change state P5a: If pump is set to PUMP_OUT and levels drop below margin, decider.decide should set pump to PUMP_OFF P5b: If pump is set to PUMP_OUT and levels are above margin, pump should not change state """ decider = Decider(100, .10) self.assertEqual(1, decider.decide(88, 0, self.actions)) # /P1 self.assertEqual(-1, decider.decide(120, 0, self.actions)) # /P2 self.assertEqual(0, decider.decide(101, 0, self.actions)) # /P3 self.assertEqual(0, decider.decide(111, 1, self.actions)) # /P4a self.assertEqual(1, decider.decide(100, 1, self.actions)) # /P4b self.assertEqual(0, decider.decide(89, -1, self.actions)) # /P5a self.assertEqual(-1, decider.decide(111, -1, self.actions)) # /P5b
class DeciderTests(unittest.TestCase): """ This method does a setup for unit testing Decider """ def setUp(self): """ This method does a setup for unit testing Decider """ self.decider = Decider(100, 0.05) def test_decide(self): """ This method performs a unit test on decide """ pump = Pump('127.0.0.1', 1000) actions = { 'PUMP_IN': pump.PUMP_IN, 'PUMP_OUT': pump.PUMP_OUT, 'PUMP_OFF': pump.PUMP_OFF, } self.assertEqual(self.decider.decide(130, 'PUMP_OFF', actions), 1) self.assertEqual(self.decider.decide(40, 'PUMP_OFF', actions), -1) self.assertEqual(self.decider.decide(105, 'PUMP_OFF', actions), 0) self.assertEqual(self.decider.decide(140, 'PUMP_IN', actions), 0) self.assertEqual(self.decider.decide(85, 'PUMP_OUT', actions), 0) self.assertEqual(self.decider.decide(110, 'PUMP_OUT', actions), -1)
class DeciderTests(unittest.TestCase): """ Unit tests for the Decider class """ def setUp(self): """ setup :return: """ self.decider = Decider(100, 0.05) def test_decide(self): """ test decide method from Decider class """ pump = Pump('127.0.0.1', 8000) actions = { 'PUMP_IN': pump.PUMP_IN, 'PUMP_OUT': pump.PUMP_OUT, 'PUMP_OFF': pump.PUMP_OFF, } self.assertEqual(self.decider.decide(110, 'PUMP_OFF', actions), 1) self.assertEqual(self.decider.decide(60, 'PUMP_OFF', actions), -1) self.assertEqual(self.decider.decide(105, 'PUMP_OFF', actions), 0) self.assertEqual(self.decider.decide(120, 'PUMP_IN', actions), 0) self.assertEqual(self.decider.decide(90, 'PUMP_OUT', actions), 0) self.assertEqual(self.decider.decide(115, 'PUMP_OUT', actions), -1)
class DeciderTests(unittest.TestCase): """ Unit tests for the Decider class """ # TODO: write a test or tests for each of the behaviors defined for # Decider.decide def setUp(self): """ Instantiates the pump, decider, sensor, and controller classes """ self.pump = Pump('127.0.0.1', 514) self.decider = Decider(100, .01) self.sensor = Sensor('127.0.0.1', 514) self.controller = Controller(self.sensor, self.pump, self.decider) def test_decider(self): """ Tests the functionality of the decider class and the decide method """ return_value_off = self.decider.decide( 200, self.controller.actions['PUMP_IN'], self.controller.actions) return_value_in = self.decider.decide( 50, self.controller.actions['PUMP_OFF'], self.controller.actions) return_value_out = self.decider.decide( 200, self.controller.actions['PUMP_OUT'], self.controller.actions) self.assertEqual(0, return_value_off) self.assertEqual(1, return_value_in) self.assertEqual(-1, return_value_out)
class DeciderTests(unittest.TestCase): """Unit tests for the Decider class""" def setUp(self): self.decider_dict = { 'PUMP_OFF': 'maintain current level', 'PUMP_IN': 'pump water in', 'PUMP_OUT': 'pump water out' } self.decider = Decider(120, 0.05) def test_lowLevel_pumpOff(self): """Tests if decider 'chooses' to pump in, given low level and pump currently off """ self.assertEqual( self.decider.decide(105, 'PUMP_OFF', self.decider_dict), 'pump water in') def test_hiLevel_pumpOff(self): """Tests if decider 'chooses' to pump out, given high level and pump currently off """ self.assertEqual( self.decider.decide(140, 'PUMP_OFF', self.decider_dict), 'pump water out') def test_okLevel_pumpOff(self): """Tests if decider 'chooses' to keep pump off, given level within margin and pump currently off """ self.assertEqual( self.decider.decide(125, 'PUMP_OFF', self.decider_dict), 'maintain current level') def test_hiLevel_pumpIn(self): """Tests if decider 'chooses' to turn pump off, given high level and pump currently in """ self.assertEqual( self.decider.decide(130, 'PUMP_IN', self.decider_dict), 'maintain current level') def test_lowLevel_pumpOut(self): """Tests if decider 'chooses' to turn pump off, given low level and pump currently out """ self.assertEqual( self.decider.decide(110, 'PUMP_OUT', self.decider_dict), 'maintain current level')
def test_decider_current_action_pumpin_less_than_target(self): decider = Decider(target_height=7500, margin=2500) pump = Pump('127.0.0.1', 8000) sensor = Sensor('127.0.0.1', 8000) controller = Controller(sensor, pump, decider) result = decider.decide(current_height=2000, current_action=Pump.PUMP_IN, actions=controller.actions) self.assertEqual(result, Pump.PUMP_IN) result = decider.decide(current_height=7499, current_action=Pump.PUMP_IN, actions=controller.actions) self.assertEqual(result, Pump.PUMP_IN)
def test_decider_current_action_pumpout_greater_than_target(self): decider = Decider(target_height=18000, margin=2000) pump = Pump('127.0.0.1', 8000) sensor = Sensor('127.0.0.1', 8000) controller = Controller(sensor, pump, decider) result = decider.decide(current_height=19000, current_action=Pump.PUMP_OUT, actions=controller.actions) self.assertEqual(result, Pump.PUMP_OUT) result = decider.decide(current_height=20050, current_action=Pump.PUMP_OUT, actions=controller.actions) self.assertEqual(result, Pump.PUMP_OUT)
class ControllerTests(unittest.TestCase): """ Unit tests for the Controller class """ def setUp(self): """ Set up method for test_controller """ self.sensor = Sensor('127.0.0.1', 8000) self.sensor.measure = MagicMock(return_value=100) self.pump = Pump('127.0.0.1', 8000) self.pump.set_state = MagicMock(return_value=True) self.pump.get_state = MagicMock(return_value=self.pump.PUMP_OFF) self.decider = Decider(100, .10) self.decider.decide = MagicMock(return_value=self.pump.PUMP_OFF) self.controller = Controller(self.sensor, self.pump, self.decider) self.controller.tick() self.actions = { 'PUMP_IN': self.pump.PUMP_IN, 'PUMP_OUT': self.pump.PUMP_OUT, 'PUMP_OFF': self.pump.PUMP_OFF } def test_controller(self): """ Test controller and tick: 1: Check sensor.measure for accuracy 2: test pump.get_state for correct return 3: decider.decide for correct 'decision' 4: Test pump.set_state for expected mocked output 5: Test sensor.measure return val is correct type 6: Round-a-bout way of testing the try/except block of controller.tick() -- if pump.set_state returns False, it is because of a TypeError, checking that if it is False, it raises TypeError """ self.assertEqual(100, self.sensor.measure()) # 1 self.assertEqual(0, self.pump.get_state()) # 2 self.assertEqual(0, self.decider.decide(100, 0, self.actions)) # 3 self.assertEqual(True, self.pump.set_state(self.actions['PUMP_OFF'])) # 4 self.assertTrue(self.sensor.measure() == float(100)) # 5 if self.pump.set_state(False): # 6 self.assertRaises(TypeError)
def test_all(self): """ Run full test on all components of waterregulation """ pump_address = Pump('127.0.0.1', '2048') sensor_address = Sensor('127.0.0.1', '2048') decider_margins = Decider(100, .10) controller_all = Controller(sensor_address, pump_address, decider_margins) controller_all.pump.set_state = MagicMock(return_value=True) for action in controller_all.actions.values(): for water_level in range(25, 150, 100): # water level (mocked) controller_all.sensor.measure = \ MagicMock(return_value=water_level) # pump state (mocked) controller_all.pump.get_state = MagicMock( return_value=decider_margins.decide( water_level, action, controller_all.actions)) controller_all.tick()
class ModuleTests(unittest.TestCase): """ Module tests for the water-regulation module """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.addr = '127.0.0.1' self.port = 8000 self.target = 100 self.margin = 0.05 self.lower_margin = self.target - (self.target * self.margin) self.upper_margin = self.target + (self.target * self.margin) self.actions = { 'PUMP_IN': Pump.PUMP_IN, 'PUMP_OUT': Pump.PUMP_OUT, 'PUMP_OFF': Pump.PUMP_OFF, } def setUp(self): self.pump = Pump(self.addr, self.port) self.sensor = Sensor(self.addr, self.port) self.decider = Decider(self.target, self.margin) self.controller = Controller(self.sensor, self.pump, self.decider) def test_waterregulation(self): """Test the waterregulation module""" self.controller.pump.set_state = MagicMock(return_value=True) test_levels = [90, 100, 110] for pump_action in self.controller.actions.values(): self.controller.pump.get_state = MagicMock(return_value=pump_action) for level in test_levels: self.controller.sensor.measure = MagicMock(return_value=level) self.controller.pump.get_state = MagicMock( return_value=self.decider.decide(level, pump_action, self.controller.actions)) self.controller.tick()
class ModuleTests(unittest.TestCase): """ Module tests for the water-regulation module """ #TODO: write an integration test that combines controller and decider, #using a MOCKED sensor and pump. def test_module(self): """ Tests the functionality of the water regulation classes and methods within those classes """ self.pump = Pump('0.0.0.0', 514) self.sensor = Sensor('0.0.0.0', 514) self.decider = Decider(100,.01) self.control = Controller(self.sensor, self.pump, self.decider) self.pump.get_state = MagicMock(return_value = 0) current_state = self.pump.get_state() self.sensor.measure = MagicMock(return_value = 50) current_height = self.sensor.measure() new_state = self.decider.decide(current_height, current_state, self.control.actions) self.assertEqual(1, new_state)
class DeciderTests(unittest.TestCase): """ Unit tests for the Decider class """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.target = 100 self.margin = 0.05 self.lower_margin = self.target - (self.target * self.margin) self.upper_margin = self.target + (self.target * self.margin) self.actions = { 'PUMP_IN': Pump.PUMP_IN, 'PUMP_OUT': Pump.PUMP_OUT, 'PUMP_OFF': Pump.PUMP_OFF, } def setUp(self): self.decider = Decider(self.target, self.margin) def test_off_below_margin(self): """Test pump off and below margin""" curr_state = Pump.PUMP_OFF curr_level = self.lower_margin - 5 next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_IN, msg=next_state) def test_off_above_margin(self): """Test pump off and above margin""" curr_state = Pump.PUMP_OFF curr_level = self.upper_margin + 5 next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_OUT, msg=next_state) def test_off_at_margin(self): """Test pump off and within margin""" curr_state = Pump.PUMP_OFF curr_level = self.lower_margin next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_OFF, msg=next_state) def test_in_above_target(self): """Test pump in and above target""" curr_state = Pump.PUMP_IN curr_level = self.target + 5 next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_OFF, msg=next_state) def test_in_below_target(self): """Test pump in and below target""" curr_state = Pump.PUMP_IN curr_level = self.target - 5 next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_IN, msg=next_state) def test_out_below_target(self): """Test pump out and below target""" curr_state = Pump.PUMP_OUT curr_level = self.target - 5 next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_OFF, msg=next_state) def test_out_above_target(self): """Test pump out and above target""" curr_state = Pump.PUMP_OUT curr_level = self.target + 5 next_state = self.decider.decide(curr_level, curr_state, self.actions) self.assertEqual(next_state, Pump.PUMP_OUT, msg=next_state)
class ControllerTests(unittest.TestCase): """ Unit tests for the Controller class """ def setUp(self): self.sensor = Sensor('127.0.0.1', '8001') self.pump = Pump('127.0.0.1', '8000') self.decider = Decider(5, .05) self.new_controller = Controller(self.sensor, self.pump, self.decider) self.actions = { 'PUMP_IN': self.pump.PUMP_IN, 'PUMP_OUT': self.pump.PUMP_OUT, 'PUMP_OFF': self.pump.PUMP_OFF, } def test_current_height(self): """1. query the sensor for the current height of liquid in the tank""" self.sensor.measure = MagicMock(return_value=2) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) # PUMP_OFF self.pump.set_state = MagicMock(return_value=True) self.new_controller.tick() # update values of tick self.assertEqual(self.new_controller.liquid_level, measurement) def test_pump_status(self): """query the pump for its current state (pumping in, pumping out, or at rest)""" self.sensor.measure = MagicMock(return_value=2) self.pump.get_state = MagicMock(return_value=0) # PUMP_OFF pump_status = self.pump.get_state() self.pump.set_state = MagicMock(return_value=True) self.new_controller.tick() # update values of tick self.assertEqual(self.new_controller.pump_status, pump_status) def test_query_decider(self): """query the decider for the next appropriate state of the pump, given the above""" self.sensor.measure = MagicMock(return_value=2) # Set Liquid Level # to 2 measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) # PUMP_OFF pump_state = self.pump.get_state() self.pump.set_state = MagicMock(return_value=True) self.new_controller.tick() # update values of tick # with pump off and liquid level to 2, the pump should be turned on. decision = self.decider.decide(measurement, pump_state, self.actions) self.assertEqual(self.new_controller.control_decision, decision) def test_set_pump_ok(self): """set the pump to that new state. With pump off and liquid level to 2, the pump should be turned on""" self.sensor.measure = MagicMock(return_value=2) # Set Liquid Level # to 2 self.pump.get_state = MagicMock(return_value=0) # PUMP_OFF self.pump.set_state = MagicMock(return_value=True) # Assume pump # set OK self.new_controller.tick() # update values of tick self.assertTrue(self.new_controller.tick() is True) def test_set_pump_not_ok(self): """set the pump to that new state. With pump off and liquid level to 2, the pump should be turned on""" self.sensor.measure = MagicMock(return_value=2) # Set Liquid Level # to 2 self.pump.get_state = MagicMock(return_value=0) # PUMP_OFF self.pump.set_state = MagicMock(return_value=False) # Assume pump # set Not OK self.new_controller.tick() # update values of tick self.assertTrue(self.new_controller.tick() is False)
class DeciderTests(unittest.TestCase): """ Unit tests for the Decider class """ def setUp(self): """Setup DeciderTests""" self.sensor = Sensor('127.0.0.1', 8001) self.pump = Pump('127.0.0.1', 8000) self.decider = Decider(5, .05) self.actions = { 'PUMP_IN': self.pump.PUMP_IN, 'PUMP_OUT': self.pump.PUMP_OUT, 'PUMP_OFF': self.pump.PUMP_OFF, } def test_pump_off_height_low(self): """1. If the pump is off and the height is below the margin region, then the pump should be turned to PUMP_IN.""" self.sensor.measure = MagicMock(return_value=4) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) pump_status = self.pump.get_state() self.assertEqual( 1, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_off_height_high(self): """2. If the pump is off and the height is above the margin region, then the pump should be turned to PUMP_OUT.""" self.sensor.measure = MagicMock(return_value=6) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) pump_status = self.pump.get_state() self.assertEqual( -1, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_off_height_plus_margin(self): """3. If the pump is off and the height is within the margin region or on the exact boundary of the margin region, then the pump shall remain at PUMP_OFF. """ self.sensor.measure = MagicMock(return_value=5.05) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) pump_status = self.pump.get_state() self.assertEqual( 0, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_off_height_minus_margin(self): """3. If the pump is off and the height is within the margin region or on the exact boundary of the margin region, then the pump shall remain at PUMP_OFF. """ self.sensor.measure = MagicMock(return_value=4.95) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) pump_status = self.pump.get_state() self.assertEqual( 0, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_in_height_high(self): """4. If the pump is performing PUMP_IN and the height is above the target height, then the pump shall be turned to PUMP_OFF, otherwise the pump shall remain at PUMP_IN. """ self.sensor.measure = MagicMock(return_value=5.01) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=1) # PUMP_IN pump_status = self.pump.get_state() self.assertEqual( 0, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_in_height_low(self): """4. If the pump is performing PUMP_IN and the height is above the target height, then the pump shall be turned to PUMP_OFF, otherwise the pump shall remain at PUMP_IN. """ self.sensor.measure = MagicMock(return_value=4.99) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=1) # PUMP_IN pump_status = self.pump.get_state() self.assertEqual( 1, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_out_height_low(self): """5. If the pump is performing PUMP_OUT and the height is below the target height (5), then the pump shall be turned to PUMP_OFF.""" self.sensor.measure = MagicMock(return_value=4.99) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=-1) # PUMP_OUT pump_status = self.pump.get_state() self.assertEqual( 0, self.decider.decide(measurement, pump_status, self.actions)) def test_pump_out_height_high(self): """5. If the pump is performing PUMP_OUT and the height is greater than or equal to the target height (5), then the pump shall remain at PUMP_OUT.""" self.sensor.measure = MagicMock(return_value=5.0) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=-1) # PUMP_OUT pump_status = self.pump.get_state() self.assertEqual( -1, self.decider.decide(measurement, pump_status, self.actions)) def test_bad_pump_status(self): """Raise Exception for Bad Pump Status""" self.sensor.measure = MagicMock(return_value=5.0) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=-2) # Enter Bad Pump # Status pump_status = self.pump.get_state() with self.assertRaises(Exception): self.decider.decide(measurement, pump_status, self.actions) def test_bad_height_setting(self): """Raise Exception for Bad Height Setting""" with self.assertRaises(Exception): self.decider = Decider(-1, .05) def test_bad_sensor_status(self): """Raise Exception for Bad Sensor Reading. Levels can't be negative.""" self.sensor.measure = MagicMock(return_value=-2) measurement = self.sensor.measure() self.pump.get_state = MagicMock(return_value=0) # Enter Bad Sensor # Status pump_status = self.pump.get_state() with self.assertRaises(Exception): self.decider.decide(measurement, pump_status, self.actions)
class DeciderTests(unittest.TestCase): """ Unit tests for the Decider class """ # TODO: write a test or tests for each of the behaviors defined for # Decider.decide def setUp(self): """ Create instance for testing """ self.decider = Decider(100, 0.05) self.pump = Pump('127.0.0.1', 8000) self.sensor = Sensor('127.0.0.1', 8000) self.controller = Controller(self.sensor, self.pump, self.decider) def test_init(self): """ Test decider instance """ self.decider.target_height == 100 self.decider.margin == 0.05 def test_1(self): """ Test - If the pump is off and the height is below the margin region, then the pump should be turned to PUMP_IN """ decision_instance = self.decider.decide( current_height=80, current_action=self.controller.actions['PUMP_OFF'], actions=self.controller.actions) self.assertTrue( decision_instance == self.controller.actions['PUMP_IN']) def test_2(self): """ Test - If the pump is off and the height is above the margin region, then the pump should be turned to PUMP_OUT. """ decision_instance = self.decider.decide( current_height=110, current_action=self.controller.actions['PUMP_OFF'], actions=self.controller.actions) self.assertTrue( decision_instance == self.controller.actions['PUMP_OUT']) def test_3(self): """ Test - If the pump is off and the height is within the margin region or on the exact boundary of the margin region, then the pump shall remain at PUMP_OFF. """ decision_instance = self.decider.decide( current_height=100, current_action=self.controller.actions['PUMP_OFF'], actions=self.controller.actions) self.assertTrue( decision_instance == self.controller.actions['PUMP_OFF']) def test_4(self): """ Test - If the pump is performing PUMP_IN and the height is above the target height, then the pump shall be turned to PUMP_OFF, otherwise the pump shall remain at PUMP_IN. """ decision_instance = self.decider.decide( current_height=120, current_action=self.controller.actions['PUMP_IN'], actions=self.controller.actions) self.assertTrue( decision_instance == self.controller.actions['PUMP_OFF']) def test_5(self): """ Test - If the pump is performing PUMP_OUT and the height is below the target height, then the pump shall be turned to PUMP_OFF, otherwise, the pump shall remain at PUMP_OUT. """ decision_instance = self.decider.decide( current_height=80, current_action=self.controller.actions['PUMP_OUT'], actions=self.controller.actions) self.assertTrue( decision_instance == self.controller.actions['PUMP_OFF'])