From bf6bca66f60de4d2bb9849d6f057900773ef6688 Mon Sep 17 00:00:00 2001 From: Flier Lu Date: Tue, 8 Apr 2014 18:05:49 +0800 Subject: [PATCH 1/2] support to delete points with test cases --- influxdb/client.py | 25 +++++++++++++++++-------- tests/influxdb/client_test.py | 18 +++++++++++++++--- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 23158c06..2922df8a 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -92,16 +92,25 @@ def write_points_with_precision(self, data, time_precision='s'): # One Time Deletes - def delete_points(self, name, - regex=None, start_epoch=None, end_epoch=None): + def delete_points(self, name): """ - TODO: Delete a range of data - - 2013-11-08: This endpoint has not been implemented yet in ver0.0.8, - but it is documented in http://influxdb.org/docs/api/http.html. - See also: src/api/http/api.go:l57 + Delete an entire series """ - raise NotImplementedError() + url_format = "{0}/db/{1}/series/{2}?u={3}&p={4}" + + response = session.delete(url_format.format( + self._baseurl, + self._database, + name, + self._username, + self._password), + headers=self._headers) + + if response.status_code == 200: + return True + else: + raise Exception( + "{0}: {1}".format(response.status_code, response.content)) # Regularly Scheduled Deletes diff --git a/tests/influxdb/client_test.py b/tests/influxdb/client_test.py index a44a7b23..2ccbc105 100644 --- a/tests/influxdb/client_test.py +++ b/tests/influxdb/client_test.py @@ -77,10 +77,22 @@ def test_write_points_with_precision_fails(self): cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') cli.write_points_with_precision([]) - @raises(NotImplementedError) def test_delete_points(self): - cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') - cli.delete_points([]) + with patch.object(session, 'delete') as mocked_post: + mocked_post.return_value = _build_response_object(status_code=200) + cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') + assert cli.delete_points("foo") is True + + assert len(mocked_post.call_args_list) == 1 + args, kwds = mocked_post.call_args_list[0] + assert args[0].endswith('/db/db/series/foo?u=username&p=password') + + @raises(Exception) + def test_delete_points_with_wrong_name(self): + with patch.object(session, 'delete') as mocked_post: + mocked_post.return_value = _build_response_object(status_code=500) + cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') + cli.delete_points("nonexist") @raises(NotImplementedError) def test_create_scheduled_delete(self): From d571a04fb0e35cff9225ccafd843cf0c164412cf Mon Sep 17 00:00:00 2001 From: Flier Lu Date: Tue, 8 Apr 2014 18:27:17 +0800 Subject: [PATCH 2/2] check 204 (HTTP No Content) when delete points --- influxdb/client.py | 2 +- tests/influxdb/client_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 2922df8a..c3687934 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -106,7 +106,7 @@ def delete_points(self, name): self._password), headers=self._headers) - if response.status_code == 200: + if response.status_code == 204: return True else: raise Exception( diff --git a/tests/influxdb/client_test.py b/tests/influxdb/client_test.py index 2ccbc105..b7add781 100644 --- a/tests/influxdb/client_test.py +++ b/tests/influxdb/client_test.py @@ -79,7 +79,7 @@ def test_write_points_with_precision_fails(self): def test_delete_points(self): with patch.object(session, 'delete') as mocked_post: - mocked_post.return_value = _build_response_object(status_code=200) + mocked_post.return_value = _build_response_object(status_code=204) cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') assert cli.delete_points("foo") is True @@ -90,7 +90,7 @@ def test_delete_points(self): @raises(Exception) def test_delete_points_with_wrong_name(self): with patch.object(session, 'delete') as mocked_post: - mocked_post.return_value = _build_response_object(status_code=500) + mocked_post.return_value = _build_response_object(status_code=400) cli = InfluxDBClient('host', 8086, 'username', 'password', 'db') cli.delete_points("nonexist")