def test_forecasts(self): forecast_product_json = load_fixture('forecast_product') self.adapter.register_uri('GET', '/products/%s' % forecast_product_json['id'], json=forecast_product_json) forecast_product = Product.find(forecast_product_json['id']) forecasts_json = load_fixture('forecasts') self.adapter.register_uri('GET', '/products/%s/forecasts' % forecast_product.id, json=forecasts_json) forecasts = forecast_product.forecasts() self.assertEqual(len(forecasts), 3)
def setUp(self): self.channel_list_one = load_fixture("channel_list_one.json") self.channel_list_two = load_fixture("channel_list_two.json") self.channel_list_three = load_fixture("channel_list_three.json") self.channel_list_four = load_fixture("channel_list_four.json") from charlesbot.slack.slack_connection import SlackConnection self.slack = SlackConnection() self.slack.initialized = True self.slack.sc = MagicMock()
def test_styles(self): product_json = load_fixture('product') self.adapter.register_uri('GET', '/products/%s' % (product_json['id']), json=product_json) product = Product.find(product_json['id']) styles_json = load_fixture('styles') self.adapter.register_uri('GET', '/products/%s/styles' % (product.id,), json=styles_json) styles = self.product.styles() self.assertEquals(len(styles), 2)
def test_analysis_frames(self): product_json = load_fixture('product') self.adapter.register_uri('GET', '/products/%s' % (product_json['id']), json=product_json) product = Product.find(product_json['id']) frames_json = load_fixture('frames') self.adapter.register_uri('GET', '/products/%s/frames' % product.id, json=frames_json) frames = product.frames() self.assertEquals(len(frames), 2)
def setUp(self): self.channel_list_one = load_fixture('channel_list_one.json') self.channel_list_two = load_fixture('channel_list_two.json') self.channel_list_three = load_fixture('channel_list_three.json') self.channel_list_four = load_fixture('channel_list_four.json') self.channel_list_five = load_fixture('channel_list_five.json') self.channel_list_six = load_fixture('channel_list_six.json') self.channel_list_seven = load_fixture('channel_list_seven.json') self.channel_list_eight = load_fixture('channel_list_eight.json') self.channel_list_nine = load_fixture('channel_list_nine.json') self.channel_list_ten = load_fixture('channel_list_ten.json')
def test_forecast_frames(self): forecast_product_json = load_fixture('forecast_product') self.adapter.register_uri('GET', '/products/%s' % forecast_product_json['id'], json=forecast_product_json) forecast_product = Product.find(forecast_product_json['id']) forecasts_json = load_fixture('forecasts') self.adapter.register_uri('GET', '/products/%s/forecasts' % forecast_product.id, json=forecasts_json) frames_json = load_fixture('frames') self.adapter.register_uri('GET', '/forecasts/4a61c817-3fc0-4dec-80ab-25936d73b2d7/frames', json=frames_json) frames = forecast_product.frames() self.assertEqual(len(frames), 2)
def test_sink(self, client, responses): signal = client.signals.get(id="zVNLr8tHvWQw") signal.sink(load_fixture("data_points"), chunk_size=2) self.assertEqual(3, len(responses.calls)) for i in range(1, 3): self.assertEqual(responses.calls[i].request.headers['Content-Type'], "application/x-ldjson") self.assertEqual(len(responses.calls[i].request.body.split("\n")), 2)
def test_google_maps_tiles(self): frame = self._register_frames().pop() tile_tiff = load_fixture('tile', extension='tiff') self.adapter.register_uri('GET', '/frames/%s/tile/1/0/0' % frame.id, content=tile_tiff) tile = frame.tile(x=0, y=0, z=1) self.assertIsNotNone(tile.content())
def test_bing_maps_tiles(self): frame = self._register_frames().pop() tile_tiff = load_fixture('tile', extension='tiff') self.adapter.register_uri('GET', '/frames/%s/tile/0' % frame.id, content=tile_tiff) tile = frame.tile(quadkey="0") self.assertIsNotNone(tile.content())
def test_datapoints(self): frame = self._register_frames().pop() datapoint_json = load_fixture('datapoint') self.adapter.register_uri('GET', '/frames/%s/datapoint/35.0/-97.0' % frame.id, json=datapoint_json) datapoint = frame.datapoint(35.0, -97.0) self.assertEqual(datapoint.value, 0.296531558)
def test_update_ok(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_1.xml") ) feed = GenericFeed(HOME_COORDINATES_1, None) assert ( repr(feed) == "<GenericFeed(home=(-31.0, 151.0), url=None, " "radius=None, categories=None)>" ) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 5 feed_entry = entries[0] assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.category == "Category 1" assert feed_entry.published == datetime.datetime(2018, 9, 23, 8, 30) assert feed_entry.updated == datetime.datetime(2018, 9, 23, 8, 35) assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1) feed_entry = entries[1] assert feed_entry.title == "Title 2" assert feed_entry.external_id == "2345" assert feed_entry.attribution == "Attribution 1" assert repr(feed_entry) == "<GenericFeedEntry(id=2345)>" feed_entry = entries[2] assert feed_entry.title == "Title 3" assert feed_entry.external_id == "Title 3" external_id = hash((-37.8901, 149.7890)) feed_entry = entries[3] self.assertIsNone(feed_entry.title) assert feed_entry.external_id == external_id feed_entry = entries[4] assert feed_entry.title == "Title 5" assert feed_entry.external_id == "5678"
def test_update_ok_feed_2(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_2.xml") ) feed = GenericFeed(HOME_COORDINATES_1, None) status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 1 feed_entry = entries[0] assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.category == "Category 1" assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1)
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("qld_bushfire_alert_feed.xml") ) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = QldBushfireAlertFeedManager( _generate_entity, _update_entity, _remove_entity, HOME_COORDINATES ) assert ( repr(feed_manager) == "<QldBushfireAlertFeedManager(" "feed=<QldBushfireAlertFeed(home=" "(-31.0, 151.0), url=https://www." "qfes.qld.gov.au/data/alerts/" "bushfireAlert.xml, " "radius=None, categories=None)>)>" ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 2 assert feed_manager.last_timestamp == datetime.datetime(2018, 9, 27, 8, 0) assert len(generated_entity_external_ids) == 2 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("natural_resources_canada_earthquakes_en_feed.xml")) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = NaturalResourcesCanadaEarthquakesFeedManager( _generate_entity, _update_entity, _remove_entity, HOME_COORDINATES, "en") assert (repr(feed_manager) == "<NaturalResourcesCanadaEarthquakes" "FeedManager(feed=<NaturalResources" "CanadaEarthquakesFeed(home=" "(49.25, -123.1), " "url=http://www.earthquakescanada." "nrcan.gc.ca/index-en.php?" "tpl_region=canada&tpl_output=rss, " "radius=None, magnitude=None)>)>") feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 2 assert feed_manager.last_timestamp == datetime.datetime( 2018, 9, 29, 8, 30, tzinfo=datetime.timezone.utc) assert len(generated_entity_external_ids) == 2 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0
def test_nested_result(self): query = "from planets as allPlanets" response_mock = mock.Mock() response_mock.status_code = 200 response_mock.json.return_value = load_fixture('nestedPlanets.json') self.requests_post_mock.return_value = response_mock client = Client('http://localhost:9000') response = client.run_query(query) self.assertEqual(self.requests_post_mock.call_count, 1) self.assertEqual(self.requests_post_mock.call_args_list, [ mock.call('http://localhost:9000/run-query', data='from planets as allPlanets', headers={'Content-type': 'text/plain'}, params={}) ]) self.assertTrue(isinstance(response.allPlanets, list)) self.assertEqual(len(response.allPlanets[0].results), 2)
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("ingv_centro_nazionale_terremoti_feed.xml")) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = IngvCentroNazionaleTerremotiFeedManager( _generate_entity, _update_entity, _remove_entity, HOME_COORDINATES) assert (repr(feed_manager) == "<IngvCentroNazionaleTerremoti" "FeedManager(feed=<IngvCentroNazionale" "TerremotiFeed(home=" "(40.84, 14.25), " "url=http://cnt.rm.ingv.it/" "feed/atom/all_week, " "radius=None, magnitude=None)>)>") feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 3 assert feed_manager.last_timestamp == datetime.datetime( 2018, 10, 6, 8, 0, tzinfo=datetime.timezone.utc) assert len(generated_entity_external_ids) == 3 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("tasmania_fire_service_incidents_feed.xml")) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = TfsIncidentsFeedManager(_generate_entity, _update_entity, _remove_entity, HOME_COORDINATES) assert (repr(feed_manager) == "<TfsIncidentsFeedManager(" "feed=<TfsIncidentsFeed(home=" "(-42.0, 147.0), url=http://www.fire." "tas.gov.au/Show?pageId=" "colBushfireSummariesRss, " "radius=None, categories=None)>)>") feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 2 assert feed_manager.last_timestamp == datetime.datetime( 2018, 9, 29, 15, 30, tzinfo=datetime.timezone.utc) assert len(generated_entity_external_ids) == 2 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("ign_sismologia_feed.xml")) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = IgnSismologiaFeedManager(_generate_entity, _update_entity, _remove_entity, HOME_COORDINATES) assert (repr(feed_manager) == "<IgnSismologiaFeedManager(" "feed=<IgnSismologiaFeed(home=" "(40.38, -3.72), " "url=http://www.ign.es/ign/" "RssTools/sismologia.xml, " "radius=None, magnitude=None)>)>") feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 3 assert feed_manager.last_timestamp == datetime.datetime( 2019, 3, 18, 19, 34, 55) assert len(generated_entity_external_ids) == 3 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0
def test_remove_org_webhook_blows_up_if_webhook_does_not_exist(self): responses.add(responses.GET, 'https://api.github.com/orgs/github', json=json.loads(load_fixture('organization.json')), status=200) # hook does not exist responses.add(responses.GET, 'https://api.github.com/orgs/github/hooks?per_page=100', json=[], status=200) mock_env = { 'LINTREVIEW_SETTINGS': 'settings.sample.py', 'LINTREVIEW_SERVER_NAME': 'example.com' } with temp_env(mock_env): with self.assertRaises(SystemExit): self._do_parse( ['org-unregister', '--user', 'cool-token', 'github'])
def test_nearest(self): # Test Defaults json = load_fixture('nearest_stations') self.adapter.register_uri('GET', '/stations/35.5/-97.3', json=json) stations = Station.nearest(35.5, -97.3) self.assertEqual(len(stations), 3) # Test Limit self.adapter.register_uri('GET', '/stations/35.5/-97.3?limit=3', json=json) stations = Station.nearest(35.5, -97.3, limit=3) self.assertEqual(len(stations), 3) # Test Radius self.adapter.register_uri('GET', '/stations/35.5/-97.3?radius=25.5', json=json) stations = Station.nearest(35.5, -97.3, radius=25.5) self.assertEqual(len(stations), 3)
def test_update_ok_en_with_magnitude(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("natural_resources_canada_earthquakes_en_feed.xml")) feed = NaturalResourcesCanadaEarthquakesFeed( HOME_COORDINATES, "en", filter_minimum_magnitude=4.0) assert (repr(feed) == "<NaturalResourcesCanadaEarthquakesFeed(" "home=(49.25, -123.1), url=http://www." "earthquakescanada.nrcan.gc.ca/index-en.php?" "tpl_region=canada&tpl_output=rss, " "radius=None, magnitude=4.0)>") status, entries = feed.update() assert status == UPDATE_OK self.assertIsNotNone(entries) assert len(entries) == 1 feed_entry = entries[0] assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234"
def test_feed_manager_no_timestamp(self, mock_session, mock_request): """Test updating feed is ok.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_5.xml") ) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = GenericFeedManager( _generate_entity, _update_entity, _remove_entity, HOME_COORDINATES_1, None ) assert ( repr(feed_manager) == "<GenericFeedManager(" "feed=<GenericFeed(home=" "(-31.0, 151.0), url=None, " "radius=None, categories=None)>)>" ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 1 self.assertIsNone(feed_manager.last_timestamp)
class OAuth2Test(unittest.TestCase): @with_mock_client(request_returns=load_fixture('access_token')) def test_get_token_params(self, client): token = client.oauth2.get_token(client_id='client_id', client_secret='client_secret', scope=['account', 'events']) client.request.assert_called_once_with('post', '/oauth2/token/', auth=('client_id', 'client_secret'), data={ 'scope': 'account events', 'grant_type': 'client_credentials' }) assert isinstance(token, AccessToken) assert token.to_primitive() == client.request.return_value with pytest.raises(ValidationError): client.oauth2.get_token(client_id=None) with pytest.raises(ValidationError): client.oauth2.get_token(invalid_arg=None) @with_mock_client() def test_revoke_token_params(self, client): result = client.oauth2.revoke_token(client_id='client_id', client_secret='client_secret', token='token123') client.request.assert_called_once_with('post', '/oauth2/revoke/', auth=('client_id', 'client_secret'), data={ 'token': 'token123', 'token_type_hint': 'access_token' }) assert result is None with pytest.raises(ValidationError): client.oauth2.revoke_token(token=None) with pytest.raises(ValidationError): client.oauth2.revoke_token(invalid_arg=None) @with_client() @with_mock_responses() def test_get_token(self, client, responses): token = client.oauth2.get_token(client_id='client_id', client_secret='client_secret', scope=['account', 'events']) assert isinstance(token, AccessToken) assert token.access_token == "token123" assert token.token_type == "Bearer" assert token.scope == ['account', 'events'] assert len(responses.calls) == 1 assert responses.calls[0].request.headers[ 'Content-Type'] == 'application/x-www-form-urlencoded' assert responses.calls[0].request.headers[ 'Authorization'] == 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=' assert dict(parse_qsl(responses.calls[0].request.body)) == { 'scope': 'account events', 'grant_type': 'client_credentials' } @with_client() @with_mock_responses() def test_revoke_token(self, client, responses): result = client.oauth2.revoke_token(client_id='client_id', client_secret='client_secret', token='token123') assert result is None assert len(responses.calls) == 1 assert responses.calls[0].request.headers[ 'Content-Type'] == 'application/x-www-form-urlencoded' assert responses.calls[0].request.headers[ 'Authorization'] == 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=' assert dict(parse_qsl(responses.calls[0].request.body)) == { 'token_type_hint': 'access_token', 'token': 'token123' }
def test_feed_manager(self, mock_session, mock_request): """Test the feed manager.""" mock_session.return_value.__enter__.return_value.send.return_value.ok = True mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_1.xml") ) # This will just record calls and keep track of external ids. generated_entity_external_ids = [] updated_entity_external_ids = [] removed_entity_external_ids = [] def _generate_entity(external_id): """Generate new entity.""" generated_entity_external_ids.append(external_id) def _update_entity(external_id): """Update entity.""" updated_entity_external_ids.append(external_id) def _remove_entity(external_id): """Remove entity.""" removed_entity_external_ids.append(external_id) feed_manager = GenericFeedManager( _generate_entity, _update_entity, _remove_entity, HOME_COORDINATES_1, None ) assert ( repr(feed_manager) == "<GenericFeedManager(" "feed=<GenericFeed(home=" "(-31.0, 151.0), url=None, " "radius=None, categories=None)>)>" ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 5 self.assertIsNotNone(feed_manager.last_update) assert feed_manager.last_timestamp == datetime.datetime(2018, 9, 23, 9, 10) assert len(generated_entity_external_ids) == 5 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 0 feed_entry = entries.get("1234") assert feed_entry.title == "Title 1" assert feed_entry.external_id == "1234" assert feed_entry.coordinates == (-37.2345, 149.1234) self.assertAlmostEqual(feed_entry.distance_to_home, 714.4, 1) assert repr(feed_entry) == "<GenericFeedEntry(id=1234)>" feed_entry = entries.get("2345") assert feed_entry.title == "Title 2" assert feed_entry.external_id == "2345" feed_entry = entries.get("Title 3") assert feed_entry.title == "Title 3" assert feed_entry.external_id == "Title 3" external_id = hash((-37.8901, 149.7890)) feed_entry = entries.get(external_id) self.assertIsNone(feed_entry.title) assert feed_entry.external_id == external_id feed_entry = entries.get("5678") assert feed_entry.title == "Title 5" assert feed_entry.external_id == "5678" # Simulate an update with several changes. generated_entity_external_ids.clear() updated_entity_external_ids.clear() removed_entity_external_ids.clear() mock_session.return_value.__enter__.return_value.send.return_value.text = ( load_fixture("generic_feed_4.xml") ) feed_manager.update() entries = feed_manager.feed_entries self.assertIsNotNone(entries) assert len(entries) == 3 assert len(generated_entity_external_ids) == 1 assert len(updated_entity_external_ids) == 2 assert len(removed_entity_external_ids) == 3 feed_entry = entries.get("1234") assert feed_entry.title == "Title 1 UPDATED" feed_entry = entries.get("2345") assert feed_entry.title == "Title 2" feed_entry = entries.get("6789") assert feed_entry.title == "Title 6" # Simulate an update with no data. generated_entity_external_ids.clear() updated_entity_external_ids.clear() removed_entity_external_ids.clear() mock_session.return_value.__enter__.return_value.send.return_value.ok = False feed_manager.update() entries = feed_manager.feed_entries assert len(entries) == 0 assert len(generated_entity_external_ids) == 0 assert len(updated_entity_external_ids) == 0 assert len(removed_entity_external_ids) == 3
def test_location(self): json = load_fixture('daily_precipitation_location') self.adapter.register_uri('GET', '/daily-precipitation/35/-97', json=json) okc_precip = DailyPrecipitation.location(35, -97) self.assertEqual(len(okc_precip.series), 3)
def test_contour(self): json = load_fixture('daily_precipitation_contour') self.adapter.register_uri('GET', '/daily-precipitation/fe68c33d-e718-4449-acae-351072ce7749/contours', json=json) DailyPrecipitation.contours('fe68c33d-e718-4449-acae-351072ce7749')
def test_asset(self): json = load_fixture('daily_precipitation_asset') self.adapter.register_uri('GET', '/daily-precipitation/fe68c33d-e718-4449-acae-351072ce7749', json=json) asset_precip = DailyPrecipitation.asset('fe68c33d-e718-4449-acae-351072ce7749') self.assertEqual(asset_precip.accumulationStatistics['mean'], 21.5)
def test_location(self): json = load_fixture('hourly_wind_speed_location') self.adapter.register_uri('GET', '/hourly-wind-speed/35/-97', json=json) okc_ws = HourlyWindSpeed.location(35, -97) self.assertEqual(len(okc_ws.series), 4)
def test_location(self): json = load_fixture('hourly_solar_radiation_location') self.adapter.register_uri('GET', '/hourly-solar-radiation/35/-97', json=json) okc_solar = HourlySolarRadiation.location(35, -97) self.assertEqual(len(okc_solar.series), 3)
def mock_github_get_hooks(self): httpretty.register_uri( httpretty.GET, 'http://api.github.com/orgs/org_name/hooks', status=200, body=load_fixture('responses/github_get_hooks_success.json'))
def test_asset(self): json = load_fixture('hourly_wind_speed_asset') self.adapter.register_uri('GET', '/hourly-wind-speed/fe68c33d-e718-4449-acae-351072ce7749', json=json) asset_ws = HourlyWindSpeed.asset('fe68c33d-e718-4449-acae-351072ce7749') self.assertEqual(asset_ws.timeSeriesStatistics['mean'], 15.0)
def test_location(self): json = load_fixture('hourly_et_short_crop_location') self.adapter.register_uri( 'GET', '/hourly-evapotranspiration-short-crop/35/-97', json=json) okc_et = HourlyEtShortCrop.location(35, -97) self.assertEqual(len(okc_et.series), 4)
class ClientTest(unittest.TestCase): def setUp(self): self.client = predicthq.Client() @with_config(ENDPOINT_URL="https://api.predicthq.com") def test_build_url(self): self.assertEqual(self.client.build_url('v1'), "https://api.predicthq.com/v1/") def test_endpoints_initialization(self): self.assertIsInstance(self.client.oauth2, endpoints.OAuth2Endpoint) self.assertIsInstance(self.client.accounts, endpoints.AccountsEndpoint) self.assertIsInstance(self.client.events, endpoints.EventsEndpoint) self.assertIsInstance(self.client.signals, endpoints.SignalsEndpoint) self.assertIsInstance(self.client.places, endpoints.PlacesEndpoint) @with_mock_responses() def test_request(self, responses): self.client.logger.setLevel('DEBUG') self.assertDictEqual(self.client.get("/v1/"), responses.calls[0].response.json()) self.assertIsNone(self.client.get("/no-content/")) self.assertIsNone(self.client.get("/invalid-json/")) with self.assertRaises(ClientError) as ctx: self.client.get("/not-found/") self.assertEqual(ctx.exception.message, responses.calls[3].response.json()) with self.assertRaises(ServerError) as ctx: self.client.get("/server-error/") self.assertEqual(ctx.exception.message, responses.calls[4].response.json()) with self.assertRaises(ServerError) as ctx: self.client.get("/no-json/") self.assertEqual(ctx.exception.message, responses.calls[5].response.content) # Test headers self.client.authenticate(client_id='client_id', client_secret='client_secret', scope=['account']) self.assertEqual(responses.calls[6].request.headers['Authorization'], 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=') self.assertEqual(responses.calls[6].request.headers['Content-Type'], 'application/x-www-form-urlencoded') self.client.accounts.self() self.assertEqual(responses.calls[7].request.headers['Authorization'], 'Bearer token123') self.assertEqual(responses.calls[7].request.headers['Accept'], 'application/json') @with_mock_client(request_returns={"result": "value"}) def test_get(self, client): result = self.client.get('/get/', params={'q': 'query'}) client.request.assert_called_once_with('get', '/get/', params={'q': 'query'}) self.assertDictEqual(result, client.request.return_value) @with_mock_client(request_returns={"result": "value"}) def test_post(self, client): result = self.client.post('/post/', data={'key': 'value'}) client.request.assert_called_once_with('post', '/post/', data={'key': 'value'}) self.assertDictEqual(result, client.request.return_value) @with_mock_client(request_returns={"result": "value"}) def test_put(self, client): result = self.client.put('/put/', data={'key': 'value'}) client.request.assert_called_once_with('put', '/put/', data={'key': 'value'}) self.assertDictEqual(result, client.request.return_value) @with_mock_client(request_returns={"result": "value"}) def test_patch(self, client): result = self.client.patch('/patch/', data={'key': 'value'}) client.request.assert_called_once_with('patch', '/patch/', data={'key': 'value'}) self.assertDictEqual(result, client.request.return_value) @with_mock_client() def test_delete(self, client): result = client.delete('/delete/') client.request.assert_called_once_with('delete', '/delete/') self.assertEqual(result, client.request.return_value) @with_mock_client(request_returns=load_fixture('access_token')) def test_authenticate(self, client): token = self.client.authenticate( client_id='client_id', client_secret='client_secret', scope=['account', 'events', 'signals']) client.request.assert_called_once_with('post', '/oauth2/token/', auth=('client_id', 'client_secret'), data={ 'scope': 'account events signals', 'grant_type': 'client_credentials' }) self.assertIsInstance(token, AccessToken) self.assertDictEqual(token.to_primitive(), client.request.return_value) self.assertEqual(self.client.access_token, 'token123') def test_construct_with_access_token(self): client = predicthq.Client(access_token='token123') self.assertEqual(client.access_token, 'token123') @with_config(OAUTH2_ACCESS_TOKEN='token123') def test_construct_with_access_token_config(self): client = predicthq.Client() self.assertEqual(client.access_token, 'token123')
def mock_slack_response(self, fixture='responses/slack_post_message_success.json'): mock_slack = Mock() mock_slack.return_value = json.loads(load_fixture(fixture)) return mock_slack
def test_generate_metrics(self): stats = load_fixture('stats') m = generate_metrics(stats) self.assertEqual(m['SignalQueueCount'], 0) self.assertEqual(m['LoadCount'], 0) self.assertEqual(m['LocksUser0Count'], 0) self.assertEqual(m['LocksFilemonCount'], 0) self.assertEqual(m['LocksTimerCount'], 0) self.assertEqual(m['LocksRbTimerCount'], 0) self.assertEqual(m['LocksCronCount'], 0) self.assertEqual(m['LocksRpcCount'], 0) self.assertEqual(m['LocksSnmpCount'], 0) self.assertEqual(m['SocketsCount'], 1) self.assertEqual(len(m['SocketsQueueCount']), 1) self.assertEqual(m['SocketsQueueCount'][0], 0) self.assertEqual(len(m['SocketsMaxQueueCount']), 1) self.assertEqual(m['SocketsMaxQueueCount'][0], 0) self.assertEqual(len(m['SocketsSharedCount']), 1) self.assertEqual(m['SocketsSharedCount'][0], 0) self.assertEqual(len(m['SocketsCanOffloadCount']), 1) self.assertEqual(m['SocketsCanOffloadCount'][0], 0) self.assertEqual(m['WorkersCount'], 2) self.assertEqual(m['WorkersIdleCount'], 0) self.assertEqual(m['WorkersBusyCount'], 2) self.assertEqual(m['WorkersAcceptingCount'], 2) self.assertEqual(len(m['WorkersRequestsCount']), 2) self.assertEqual(m['WorkersRequestsCount'][0], 305) self.assertEqual(m['WorkersRequestsCount'][1], 307) self.assertEqual(len(m['WorkersDeltaRequestsCount']), 2) self.assertEqual(m['WorkersDeltaRequestsCount'][0], 305) self.assertEqual(m['WorkersDeltaRequestsCount'][1], 307) self.assertEqual(len(m['WorkersAverageResponseTimeMilliseconds']), 2) self.assertEqual(m['WorkersAverageResponseTimeMilliseconds'][0], 3017) self.assertEqual(m['WorkersAverageResponseTimeMilliseconds'][1], 3017) self.assertEqual(len(m['WorkersExceptionsCount']), 2) self.assertEqual(m['WorkersExceptionsCount'][0], 0) self.assertEqual(m['WorkersExceptionsCount'][1], 0) self.assertEqual(len(m['WorkersHarakiriCount']), 2) self.assertEqual(m['WorkersHarakiriCount'][0], 0) self.assertEqual(m['WorkersHarakiriCount'][1], 0) self.assertEqual(len(m['WorkersSignalsCount']), 2) self.assertEqual(m['WorkersSignalsCount'][0], 0) self.assertEqual(m['WorkersSignalsCount'][1], 0) self.assertEqual(len(m['WorkersRssBytes']), 2) self.assertEqual(m['WorkersRssBytes'][0], 0) self.assertEqual(m['WorkersRssBytes'][1], 0) self.assertEqual(len(m['WorkersVszBytes']), 2) self.assertEqual(m['WorkersVszBytes'][0], 0) self.assertEqual(m['WorkersVszBytes'][1], 0) self.assertEqual(len(m['WorkersRunningTimeMilliseconds']), 2) self.assertEqual(m['WorkersRunningTimeMilliseconds'][0], 917565) self.assertEqual(m['WorkersRunningTimeMilliseconds'][1], 923616) self.assertEqual(len(m['WorkerCoresCount']), 2) self.assertEqual(m['WorkerCoresCount'][0], 5) self.assertEqual(m['WorkerCoresCount'][1], 5) self.assertEqual(len(m['WorkerCoresStaticRequestsCount']), 10) self.assertEqual(m['WorkerCoresStaticRequestsCount'][0], 0) self.assertEqual(m['WorkerCoresStaticRequestsCount'][1], 0) self.assertEqual(len(m['WorkerCoresRoutedRequestsCount']), 10) self.assertEqual(m['WorkerCoresRoutedRequestsCount'][0], 0) self.assertEqual(m['WorkerCoresRoutedRequestsCount'][1], 0) self.assertEqual(len(m['WorkerCoresWriteErrorsCount']), 10) self.assertEqual(m['WorkerCoresWriteErrorsCount'][0], 0) self.assertEqual(m['WorkerCoresWriteErrorsCount'][1], 0) self.assertEqual(len(m['WorkerCoresReadErrorsCount']), 10) self.assertEqual(m['WorkerCoresReadErrorsCount'][0], 0) self.assertEqual(m['WorkerCoresReadErrorsCount'][1], 0)
def test_asset(self): json = load_fixture('daily_low_temperature_asset') self.adapter.register_uri('GET', '/daily-low-temperature/fe68c33d-e718-4449-acae-351072ce7749', json=json) asset_hi_temp = DailyLowTemperature.asset('fe68c33d-e718-4449-acae-351072ce7749') self.assertEqual(asset_hi_temp.timeSeriesStatistics['mean'], 34.3)
def test_contour(self): json = load_fixture('hourly_solar_radiation_contour') self.adapter.register_uri('GET', '/hourly-solar-radiation/fe68c33d-e718-4449-acae-351072ce7749/contours', json=json) HourlySolarRadiation.contours('fe68c33d-e718-4449-acae-351072ce7749')
def test_contour(self): json = load_fixture('daily_low_temperature_contour') self.adapter.register_uri('GET', '/daily-low-temperature/fe68c33d-e718-4449-acae-351072ce7749/contours', json=json) DailyLowTemperature.contours('fe68c33d-e718-4449-acae-351072ce7749')
def test_location(self): json = load_fixture('daily_low_temperature_location') self.adapter.register_uri('GET', '/daily-low-temperature/35/-97', json=json) okc_low_temp = DailyLowTemperature.location(35, -97) self.assertEqual(len(okc_low_temp.series), 3)
def test_contour(self): json = load_fixture('hourly_wind_speed_contour') self.adapter.register_uri('GET', '/hourly-wind-speed/fe68c33d-e718-4449-acae-351072ce7749/contours', json=json) HourlyWindSpeed.contours('fe68c33d-e718-4449-acae-351072ce7749')