# -*- coding: utf8 -*- from __future__ import print_function from datasift import Client client = Client("your username", "your API key") csdl = 'interaction.content contains "python"' if client.is_valid(csdl): response = client.compile(csdl) stream = response['hash'] print('Stream %s created' % stream) print('It takes %s DPUs' % client.dpu(stream)['dpu']) print('Usage INFO \n %s' % client.usage()) print('Account balance is %s ' % client.balance()) else: print('Could not validate CSDL')
class TestMockedClient(TestCase): def setUp(self): TestCase.setUp(self) self.client = Client("testuser", "testapikey") def test_creation_of_client(self): self.assertTrue(self.client) def test_handling_of_authorization_failed(self): with HTTMock(authorization_failed): self.assertRaises(AuthException, self.client.balance) def test_output_of_balance(self): mock, expected = mock_output_of(self.client.balance) with HTTMock(mock): runs = 0 for expecteditem in expected: runs += 1 results = self.client.balance() assert_dict_structure(self, results, expecteditem) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_rate_limit_exception(self): with HTTMock(rate_limited): self.assertRaises(RateLimitException, self.client.usage) def test_ratelimit_headers(self): with HTTMock(rate_limit_headers): result = self.client.usage() ratelimits = result.ratelimits self.assertNotEqual(len(ratelimits), 0, "ensure that we got some rate limit headers") self.assertEqual(ratelimits["cost"], 25) def test_compile_raw_output(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): runs = 0 for item in expected: runs += 1 assert_dict_structure(self, item, self.client.compile("dummy csdl that is valid").raw) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_compile_python_friendly_output(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): runs = 0 for item in expected: runs += 1 result = self.client.compile("dummy csdl that is valid") assert_dict_structure(self, item, result.raw) assert_dict_structure(self, item, result) self.assertEqual(result["created_at"], datetime.strptime(result.raw["created_at"], "%Y-%m-%d %H:%M:%S")) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_compile_with_valid_output(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): runs = 0 for item in expected: runs += 1 assert_dict_structure(self, item, self.client.compile("dummy csdl that is valid")) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_compile_invalid_csdl(self): with HTTMock(failed_compilation_of_csdl): self.assertRaises(DataSiftApiException, self.client.compile, ("dummy csdl which is bad")) def test_is_valid_csdl_with_bad_data(self): with HTTMock(failed_compilation_of_csdl): self.assertFalse(self.client.is_valid("dummy csdl which is bad")) def test_is_valid_csdl_with_good_data(self): mock, expected = mock_output_of(self.client.validate) with HTTMock(mock): runs = 0 for item in expected: runs+=1 r = self.client.is_valid("dummy csdl which is valid") self.assertTrue(r) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_is_valid_csdl_cause_exception(self): with HTTMock(internal_server_error_with_json): self.assertRaises(DataSiftApiException, self.client.is_valid, ("csdl which turns into a teapot")) def test_error_handling_of_internal_server_errors(self): with HTTMock(internal_server_error): self.assertRaises(DataSiftApiFailure, self.client.balance) def test_error_handling_of_weird_errors(self): with HTTMock(weird_error): self.assertRaises(HTTPError, self.client.validate, ("csdl which turns into a teapot")) def test_client_usage(self): mock, expected = mock_output_of(self.client.usage) with HTTMock(mock): runs = 0 for expected_output in expected: runs += 1 results = self.client.usage() self.assertDictEqual(results.headers, {'content-type': 'application/json'}) assert_dict_structure(self, results, expected_output) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_client_usage_with_parameter(self): mock, expected = mock_output_of(self.client.usage) with HTTMock(mock): runs = 0 for expected_output in expected: runs += 1 results = self.client.usage(period="day") assert_dict_structure(self, results, expected_output) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_client_dpu(self): mock, expected = mock_output_of(self.client.dpu) with HTTMock(mock): runs = 0 for expected_output in expected: runs += 1 results = self.client.dpu("valid stream id") assert_dict_structure(self, results, expected_output) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") @unittest.skipIf(sys.version_info >= (3,0), "Mocking requests does not work correctly on py3") def test_client_pull(self): mock, expected = normal_pull_output() with HTTMock(mock): print("expected", expected) results = self.client.pull("dummy valid subscription id", size=2048, cursor=512) print("got", results) self.assertEquals(results.status_code, 200) self.assertEqual(len(results), len(expected), msg="get the same number of interactions out") self.assertEqual(len(results), len(results.raw), msg="number of messages mangled by conversion") self.assertDictEqual(results.headers, {}) self.assertTrue(results.status_code == 200) for output, expected in zip(results, expected): assert_dict_structure(self, output, expected) def test_client_pull_json_meta(self): with HTTMock(pull_json_meta): result = self.client.pull("dummy valid subscription id") self.assertIn("interactions", result, msg="ensure that there is an interactions section in the response") self.assertEqual(len(result["interactions"]), 2, msg="check that both interactions made it") @unittest.skipIf(sys.version_info >= (3,0), "Mocking requests does not work correctly on py3") def test_client_pull_json_array(self): with HTTMock(pull_json_array): result = self.client.pull("dummy valid subscription id") self.assertEqual(len(result), 2, msg="check that both interactions made it") def test_live_streaming_exceptions_warn_on_bad_starts(self): self.assertRaises(StreamSubscriberNotStarted, self.client.subscribe, ("hash")) self.client._stream_process_started = True func = self.client.subscribe("hash") self.assertRaises(DeleteRequired, func, ("hash")) def test_live_streaming_client_setup(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): @self.client.on_delete def on_delete(interaction): print( 'Deleted interaction %s ' % interaction) @self.client.on_open def on_open(): print( 'Streaming ready, can start subscribing') csdl = 'interaction.content contains "music"' stream = self.client.compile(csdl)['hash'] @self.client.subscribe(stream) def subscribe_to_hash(msg): print(msg) @self.client.on_closed def on_close(wasClean, code, reason): print('Streaming connection closed') @self.client.on_ds_message def on_ds_message(msg): print('DS Message %s' % msg) self.client._stream_process_started = True self.client.start_stream_subscriber()
class TestMockedClient(TestCase): def setUp(self): TestCase.setUp(self) self.client = Client("testuser", "testapikey") def test_creation_of_client(self): self.assertTrue(self.client) def test_handling_of_authorization_failed(self): with HTTMock(authorization_failed): self.assertRaises(AuthException, self.client.balance) def test_output_of_balance(self): mock, expected = mock_output_of(self.client.balance) with HTTMock(mock): runs = 0 for expecteditem in expected: runs += 1 results = self.client.balance() assert_dict_structure(self, results, expecteditem) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_rate_limit_exception(self): with HTTMock(rate_limited): self.assertRaises(RateLimitException, self.client.usage) def test_ratelimit_headers(self): with HTTMock(rate_limit_headers): result = self.client.usage() ratelimits = result.ratelimits self.assertNotEqual(len(ratelimits), 0, "ensure that we got some rate limit headers") self.assertEqual(ratelimits["cost"], 25) def test_compile_raw_output(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): runs = 0 for item in expected: runs += 1 assert_dict_structure(self, item, self.client.compile("dummy csdl that is valid").raw) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_compile_python_friendly_output(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): runs = 0 for item in expected: runs += 1 result = self.client.compile("dummy csdl that is valid") assert_dict_structure(self, item, result.raw) assert_dict_structure(self, item, result) self.assertEqual(result["created_at"], datetime.strptime(result.raw["created_at"], "%Y-%m-%d %H:%M:%S")) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_compile_with_valid_output(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): runs = 0 for item in expected: runs += 1 assert_dict_structure(self, item, self.client.compile("dummy csdl that is valid")) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_compile_invalid_csdl(self): with HTTMock(failed_compilation_of_csdl): self.assertRaises(DataSiftApiException, self.client.compile, ("dummy csdl which is bad")) def test_is_valid_csdl_with_bad_data(self): with HTTMock(failed_compilation_of_csdl): self.assertFalse(self.client.is_valid("dummy csdl which is bad")) def test_is_valid_csdl_with_good_data(self): mock, expected = mock_output_of(self.client.validate) with HTTMock(mock): runs = 0 for item in expected: runs+=1 r = self.client.is_valid("dummy csdl which is valid") self.assertTrue(r) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_is_valid_csdl_cause_exception(self): with HTTMock(internal_server_error_with_json): self.assertRaises(DataSiftApiException, self.client.is_valid, ("csdl which turns into a teapot")) def test_error_handling_of_internal_server_errors(self): with HTTMock(internal_server_error): self.assertRaises(DataSiftApiFailure, self.client.balance) def test_error_handling_of_weird_errors(self): with HTTMock(weird_error): self.assertRaises(HTTPError, self.client.validate, ("csdl which turns into a teapot")) def test_client_usage(self): mock, expected = mock_output_of(self.client.usage) with HTTMock(mock): runs = 0 for expected_output in expected: runs += 1 results = self.client.usage() self.assertDictEqual(results.headers, {'content-type': 'application/json'}) assert_dict_structure(self, results, expected_output) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_client_usage_with_parameter(self): mock, expected = mock_output_of(self.client.usage) with HTTMock(mock): runs = 0 for expected_output in expected: runs += 1 results = self.client.usage(period="day") assert_dict_structure(self, results, expected_output) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") def test_client_dpu(self): mock, expected = mock_output_of(self.client.dpu) with HTTMock(mock): runs = 0 for expected_output in expected: runs += 1 results = self.client.dpu("valid stream id") assert_dict_structure(self, results, expected_output) self.assertNotEqual(runs, 0, "ensure that at least one case was tested") @unittest.skipIf(sys.version_info >= (3,0), "Mocking requests does not work correctly on py3") def test_client_pull(self): mock, expected = normal_pull_output() with HTTMock(mock): print("expected", expected) results = self.client.pull("dummy valid subscription id", size=2048, cursor=512) print("got", results) self.assertEquals(results.status_code, 200) self.assertEqual(len(results), len(expected), msg="get the same number of interactions out") self.assertEqual(len(results), len(results.raw), msg="number of messages mangled by conversion") self.assertDictEqual(results.headers, {}) self.assertTrue(results.status_code == 200) for output, expected in zip(results, expected): assert_dict_structure(self, output, expected) def test_live_streaming_exceptions_warn_on_bad_starts(self): self.assertRaises(StreamSubscriberNotStarted, self.client.subscribe, ("hash")) self.client._stream_process_started = True func = self.client.subscribe("hash") self.assertRaises(DeleteRequired, func, ("hash")) def test_live_streaming_client_setup(self): mock, expected = mock_output_of(self.client.compile) with HTTMock(mock): @self.client.on_delete def on_delete(interaction): print( 'Deleted interaction %s ' % interaction) @self.client.on_open def on_open(): print( 'Streaming ready, can start subscribing') csdl = 'interaction.content contains "music"' stream = self.client.compile(csdl)['hash'] @self.client.subscribe(stream) def subscribe_to_hash(msg): print(msg) @self.client.on_closed def on_close(wasClean, code, reason): print('Streaming connection closed') @self.client.on_ds_message def on_ds_message(msg): print('DS Message %s' % msg) self.client._stream_process_started = True self.client.start_stream_subscriber()