Skip to content

Commit caa9e7d

Browse files
authored
Pass through method kwarg to DataFrameClient query method (influxdata#617)
Also, add db maintenance tests from InfluxDBClient fixes influxdata#616
1 parent 1c96ce2 commit caa9e7d

File tree

3 files changed

+246
-1
lines changed

3 files changed

+246
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88
### Added
99

1010
### Changed
11+
- Pass through the "method" kwarg to DataFrameClient queries
1112

1213
### Removed
1314

influxdb/_dataframe_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ def query(self,
148148
raise_errors=True,
149149
chunked=False,
150150
chunk_size=0,
151+
method="GET",
151152
dropna=True):
152153
"""
153-
Quering data into a DataFrame.
154+
Query data into a DataFrame.
154155
155156
:param query: the actual query string
156157
:param params: additional parameters for the request, defaults to {}
@@ -176,6 +177,7 @@ def query(self,
176177
raise_errors=raise_errors,
177178
chunked=chunked,
178179
database=database,
180+
method=method,
179181
chunk_size=chunk_size)
180182
results = super(DataFrameClient, self).query(query, **query_args)
181183
if query.strip().upper().startswith("SELECT"):

influxdb/tests/dataframe_client_test.py

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,248 @@ def test_write_points_from_dataframe_fails_with_series(self):
552552
cli = DataFrameClient(database='db')
553553
cli.write_points(dataframe, "foo")
554554

555+
def test_create_database(self):
556+
"""Test create database for TestInfluxDBClient object."""
557+
cli = DataFrameClient(database='db')
558+
with requests_mock.Mocker() as m:
559+
m.register_uri(
560+
requests_mock.POST,
561+
"http://localhost:8086/query",
562+
text='{"results":[{}]}'
563+
)
564+
cli.create_database('new_db')
565+
self.assertEqual(
566+
m.last_request.qs['q'][0],
567+
'create database "new_db"'
568+
)
569+
570+
def test_create_numeric_named_database(self):
571+
"""Test create db w/numeric name for TestInfluxDBClient object."""
572+
cli = DataFrameClient(database='db')
573+
with requests_mock.Mocker() as m:
574+
m.register_uri(
575+
requests_mock.POST,
576+
"http://localhost:8086/query",
577+
text='{"results":[{}]}'
578+
)
579+
cli.create_database('123')
580+
self.assertEqual(
581+
m.last_request.qs['q'][0],
582+
'create database "123"'
583+
)
584+
585+
@raises(Exception)
586+
def test_create_database_fails(self):
587+
"""Test create database fail for TestInfluxDBClient object."""
588+
cli = DataFrameClient(database='db')
589+
with _mocked_session(cli, 'post', 401):
590+
cli.create_database('new_db')
591+
592+
def test_drop_database(self):
593+
"""Test drop database for TestInfluxDBClient object."""
594+
cli = DataFrameClient(database='db')
595+
with requests_mock.Mocker() as m:
596+
m.register_uri(
597+
requests_mock.POST,
598+
"http://localhost:8086/query",
599+
text='{"results":[{}]}'
600+
)
601+
cli.drop_database('new_db')
602+
self.assertEqual(
603+
m.last_request.qs['q'][0],
604+
'drop database "new_db"'
605+
)
606+
607+
def test_drop_measurement(self):
608+
"""Test drop measurement for TestInfluxDBClient object."""
609+
cli = DataFrameClient(database='db')
610+
with requests_mock.Mocker() as m:
611+
m.register_uri(
612+
requests_mock.POST,
613+
"http://localhost:8086/query",
614+
text='{"results":[{}]}'
615+
)
616+
cli.drop_measurement('new_measurement')
617+
self.assertEqual(
618+
m.last_request.qs['q'][0],
619+
'drop measurement "new_measurement"'
620+
)
621+
622+
def test_drop_numeric_named_database(self):
623+
"""Test drop numeric db for TestInfluxDBClient object."""
624+
cli = DataFrameClient(database='db')
625+
with requests_mock.Mocker() as m:
626+
m.register_uri(
627+
requests_mock.POST,
628+
"http://localhost:8086/query",
629+
text='{"results":[{}]}'
630+
)
631+
cli.drop_database('123')
632+
self.assertEqual(
633+
m.last_request.qs['q'][0],
634+
'drop database "123"'
635+
)
636+
637+
@raises(Exception)
638+
def test_get_list_database_fails(self):
639+
"""Test get list of dbs fail for TestInfluxDBClient object."""
640+
cli = DataFrameClient('host', 8086, 'username', 'password')
641+
with _mocked_session(cli, 'get', 401):
642+
cli.get_list_database()
643+
644+
def test_get_list_measurements(self):
645+
"""Test get list of measurements for TestInfluxDBClient object."""
646+
cli = DataFrameClient(database='db')
647+
data = {
648+
"results": [{
649+
"series": [
650+
{"name": "measurements",
651+
"columns": ["name"],
652+
"values": [["cpu"], ["disk"]
653+
]}]}
654+
]
655+
}
656+
657+
with _mocked_session(cli, 'get', 200, json.dumps(data)):
658+
self.assertListEqual(
659+
cli.get_list_measurements(),
660+
[{'name': 'cpu'}, {'name': 'disk'}]
661+
)
662+
663+
def test_create_retention_policy_default(self):
664+
"""Test create default ret policy for TestInfluxDBClient object."""
665+
cli = DataFrameClient(database='db')
666+
example_response = '{"results":[{}]}'
667+
668+
with requests_mock.Mocker() as m:
669+
m.register_uri(
670+
requests_mock.POST,
671+
"http://localhost:8086/query",
672+
text=example_response
673+
)
674+
cli.create_retention_policy(
675+
'somename', '1d', 4, default=True, database='db'
676+
)
677+
678+
self.assertEqual(
679+
m.last_request.qs['q'][0],
680+
'create retention policy "somename" on '
681+
'"db" duration 1d replication 4 shard duration 0s default'
682+
)
683+
684+
def test_create_retention_policy(self):
685+
"""Test create retention policy for TestInfluxDBClient object."""
686+
cli = DataFrameClient(database='db')
687+
example_response = '{"results":[{}]}'
688+
689+
with requests_mock.Mocker() as m:
690+
m.register_uri(
691+
requests_mock.POST,
692+
"http://localhost:8086/query",
693+
text=example_response
694+
)
695+
cli.create_retention_policy(
696+
'somename', '1d', 4, database='db'
697+
)
698+
699+
self.assertEqual(
700+
m.last_request.qs['q'][0],
701+
'create retention policy "somename" on '
702+
'"db" duration 1d replication 4 shard duration 0s'
703+
)
704+
705+
def test_alter_retention_policy(self):
706+
"""Test alter retention policy for TestInfluxDBClient object."""
707+
cli = DataFrameClient(database='db')
708+
example_response = '{"results":[{}]}'
709+
710+
with requests_mock.Mocker() as m:
711+
m.register_uri(
712+
requests_mock.POST,
713+
"http://localhost:8086/query",
714+
text=example_response
715+
)
716+
# Test alter duration
717+
cli.alter_retention_policy('somename', 'db',
718+
duration='4d')
719+
self.assertEqual(
720+
m.last_request.qs['q'][0],
721+
'alter retention policy "somename" on "db" duration 4d'
722+
)
723+
# Test alter replication
724+
cli.alter_retention_policy('somename', 'db',
725+
replication=4)
726+
self.assertEqual(
727+
m.last_request.qs['q'][0],
728+
'alter retention policy "somename" on "db" replication 4'
729+
)
730+
731+
# Test alter shard duration
732+
cli.alter_retention_policy('somename', 'db',
733+
shard_duration='1h')
734+
self.assertEqual(
735+
m.last_request.qs['q'][0],
736+
'alter retention policy "somename" on "db" shard duration 1h'
737+
)
738+
739+
# Test alter default
740+
cli.alter_retention_policy('somename', 'db',
741+
default=True)
742+
self.assertEqual(
743+
m.last_request.qs['q'][0],
744+
'alter retention policy "somename" on "db" default'
745+
)
746+
747+
@raises(Exception)
748+
def test_alter_retention_policy_invalid(self):
749+
"""Test invalid alter ret policy for TestInfluxDBClient object."""
750+
cli = DataFrameClient('host', 8086, 'username', 'password')
751+
with _mocked_session(cli, 'get', 400):
752+
cli.alter_retention_policy('somename', 'db')
753+
754+
def test_drop_retention_policy(self):
755+
"""Test drop retention policy for TestInfluxDBClient object."""
756+
cli = DataFrameClient(database='db')
757+
example_response = '{"results":[{}]}'
758+
759+
with requests_mock.Mocker() as m:
760+
m.register_uri(
761+
requests_mock.POST,
762+
"http://localhost:8086/query",
763+
text=example_response
764+
)
765+
cli.drop_retention_policy('somename', 'db')
766+
self.assertEqual(
767+
m.last_request.qs['q'][0],
768+
'drop retention policy "somename" on "db"'
769+
)
770+
771+
@raises(Exception)
772+
def test_drop_retention_policy_fails(self):
773+
"""Test failed drop ret policy for TestInfluxDBClient object."""
774+
cli = DataFrameClient('host', 8086, 'username', 'password')
775+
with _mocked_session(cli, 'delete', 401):
776+
cli.drop_retention_policy('default', 'db')
777+
778+
def test_get_list_retention_policies(self):
779+
"""Test get retention policies for TestInfluxDBClient object."""
780+
cli = DataFrameClient(database='db')
781+
example_response = \
782+
'{"results": [{"series": [{"values": [["fsfdsdf", "24h0m0s", 2]],'\
783+
' "columns": ["name", "duration", "replicaN"]}]}]}'
784+
785+
with requests_mock.Mocker() as m:
786+
m.register_uri(
787+
requests_mock.GET,
788+
"http://localhost:8086/query",
789+
text=example_response
790+
)
791+
self.assertListEqual(
792+
cli.get_list_retention_policies("db"),
793+
[{'duration': '24h0m0s',
794+
'name': 'fsfdsdf', 'replicaN': 2}]
795+
)
796+
555797
def test_query_into_dataframe(self):
556798
"""Test query into df for TestDataFrameClient object."""
557799
data = {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy