class ControllerTests(unittest.TestCase):
    """
    Unit tests for the Controller class
    """
    def setUp(self):
        """
            Sets the necessary varialbles to test the Controller class
        """

        self.pump = Pump('127.0.0.1', 8000)
        self.pump.set_state = MagicMock(return_value=True)

        self.new_sensor = Sensor('127.0.0.1', 8000)

        self.new_decider = Decider(100, .10)

        self.new_controller = Controller(self.new_sensor, self.pump,
                                         self.new_decider)

        self.actions = {
            'PUMP_IN': self.pump.PUMP_IN,
            'PUMP_OUT': self.pump.PUMP_OUT,
            'PUMP_OFF': self.pump.PUMP_OFF,
        }

    def test_controller_tick(self):
        """
            Tests each of the behaviors defined in the Controller class
        """
        self.pump.get_state = MagicMock(return_value=self.pump.PUMP_OFF)
        self.new_sensor.measure = MagicMock(return_value=89)
        self.new_decider.decide = MagicMock(return_value=self.pump.PUMP_IN)

        # 1. query the sensor for the current height of liquid in the tank

        self.assertEqual(self.new_sensor.measure(), 89)

        # 2. query the pump for its current state (pumping in,
        #    pumping out, or at rest)

        self.assertEqual(self.pump.get_state(), self.actions['PUMP_OFF'])

        # 3. query the decider for the next appropriate state of the pump,
        #    given the above
        next_state = self.new_decider.decide(self.new_sensor.measure,
                                             self.pump.get_state(),
                                             self.actions)
        self.assertEqual(next_state, self.actions['PUMP_IN'])

        # 4. set the pump to that new state
        self.assertEqual(self.new_controller.tick(), True)
Exemple #2
0
    def test_integration(self):
        '''
        I can see how you could use mocks to provide data for classes that
        aren't
        actually implemented yet.  However, since both the set_state and

        get_state methods of pump are overridden the code doesn't seem to
         really
        test anything except that it doesn't crash.

        The important part, the decide method, is covered in the unit tests
        so I guess this mock method is as good as we can test until the
        pump and sensor are actually hooked up to live objects.

        I can see how the test can be converted to test real objects by just
        taking out the mock method assignments.
        '''
        decider = Decider(4, 3)
        pump = Pump('127.0.0.1', 8000)

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

        sensor = Sensor('127.0.0.1', 8000)
        sensor.measure = MagicMock(return_value=3)

        controller = Controller(sensor, pump, decider)
        self.assertEqual(controller.tick(), False)
    def test_controller_tick(self):
        """test the tick function in the controller"""

        sensor = Sensor('127.0.0.1', 8000)
        pump = Pump('127.0.0.1', 8000)
        dec = Decider(100, .05)
        con = Controller(sensor, pump, dec)

        # liquid height
        sensor.measure = MagicMock(return_value=94)

        # state of pump
        pump.get_state = MagicMock(return_value=pump.PUMP_IN)

        # decider next state for pump
        dec.decide = MagicMock(return_value=pump.PUMP_IN)

        # this line was added to fix my error
        pump.set_state = MagicMock(return_value=True)

        con.tick()

        sensor.measure.assert_called_with()
        pump.get_state.assert_called_with()
        dec.decide.assert_called_with(94, pump.PUMP_IN, actions)
Exemple #4
0
    def test_tick(self):
        """
        Tests for the tick method
        """
        sensor = Sensor("127.0.0.1", "5150")
        pump = Pump("127.0.0.1", "5051")
        decider = Decider(500, .05)

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

        sensor.measure = MagicMock(return_value=400.0)
        pump.get_state = MagicMock(return_value=Pump.PUMP_OFF)
        decider.decide = MagicMock(return_value=Pump.PUMP_IN)
        pump.set_state = MagicMock(return_value=True)

        controller.tick()

        decider.decide.assert_called_with(sensor.measure(), pump.get_state(),
                                          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()
Exemple #6
0
    def test_controller_tick(self):
        '''testing controller'''
        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(100, 0.05)

        controller = Controller(sensor, pump, decider)

        self.assertTrue(controller.tick())
Exemple #7
0
class ControllerTests(unittest.TestCase):
    """
    Unit tests for the Controller class
    """

    def setUp(self):
        # sensor
        self.sensor = Sensor('127.0.0.1', 8000)
        self.sensor.measure = MagicMock(return_value=50)

        # pump
        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)

        # decider
        self.decider = Decider(50, .05)
        self.decider.decide = MagicMock(return_value=self.pump.PUMP_OFF)

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

        # actions
        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
        """

        # measure
        self.sensor.measure.assert_called_with()
        self.assertEqual(50, self.sensor.measure())

        # get state
        self.pump.get_state.assert_called_with()
        self.assertEqual(0, self.pump.get_state())

        # decide
        self.decider.decide.assert_called_with(
            50, self.actions['PUMP_OFF'], self.actions
        )
        self.assertEqual(0, self.decider.decide(50, 0, self.actions))

        # set state
        self.pump.set_state.assert_called_with(self.actions['PUMP_OFF'])
        self.assertEqual(True, self.pump.set_state(self.actions['PUMP_OFF']))
Exemple #8
0
    def test_tick(self):
        """
        test case to test tick method
        """

        pump = Pump('127.0.0', 8000)
        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=Pump.PUMP_OFF)
        sensor = Sensor('127.0.0', 8000)
        sensor.measure = MagicMock(return_value=89)
        decider = Decider(100, 0.1)
        controller = Controller(sensor, pump, decider)
        self.assertEqual(Pump.PUMP_IN, controller.tick())
class PumpTests(unittest.TestCase):
    """Unit tests for the Pump class"""
    def setUp(self):
        self.pump = Pump('127.0.0.1', 8000)

    def test_pump_get_state(self):
        """Tests whether pump state can be obtained"""
        self.pump.get_state = MagicMock(return_value='PUMP_OFF')
        self.assertEqual(self.pump.get_state(), 'PUMP_OFF')

    def test_pump_set_state(self):
        """Tests whether pump state can be set"""
        self.pump.set_state = MagicMock(return_value='PUMP_OFF')
        self.assertTrue(self.pump.set_state('PUMP_OFF'))
Exemple #10
0
    def test_tick(self):
        """Method docstring."""
        ACTIONS = {'PUMP_IN': 1, 'PUMP_OFF': 0, 'PUMP_OUT': -1}

        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)
Exemple #11
0
    def test_control(self):
        """
        Method to test function of control method
        """
        pump = Pump('127.0.0.1', 8000)
        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=0)
        sensor = Sensor('127.0.0.1', 8000)
        sensor.measure = MagicMock(return_value=10.)
        decider = Decider(10., 0.05)
        decider.decide = MagicMock(return_value=True)
        controller = Controller(sensor, pump, decider)

        self.assertEqual(controller.tick(), True)
Exemple #12
0
    def test_tick_false(self):
        """
        Test that tick return False if pump.set_state returns False
        """
        sensor = Sensor('127.0.0.1', 8000)
        pump = Pump('127.0.0.1', 8000)
        decider = Decider(10, .1)
        controller = Controller(sensor, pump, decider)

        sensor.measure = MagicMock(return_value=10)
        pump.get_state = MagicMock(return_value=pump.PUMP_OFF)
        pump.set_state = MagicMock(return_value=False)

        self.assertFalse(controller.tick())
Exemple #13
0
    def test_sanity(self):
        '''
        Test for a failed pump set state return value
        '''
        decider = Decider(5, 2)
        pump = Pump('127.0.0.1', 8000)

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

        sensor = Sensor('127.0.0.1', 8000)
        sensor.measure = MagicMock(return_value=3)

        controller = Controller(sensor, pump, decider)
        self.assertEqual(controller.tick(), False)
Exemple #14
0
    def test_integration_set_pump_state(self):
        '''
        Test for a successful pump set state return value
        '''
        decider = Decider(5, 2)
        pump = Pump('127.0.0.1', 8000)

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=Pump.PUMP_IN)

        sensor = Sensor('127.0.0.1', 8000)
        sensor.measure = MagicMock(return_value=3)

        controller = Controller(sensor, pump, decider)
        self.assertEqual(controller.tick(), True)
    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())
Exemple #16
0
    def test_tick(self):
        """
        Tests if tick method works.
        """
        # Initialize controller, decider, pump, sensor.
        sensor = Sensor('127.0.0.1', 8000)
        pump = Pump('127.0.0.1', 8000)
        decider = Decider(50, 5)
        controller = Controller(sensor, pump, decider)

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=pump.PUMP_OFF)
        sensor.measure = MagicMock(return_value=25)

        self.assertEqual(Pump.PUMP_IN, controller.tick())
Exemple #17
0
    def second_controller_test(self, address, port):
        '''Tests the ability to query the pump for its current state'''

        logger.info("Starting second controller test")
        try:
            #Creating pump, sensor, and decider for test case
            my_pump = Pump(address, port)
            my_sensor = Sensor(address, port)
            my_decider = Decider(10, 1)
            #Passing pump, sensor, and decider to controller
            my_controller = Controller(my_sensor, my_pump, my_decider)
            logger.info("Done with second controller test")
            self.assertTrue(my_pump.get_state() in [1, -1, 0])

        except Exception as exception:
            logger.critical(exception)
Exemple #18
0
    def test_ctrl_decide(self):
        """
        Method tests combined function of controller and decider
        """
        pump = Pump('127.0.0.1', 8000)
        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=0)
        sensor = Sensor('127.0.0.1', 8000)
        sensor.measure = MagicMock(return_value=10.)
        decider = Decider(10., 0.05)
        controller = Controller(sensor, pump, decider)

        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_OFF'])

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=0)
        sensor.measure = MagicMock(return_value=1.)
        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_IN'])

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=0)
        sensor.measure = MagicMock(return_value=20.)
        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_OUT'])

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=1)
        sensor.measure = MagicMock(return_value=20.)
        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_OFF'])

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=1)
        sensor.measure = MagicMock(return_value=1.)
        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_IN'])

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=-1)
        sensor.measure = MagicMock(return_value=1.)
        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_OFF'])

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=-1)
        sensor.measure = MagicMock(return_value=20.)
        self.assertEqual(controller.tick(), True)
        pump.set_state.assert_called_with(controller.actions['PUMP_OUT'])
Exemple #19
0
 def test_tick(self):
     """
     Testing the tick method in controller
     """
     pump = Pump('127.0.0.1', 8000)
     sensor = Sensor('127.0.0.1', 8000)
     decider = Decider(100, .05)
     controller = Controller(sensor, pump, decider)
     sensor.measure = MagicMock(return_value=95)
     pump.get_state = MagicMock(return_value=pump.PUMP_IN)
     decider.decide = MagicMock(return_value=pump.PUMP_IN)
     pump.set_state = MagicMock(return_value=True)
     controller.tick()
     sensor.measure.assert_called_with()
     pump.get_state.assert_called_with()
     decider.decide.assert_called_with(95, pump.PUMP_IN, actions)
Exemple #20
0
    def test_controller(self):
        """Tests for each of the behaviors defined for Controller.tick"""
        sensor = Sensor('127.0.0.1', 8000)
        pump = Pump('127.0.0.1', 8000)
        decider = Decider(100, 0.05)
        controller = Controller(sensor, pump, decider)

        sensor.measure = MagicMock(return_value=95)
        pump.get_state = MagicMock(return_value=pump.PUMP_IN)
        decider.decide = MagicMock(return_value=pump.PUMP_IN)
        pump.set_state = MagicMock(return_value=True)

        controller.tick()
        sensor.measure.assert_called_with()
        pump.get_state.assert_called_with()
        decider.decide.assert_called_with(95, pump.PUMP_IN, ACTIONS)
 def test_integration(self):
     """
     Test full integration of controller and decider components.
     """
     pump = Pump('127.0.0.1', "8000")
     pump.get_state = MagicMock(return_value=1)
     pump.set_state = MagicMock(return_value=True)
     sensor = Sensor('127.0.0.1', "8080")
     sensor.measure = MagicMock(return_value=120)
     decider = Decider(100, 0.05)
     controller = Controller(sensor, pump, decider)
     controller_called = controller.tick()
     sensor.measure.assert_called()
     pump.get_state.assert_called()
     pump.set_state.assert_called_with(-1)
     self.assertEqual(controller_called, True)
Exemple #22
0
    def test_tick(self):
        """Test logic of sensor, pump."""
        sensor = Sensor(ControllerTests.DUMMY_ADDR, ControllerTests.DUMMY_PORT)
        sensor.measure = MagicMock(return_value=10)
        pump = Pump(ControllerTests.DUMMY_ADDR, ControllerTests.DUMMY_PORT)
        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=Pump.PUMP_IN)
        decider = Decider(10, 0.05)
        decider.decide = MagicMock(return_value=100)

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

        sensor.measure.assert_called_once_with()
        pump.get_state.assert_called_once_with()
        pump.set_state.assert_called_once_with(100)
Exemple #23
0
    def test_modules(self):
        """
        Tests that all modules are called correctly
        """

        # Initialize controller, decider, pump, sensor.
        sensor = Sensor('127.0.0.1', 9000)
        pump = Pump('127.0.0.1', 9000)
        decider = Decider(50, 10)
        controller = Controller(sensor, pump, decider)

        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=pump.PUMP_IN)
        sensor.measure = MagicMock(return_value=65)

        self.assertEqual(Pump.PUMP_IN, controller.tick())
Exemple #24
0
    def test_tick(self):
        """
        Tests the tick method
        """
        pump = Pump('127.0.0.1', 8000)
        pump.set_state = MagicMock(return_value=True)
        pump.get_state = MagicMock(return_value=Pump.PUMP_OUT)

        sensor = Sensor('127.0.0.1', 8000)
        sensor.measure = MagicMock(return_value=1.0)

        decider = Decider(1000.0, 0.5)

        controller = Controller(sensor, pump, decider)

        self.assertTrue(controller.tick())
Exemple #25
0
    def fourth_controller_test(self, address, port):
        '''Tests the ability to set the pump to a new state'''
        try:
            logger.info("Starting fourth controller test")
            print("here")
            #Creating pump, sensor, and decider for test case
            my_pump = Pump(address, port)
            my_sensor = Sensor(address, port)
            my_decider = Decider(10, 1)
            #Passing pump, sensor, and decider to controller
            my_controller = Controller(my_sensor, my_pump, my_decider)
            logger.info("Done with fourth controller test")
            my_pump.set_state(0)
            self.assertequal(my_pump.get_state(), 0)

        except Exception as exception:
            logger.critical(exception)
Exemple #26
0
    def test_tick(self):
        """
        Test Controller tick function
        """
        pump = Pump('127.0.0.1', 8080)
        sensor = Sensor('127.0.0.1', 8083)
        decider = Decider(100, .1)
        controller = Controller(sensor, pump, decider)
        sensor.measure = MagicMock(return_value=127)
        pump.get_state = MagicMock(return_value=pump.PUMP_IN)
        decider.decide = MagicMock(return_value=pump.PUMP_IN)
        pump.set_state = MagicMock(return_value=True)

        controller.tick()
        sensor.measure.assert_called_with()
        pump.get_state.assert_called_with()
        decider.decide.assert_called_with(127, pump.PUMP_IN, ACTIONS)
 def test_tick(self):
     """
     Testing the tick method in controller
     :return:
     """
     p = Pump('127.0.0.1', 8080)
     s = Sensor('127.0.0.1', 8083)
     d = Decider(100, .10)
     c = Controller(s, p, d)
     s.measure = MagicMock(return_value=95)
     p.get_state = MagicMock(return_value=p.PUMP_IN)
     d.decide = MagicMock(return_value=p.PUMP_IN)
     p.set_state = MagicMock(return_value=True)
     c.tick()
     s.measure.assert_called_with()
     p.get_state.assert_called_with()
     d.decide.assert_called_with(95, p.PUMP_IN, actions)
Exemple #28
0
    def test_decider_controller_integration(self):
        """
        Method that tests and ensures that the decider and controller work
        well together
        """

        pump = Pump('0.0.0.1', 541)
        sensor = Sensor('0.0.0.1', 541)
        decider = Decider(85, .05)
        controller = Controller(sensor, pump, decider)
        sensor.measure = MagicMock(return_value=60)
        pump.get_state = MagicMock(return_value=1)
        pump.set_state = MagicMock(return_value=True)
        result = decider.decide(65, 0, controller.actions)

        controller.tick()

        self.assertEqual(result, 1)
 def test_tick(self):
     """
     Testing the tick method in controller
     :return:
     """
     pump_address = Pump('127.0.0.1', 8080)
     sensor_address = Sensor('127.0.0.1', 8083)
     decider_vals = Decider(100, .10)
     controller_all = Controller(sensor_address, pump_address, decider_vals)
     sensor_address.measure = MagicMock(return_value=95)
     pump_address.get_state = MagicMock(return_value=pump_address.PUMP_IN)
     decider_vals.decide = MagicMock(return_value=pump_address.PUMP_IN)
     pump_address.set_state = MagicMock(return_value=True)
     controller_all.tick()
     sensor_address.measure.assert_called_with()
     pump_address.get_state.assert_called_with()
     decider_vals.decide.assert_called_with(95, pump_address.PUMP_IN,
                                            ACTIONS)
    def test_tick(self):
        """
        Tests each tick
        """

        pump = Pump('127.0.0.1', '8000')
        decider = Decider(100, 2)
        sensor = Sensor('127.0.0.1', '8000')
        controller = Controller(sensor, pump, decider)
        sensor.measure = MagicMock(return_value=102)
        pump.set_state = MagicMock(return_value=pump.PUMP_OFF)
        pump.get_state = MagicMock(return_value=pump.PUMP_OFF)
        decider.decide = MagicMock(return_value=pump.PUMP_OFF)
        controller.tick()
        sensor.measure.assert_called_with()
        pump.get_state.assert_called_with()
        decider.decide.assert_called_with(102, pump.PUMP_OFF,
                                          controller.actions)