17
17
18
18
from datetime import datetime
19
19
20
+ import pytest
21
+
20
22
from elasticsearch_dsl import A , Boolean , Date , Document , Keyword
21
23
from elasticsearch_dsl .faceted_search import (
22
24
DateHistogramFacet ,
29
31
from .test_document import PullRequest
30
32
31
33
32
- class CommitSearch (FacetedSearch ):
33
- index = "flat-git"
34
- fields = (
35
- "description" ,
36
- "files" ,
37
- )
38
-
39
- facets = {
40
- "files" : TermsFacet (field = "files" ),
41
- "frequency" : DateHistogramFacet (
42
- field = "authored_date" , interval = "day" , min_doc_count = 1
43
- ),
44
- "deletions" : RangeFacet (
45
- field = "stats.deletions" ,
46
- ranges = [("ok" , (None , 1 )), ("good" , (1 , 5 )), ("better" , (5 , None ))],
47
- ),
48
- }
49
-
50
-
51
34
class Repos (Document ):
52
35
is_public = Boolean ()
53
36
created_at = Date ()
@@ -64,19 +47,6 @@ class Index:
64
47
name = "git"
65
48
66
49
67
- class RepoSearch (FacetedSearch ):
68
- index = "git"
69
- doc_types = [Repos ]
70
- facets = {
71
- "public" : TermsFacet (field = "is_public" ),
72
- "created" : DateHistogramFacet (field = "created_at" , interval = "month" ),
73
- }
74
-
75
- def search (self ):
76
- s = super (RepoSearch , self ).search ()
77
- return s .filter ("term" , commit_repo = "repo" )
78
-
79
-
80
50
class MetricSearch (FacetedSearch ):
81
51
index = "git"
82
52
doc_types = [Commit ]
@@ -86,15 +56,72 @@ class MetricSearch(FacetedSearch):
86
56
}
87
57
88
58
89
- class PRSearch (FacetedSearch ):
90
- index = "test-prs"
91
- doc_types = [PullRequest ]
92
- facets = {
93
- "comments" : NestedFacet (
94
- "comments" ,
95
- DateHistogramFacet (field = "comments.created_at" , interval = "month" ),
59
+ @pytest .fixture (scope = "session" )
60
+ def commit_search_cls (es_version ):
61
+ if es_version >= (7 , 2 ):
62
+ interval_kwargs = {"fixed_interval" : "1d" }
63
+ else :
64
+ interval_kwargs = {"interval" : "day" }
65
+
66
+ class CommitSearch (FacetedSearch ):
67
+ index = "flat-git"
68
+ fields = (
69
+ "description" ,
70
+ "files" ,
96
71
)
97
- }
72
+
73
+ facets = {
74
+ "files" : TermsFacet (field = "files" ),
75
+ "frequency" : DateHistogramFacet (
76
+ field = "authored_date" , min_doc_count = 1 , ** interval_kwargs
77
+ ),
78
+ "deletions" : RangeFacet (
79
+ field = "stats.deletions" ,
80
+ ranges = [("ok" , (None , 1 )), ("good" , (1 , 5 )), ("better" , (5 , None ))],
81
+ ),
82
+ }
83
+
84
+ return CommitSearch
85
+
86
+
87
+ @pytest .fixture (scope = "session" )
88
+ def repo_search_cls (es_version ):
89
+ interval_type = "calendar_interval" if es_version >= (7 , 2 ) else "interval"
90
+
91
+ class RepoSearch (FacetedSearch ):
92
+ index = "git"
93
+ doc_types = [Repos ]
94
+ facets = {
95
+ "public" : TermsFacet (field = "is_public" ),
96
+ "created" : DateHistogramFacet (
97
+ field = "created_at" , ** {interval_type : "month" }
98
+ ),
99
+ }
100
+
101
+ def search (self ):
102
+ s = super (RepoSearch , self ).search ()
103
+ return s .filter ("term" , commit_repo = "repo" )
104
+
105
+ return RepoSearch
106
+
107
+
108
+ @pytest .fixture (scope = "session" )
109
+ def pr_search_cls (es_version ):
110
+ interval_type = "calendar_interval" if es_version >= (7 , 2 ) else "interval"
111
+
112
+ class PRSearch (FacetedSearch ):
113
+ index = "test-prs"
114
+ doc_types = [PullRequest ]
115
+ facets = {
116
+ "comments" : NestedFacet (
117
+ "comments" ,
118
+ DateHistogramFacet (
119
+ field = "comments.created_at" , ** {interval_type : "month" }
120
+ ),
121
+ )
122
+ }
123
+
124
+ return PRSearch
98
125
99
126
100
127
def test_facet_with_custom_metric (data_client ):
@@ -106,36 +133,36 @@ def test_facet_with_custom_metric(data_client):
106
133
assert dates [0 ] == 1399038439000
107
134
108
135
109
- def test_nested_facet (pull_request ):
110
- prs = PRSearch ()
136
+ def test_nested_facet (pull_request , pr_search_cls ):
137
+ prs = pr_search_cls ()
111
138
r = prs .execute ()
112
139
113
140
assert r .hits .total .value == 1
114
141
assert [(datetime (2018 , 1 , 1 , 0 , 0 ), 1 , False )] == r .facets .comments
115
142
116
143
117
- def test_nested_facet_with_filter (pull_request ):
118
- prs = PRSearch (filters = {"comments" : datetime (2018 , 1 , 1 , 0 , 0 )})
144
+ def test_nested_facet_with_filter (pull_request , pr_search_cls ):
145
+ prs = pr_search_cls (filters = {"comments" : datetime (2018 , 1 , 1 , 0 , 0 )})
119
146
r = prs .execute ()
120
147
121
148
assert r .hits .total .value == 1
122
149
assert [(datetime (2018 , 1 , 1 , 0 , 0 ), 1 , True )] == r .facets .comments
123
150
124
- prs = PRSearch (filters = {"comments" : datetime (2018 , 2 , 1 , 0 , 0 )})
151
+ prs = pr_search_cls (filters = {"comments" : datetime (2018 , 2 , 1 , 0 , 0 )})
125
152
r = prs .execute ()
126
153
assert not r .hits
127
154
128
155
129
- def test_datehistogram_facet (data_client ):
130
- rs = RepoSearch ()
156
+ def test_datehistogram_facet (data_client , repo_search_cls ):
157
+ rs = repo_search_cls ()
131
158
r = rs .execute ()
132
159
133
160
assert r .hits .total .value == 1
134
161
assert [(datetime (2014 , 3 , 1 , 0 , 0 ), 1 , False )] == r .facets .created
135
162
136
163
137
- def test_boolean_facet (data_client ):
138
- rs = RepoSearch ()
164
+ def test_boolean_facet (data_client , repo_search_cls ):
165
+ rs = repo_search_cls ()
139
166
r = rs .execute ()
140
167
141
168
assert r .hits .total .value == 1
@@ -144,9 +171,8 @@ def test_boolean_facet(data_client):
144
171
assert value is True
145
172
146
173
147
- def test_empty_search_finds_everything (data_client ):
148
- cs = CommitSearch ()
149
-
174
+ def test_empty_search_finds_everything (data_client , es_version , commit_search_cls ):
175
+ cs = commit_search_cls ()
150
176
r = cs .execute ()
151
177
152
178
assert r .hits .total .value == 52
@@ -190,8 +216,10 @@ def test_empty_search_finds_everything(data_client):
190
216
] == r .facets .deletions
191
217
192
218
193
- def test_term_filters_are_shown_as_selected_and_data_is_filtered (data_client ):
194
- cs = CommitSearch (filters = {"files" : "test_elasticsearch_dsl" })
219
+ def test_term_filters_are_shown_as_selected_and_data_is_filtered (
220
+ data_client , commit_search_cls
221
+ ):
222
+ cs = commit_search_cls (filters = {"files" : "test_elasticsearch_dsl" })
195
223
196
224
r = cs .execute ()
197
225
@@ -234,16 +262,18 @@ def test_term_filters_are_shown_as_selected_and_data_is_filtered(data_client):
234
262
] == r .facets .deletions
235
263
236
264
237
- def test_range_filters_are_shown_as_selected_and_data_is_filtered (data_client ):
238
- cs = CommitSearch (filters = {"deletions" : "better" })
265
+ def test_range_filters_are_shown_as_selected_and_data_is_filtered (
266
+ data_client , commit_search_cls
267
+ ):
268
+ cs = commit_search_cls (filters = {"deletions" : "better" })
239
269
240
270
r = cs .execute ()
241
271
242
272
assert 19 == r .hits .total .value
243
273
244
274
245
- def test_pagination (data_client ):
246
- cs = CommitSearch ()
275
+ def test_pagination (data_client , commit_search_cls ):
276
+ cs = commit_search_cls ()
247
277
cs = cs [0 :20 ]
248
278
249
279
assert 52 == cs .count ()
0 commit comments