diff --git a/.gitignore b/.gitignore index 859abcfe..2fe116ca 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,7 @@ build/ dist/ pyDruid.egg-info/ __pycache__ -.eggs \ No newline at end of file +.eggs +\#*# +.#* +*~ diff --git a/tests/test_client.py b/tests/test_client.py index 9f1d3815..f3289b9e 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -2,18 +2,25 @@ import os import pytest -import pandas -from pandas.util.testing import assert_frame_equal + +try: + import pandas + from pandas.util.testing import assert_frame_equal +except ImportError: + pandas = None + from six import PY3 from pydruid.client import PyDruid -from pydruid.utils.aggregators import * -from pydruid.utils.postaggregator import * -from pydruid.utils.filters import * -from pydruid.utils.having import * +from pydruid.utils import aggregators +from pydruid.utils import postaggregator +from pydruid.utils import filters +from pydruid.utils import having + def create_client(): return PyDruid('http://localhost:8083', 'druid/v2/') + def create_client_with_results(): client = create_client() client.query_type = 'timeseries' @@ -23,30 +30,34 @@ def create_client_with_results(): ] return client + def line_ending(): if PY3: return os.linesep return "\r\n" + class TestClient: + def test_build_query(self): client = create_client() - assert client.query_dict == None + assert client.query_dict is None client.build_query({ 'datasource': 'things', 'aggregations': { - 'count': count('thing'), + 'count': aggregators.count('thing'), }, 'post_aggregations': { - 'avg': Field('sum') / Field('count'), + 'avg': (postaggregator.Field('sum') / + postaggregator.Field('count')), }, 'paging_spec': { 'pagingIdentifies': {}, 'threshold': 1, }, - 'filter': Dimension('one') == 1, - 'having': Aggregation('sum') > 1, + 'filter': filters.Dimension('one') == 1, + 'having': having.Aggregation('sum') > 1, 'new_key': 'value', }) expected_query_dict = { @@ -81,6 +92,7 @@ def test_export_tsv(self, tmpdir): client.export_tsv(str(file_path)) assert file_path.read() == "value2\tvalue1\ttimestamp" + line_ending() + "㬓\t1\t2015-01-01T00:00:00.000-05:00" + line_ending() + "㬓\t2\t2015-01-02T00:00:00.000-05:00" + line_ending() + @pytest.mark.skipif(pandas is None, reason="requires pandas") def test_export_pandas(self): client = create_client_with_results() df = client.export_pandas() @@ -94,4 +106,3 @@ def test_export_pandas(self): 'value2': '㬓', }]) assert_frame_equal(df, expected_df) - diff --git a/tests/utils/test_aggregators.py b/tests/utils/test_aggregators.py new file mode 100644 index 00000000..0e2d56b6 --- /dev/null +++ b/tests/utils/test_aggregators.py @@ -0,0 +1,38 @@ +# -*- coding: UTF-8 -*- + +from operator import itemgetter + +from pydruid.utils import aggregators + + +class TestAggregators: + + def test_aggregators(self): + aggs = [('longsum', 'longSum'), ('doublesum', 'doubleSum'), + ('min', 'min'), ('max', 'max'), ('count', 'count'), + ('hyperunique', 'hyperUnique')] + aggs_funcs = [(getattr(aggregators, agg_name), agg_type) + for agg_name, agg_type in aggs] + for f, agg_type in aggs_funcs: + assert f('metric') == {'type': agg_type, 'fieldName': 'metric'} + + def test_build_aggregators(self): + agg_input = { + 'agg1': aggregators.count('metric1'), + 'agg2': aggregators.longsum('metric2'), + 'agg3': aggregators.doublesum('metric3'), + 'agg4': aggregators.min('metric4'), + 'agg5': aggregators.max('metric5'), + 'agg6': aggregators.hyperunique('metric6') + } + built_agg = aggregators.build_aggregators(agg_input) + expected = [ + {'name': 'agg1', 'type': 'count', 'fieldName': 'metric1'}, + {'name': 'agg2', 'type': 'longSum', 'fieldName': 'metric2'}, + {'name': 'agg3', 'type': 'doubleSum', 'fieldName': 'metric3'}, + {'name': 'agg4', 'type': 'min', 'fieldName': 'metric4'}, + {'name': 'agg5', 'type': 'max', 'fieldName': 'metric5'}, + {'name': 'agg6', 'type': 'hyperUnique', 'fieldName': 'metric6'}, + ] + assert (sorted(built_agg, key=itemgetter('name')) == + sorted(expected, key=itemgetter('name'))) diff --git a/tests/utils/test_query_utils.py b/tests/utils/test_query_utils.py index ba4f8172..cfb104fd 100644 --- a/tests/utils/test_query_utils.py +++ b/tests/utils/test_query_utils.py @@ -1,9 +1,9 @@ # -*- coding: UTF-8 -*- import os -import pytest from six import PY3 -from pydruid.utils.query_utils import * +from pydruid.utils import query_utils + def open_file(file_path): if PY3: @@ -12,16 +12,19 @@ def open_file(file_path): f = open(file_path, 'wb') return f + def line_ending(): if PY3: return os.linesep return "\r\n" + class TestUnicodeWriter: + def test_writerow(self, tmpdir): file_path = tmpdir.join("out.tsv") f = open_file(str(file_path)) - w = UnicodeWriter(f) + w = query_utils.UnicodeWriter(f) w.writerow(['value1', '㬓']) f.close() assert file_path.read() == "value1\t㬓" + line_ending() @@ -29,11 +32,10 @@ def test_writerow(self, tmpdir): def test_writerows(self, tmpdir): file_path = tmpdir.join("out.tsv") f = open_file(str(file_path)) - w = UnicodeWriter(f) + w = query_utils.UnicodeWriter(f) w.writerows([ ['header1', 'header2'], ['value1', '㬓'] ]) f.close() assert file_path.read() == "header1\theader2" + line_ending() + "value1\t㬓" + line_ending() -