def test_setup(self, m, mock_get_forecast): """Test for successfully setting up the forecast.io platform.""" uri = ('https://api.forecast.io\/forecast\/(\w+)\/' '(-?\d+\.?\d*),(-?\d+\.?\d*)') m.get(re.compile(uri), text=load_fixture('forecast.json')) forecast.setup_platform(self.hass, self.config, MagicMock()) self.assertTrue(mock_get_forecast.called) self.assertEqual(mock_get_forecast.call_count, 1)
def test_setup_bad_api_key(self, mock_get_forecast): """Test for handling a bad API key.""" # The forecast API wrapper that we use raises an HTTP error # when you try to use a bad (or no) API key. url = 'https://api.forecast.io/forecast/{}/{},{}?units=auto'.format( self.key, str(self.lat), str(self.lon)) msg = '400 Client Error: Bad Request for url: {}'.format(url) mock_get_forecast.side_effect = HTTPError(msg, ) response = forecast.setup_platform(self.hass, self.config, MagicMock()) self.assertFalse(response)
def test_setup_bad_api_key(self, mock_get_forecast): """Test for handling a bad API key.""" # The forecast API wrapper that we use raises an HTTP error # when you try to use a bad (or no) API key. url = 'https://api.forecast.io/forecast/{}/{},{}?units=auto'.format( self.key, str(self.lat), str(self.lon) ) msg = '400 Client Error: Bad Request for url: {}'.format(url) mock_get_forecast.side_effect = HTTPError(msg,) response = forecast.setup_platform(self.hass, self.config, MagicMock()) self.assertFalse(response)
def test_setup(self, mock_get_forecast): """Test for successfully setting up the forecast.io platform.""" def load_fixture_from_json(): cwd = os.path.dirname(__file__) fixture_path = os.path.join(cwd, '..', 'fixtures', 'forecast.json') with open(fixture_path) as file: content = json.load(file) return json.dumps(content) # Mock out any calls to the actual API and # return the fixture json instead uri = 'api.forecast.io\/forecast\/(\w+)\/(-?\d+\.?\d*),(-?\d+\.?\d*)' httpretty.register_uri( httpretty.GET, re.compile(uri), body=load_fixture_from_json(), ) # The following will raise an error if the regex for the mock was # incorrect and we actually try to go out to the internet. httpretty.HTTPretty.allow_net_connect = False forecast.setup_platform(self.hass, self.config, MagicMock()) self.assertTrue(mock_get_forecast.called) self.assertEqual(mock_get_forecast.call_count, 1)
def test_setup(self, mock_get_forecast): """Test for successfully setting up the forecast.io platform.""" def load_fixture_from_json(): cwd = os.path.dirname(__file__) fixture_path = os.path.join(cwd, '..', 'fixtures', 'forecast.json') with open(fixture_path) as file: content = json.load(file) return json.dumps(content) # Mock out any calls to the actual API and # return the fixture json instead uri = 'api.forecast.io\/forecast\/(\w+)\/(-?\d+\.?\d*),(-?\d+\.?\d*)' httpretty.register_uri( httpretty.GET, re.compile(uri), body=load_fixture_from_json(), ) # The following will raise an error if the regex for the mock was # incorrect and we actually try to go out to the internet. httpretty.HTTPretty.allow_net_connect = False forecast.setup_platform(self.hass, self.config, MagicMock()) self.assertTrue(mock_get_forecast.called) self.assertEqual(mock_get_forecast.call_count, 1)
def test_setup_no_latitude(self): """Test that the component is not loaded without required config.""" self.hass.config.latitude = None self.assertFalse(forecast.setup_platform(self.hass, {}, MagicMock()))
def test_setup_no_latitude(self): """Test that the component is not loaded without required config.""" self.hass.config.latitude = None self.assertFalse(forecast.setup_platform(self.hass, {}, MagicMock()))