示例#1
0
class ControllerTests(unittest.TestCase):
    """Unit tests for the Controller class"""
    def setUp(self):
        """A controller with basic configuration."""
        self.ip_addr = '127.0.0.1'
        self.port = '8000'
        urllib.request.urlopen = MagicMock(return_value=5)
        self.sensor = Sensor(self.ip_addr, self.port)
        self.pump = Pump(self.ip_addr, self.port)
        self.pump.set_state(MagicMock(return_value=True))
        self.decider = Decider(100, 0.05)
        self.controller = Controller(self.sensor, self.pump, self.decider)

    def test_controller_tick(self):
        """Verify the Controller ticks as expected"""
        self.sensor.measure = MagicMock(return_value=110)
        self.pump.get_state = MagicMock(return_value=self.pump.PUMP_IN)
        self.decider.decide = MagicMock(return_value=self.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(110, self.pump.PUMP_IN)
        self.pump.set_state.assert_called_with(self.pump.PUMP_OFF)
示例#2
0
 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()
    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)
示例#4
0
    def test_decide(self):
        """
        Method to test decide function
        """
        pump = Pump('127.0.0.1', 8000)
        sensor = Sensor('127.0.0.1', 8000)
        pump.set_state = MagicMock(return_value=True)
        decider = Decider(10., 0.05)
        controller = Controller(sensor, pump, decider)

        self.assertEqual(decider.decide(1.0, int(0), controller.actions),
                         controller.actions['PUMP_IN'])
        self.assertEqual(decider.decide(20.0, int(0), controller.actions),
                         controller.actions['PUMP_OUT'])
        self.assertEqual(decider.decide(10.0, int(0), controller.actions),
                         controller.actions['PUMP_OFF'])
        self.assertEqual(decider.decide(20.0, int(1), controller.actions),
                         controller.actions['PUMP_OFF'])
        self.assertEqual(decider.decide(1.0, int(1), controller.actions),
                         controller.actions['PUMP_IN'])
        self.assertEqual(decider.decide(1.0, int(-1), controller.actions),
                         controller.actions['PUMP_OFF'])
        self.assertEqual(decider.decide(20.0, int(-1), controller.actions),
                         controller.actions['PUMP_OUT'])
        with self.assertRaises(ValueError):
            decider.decide(10., 2.5, controller.actions)
示例#5
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)
class ModuleTests(unittest.TestCase):
    """
    Module tests for the water-regulation module
    using a MOCKED sensor ad pump
    """
    def setUp(self):
        self.pump = Pump('127.0.0.1', 8000)
        self.sensor = Sensor('127.0.0.1', 7000)
        self.decider = Decider(100, 0.5)
        self.controller = Controller(self.sensor, self.pump, self.decider)
        self.controller.pump.set_state = MagicMock(return_value=True)
        self.actions = {
            'PUMP_IN': 1,
            'PUMP_OFF': 0,
            'PUMP_OUT': -1,
        }

    def test_integration(self):
        """
        Integration test for water regulation module
        """
        value_pump_get_state = self.actions['PUMP_OFF']
        self.pump.get_state = MagicMock(return_value=value_pump_get_state)

        sensor_measurement = 99.4
        self.sensor.measure = MagicMock(return_value=sensor_measurement)

        new_state = self.decider.decide(sensor_measurement,
                                        value_pump_get_state, self.actions)

        self.controller.tick()

        self.assertEqual(self.pump.set_state(new_state), True)
示例#7
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)
    def test_dummy(self):
        """
        Just some example syntax that you might use
        """

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

        self.fail("Remove this test.")
    def setUp(self):

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

        self.actions = {
            'PUMP_IN': pump.PUMP_IN,
            'PUMP_OUT': pump.PUMP_OUT,
            'PUMP_OFF': pump.PUMP_OFF,
        }
示例#10
0
    def test_tick(self):
        """Tests the tick"""
        # Create a controller, and all the things it needs
        pump = Pump('127.0.0.1', 8000)
        sensor = Sensor('127.0.0.1', 8000)
        decider = Decider(5, 5)
        controller = Controller(sensor, pump, decider)

        # Now mock a few items
        pump.get_state = MagicMock(return_value=Pump.PUMP_OFF)
        sensor.measure = MagicMock(return_value=5)
        decider.decide = MagicMock(return_value=True)

        # Testing if true/normal
        pump.set_state = MagicMock(return_value=True)
        self.assertEqual(controller.tick(), True)

        # Testing if false/abnormal
        pump.set_state = MagicMock(return_value=False)
        self.assertEqual(controller.tick(), False)
示例#11
0
 def test_decider1(self):
     """Does it return 1 to pump in when below margins? """
     pump = Pump('127.0.0.1', 8000)
     pump.set_state = MagicMock(return_value=True)
     self.decider = Decider(100, 10)
     self.actions = {
     'PUMP_IN': pump.PUMP_IN,
     'PUMP_OUT': pump.PUMP_OUT,
     'PUMP_OFF': pump.PUMP_OFF,
 }
     assert self.decider.decide(50, "PUMP_IN", self.actions) == 1
示例#12
0
 def test_decider2(self):
     """ Does it return 0 to shut off when within margins?"""
     pump = Pump('127.0.0.1', 8000)
     pump.set_state = MagicMock(return_value=True)
     self.decider = Decider(100, 10)
     self.actions = {
     'PUMP_IN': pump.PUMP_IN,
     'PUMP_OUT': pump.PUMP_OUT,
     'PUMP_OFF': pump.PUMP_OFF,
 }
     assert self.decider.decide(101, "PUMP_IN", self.actions) == 0
示例#13
0
 def test_decider3(self):
     """Does it return -1 to pump out when above margin?"""
     pump = Pump('127.0.0.1', 8000)
     pump.set_state = MagicMock(return_value=True)
     self.decider = Decider(100, 10)
     self.actions = {
     'PUMP_IN': pump.PUMP_IN,
     'PUMP_OUT': pump.PUMP_OUT,
     'PUMP_OFF': pump.PUMP_OFF,
 }
     assert self.decider.decide(250, "PUMP_IN", self.actions) == -1
示例#14
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())
示例#15
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']))
示例#16
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())
示例#17
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)
示例#18
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())
示例#19
0
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'))
示例#20
0
    def setUp(self):
        """
            Sets the necessary varialbles to test the Decider class
        """

        pump = Pump('127.0.0.1', 8000)
        pump.set_state = MagicMock(return_value=True)
        self.new_decider = Decider(100, .10)

        self.actions = {
            'PUMP_IN': pump.PUMP_IN,
            'PUMP_OUT': pump.PUMP_OUT,
            'PUMP_OFF': pump.PUMP_OFF,
        }
示例#21
0
 def test_in_not_enough(self):
     """
     scenario: PUMP_IN, below target water level
     :return: PUMP_IN
     """
     decider = Decider(100, 0.05)
     pump = Pump('127.0.0.1', 8000)
     pump.set_state = MagicMock(return_value=True)
     actions = {
         'PUMP_IN': pump.PUMP_IN,
         'PUMP_OUT': pump.PUMP_OUT,
         'PUMP_OFF': pump.PUMP_OFF,
     }
     result = decider.decide(96, "PUMP_IN", actions)
     self.assertEqual(actions["PUMP_IN"], result)
示例#22
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())
示例#23
0
class PumpTests(unittest.TestCase):
    """
    This class performs a unit test on Pump
    """
    def setUp(self):
        """
        This method does a setup for unit testing Pump
        """
        self.pump = Pump('127.0.0.1', 1000)

    def test_set_state(self):
        """
        This method tests the set_state for Pump
        """
        self.pump.set_state = MagicMock(return_value=True)
        self.pump.set_state('PUMP_OUT')
        self.pump.set_state.assert_called_with('PUMP_OUT')

    def test_get_state(self):
        """
        This method tests the get_state for Pump
        """
        self.pump.get_state = MagicMock(return_value=1)
        self.assertEqual(self.pump.get_state(), 1)
示例#24
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)
示例#25
0
 def test_out_too_much(self):
     """
     scenario: PUMP_OUT, above target water level
     :return: PUMP_OUT
     """
     decider = Decider(100, 0.05)
     pump = Pump('127.0.0.1', 8000)
     pump.set_state = MagicMock(return_value=True)
     actions = {
         'PUMP_IN': pump.PUMP_IN,
         'PUMP_OUT': pump.PUMP_OUT,
         'PUMP_OFF': pump.PUMP_OFF,
     }
     result = decider.decide(101, "PUMP_OUT", actions)
     self.assertEqual(actions["PUMP_OUT"], result)
示例#26
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())
示例#27
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)
示例#28
0
    def test_pump_set_state_false(self):
        """Test Pumpt Set State False."""
        def bad_urlopen(request):
            """Dummy function to raise an HTTPError."""
            del request  # Unused
            raise urllib.error.HTTPError(code=3,
                                         msg='A message',
                                         hdrs='some headers',
                                         fp=None,
                                         url='www.google.com')

        urllib.request.urlopen = bad_urlopen
        pump = Pump(ControllerTests.DUMMY_ADDR, ControllerTests.DUMMY_PORT)
        return_value = pump.set_state(12)
        self.assertEqual(return_value, False)
示例#29
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())
示例#30
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)