# Path: pyeqs/dsl/aggregations.py
# class Aggregations(dict):
# 
#     def __init__(self, agg_name, field_name, metric, size=0, min_doc_count=1,
#                  order_type=None, order_dir="desc", filter_val=None, filter_name=None,
#                  global_name=None, nested_path=None, range_list=None, range_name=None,
#                  histogram_interval=None):
#         super(Aggregations, self).__init__()
#         self.agg_name = agg_name
#         self.field_name = field_name
#         self.metric = metric
#         self.size = size
#         self.min_doc_count = min_doc_count
#         self.order_type = self._pick_order_type(order_type, histogram_interval)
#         self.order_dir = order_dir
#         self.filter_val = filter_val
#         self.filter_name = filter_name
#         self.global_name = global_name
#         self.nested_path = nested_path
#         self.range_list = range_list
#         self.range_name = range_name
#         self.interval = histogram_interval
#         self._build_dict()
# 
#     def _build_dict(self):
#         if self.nested_path:
#             self[self.nested_path] = self._nesting()
#         else:
#             self[self.agg_name] = {self.metric: {"field": self.field_name}}
#             if self.metric == "terms":
#                 self[self.agg_name][self.metric].update({
#                     "size": self.size,
#                     "order": {self.order_type: self.order_dir},
#                     "min_doc_count": self.min_doc_count
#                 })
# 
#         if self.range_list:
#             if not self.range_name:
#                 range_name = "{name}_ranges".format(name=self.field_name)
#             else:
#                 range_name = self.range_name
#             self[range_name] = {"range": {
#                 "field": self.field_name,
#                 "ranges": self._ranging()
#             }}
#             self.pop(self.agg_name)
#         if self.interval:
#             self[self.agg_name]["histogram"] = {
#                 "field": self.field_name,
#                 "interval": self.interval,
#                 "order": {self.order_type: self.order_dir},
#                 "min_doc_count": self.min_doc_count
#             }
#             self[self.agg_name].pop(self.metric)
#         elif self.filter_val and self.filter_name:
#             self[self.filter_name] = {'filter': self.filter_val, 'aggregations': {}}
#             self[self.filter_name]['aggregations'][self.agg_name] = self.pop(self.agg_name)
#         elif self.global_name:
#             self[self.global_name] = {"global": {}, "aggregations": {}}
#             self[self.global_name]['aggregations'][self.agg_name] = self.pop(self.agg_name)
# 
#     def _nesting(self):
#         nesting = {
#             "nested": {"path": self.nested_path},
#             "aggregations": {
#                 self.agg_name: {
#                     self.metric: {"field": "{path}.{name}".format(path=self.nested_path, name=self.field_name)}
#                 }}
#         }
#         if self.metric == "terms":
#             nesting["aggregations"][self.agg_name][self.metric].update({
#                 "size": self.size,
#                 "order": {self.order_type: self.order_dir},
#                 "min_doc_count": self.min_doc_count
#             })
#         return nesting
# 
#     def _ranging(self):
#         """
#         Should be a list of values to designate the buckets
#         """
#         agg_ranges = []
#         for i, val in enumerate(self.range_list):
#             if i == 0:
#                 agg_ranges.append({"to": val})
#             else:
#                 previous = self.range_list[i - 1]
#                 agg_ranges.append({"from": previous, "to": val})
# 
#             if i + 1 == len(self.range_list):
#                 agg_ranges.append({"from": val})
#         return agg_ranges
# 
#     def _pick_order_type(self, order_type, hist):
#         if order_type:
#             return order_type
#         elif hist:
#             return "_key"
#         else:
#             return "_count"
# 
# Path: tests/helpers.py
# def homogeneous(a, b):
#     json.dumps(a, sort_keys=True).should.equal(json.dumps(b, sort_keys=True))


from pyeqs.dsl import Aggregations
from tests.helpers import homogeneous
                "field": "field_name",
                "order": {"_count": "desc"},
                "min_doc_count": 1,
                "size": 1
            }
        }
    }

    homogeneous(t, results)


def test_add_agg_with_order():
    """
    Create aggregations block specifying order type and direction
    """
    # When add a terms agg block w/ size
    t = Aggregations("agg_name", "field_name", "terms", order_type="_term", order_dir="asc")

    # Then I see correct json
    results = {
        "agg_name": {
            "terms": {
                "field": "field_name",
                "order": {"_term": "asc"},
                "min_doc_count": 1,
                "size": 0
            }
        }
    }


# Path: pyeqs/dsl/aggregations.py
# class Aggregations(dict):
# 
#     def __init__(self, agg_name, field_name, metric, size=0, min_doc_count=1,
#                  order_type=None, order_dir="desc", filter_val=None, filter_name=None,
#                  global_name=None, nested_path=None, range_list=None, range_name=None,
#                  histogram_interval=None):
#         super(Aggregations, self).__init__()
#         self.agg_name = agg_name
#         self.field_name = field_name
#         self.metric = metric
#         self.size = size
#         self.min_doc_count = min_doc_count
#         self.order_type = self._pick_order_type(order_type, histogram_interval)
#         self.order_dir = order_dir
#         self.filter_val = filter_val
#         self.filter_name = filter_name
#         self.global_name = global_name
#         self.nested_path = nested_path
#         self.range_list = range_list
#         self.range_name = range_name
#         self.interval = histogram_interval
#         self._build_dict()
# 
#     def _build_dict(self):
#         if self.nested_path:
#             self[self.nested_path] = self._nesting()
#         else:
#             self[self.agg_name] = {self.metric: {"field": self.field_name}}
#             if self.metric == "terms":
#                 self[self.agg_name][self.metric].update({
#                     "size": self.size,
#                     "order": {self.order_type: self.order_dir},
#                     "min_doc_count": self.min_doc_count
#                 })
# 
#         if self.range_list:
#             if not self.range_name:
#                 range_name = "{name}_ranges".format(name=self.field_name)
#             else:
#                 range_name = self.range_name
#             self[range_name] = {"range": {
#                 "field": self.field_name,
#                 "ranges": self._ranging()
#             }}
#             self.pop(self.agg_name)
#         if self.interval:
#             self[self.agg_name]["histogram"] = {
#                 "field": self.field_name,
#                 "interval": self.interval,
#                 "order": {self.order_type: self.order_dir},
#                 "min_doc_count": self.min_doc_count
#             }
#             self[self.agg_name].pop(self.metric)
#         elif self.filter_val and self.filter_name:
#             self[self.filter_name] = {'filter': self.filter_val, 'aggregations': {}}
#             self[self.filter_name]['aggregations'][self.agg_name] = self.pop(self.agg_name)
#         elif self.global_name:
#             self[self.global_name] = {"global": {}, "aggregations": {}}
#             self[self.global_name]['aggregations'][self.agg_name] = self.pop(self.agg_name)
# 
#     def _nesting(self):
#         nesting = {
#             "nested": {"path": self.nested_path},
#             "aggregations": {
#                 self.agg_name: {
#                     self.metric: {"field": "{path}.{name}".format(path=self.nested_path, name=self.field_name)}
#                 }}
#         }
#         if self.metric == "terms":
#             nesting["aggregations"][self.agg_name][self.metric].update({
#                 "size": self.size,
#                 "order": {self.order_type: self.order_dir},
#                 "min_doc_count": self.min_doc_count
#             })
#         return nesting
# 
#     def _ranging(self):
#         """
#         Should be a list of values to designate the buckets
#         """
#         agg_ranges = []
#         for i, val in enumerate(self.range_list):
#             if i == 0:
#                 agg_ranges.append({"to": val})
#             else:
#                 previous = self.range_list[i - 1]
#                 agg_ranges.append({"from": previous, "to": val})
# 
#             if i + 1 == len(self.range_list):
#                 agg_ranges.append({"from": val})
#         return agg_ranges
# 
#     def _pick_order_type(self, order_type, hist):
#         if order_type:
#             return order_type
#         elif hist:
#             return "_key"
#         else:
#             return "_count"
# 
# Path: tests/helpers.py
# def homogeneous(a, b):
#     json.dumps(a, sort_keys=True).should.equal(json.dumps(b, sort_keys=True))


# Path: tests/unit/test_aggregation.py
from pyeqs.dsl import Aggregations
from tests.helpers import homogeneous

                "field": "field_name",
                "order": {"_count": "desc"},
                "min_doc_count": 1,
                "size": 1
            }
        }
    }

    homogeneous(t, results)


def test_add_agg_with_order():
    """
    Create aggregations block specifying order type and direction
    """
    # When add a terms agg block w/ size
    t = Aggregations("agg_name", "field_name", "terms", order_type="_term", order_dir="asc")

    # Then I see correct json
    results = {
        "agg_name": {
            "terms": {
                "field": "field_name",
                "order": {"_term": "asc"},
                "min_doc_count": 1,
                "size": 0
            }
        }
    }


    homogeneous(t, results)