def test_post_block(self, mock_requests_post, mock_timestamp): with fake_creds('.jovian', 'credentials.json'): post_block('metrics', {'key': 'value'}) mock_requests_post.assert_called_with( 'https://api-staging.jovian.ai/data/record', data=None, headers={ "Authorization": "Bearer fake_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging" }, json=[{ 'localTimestamp': 1582550133094, 'data': 'metrics', 'recordType': { 'key': 'value' } }])
def test_post_block_raises_api_error( mock_requests_post, mock_get_api_key, mock_timestamp ): with fake_creds(): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai", "API_KEY": "fake_invalid_api_key", "ORG_ID": "staging", } write_creds(creds) with pytest.raises(ApiError) as context: post_block("metrics", {"key": "value"}) mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/data/record", data=None, headers={ "Authorization": "Bearer fake_invalid_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json=[ { "localTimestamp": 1582550133094, "data": "metrics", "recordType": {"key": "value"}, } ], ) assert ( str(context.value) == "Data logging failed: (HTTP 500) Internal Server Error" )
def test_post_block(mock_requests_post, mock_timestamp): with fake_creds(): post_block("metrics", {"key": "value"}) mock_requests_post.assert_called_with( "https://api-staging.jovian.ai/data/record", data=None, headers={ "Authorization": "Bearer fake_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging", }, json=[ { "localTimestamp": 1582550133094, "data": "metrics", "recordType": {"key": "value"}, } ], )
def log_record(record_type, data=None, verbose=True, **data_args): """Create records with the given data & type""" global _data_blocks # Create the combined data dictionary data = _parse_data(data, data_args) if data is None and verbose: log('Nothing to record. Skipping..', error=True) return # Send to API endpoint res = api.post_block(data, record_type) tracking_slug = res['tracking']['trackingSlug'] # Save to data block _data_blocks.append((tracking_slug, record_type, data)) if verbose: log(record_type.capitalize() + ' logged.')
def test_post_block_raises_api_error(self, mock_requests_post, mock_get_api_key, mock_timestamp): with fake_creds('.jovian-invalid-key', 'credentials.json'): # setUp creds = { "WEBAPP_URL": "https://staging.jovian.ml/", "GUEST_KEY": "b6538d4dfde04fcf993463a828a9cec6", "API_URL": "https://api-staging.jovian.ai", "API_KEY": "fake_invalid_api_key", "ORG_ID": "staging" } write_creds(creds) with self.assertRaises(ApiError) as context: post_block('metrics', {'key': 'value'}) mock_requests_post.assert_called_with( 'https://api-staging.jovian.ai/data/record', data=None, headers={ "Authorization": "Bearer fake_invalid_api_key", "x-jovian-source": "library", "x-jovian-library-version": __version__, "x-jovian-guest": "b6538d4dfde04fcf993463a828a9cec6", "x-jovian-org": "staging" }, json=[{ 'localTimestamp': 1582550133094, 'data': 'metrics', 'recordType': { 'key': 'value' } }]) assert context.exception.args[ 0] == 'Data logging failed: (HTTP 500) Internal Server Error'