Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
 def test_tick(self):
     """
     Testing Sensor.tick
     """
     sensor = Sensor('127.0.0.1', '8000')
     sensor.measure = MagicMock(return_value=10)
     pump = Pump('127.0.0.1', '8000')
     pump.get_state = MagicMock(return_value='PUMP_OFF')
     pump.set_state = MagicMock(return_value='PUMP_OFF')
     decider = Decider(10, 0.1)
     decider.decide = MagicMock(return_value='PUMP_OFF')
     controller = Controller(sensor, pump, decider)
     self.assertTrue(controller.tick())
Ejemplo n.º 4
0
class SensorTests(unittest.TestCase):
    """
    Unit tests for the Sensor class
    """
    def setUp(self):
        """
        setup for PumpTest class
        :return:
        """
        self.sensor = Sensor('127.1.1.3', 9000)

    def test_set_state(self):
        """
        test setting the state of pump
        :return:
        """
        self.sensor.measure = MagicMock(return_value=True)
        self.assertEqual(self.sensor.measure(), True)
Ejemplo n.º 5
0
class SensorTests(unittest.TestCase):
    """
        This method does a setup for unit testing Sensor
    """
    def setUp(self):
        """
        This method does a setup for unit testing Sensor
        """
        self.sensor = Sensor('127.0.0.2', 2000)

    def test_set_state(self):
        """
        This method tests the set_state for Sensor
        """
        self.sensor.measure = MagicMock(return_value=True)
        self.assertEqual(self.sensor.measure(), True)

    def test_get_state(self):
        """
        This method tests the get_state for Sensor
        """
        self.sensor.get_state = MagicMock(return_value=1)
        self.assertEqual(self.sensor.get_state(), 1)