def test_event_listener(self, mock_gauge, mock_connection): config = { 'statsd': { 'host': 'host', } } hass = mock.MagicMock() statsd.setup(hass, config) self.assertTrue(hass.bus.listen.called) handler_method = hass.bus.listen.call_args_list[0][0][1] valid = {'1': 1, '1.0': 1.0, STATE_ON: 1, STATE_OFF: 0} for in_, out in valid.items(): state = mock.MagicMock(state=in_) handler_method(mock.MagicMock(data={'new_state': state})) mock_gauge.return_value.send.assert_called_once_with( state.entity_id, out) mock_gauge.return_value.send.reset_mock() for invalid in ('foo', '', object): handler_method(mock.MagicMock(data={ 'new_state': ha.State('domain.test', invalid, {})})) self.assertFalse(mock_gauge.return_value.send.called)
def test_event_listener_defaults(self, mock_client): """Test event listener.""" config = { 'statsd': { 'host': 'host', } } hass = mock.MagicMock() statsd.setup(hass, config) self.assertTrue(hass.bus.listen.called) handler_method = hass.bus.listen.call_args_list[0][0][1] valid = {'1': 1, '1.0': 1.0, STATE_ON: 1, STATE_OFF: 0} for in_, out in valid.items(): state = mock.MagicMock(state=in_, attributes={"attribute key": 3.2}) handler_method(mock.MagicMock(data={'new_state': state})) mock_client.return_value.gauge.assert_has_calls([ mock.call(state.entity_id, out, statsd.DEFAULT_RATE), ]) mock_client.return_value.gauge.reset_mock() mock_client.return_value.incr.assert_called_once_with( state.entity_id, rate=statsd.DEFAULT_RATE) mock_client.return_value.incr.reset_mock() for invalid in ('foo', '', object): handler_method(mock.MagicMock(data={ 'new_state': ha.State('domain.test', invalid, {})})) self.assertFalse(mock_client.return_value.gauge.called) self.assertFalse(mock_client.return_value.incr.called)
def test_event_listener(self, mock_gauge, mock_connection): """Test event listener.""" config = { 'statsd': { 'host': 'host', } } hass = mock.MagicMock() statsd.setup(hass, config) self.assertTrue(hass.bus.listen.called) handler_method = hass.bus.listen.call_args_list[0][0][1] valid = {'1': 1, '1.0': 1.0, STATE_ON: 1, STATE_OFF: 0} for in_, out in valid.items(): state = mock.MagicMock(state=in_) handler_method(mock.MagicMock(data={'new_state': state})) mock_gauge.return_value.send.assert_called_once_with( state.entity_id, out) mock_gauge.return_value.send.reset_mock() for invalid in ('foo', '', object): handler_method( mock.MagicMock( data={'new_state': ha.State('domain.test', invalid, {})})) self.assertFalse(mock_gauge.return_value.send.called)
def test_event_listener(self, mock_gauge, mock_connection): """Test event listener.""" config = {"statsd": {"host": "host"}} hass = mock.MagicMock() statsd.setup(hass, config) self.assertTrue(hass.bus.listen.called) handler_method = hass.bus.listen.call_args_list[0][0][1] valid = {"1": 1, "1.0": 1.0, STATE_ON: 1, STATE_OFF: 0} for in_, out in valid.items(): state = mock.MagicMock(state=in_) handler_method(mock.MagicMock(data={"new_state": state})) mock_gauge.return_value.send.assert_called_once_with(state.entity_id, out) mock_gauge.return_value.send.reset_mock() for invalid in ("foo", "", object): handler_method(mock.MagicMock(data={"new_state": ha.State("domain.test", invalid, {})})) self.assertFalse(mock_gauge.return_value.send.called)
def test_statsd_setup_full(self, mock_gauge, mock_connection): """Test setup with all data.""" config = {"statsd": {"host": "host", "port": 123, "sample_rate": 1, "prefix": "foo"}} hass = mock.MagicMock() self.assertTrue(statsd.setup(hass, config)) mock_connection.assert_called_once_with(host="host", port=123, sample_rate=1, disabled=False) mock_gauge.assert_called_once_with("foo", mock_connection.return_value) self.assertTrue(hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, hass.bus.listen.call_args_list[0][0][0])
def test_statsd_setup_defaults(self, mock_gauge, mock_connection): """Test setup with defaults.""" config = {"statsd": {"host": "host"}} hass = mock.MagicMock() self.assertTrue(statsd.setup(hass, config)) mock_connection.assert_called_once_with( host="host", port=statsd.DEFAULT_PORT, sample_rate=statsd.DEFAULT_RATE, disabled=False ) mock_gauge.assert_called_once_with(statsd.DEFAULT_PREFIX, mock_connection.return_value) self.assertTrue(hass.bus.listen.called)
def test_statsd_setup_defaults(self, mock_connection): """Test setup with defaults.""" config = { 'statsd': { 'host': 'host', } } hass = mock.MagicMock() self.assertTrue(statsd.setup(hass, config)) mock_connection.assert_called_once_with( host='host', port=statsd.DEFAULT_PORT, prefix=statsd.DEFAULT_PREFIX) self.assertTrue(hass.bus.listen.called)
def test_statsd_setup_defaults(self, mock_gauge, mock_connection): config = { 'statsd': { 'host': 'host', } } hass = mock.MagicMock() self.assertTrue(statsd.setup(hass, config)) mock_connection.assert_called_once_with( host='host', port=statsd.DEFAULT_PORT, sample_rate=statsd.DEFAULT_RATE, disabled=False) mock_gauge.assert_called_once_with(statsd.DEFAULT_PREFIX, mock_connection.return_value) self.assertTrue(hass.bus.listen.called)
def test_statsd_setup_full(self, mock_gauge, mock_connection): config = { 'statsd': { 'host': 'host', 'port': 123, 'sample_rate': 1, 'prefix': 'foo', } } hass = mock.MagicMock() self.assertTrue(statsd.setup(hass, config)) mock_connection.assert_called_once_with(host='host', port=123, sample_rate=1, disabled=False) mock_gauge.assert_called_once_with('foo', mock_connection.return_value) self.assertTrue(hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, hass.bus.listen.call_args_list[0][0][0])
def test_statsd_setup_full(self, mock_connection): """Test setup with all data.""" config = { 'statsd': { 'host': 'host', 'port': 123, 'sample_rate': 1, 'prefix': 'foo', } } hass = mock.MagicMock() self.assertTrue(statsd.setup(hass, config)) mock_connection.assert_called_once_with( host='host', port=123, prefix='foo') self.assertTrue(hass.bus.listen.called) self.assertEqual(EVENT_STATE_CHANGED, hass.bus.listen.call_args_list[0][0][0])