From 3c9d8468a9c0532f65d7e1a625eb6439f9b14d3e Mon Sep 17 00:00:00 2001 From: JeetShetty Date: Wed, 21 Sep 2016 22:19:36 -0400 Subject: [PATCH] Update Poller and Store tests. --- tests/test_db_store.py | 2 - tests/test_poller.py | 167 ++++++++++++++++++++--------------------- 2 files changed, 80 insertions(+), 89 deletions(-) diff --git a/tests/test_db_store.py b/tests/test_db_store.py index 2b25adc..83e9a97 100755 --- a/tests/test_db_store.py +++ b/tests/test_db_store.py @@ -25,7 +25,6 @@ def test_get_latest_soil_moisture(self): store = db_store.SoilMoistureStore(mock_cursor) mock_cursor.fetchall.return_value = [(300,)] moisture = store.get_latest_soil_moisture() - self.assertTrue(mock_cursor.execute.called) self.assertEqual(300, moisture) def test_get_latest_soil_moisture_empty_database(self): @@ -33,7 +32,6 @@ def test_get_latest_soil_moisture_empty_database(self): store = db_store.SoilMoistureStore(mock_cursor) mock_cursor.fetchall.return_value = [] moisture = store.get_latest_soil_moisture() - self.assertTrue(mock_cursor.execute.called) self.assertIsNone(moisture) def test_store_ambient_light(self): diff --git a/tests/test_poller.py b/tests/test_poller.py index 3f9c562..0a077bc 100755 --- a/tests/test_poller.py +++ b/tests/test_poller.py @@ -8,120 +8,116 @@ from greenpithumb import poller +TEST_TIMEOUT_SECONDS = 3.0 + class PollerClassesTest(unittest.TestCase): + def setUp(self): + self.clock_wait_event = threading.Event() + self.mock_local_clock = mock.Mock() + self.mock_sensor = mock.Mock() + self.mock_store = mock.Mock() + def test_temperature_poller(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 - mock_temperature_sensor = mock.Mock() - mock_temperature_store = mock.Mock() temperature_poller = poller.TemperaturePoller( - mock_local_clock, poll_interval, mock_temperature_sensor, - mock_temperature_store) + self.mock_local_clock, poll_interval, self.mock_sensor, + self.mock_store) now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000) - mock_local_clock.now.return_value = now - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() - mock_temperature_sensor.get_temperature.return_value = 21.0 + self.mock_local_clock.now.return_value = now + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) + temperature = 21.0 + self.mock_sensor.get_temperature.return_value = temperature temperature_poller.start_polling_async() - clock_wait_event.wait(3.0) - mock_temperature_store.store_temperature.assert_called_with(now, 21.0) - mock_local_clock.wait.assert_called_with(poll_interval) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) + self.mock_store.store_temperature.assert_called_with(now, temperature) + self.mock_local_clock.wait.assert_called_with(poll_interval) def test_humidity_poller(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 - mock_humidity_sensor = mock.Mock() - mock_humidity_store = mock.Mock() - humidity_poller = poller.HumidityPoller(mock_local_clock, poll_interval, - mock_humidity_sensor, - mock_humidity_store) + humidity_poller = poller.HumidityPoller(self.mock_local_clock, + poll_interval, self.mock_sensor, + self.mock_store) now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000) - mock_local_clock.now.return_value = now - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() - mock_humidity_sensor.get_humidity_level.return_value = 50.0 + self.mock_local_clock.now.return_value = now + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) + humidity = 50.0 + self.mock_sensor.get_humidity_level.return_value = humidity humidity_poller.start_polling_async() - clock_wait_event.wait(3.0) - mock_humidity_store.store_humidity.assert_called_with(now, 50.0) - mock_local_clock.wait.assert_called_with(poll_interval) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) + self.mock_store.store_humidity.assert_called_with(now, humidity) + self.mock_local_clock.wait.assert_called_with(poll_interval) def test_moisture_poller(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 - mock_moisture_sensor = mock.Mock() - mock_soil_moisture_store = mock.Mock() - moisture_poller = poller.MoisturePoller(mock_local_clock, poll_interval, - mock_moisture_sensor, - mock_soil_moisture_store) + moisture_poller = poller.MoisturePoller(self.mock_local_clock, + poll_interval, self.mock_sensor, + self.mock_store) now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000) - mock_local_clock.now.return_value = now - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() - mock_moisture_sensor.moisture.return_value = 300 + self.mock_local_clock.now.return_value = now + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) + moisture = 300 + self.mock_sensor.moisture.return_value = moisture moisture_poller.start_polling_async() - clock_wait_event.wait(3.0) - mock_soil_moisture_store.store_soil_moisture.assert_called_with(now, - 300) - mock_local_clock.wait.assert_called_with(poll_interval) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) + self.mock_store.store_soil_moisture.assert_called_with(now, moisture) + self.mock_local_clock.wait.assert_called_with(poll_interval) def test_ambient_light_poller(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 - mock_light_sensor = mock.Mock() - mock_ambient_light_store = mock.Mock() ambient_light_poller = poller.AmbientLightPoller( - mock_local_clock, poll_interval, mock_light_sensor, - mock_ambient_light_store) + self.mock_local_clock, poll_interval, self.mock_sensor, + self.mock_store) now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000) - mock_local_clock.now.return_value = now - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() - mock_light_sensor.get_light_level.return_value = 50.0 + self.mock_local_clock.now.return_value = now + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) + ambient_light = 50.0 + self.mock_sensor.get_light_level.return_value = ambient_light ambient_light_poller.start_polling_async() - clock_wait_event.wait(3.0) - mock_ambient_light_store.store_ambient_light.assert_called_with(now, - 50.0) - mock_local_clock.wait.assert_called_with(poll_interval) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) + self.mock_store.store_ambient_light.assert_called_with(now, + ambient_light) + self.mock_local_clock.wait.assert_called_with(poll_interval) def test_reservoir_poller(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 - mock_reservoir = mock.Mock() - mock_reservoir_level_store = mock.Mock() - reservoir_poller = poller.ReservoirPoller(mock_local_clock, - poll_interval, mock_reservoir, - mock_reservoir_level_store) + reservoir_poller = poller.ReservoirPoller( + self.mock_local_clock, poll_interval, self.mock_sensor, + self.mock_store) now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000) - mock_local_clock.now.return_value = now - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() - mock_reservoir.reservoir_level.return_value = 500.0 + self.mock_local_clock.now.return_value = now + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) + reservoir_level = 500.0 + self.mock_sensor.reservoir_level.return_value = reservoir_level reservoir_poller.start_polling_async() - clock_wait_event.wait(3.0) - mock_reservoir_level_store.store_reservoir_level.assert_called_with( - now, 500.0) - mock_local_clock.wait.assert_called_with(poll_interval) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) + self.mock_store.store_reservoir_level.assert_called_with( + now, reservoir_level) + self.mock_local_clock.wait.assert_called_with(poll_interval) def test_watering_event_poller_when_pump_run(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 mock_pump_manager = mock.Mock() mock_watering_event_store = mock.Mock() mock_soil_moisture_store = mock.Mock() watering_event_poller = poller.WateringEventPoller( - mock_local_clock, poll_interval, mock_pump_manager, + self.mock_local_clock, poll_interval, mock_pump_manager, mock_watering_event_store, mock_soil_moisture_store) now = datetime.datetime(2016, 7, 23, 10, 51, 9, 928000) - mock_local_clock.now.return_value = now - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() + self.mock_local_clock.now.return_value = now + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) pump_amount = 200 mock_pump_manager.pump_if_needed.return_value = pump_amount moisture = 100 @@ -129,51 +125,48 @@ def test_watering_event_poller_when_pump_run(self): moisture) watering_event_poller.start_polling_async() - clock_wait_event.wait(3.0) - mock_watering_event_store.store_water_pumped.assert_called_with(now, - 200) - mock_local_clock.wait.assert_called_with(poll_interval) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) + mock_watering_event_store.store_water_pumped.assert_called_with( + now, pump_amount) + self.mock_local_clock.wait.assert_called_with(poll_interval) mock_pump_manager.pump_if_needed.assert_called_with(moisture) def test_watering_event_poller_when_pump_not_run(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 mock_pump_manager = mock.Mock() mock_watering_event_store = mock.Mock() mock_soil_moisture_store = mock.Mock() watering_event_poller = poller.WateringEventPoller( - mock_local_clock, poll_interval, mock_pump_manager, + self.mock_local_clock, poll_interval, mock_pump_manager, mock_watering_event_store, mock_soil_moisture_store) - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() + self.mock_local_clock.wait.side_effect = lambda _: self.clock_wait_event.set() mock_pump_manager.pump_if_needed.return_value = 0 moisture = 500 mock_soil_moisture_store.get_latest_soil_moisture.return_value = ( moisture) watering_event_poller.start_polling_async() - clock_wait_event.wait(3.0) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) self.assertFalse(mock_watering_event_store.store_water_pumped.called) - mock_local_clock.wait.assert_called_with(poll_interval) + self.mock_local_clock.wait.assert_called_with(poll_interval) mock_pump_manager.pump_if_needed.assert_called_with(moisture) def test_watering_event_poller_when_moisture_is_None(self): - clock_wait_event = threading.Event() - mock_local_clock = mock.Mock() poll_interval = 1 mock_pump_manager = mock.Mock() mock_watering_event_store = mock.Mock() mock_soil_moisture_store = mock.Mock() watering_event_poller = poller.WateringEventPoller( - mock_local_clock, poll_interval, mock_pump_manager, + self.mock_local_clock, poll_interval, mock_pump_manager, mock_watering_event_store, mock_soil_moisture_store) - mock_local_clock.wait.side_effect = lambda _: clock_wait_event.set() + self.mock_local_clock.wait.side_effect = ( + lambda _: self.clock_wait_event.set()) moisture = None mock_soil_moisture_store.get_latest_soil_moisture.return_value = ( moisture) watering_event_poller.start_polling_async() - clock_wait_event.wait(3.0) + self.clock_wait_event.wait(TEST_TIMEOUT_SECONDS) self.assertFalse(mock_watering_event_store.store_water_pumped.called) self.assertFalse(mock_pump_manager.pump_if_needed.called) - mock_local_clock.wait.assert_called_with(poll_interval) + self.mock_local_clock.wait.assert_called_with(poll_interval)