class FlowSensorSaveThresholdsTest(unittest.TestCase): def setUp(self): self.flow = FlowSensor() def test_1_volume_duration_valid(self): recorded_data = { 'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] + 100, 'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] + 100 } satisfied = self.flow.are_thresholds_satisfied(recorded_data) self.assertTrue(satisfied) def test_2_invalid_volume(self): recorded_data = { 'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] - 100, 'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] + 100 } satisfied = self.flow.are_thresholds_satisfied(recorded_data) self.assertTrue(satisfied) def test_3_invalid_duration(self): recorded_data = { 'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] + 100, 'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] - 100 } satisfied = self.flow.are_thresholds_satisfied(recorded_data) self.assertTrue(satisfied) def test_4_volume_duration_invalid(self): recorded_data = { 'volume': config['MIN_FLOW_VOLUME_FOR_SAVE'] - 100, 'duration': config['MAX_FLOW_DURATION_FOR_SAVE'] - 100 } satisfied = self.flow.are_thresholds_satisfied(recorded_data) self.assertFalse(satisfied)
class FlowSensorRecordPulseTest(unittest.TestCase): def setUp(self): self.flow = FlowSensor() def test_1_record_one(self): self.assertEqual(self.flow.pulses, 0) self.flow.record_pulse() self.assertEqual(self.flow.pulses, 1) def test_2_record_many_sequential(self): self.assertEqual(self.flow.pulses, 0) num_records = 100 for _ in range(num_records): self.flow.record_pulse() self.assertEqual(self.flow.pulses, num_records) def test_2_record_many_parallel(self): self.assertEqual(self.flow.pulses, 0) num_records = 100 threads = [ threading.Thread(target=self.flow.record_pulse) for _ in range(num_records) ] for t in threads: t.start() for t in threads: t.join() self.assertEqual(self.flow.pulses, num_records)
class FlowSensorResetDataTest(unittest.TestCase): def setUp(self): self.flow = FlowSensor() def test_1_reset_pulse_to_zero(self): self.flow.pulses = 100 self.flow.last_read_time = None recorded_data = {'recorded_pulses': 100, 'new_time': time.time()} self.flow.reset_flow_data(recorded_data) self.assertEqual(self.flow.pulses, 0) self.assertIsNotNone(self.flow.last_read_time) def test_2_reset_pulse_to_non_zero(self): self.flow.pulses = 200 self.flow.last_read_time = None recorded_data = {'recorded_pulses': 100, 'new_time': time.time()} self.flow.reset_flow_data(recorded_data) self.assertEqual(self.flow.pulses, 100) self.assertIsNotNone(self.flow.last_read_time)
class FlowSensorSaveFlowProcessTest(unittest.TestCase): def setUp(self): self.flow = FlowSensor() self.config_backup = config.copy() def tearDown(self): config.update(self.config_backup) def test_1_process_success(self): self.flow.pulses = 10000 last_read_time = self.flow.last_read_time success = self.flow.save_flow_process() self.assertTrue(success) self.assertEqual(self.flow.pulses, 0) self.assertIsNot(self.flow.last_read_time, last_read_time) def test_2_process_fail_invalid_key(self): TestUtils.configure_invalid_product_key() pulses = self.flow.pulses = 10000 last_read_time = self.flow.last_read_time success = self.flow.save_flow_process() self.assertFalse(success) self.assertEqual(self.flow.pulses, pulses) self.assertIs(self.flow.last_read_time, last_read_time) def test_3_process_fail_server_down(self): TestUtils.configure_server_down() pulses = self.flow.pulses = 10000 last_read_time = self.flow.last_read_time success = self.flow.save_flow_process() self.assertFalse(success) self.assertEqual(self.flow.pulses, pulses) self.assertIs(self.flow.last_read_time, last_read_time) def test_4_process_fail_threshold_not_satisfied(self): last_read_time = self.flow.last_read_time success = self.flow.save_flow_process() self.assertFalse(success) self.assertEqual(self.flow.pulses, 0) self.assertIs(self.flow.last_read_time, last_read_time)
class FlowSensorReadFlowDataTest(unittest.TestCase): def setUp(self): self.flow = FlowSensor() def test_1_read(self): pulses = self.flow.pulses = 100 recorded_data = self.flow.read_flow_data() expected_volume = float(pulses) / config['PULSES_PER_LITRE'] expected_duration = recorded_data['new_time'] - self.flow.last_read_time self.assertEqual( recorded_data, { 'new_time': recorded_data['new_time'], 'recorded_pulses': pulses, 'volume': expected_volume, 'duration': expected_duration })
def setUp(self): self.flow = FlowSensor() self.config_backup = config.copy()
def test_3_server_down(self): TestUtils.configure_server_down() volume = 10 duration = 20 success = FlowSensor.send_flow_data(volume, duration) self.assertFalse(success)
def test_2_invalid_request(self): TestUtils.configure_invalid_product_key() volume = 10 duration = 20 success = FlowSensor.send_flow_data(volume, duration) self.assertFalse(success)
def test_2_invalid_flow_info_None(self): volume = -1 duration = None success = FlowSensor.send_flow_data(volume, duration) self.assertFalse(success)
def test_1_valid_request(self): volume = 10 duration = 20 success = FlowSensor.send_flow_data(volume, duration) self.assertTrue(success)
def setUp(self): self.flow = FlowSensor()
from PinsControl import PinsController from ValveControl import Valve # App entry point active = True # Configure logging SprinklerLogging.configure_logging() logger = logging.getLogger(__name__) # Start components pins_controller = PinsController() valve = Valve() valve.start() flow_sensor = FlowSensor() flow_sensor.start() # Cleanup def cleanup(sig_num, stack_frame): logger.info('Interrupted by sig_num %d. Cleaning up before exit', sig_num) valve.stop() flow_sensor.stop() pins_controller.clean_up() global active active = False signals = [signal.SIGINT, signal.SIGTERM] for s in signals: