Пример #1
0
def test_A_fails_with_incorrect_dict():
    correct_d = {
        'terms': {
            'field': 'tags'
        },
        'aggs': {
            'per_author': {
                'terms': {
                    'field': 'author.raw'
                }
            }
        },
    }

    with raises(Exception):
        aggs.A(correct_d, field='f')

    d = correct_d.copy()
    del d['terms']
    with raises(Exception):
        aggs.A(d)

    d = correct_d.copy()
    d['xx'] = {}
    with raises(Exception):
        aggs.A(d)
Пример #2
0
def test_A_fails_with_incorrect_dict():
    correct_d = {
        "terms": {
            "field": "tags"
        },
        "aggs": {
            "per_author": {
                "terms": {
                    "field": "author.raw"
                }
            }
        },
    }

    with raises(Exception):
        aggs.A(correct_d, field="f")

    d = correct_d.copy()
    del d["terms"]
    with raises(Exception):
        aggs.A(d)

    d = correct_d.copy()
    d["xx"] = {}
    with raises(Exception):
        aggs.A(d)
Пример #3
0
def test_A_from_dict():
    d = {
        'per_tag': {
            'terms': {
                'field': 'tags'
            },
            'aggs': {
                'per_author': {
                    'terms': {
                        'field': 'author.raw'
                    }
                }
            },
        }
    }
    a = aggs.A(d)

    assert isinstance(a, aggs.Terms)
    assert a._params == {
        'field': 'tags',
        'aggs': {
            'per_author': aggs.A('per_author', 'terms', field='author.raw')
        }
    }
    assert a._name == 'per_tag'
    assert a['per_author'] == aggs.A('per_author', 'terms', field='author.raw')
    assert a.aggs.per_author == aggs.A('per_author',
                                       'terms',
                                       field='author.raw')
Пример #4
0
def test_meta_from_dict():
    max_score = aggs.Max(field="score")
    a = aggs.A(
        "terms", field="tags", aggs={"max_score": max_score}, meta={"some": "metadata"}
    )

    assert aggs.A(a.to_dict()) == a
Пример #5
0
def test_meta_from_dict():
    max_score = aggs.Max(field='score')
    a = aggs.A('terms',
               field='tags',
               aggs={'max_score': max_score},
               meta={'some': 'metadata'})

    assert aggs.A(a.to_dict()) == a
Пример #6
0
def test_A_fails_with_agg_and_name_or_params():
    a = aggs.A('per_tag', 'terms', field='tags')

    with raises(Exception):
        aggs.A(a, 'name')

    with raises(Exception):
        aggs.A(a, field='score')
Пример #7
0
def test_A_from_dict():
    d = {
        "terms": {"field": "tags"},
        "aggs": {"per_author": {"terms": {"field": "author.raw"}}},
    }
    a = aggs.A(d)

    assert isinstance(a, aggs.Terms)
    assert a._params == {
        "field": "tags",
        "aggs": {"per_author": aggs.A("terms", field="author.raw")},
    }
    assert a["per_author"] == aggs.A("terms", field="author.raw")
    assert a.aggs.per_author == aggs.A("terms", field="author.raw")
Пример #8
0
def test_filters_correctly_identifies_the_hash():
    a = aggs.A(
        "filters",
        filters={
            "group_a": {
                "term": {
                    "group": "a"
                }
            },
            "group_b": {
                "term": {
                    "group": "b"
                }
            },
        },
    )

    assert {
        "filters": {
            "filters": {
                "group_a": {
                    "term": {
                        "group": "a"
                    }
                },
                "group_b": {
                    "term": {
                        "group": "b"
                    }
                },
            }
        }
    } == a.to_dict()
    assert a.filters.group_a == query.Q("term", group="a")
Пример #9
0
def test_A_handles_nested_aggs_properly():
    max_score = aggs.Max('max_score', field='score')
    a = aggs.A('per_tag', 'terms', field='tags', aggs={'max_score': max_score})

    assert isinstance(a, aggs.Terms)
    assert a._params == {'field': 'tags', 'aggs': {'max_score': max_score}}
    assert a._name == 'per_tag'
Пример #10
0
def test_filters_correctly_identifies_the_hash():
    a = aggs.A('filters',
               filters={
                   'group_a': {
                       'term': {
                           'group': 'a'
                       }
                   },
                   'group_b': {
                       'term': {
                           'group': 'b'
                       }
                   }
               })

    assert {
        'filters': {
            'filters': {
                'group_a': {
                    'term': {
                        'group': 'a'
                    }
                },
                'group_b': {
                    'term': {
                        'group': 'b'
                    }
                }
            }
        }
    } == a.to_dict()
    assert a.filters.group_a == filter.F('term', group='a')
Пример #11
0
def test_meta():
    max_score = aggs.Max(field="score")
    a = aggs.A(
        "terms", field="tags", aggs={"max_score": max_score}, meta={"some": "metadata"}
    )

    assert {
        "terms": {"field": "tags"},
        "aggs": {"max_score": {"max": {"field": "score"}}},
        "meta": {"some": "metadata"},
    } == a.to_dict()
Пример #12
0
def test_meta():
    max_score = aggs.Max(field='score')
    a = aggs.A('terms',
               field='tags',
               aggs={'max_score': max_score},
               meta={'some': 'metadata'})

    assert {
        'terms': {
            'field': 'tags'
        },
        'aggs': {
            'max_score': {
                'max': {
                    'field': 'score'
                }
            }
        },
        'meta': {
            'some': 'metadata'
        }
    } == a.to_dict()
Пример #13
0
    def filter_queryset(self, request, queryset, view):
        daily_seed = int(datetime.datetime.now().strftime('%Y%m%d'))

        # Map over the game categories to create a function score query for one
        # and dump it into a Bool should.
        game_query = query.Q(
            'function_score',
            filter=es_filter.Bool(
                should=[es_filter.Term(tags=cat) for cat in GAME_CATEGORIES]),
            # Consistently random based on the day.
            functions=[SF('random_score', seed=daily_seed)],
        )

        # Buckets by tag. Run a size=1 TopHits aggregation to only select one
        # game from each tag. Results will have to be pulled out of
        # S.execute().aggregations rather than S.execute().hits.
        top_hits = aggs.TopHits(size=1)
        a = aggs.A('terms', field='tags', aggs={'first_game': top_hits})

        queryset = queryset.query(game_query)[0:4]
        queryset.aggs.bucket('top_hits', a)  # Not chainable.
        return queryset
Пример #14
0
def test_A_passes_aggs_through():
    a = aggs.A("terms", field="tags")
    assert aggs.A(a) is a
Пример #15
0
def test_A_handles_nested_aggs_properly():
    max_score = aggs.Max(field="score")
    a = aggs.A("terms", field="tags", aggs={"max_score": max_score})

    assert isinstance(a, aggs.Terms)
    assert a._params == {"field": "tags", "aggs": {"max_score": max_score}}
Пример #16
0
def test_A_creates_proper_agg():
    a = aggs.A("terms", field="tags")

    assert isinstance(a, aggs.Terms)
    assert a._params == {"field": "tags"}
Пример #17
0
def test_A_fails_without_agg_type():
    with raises(Exception):
        aggs.A('name', field='f')
Пример #18
0
def test_filter_can_be_instantiated_using_positional_args():
    a = aggs.Filter(query.Q("term", f=42))

    assert {"filter": {"term": {"f": 42}}} == a.to_dict()

    assert a == aggs.A("filter", query.Q("term", f=42))
Пример #19
0
def test_repr():
    max_score = aggs.Max(field="score")
    a = aggs.A("terms", field="tags", aggs={"max_score": max_score})

    assert "Terms(aggs={'max_score': Max(field='score')}, field='tags')" == repr(
        a)
Пример #20
0
def test_nested_buckets_are_settable_as_getitem():
    a = aggs.Terms(field="tags")
    b = a["per_author"] = aggs.A("terms", field="author.raw")

    assert a.aggs["per_author"] is b
Пример #21
0
def test_A_fails_with_agg_and_params():
    a = aggs.A("terms", field="tags")

    with raises(Exception):
        aggs.A(a, field="score")
Пример #22
0
def test_A_fails_with_agg_and_params():
    a = aggs.A('terms', field='tags')

    with raises(Exception):
        aggs.A(a, field='score')
Пример #23
0
def test_repr():
    max_score = aggs.Max(field='score')
    a = aggs.A('terms', field='tags', aggs={'max_score': max_score})

    assert "Terms(aggs={'max_score': Max(field='score')}, field='tags')" == repr(
        a)
Пример #24
0
def test_A_passes_aggs_through():
    a = aggs.A('terms', field='tags')
    assert aggs.A(a) is a
Пример #25
0
def test_A_creates_proper_agg():
    a = aggs.A('terms', field='tags')

    assert isinstance(a, aggs.Terms)
    assert a._params == {'field': 'tags'}
Пример #26
0
def test_filter_can_be_instantiated_using_positional_args():
    a = aggs.Filter(filter.F('term', f=42))

    assert {'filter': {'term': {'f': 42}}} == a.to_dict()

    assert a == aggs.A('filter', filter.F('term', f=42))
Пример #27
0
def test_nested_buckets_are_settable_as_getitem():
    a = aggs.Terms(field='tags')
    b = a['per_author'] = aggs.A('terms', field='author.raw')

    assert a.aggs['per_author'] is b