-
Notifications
You must be signed in to change notification settings - Fork 731
/
DateHistogramTest.php
178 lines (146 loc) · 4.83 KB
/
DateHistogramTest.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Elastica\Test\Aggregation;
use Elastica\Aggregation\DateHistogram;
use Elastica\Document;
use Elastica\Index;
use Elastica\Mapping;
use Elastica\Query;
/**
* @internal
*/
class DateHistogramTest extends BaseAggregationTest
{
/**
* @group functional
*/
public function testDateHistogramAggregation(): void
{
$this->_checkVersion('7.2');
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1h');
$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
$docCount = 0;
$nonDocCount = 0;
foreach ($results['buckets'] as $bucket) {
if (1 == $bucket['doc_count']) {
++$docCount;
} else {
++$nonDocCount;
}
}
// 3 Documents that were added
$this->assertEquals(3, $docCount);
// 1 document that was generated in between for the missing hour
$this->assertEquals(1, $nonDocCount);
}
/**
* @group unit
* @group legacy
*/
public function testDateHistogramAggregationWithIntervalTriggersADeprecation(): void
{
$this->expectDeprecation('Since ruflin/elastica 7.1.0: Argument 3 passed to "__construct()" is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.');
new DateHistogram('hist', 'created', 'day');
}
/**
* @group unit
* @group legacy
*/
public function testDateHistogramAggregationSetIntervalTriggersADeprecation(): void
{
$agg = new DateHistogram('hist', 'created');
$this->expectDeprecation('Since ruflin/elastica 7.1.0: The "setInterval()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.');
$agg->setInterval('day');
}
/**
* @group functional
*/
public function testDateHistogramCalendarAggregation(): void
{
$this->_checkVersion('7.2');
$agg = new DateHistogram('hist', 'created');
$agg->setCalendarInterval('1h');
$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
$docCount = 0;
$nonDocCount = 0;
foreach ($results['buckets'] as $bucket) {
if (1 == $bucket['doc_count']) {
++$docCount;
} else {
++$nonDocCount;
}
}
// 3 Documents that were added
$this->assertEquals(3, $docCount);
// 1 document that was generated in between for the missing hour
$this->assertEquals(1, $nonDocCount);
}
/**
* @group unit
*/
public function testSetOffset(): void
{
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1h');
$agg->setOffset('3m');
$expected = [
'date_histogram' => [
'field' => 'created',
'offset' => '3m',
'fixed_interval' => '1h',
],
];
$this->assertEquals($expected, $agg->toArray());
$this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m'));
}
/**
* @group functional
*/
public function testSetOffsetWorks(): void
{
$this->_checkVersion('7.2');
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1m');
$agg->setOffset('+40s');
$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
$this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
}
/**
* @group unit
*/
public function testSetTimezone(): void
{
$agg = new DateHistogram('hist', 'created');
$agg->setFixedInterval('1h');
$agg->setTimezone('-02:30');
$expected = [
'date_histogram' => [
'field' => 'created',
'time_zone' => '-02:30',
'fixed_interval' => '1h',
],
];
$this->assertEquals($expected, $agg->toArray());
$this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30'));
}
protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();
$index->setMapping(new Mapping([
'created' => ['type' => 'date'],
]));
$index->addDocuments([
new Document(1, ['created' => '2014-01-29T00:20:00']),
new Document(2, ['created' => '2014-01-29T02:20:00']),
new Document(3, ['created' => '2014-01-29T03:20:00']),
]);
$index->refresh();
return $index;
}
}