def test_range_aggregation(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [{
                            "match_all": {}
                        }]
                    },
                    "query": {
                        "match_all": {}
                    }
                }
            },
            "aggs": {
                "by_date": {
                    "range": {
                        "field":
                        "name",
                        "keyed":
                        True,
                        "ranges": [
                            {
                                "to": "c"
                            },
                            {
                                "from": "f"
                            },
                            {
                                "from": "k",
                                "to": "p",
                                "key": "k-p"
                            },
                        ]
                    }
                },
            },
            "size": SIZE_LIMIT
        }
        query = HQESQuery('cases').aggregation(
            RangeAggregation('by_date', 'name', [
                AggregationRange(end='c'),
                AggregationRange(start='f'),
                AggregationRange(start='k', end='p', key='k-p')
            ]))

        self.checkQuery(query, json_output)
    def test_result_parsing_complex(self):
        query = HQESQuery('cases').aggregation(
            TermsAggregation("users", 'user_id').aggregation(
                FilterAggregation('closed', filters.term(
                    'closed', True))).aggregation(
                        FilterAggregation('open', filters.term(
                            'closed', False)))).aggregation(
                                RangeAggregation('by_date', 'name', [
                                    AggregationRange(end='c'),
                                    AggregationRange(start='f'),
                                    AggregationRange(start='k', end='p')
                                ]))

        raw_result = {
            "aggregations": {
                "users": {
                    "buckets": [{
                        "closed": {
                            "doc_count": 0
                        },
                        "doc_count": 2,
                        "key": "user1",
                        "open": {
                            "doc_count": 2
                        }
                    }],
                    "doc_count_error_upper_bound":
                    0,
                    "sum_other_doc_count":
                    0
                },
                "by_date": {
                    "buckets": {
                        "*-c": {
                            "to": "c",
                            "doc_count": 3
                        },
                        "f-*": {
                            "from": "f",
                            "doc_count": 8
                        },
                        "k-p": {
                            "from": "k",
                            "to": "p",
                            "doc_count": 6
                        }
                    }
                }
            },
        }
        queryset = ESQuerySet(raw_result, deepcopy(query))
        self.assertEqual(queryset.aggregations.users.buckets.user1.key,
                         'user1')
        self.assertEqual(queryset.aggregations.users.buckets.user1.doc_count,
                         2)
        self.assertEqual(
            queryset.aggregations.users.buckets.user1.closed.doc_count, 0)
        self.assertEqual(
            queryset.aggregations.users.buckets.user1.open.doc_count, 2)
        self.assertEqual(
            queryset.aggregations.users.buckets_dict['user1'].open.doc_count,
            2)
        self.assertEqual(queryset.aggregations.users.counts_by_bucket(),
                         {'user1': 2})
        self.assertEqual(queryset.aggregations.by_date.counts_by_bucket(), {
            '*-c': 3,
            'f-*': 8,
            'k-p': 6,
        })