def __init__(self, **args): # Just a hack to prevent any exceptions Having.__init__(self, type='equalTo', aggregation=None, value=None) self.having = {'having': { 'type': 'dimSelector', 'dimension': args['dimension'], 'value': args['value'], }}
def __init__(self, **args): # Just a hack to prevent any exceptions Having.__init__(self, type="equalTo", aggregation=None, value=None) self.having = { "having": { "type": "dimSelector", "dimension": args["dimension"], "value": args["value"], } }
def test_query_filter_having(self): f1 = Filter(type="selector", dimension="foo", value="bar") query_filter = Filter.build_filter(f1) h1 = Having(type="filter", filter=query_filter) actual = Having.build_having(h1) expected = { "type": "filter", "filter": { "type": "selector", "dimension": "foo", "value": "bar" }, } assert actual == expected
def build_query(self, query_type, args): """ Build query based on given query type and arguments. :param string query_type: a type of query :param dict args: the dict of args to be sent :return: the resulting query :rtype: Query """ query_dict = {'queryType': query_type} for key, val in six.iteritems(args): if key == 'aggregations': query_dict[key] = build_aggregators(val) elif key == 'post_aggregations': query_dict['postAggregations'] = Postaggregator.build_post_aggregators(val) elif key == 'datasource': query_dict['dataSource'] = val elif key == 'paging_spec': query_dict['pagingSpec'] = val elif key == 'limit_spec': query_dict['limitSpec'] = val elif key == "filter": query_dict[key] = Filter.build_filter(val) elif key == "having": query_dict[key] = Having.build_having(val) elif key == 'dimension': query_dict[key] = build_dimension(val) elif key == 'dimensions': query_dict[key] = [build_dimension(v) for v in val] else: query_dict[key] = val self.last_query = Query(query_dict, query_type) return self.last_query
def test_not_having(self): h1 = Aggregation("sum") > 1 actual = Having.build_having(~h1) expected = { "type": "not", "havingSpec": { "type": "greaterThan", "aggregation": "sum", "value": 1 }, } assert actual == expected
def test_or_having(self): h1 = Aggregation("sum1") > 1 h2 = Aggregation("sum2") > 2 actual = Having.build_having(h1 | h2) expected = { "type": "or", "havingSpecs": [ { "type": "greaterThan", "aggregation": "sum1", "value": 1 }, { "type": "greaterThan", "aggregation": "sum2", "value": 2 }, ], } assert actual == expected
def build_query(self, query_type, args): """ Build query based on given query type and arguments. :param string query_type: a type of query :param dict args: the dict of args to be sent :return: the resulting query :rtype: Query """ query_dict = {"queryType": query_type} for key, val in six.iteritems(args): if key == "aggregations": query_dict[key] = build_aggregators(val) elif key == "post_aggregations": query_dict[ "postAggregations"] = Postaggregator.build_post_aggregators( val) elif key == "context": query_dict["context"] = val elif key == "datasource": query_dict["dataSource"] = self.parse_datasource( val, query_type) elif key == "paging_spec": query_dict["pagingSpec"] = val elif key == "limit_spec": query_dict["limitSpec"] = val elif key == "filter" and val is not None: query_dict[key] = Filter.build_filter(val) elif key == "having" and val is not None: query_dict[key] = Having.build_having(val) elif key == "dimension" and val is not None: query_dict[key] = build_dimension(val) elif key == "dimensions": query_dict[key] = [build_dimension(v) for v in val] else: query_dict[key] = val self.last_query = Query(query_dict, query_type) return self.last_query
def build_query_dict(self, query_type, args): query_dict = {"queryType": query_type} for key, val in six.iteritems(args): if key == "aggregations": query_dict[key] = build_aggregators(val) elif key == "post_aggregations": query_dict[ "postAggregations"] = Postaggregator.build_post_aggregators( val) elif key == "context": query_dict["context"] = val elif key == "datasource": query_dict["dataSource"] = self.parse_datasource( val, query_type) elif key == "paging_spec": query_dict["pagingSpec"] = val elif key == "limit_spec": query_dict["limitSpec"] = val elif key == "filter" and val is not None: query_dict[key] = Filter.build_filter(val) elif key == "having" and val is not None: query_dict[key] = Having.build_having(val) elif key == "dimension" and val is not None: query_dict[key] = build_dimension(val) elif key == "dimensions": query_dict[key] = [build_dimension(v) for v in val] elif key == 'virtualColumns': query_dict[key] = [ VirtualColumn.build_virtual_column(v) for v in val ] elif key == 'sub_query' and val is not None: query_dict['dataSource'] = { 'type': 'query', 'query': self.build_query_dict(query_type, val) } elif val is not None: query_dict[key] = val return query_dict
def test_equalTo_having(self): h1 = Having(type="equalTo", aggregation="sum", value=1) actual = Having.build_having(h1) expected = {"type": "equalTo", "aggregation": "sum", "value": 1} assert actual == expected
def test_greaterThan_having_Aggregation(self): h1 = Aggregation("sum") > 1 actual = Having.build_having(h1) expected = {"type": "greaterThan", "aggregation": "sum", "value": 1} assert actual == expected
def test_greaterThan_having(self): h1 = Having(type="greaterThan", aggregation="sum", value=1) actual = Having.build_having(h1) expected = {"type": "greaterThan", "aggregation": "sum", "value": 1} assert actual == expected
def test_equalTo_having_Aggregation(self): h1 = Aggregation("sum") == 1 actual = Having.build_having(h1) expected = {"type": "equalTo", "aggregation": "sum", "value": 1} assert actual == expected
def test_not_exists_having_type(self): with pytest.raises(NotImplementedError): Having(type="notExists")
def test_dimension(self): h1 = Dimension("foo") == "bar" actual = Having.build_having(h1) expected = {"type": "dimSelector", "dimension": "foo", "value": "bar"} assert actual == expected
def test_dimSelector_having(self): h1 = Having(type="dimSelector", dimension="foo", value="bar") actual = Having.build_having(h1) expected = {"type": "dimSelector", "dimension": "foo", "value": "bar"} assert actual == expected