Ejemplo n.º 1
0
class ControllerTests(unittest.TestCase):
    """
    Unit tests for the Controller class
    """
    def setUp(self):
        """
        setup
        :return:
        """
        self.pump = Pump('127.0.0.1', 8000)
        self.sensor = Sensor('127.1.1.3', 9000)
        self.decider = Decider(100, 0.05)

        self.controller = Controller(self.sensor, self.pump, self.decider)

    def test_tick(self):
        """
        test tick of Controller class
        :return:
        """
        self.sensor.measure = MagicMock(return_value=110)
        self.pump.get_state = MagicMock(return_value='PUMP_OFF')
        self.controller.tick = MagicMock(return_value='PUMP_IN')
        self.controller.tick()
        self.assertEqual(self.controller.tick(), 'PUMP_IN')
Ejemplo n.º 2
0
class ControllerTests(unittest.TestCase):
    """
    Unit tests for the Controller class
    """

    #TODO: write a test or tests for each of the behaviors defined for
    #Controller.tick
    def setUp(self):
        """
        Instantiates the pump, decider, sensor and controller classes
        """
        self.pump = Pump('127.0.0.1', 514)
        self.sensor = Sensor('127.0.0.1', 514)
        self.decider = Decider(100, .01)
        self.controller = Controller(self.sensor, self.pump, self.decider)

    def test_pump_call(self):
        """
        Tests the functionality of the controller class and the tick method
        """
        self.pump.set_state = MagicMock(return_value=True)
        self.pump.get_state = MagicMock(return_value=True)
        self.sensor.measure = MagicMock(return_value=True)
        self.decider.decide = MagicMock(return_value=True)
        self.controller.tick()
        self.pump.set_state.assert_called_with(True)
        self.pump.get_state.assert_called_with()
        self.sensor.measure.assert_called_with()
        self.decider.decide.assert_called_with(True, True,
                                               self.controller.actions)
 def test_integration(self):
     """Method docstring."""
     sensor = Sensor(self.address, self.port)
     pump = Pump(self.address, self.port)
     decider = Decider(100, .05)
     controller = Controller(sensor, pump, decider)
     for level in range(0, 200, 10):
         for action in controller.actions.values():
             sensor.measure = MagicMock(return_value=level)
             pump.get_state = MagicMock(return_value=action)
             pump.set_state = MagicMock(return_value=True)
             controller.tick()
     pump.set_state = MagicMock(return_value=False)
     controller.tick()
Ejemplo n.º 4
0
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()
Ejemplo n.º 6
0
class ControllerTests(unittest.TestCase):
    """
    Unit tests for the Controller class
    """

    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_tick(self):
        """Test controller tick function"""
        self.sensor.measure = MagicMock(return_value=self.target)
        self.pump.get_state = MagicMock(return_value=Pump.PUMP_OFF)
        self.decider.decide = MagicMock(return_value=Pump.PUMP_OFF)
        self.pump.set_state = MagicMock(return_value=True)

        self.controller.tick()

        self.sensor.measure.assert_called_with()
        self.pump.get_state.assert_called_with()
        self.decider.decide.assert_called_with(self.target,
                                               Pump.PUMP_OFF,
                                               self.actions)
        self.pump.set_state.assert_called_with(Pump.PUMP_OFF)
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()
Ejemplo n.º 8
0
    def test_integration_set_pump_state_fail(self):
        decider = Decider(target_height=3000, margin=500)
        pump = Pump('127.0.0.1', 8000)
        sensor = Sensor('127.0.0.1', 8000)

        pump.set_state = MagicMock(return_value=False)
        pump.get_state = MagicMock(return_value=Pump.PUMP_OFF)

        sensor.measure = MagicMock(return_value=3500)
        controller = Controller(sensor, pump, decider)

        result = controller.tick()
        self.assertEqual(result, False)
class ModuleTests(unittest.TestCase):
    """
    Module tests for the water-regulation module
    """
    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_pump_out(self):
        """Given a high water level (7) and the pump is off (0),
        show that the controller starts PUMP_OUT (-1).
        """
        # Set Liquid Level to 7, above target height of 5
        self.sensor.measure = MagicMock(return_value=7)
        self.pump.get_state = MagicMock(return_value=0)  # PUMP_OFF
        self.pump.set_state = MagicMock(return_value=True)  # Assume connection
        # with PUMP is made
        self.new_controller.tick()  # update values of tick
        self.assertEqual(self.new_controller.control_decision, -1)

    def test_pump_in(self):
        """Given a high water level (7) and PUMP_IN (1) show that the
        controller turns off pump.
        """
        # Set Liquid Level to 7, above target height of 5
        self.sensor.measure = MagicMock(return_value=7)
        self.pump.get_state = MagicMock(return_value=1)  # PUMP_IN
        self.pump.set_state = MagicMock(return_value=True)  # Assume
        # connection with PUMP is made
        self.new_controller.tick()  # update values of tick
        self.assertEqual(self.new_controller.control_decision, 0)
Ejemplo n.º 10
0
class ControllerTests(unittest.TestCase):
    """
    This class performs a unit test on Controller
    """
    def setUp(self):
        """
        This method does a setup for unit testing Controller
        """
        self.pump = Pump('127.0.0.1', 1000)
        self.sensor = Sensor('127.0.0.2', 2000)
        self.decider = Decider(100, 0.05)

        self.controller = Controller(self.sensor, self.pump, self.decider)

    def test_tick(self):
        """
        This method performs a unit test on tick
        """
        self.sensor.measure = MagicMock(return_value=130)
        self.pump.get_state = MagicMock(return_value='PUMP_OFF')
        self.controller.tick = MagicMock(return_value='PUMP_IN')
        self.controller.tick()

        self.assertEqual(self.controller.tick(), 'PUMP_IN')
Ejemplo n.º 11
0
    def test_controller_tick(self):
        """Tests if, on each call to tick(), the controller object
        queries each of the sensor, pump, and decider, then sets pump
        to new state
        """
        sensor = Sensor('127.0.0.1', '514')
        sensor.measure = MagicMock(return_value=105)
        pump = Pump('127.0.0.1', '8000')
        pump.get_state = MagicMock(return_value='PUMP_OFF')
        pump.set_state = MagicMock(return_value='PUMP_IN')
        decider = Decider(120, 0.05)

        controller = Controller(sensor, pump, decider)

        self.assertTrue(controller.tick())
Ejemplo n.º 12
0
class ControllerTests(unittest.TestCase):
    """
    Unit tests for the Controller class
    """
    def setUp(self):
        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_tick(self):
        """
        Test tick method
        """
        self.controller.sensor.measure = MagicMock(return_value=80)
        self.controller.pump.get_state = MagicMock(return_value=0)
        self.controller.pump.set_state = MagicMock(return_value=True)

        self.assertTrue(self.controller.tick())
Ejemplo n.º 13
0
class ModuleTests(unittest.TestCase):
    """
    This class performs an integration test on waterregulation
    """
    def setUp(self):
        """
        This method does a setup for integration testing raterregulation
        """
        self.pump = Pump('127.0.0.1', 1000)
        self.sensor = Sensor('127.0.0.2', 2000)
        self.decider = Decider(100, 0.05)
        self.controller = Controller(self.sensor, self.pump, self.decider)

    def test_tick(self):
        """
        This method performs an integration test for tick
        """
        self.sensor.measure = MagicMock(return_value=75)
        self.pump.get_state = MagicMock(return_value='PUMP_OFF')
        self.controller.tick = MagicMock(return_value='PUMP_OUT')
        self.assertEqual(self.controller.tick(), 'PUMP_OUT')
Ejemplo n.º 14
0
    def test_tick(self):
        """Method docstring."""

        pump_address = Pump('127.0.0.1', 8000)
        sensor_address = Sensor('127.0.0.1', 8000)
        decider_vals = Decider(100, 5)
        controller_all = Controller(sensor_address, pump_address, decider_vals)

        sensor_address.measure = MagicMock(return_value=90)
        pump_address.get_state = MagicMock(return_value=pump_address.PUMP_OFF)
        decider_vals.decide_pump_action = MagicMock(
            return_value=pump_address.PUMP_IN)
        pump_address.set_state = MagicMock(return_value=True)
        controller_all.tick()
        pump_address.get_state = MagicMock(return_value=pump_address.PUMP_IN)
        controller_all.tick()
        pump_address.get_state = MagicMock(return_value=pump_address.PUMP_OUT)
        controller_all.tick()
        sensor_address.measure.assert_called_with()
        pump_address.get_state.assert_called_with()
        pump_address.set_state.assert_called_with(pump_address.PUMP_OFF)
Ejemplo n.º 15
0
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)