forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_composite_query.py
More file actions
86 lines (69 loc) · 2.63 KB
/
test_composite_query.py
File metadata and controls
86 lines (69 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pytest
from datetime import datetime
from osf_tests.factories import (
PreprintFactory,
AuthUserFactory
)
from osf.metrics import PreprintDownload
from api.base.settings import API_PRIVATE_BASE as API_BASE
@pytest.fixture()
def preprint():
return PreprintFactory()
@pytest.fixture()
def user():
user = AuthUserFactory()
user.is_staff = True
user.add_system_tag('preprint_metrics')
user.save()
return user
@pytest.fixture
def base_url():
return f'/{API_BASE}metrics/preprints/'
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestElasticSearch:
def test_elasticsearch_agg_query(self, app, user, base_url, preprint):
post_url = f'{base_url}downloads/'
payload = {
'data': {
'type': 'preprint_metrics',
'attributes': {
'query': {
'aggs': {
'preprints_by_year': {
'composite': {
'sources': [{
'date': {
'date_histogram': {
'field': 'timestamp',
'interval': 'year'
}
}
}]
}
}
}
}
}
}
}
resp = app.post_json_api(post_url, payload, auth=user.auth)
assert resp.status_code == 200
assert resp.json['hits']['hits'] == []
PreprintDownload.record_for_preprint(
preprint,
path=preprint.primary_file.path,
timestamp=datetime(year=2020, month=1, day=1),
)
PreprintDownload.record_for_preprint(
preprint,
path=preprint.primary_file.path,
timestamp=datetime(year=2020, month=2, day=1)
)
PreprintDownload._get_connection().indices.refresh(PreprintDownload._template_pattern)
resp = app.post_json_api(post_url, payload, auth=user.auth)
assert resp.status_code == 200
assert len(resp.json['aggregations']['preprints_by_year']['buckets']) == 1
payload['data']['attributes']['query']['aggs']['preprints_by_year']['composite']['sources'][0]['date']['date_histogram']['interval'] = 'month'
resp = app.post_json_api(post_url, payload, auth=user.auth)
assert len(resp.json['aggregations']['preprints_by_year']['buckets']) == 2