-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix regressions introduced by support for Python 3 and add initial te…
…st coverage
- Loading branch information
Showing
7 changed files
with
194 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ pydruid/bard.py | |
build/ | ||
dist/ | ||
pyDruid.egg-info/ | ||
|
||
__pycache__ | ||
.eggs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# -*- coding: UTF-8 -*- | ||
|
||
import os | ||
import pytest | ||
import pandas | ||
from pandas.util.testing import assert_frame_equal | ||
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 * | ||
|
||
def create_client(): | ||
return PyDruid('http://localhost:8083', 'druid/v2/') | ||
|
||
def create_client_with_results(): | ||
client = create_client() | ||
client.query_type = 'timeseries' | ||
client.result = [ | ||
{'result': {'value1': 1, 'value2': '㬓'}, 'timestamp': '2015-01-01T00:00:00.000-05:00'}, | ||
{'result': {'value1': 2, 'value2': '㬓'}, 'timestamp': '2015-01-02T00:00:00.000-05:00'} | ||
] | ||
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 | ||
|
||
client.build_query({ | ||
'datasource': 'things', | ||
'aggregations': { | ||
'count': count('thing'), | ||
}, | ||
'post_aggregations': { | ||
'avg': Field('sum') / Field('count'), | ||
}, | ||
'paging_spec': { | ||
'pagingIdentifies': {}, | ||
'threshold': 1, | ||
}, | ||
'filter': Dimension('one') == 1, | ||
'having': Aggregation('sum') > 1, | ||
'new_key': 'value', | ||
}) | ||
expected_query_dict = { | ||
'queryType': None, | ||
'dataSource': 'things', | ||
'aggregations': [{'fieldName': 'thing', 'name': 'count', 'type': 'count'}], | ||
'postAggregations': [{ | ||
'fields': [{ | ||
'fieldName': 'sum', 'type': 'fieldAccess', | ||
}, { | ||
'fieldName': 'count', 'type': 'fieldAccess', | ||
}], | ||
'fn': '/', | ||
'name': 'avg', | ||
'type': 'arithmetic', | ||
}], | ||
'pagingSpec': {'pagingIdentifies': {}, 'threshold': 1}, | ||
'filter': {'dimension': 'one', 'type': 'selector', 'value': 1}, | ||
'having': {'aggregation': 'sum', 'type': 'greaterThan', 'value': 1}, | ||
'new_key': 'value', | ||
} | ||
assert client.query_dict == expected_query_dict | ||
|
||
def test_validate_query(self): | ||
client = create_client() | ||
client.validate_query(['validkey'], {'validkey': 'value'}) | ||
pytest.raises(ValueError, client.validate_query, *[['validkey'], {'invalidkey': 'value'}]) | ||
|
||
def test_export_tsv(self, tmpdir): | ||
client = create_client_with_results() | ||
file_path = tmpdir.join('out.tsv') | ||
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() | ||
|
||
def test_export_pandas(self): | ||
client = create_client_with_results() | ||
df = client.export_pandas() | ||
expected_df = pandas.DataFrame([{ | ||
'timestamp': '2015-01-01T00:00:00.000-05:00', | ||
'value1': 1, | ||
'value2': '㬓', | ||
}, { | ||
'timestamp': '2015-01-02T00:00:00.000-05:00', | ||
'value1': 2, | ||
'value2': '㬓', | ||
}]) | ||
assert_frame_equal(df, expected_df) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: UTF-8 -*- | ||
|
||
import os | ||
import pytest | ||
from six import PY3 | ||
from pydruid.utils.query_utils import * | ||
|
||
def open_file(file_path): | ||
if PY3: | ||
f = open(file_path, 'w', newline='', encoding='utf-8') | ||
else: | ||
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.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.writerows([ | ||
['header1', 'header2'], | ||
['value1', '㬓'] | ||
]) | ||
f.close() | ||
assert file_path.read() == "header1\theader2" + line_ending() + "value1\t㬓" + line_ending() | ||
|