def test_run_with_zone_events(self): """Test the zone events.""" empty_me = [1, 2] def fake_get_events(): """Return nothing twice, then some events.""" if empty_me: empty_me.pop() else: return fake_events client = mock.MagicMock() fake_events = [ { 'zone': 1, 'zone_state': True, 'type': 'zone_status' }, { 'zone': 2, 'foo': False }, ] client.get_events.side_effect = fake_get_events watcher = nx584.NX584Watcher(client, {}) @mock.patch.object(watcher, '_process_zone_event') def run(fake_process): fake_process.side_effect = StopMe self.assertRaises(StopMe, watcher._run) self.assertEqual(fake_process.call_count, 1) self.assertEqual(fake_process.call_args, mock.call(fake_events[0])) run() self.assertEqual(3, client.get_events.call_count)
def test_process_zone_event(self, mock_update): zone1 = {'number': 1, 'name': 'foo', 'state': True} zone2 = {'number': 2, 'name': 'bar', 'state': True} zones = { 1: nx584.NX584ZoneSensor(zone1, 'motion'), 2: nx584.NX584ZoneSensor(zone2, 'motion'), } watcher = nx584.NX584Watcher(None, zones) watcher._process_zone_event({'zone': 1, 'zone_state': False}) self.assertFalse(zone1['state']) self.assertEqual(1, mock_update.call_count)
def test_process_zone_event(self, mock_update): """Test the processing of zone events.""" zone1 = {'number': 1, 'name': 'foo', 'state': True} zone2 = {'number': 2, 'name': 'bar', 'state': True} zones = { 1: nx584.NX584ZoneSensor(zone1, 'motion'), 2: nx584.NX584ZoneSensor(zone2, 'motion'), } watcher = nx584.NX584Watcher(None, zones) watcher._process_zone_event({'zone': 1, 'zone_state': False}) assert not zone1['state'] assert 1 == mock_update.call_count
def test_run_retries_failures(self, mock_sleep): empty_me = [1, 2] def fake_run(): if empty_me: empty_me.pop() raise requests.exceptions.ConnectionError() else: raise StopMe() watcher = nx584.NX584Watcher(None, {}) with mock.patch.object(watcher, '_run') as mock_inner: mock_inner.side_effect = fake_run self.assertRaises(StopMe, watcher.run) self.assertEqual(3, mock_inner.call_count) mock_sleep.assert_has_calls([mock.call(10), mock.call(10)])
def test_run_retries_failures(self, mock_sleep): """Test the retries with failures.""" empty_me = [1, 2] def fake_run(): """Fake runner.""" if empty_me: empty_me.pop() raise requests.exceptions.ConnectionError() else: raise StopMe() watcher = nx584.NX584Watcher(None, {}) with mock.patch.object(watcher, '_run') as mock_inner: mock_inner.side_effect = fake_run with pytest.raises(StopMe): watcher.run() assert 3 == mock_inner.call_count mock_sleep.assert_has_calls([mock.call(10), mock.call(10)])
def test_process_zone_event_missing_zone(self, mock_update): """Test the processing of zone events with missing zones.""" watcher = nx584.NX584Watcher(None, {}) watcher._process_zone_event({'zone': 1, 'zone_state': False}) self.assertFalse(mock_update.called)
def test_process_zone_event_missing_zone(self, mock_update): watcher = nx584.NX584Watcher(None, {}) watcher._process_zone_event({'zone': 1, 'zone_state': False}) self.assertFalse(mock_update.called)