Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some tests #32

Merged
merged 8 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ build/
dist/
pyDruid.egg-info/
__pycache__
.eggs
.eggs
\#*#
.#*
*~
35 changes: 23 additions & 12 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 = {
Expand Down Expand Up @@ -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()
Expand All @@ -94,4 +106,3 @@ def test_export_pandas(self):
'value2': '㬓',
}])
assert_frame_equal(df, expected_df)

38 changes: 38 additions & 0 deletions tests/utils/test_aggregators.py
Original file line number Diff line number Diff line change
@@ -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')))
12 changes: 7 additions & 5 deletions tests/utils/test_query_utils.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -12,28 +12,30 @@ 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()

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()