1 <?php |
1 <?php |
2 |
2 |
3 use Mockery as m; |
3 use Mockery as m; |
|
4 |
|
5 use Es; |
4 |
6 |
5 use EasyRdf\Literal; |
7 use EasyRdf\Literal; |
6 |
8 |
7 class DateStatsControllerTest extends TestCase |
9 class DateStatsControllerTest extends TestCase |
8 { |
10 { |
9 private $sparqlClient; |
11 private $sparqlClient; |
10 |
12 |
|
13 const ES_QUERY = [ |
|
14 'index' => 'corpus', |
|
15 'body' => [ |
|
16 "size" => 0, |
|
17 "query" => [ "match_all" => []], |
|
18 "aggs" => [ |
|
19 "datestats" => [ |
|
20 "nested"=> [ |
|
21 "path" => "creation_years" |
|
22 ], |
|
23 "aggs" => [ |
|
24 "years" => [ |
|
25 "terms"=> [ |
|
26 "field" => "creation_years.year", |
|
27 "size" => 0, |
|
28 "order" => [ |
|
29 "_term" => "asc" |
|
30 ] |
|
31 ], |
|
32 "aggs" => [ |
|
33 "year_count" => [ |
|
34 "sum" => [ |
|
35 "field" => "creation_years.weight" |
|
36 ] |
|
37 ] |
|
38 ] |
|
39 ] |
|
40 ] |
|
41 ] |
|
42 ] |
|
43 ] |
|
44 ]; |
|
45 |
11 public function setUp() { |
46 public function setUp() { |
12 |
47 |
13 parent::setup(); |
48 parent::setup(); |
14 |
|
15 // create a mock of the post repository interface and inject it into the |
|
16 // IoC container |
|
17 $this->sparqlClient = m::mock('CorpusParole\Libraries\Sparql\SparqlClient'); |
|
18 $this->app->instance('CorpusParole\Libraries\Sparql\SparqlClient', $this->sparqlClient); |
|
19 } |
49 } |
20 |
50 |
21 public function tearDown() { |
51 public function tearDown() { |
22 m::close(); |
52 m::close(); |
23 parent::tearDown(); |
53 parent::tearDown(); |
24 } |
54 } |
25 |
55 |
26 |
56 |
27 public function testIndexQuery() { |
57 public function testIndexQuery() { |
28 |
58 |
29 $query = preg_replace('/\s+/', ' ', "SELECT (?d as ?date) (COUNT(?d) AS ?count) |
59 Es::shouldReceive('search') |
30 WHERE { |
60 ->once() |
31 ?_ a <http://www.europeana.eu/schemas/edm/ProvidedCHO>. |
61 ->with(self::ES_QUERY) |
32 ?_ <http://purl.org/dc/terms/created> ?d |
62 ->andReturn(json_decode('{ |
33 } |
63 "took" : 132, |
34 GROUP BY ?d |
64 "timed_out" : false, |
35 ORDER BY ?d"); |
65 "_shards" : { |
36 |
66 "total" : 1, |
37 |
67 "successful" : 1, |
38 $this->sparqlClient |
68 "failed" : 0 |
39 ->shouldReceive('query') |
69 }, |
40 ->with($query) |
70 "hits" : { |
41 ->once() |
71 "total" : 3373, |
42 ->andReturn(new \ArrayIterator([])); |
72 "max_score" : 0.0, |
43 $this->get('/api/v1/stats/datestats/'); |
73 "hits" : [ ] |
44 $this->seeJsonEquals(["datestats" => []]); |
74 }, |
|
75 "aggregations" : { |
|
76 "datestats" : { |
|
77 "doc_count" : 3725, |
|
78 "years" : { |
|
79 "doc_count_error_upper_bound" : 0, |
|
80 "sum_other_doc_count" : 0, |
|
81 "buckets" : [] |
|
82 } |
|
83 } |
|
84 } |
|
85 }', true)); |
|
86 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
87 $this->seeJsonEquals(["datestats" => []]); |
45 } |
88 } |
46 |
89 |
47 public function testIndexMultiple() { |
90 public function testIndexResult() { |
48 |
91 |
49 $this->sparqlClient |
92 Es::shouldReceive('search') |
50 ->shouldReceive('query') |
93 ->once() |
51 ->once() |
94 ->with(self::ES_QUERY) |
52 ->andReturn(new \ArrayIterator([ |
95 ->andReturn(json_decode('{ |
53 (object)['date'=>new Literal('1975', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
96 "took" : 132, |
54 (object)['date'=>new Literal('1965', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(2)], |
97 "timed_out" : false, |
55 (object)['date'=>new Literal('1955', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(3)], |
98 "_shards" : { |
56 ])); |
99 "total" : 1, |
57 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
100 "successful" : 1, |
58 $this->seeJsonEquals(["datestats" => [ |
101 "failed" : 0 |
59 "1955" => 3, |
102 }, |
60 "1965" => 2, |
103 "hits" : { |
61 "1975" => 1, |
104 "total" : 3373, |
62 ]]); |
105 "max_score" : 0.0, |
|
106 "hits" : [ ] |
|
107 }, |
|
108 "aggregations" : { |
|
109 "datestats" : { |
|
110 "doc_count" : 3725, |
|
111 "years" : { |
|
112 "doc_count_error_upper_bound" : 0, |
|
113 "sum_other_doc_count" : 0, |
|
114 "buckets" : [ { |
|
115 "key" : 1948, |
|
116 "doc_count" : 3, |
|
117 "year_count" : { "value" : 3.0 } |
|
118 }, { |
|
119 "key" : 1957, |
|
120 "doc_count" : 29, |
|
121 "year_count" : { "value" : 29.0 } |
|
122 }, { |
|
123 "key" : 1963, |
|
124 "doc_count" : 22, |
|
125 "year_count" : { "value" : 21.5 } |
|
126 }, { |
|
127 "key" : 1970, |
|
128 "doc_count" : 411, |
|
129 "year_count" : { "value" : 403.68333334475756 } |
|
130 }, { |
|
131 "key" : 1986, |
|
132 "doc_count" : 68, |
|
133 "year_count" : { "value" : 14.133333388715982 } |
|
134 }, { |
|
135 "key" : 1996, |
|
136 "doc_count" : 40, |
|
137 "year_count" : { "value" : 36.05000001564622 } |
|
138 } ] |
|
139 } |
|
140 } |
|
141 } |
|
142 }', true)); |
|
143 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
144 $this->seeJsonEquals(["datestats" => [ |
|
145 "1948" => 3, |
|
146 "1957" => 29, |
|
147 "1963" => 22, |
|
148 "1970" => 404, |
|
149 "1986" => 14, |
|
150 "1996" => 36 |
|
151 ]]); |
63 } |
152 } |
64 |
153 |
65 public function testIndexSimple() { |
|
66 |
|
67 $this->sparqlClient |
|
68 ->shouldReceive('query') |
|
69 ->once() |
|
70 ->andReturn(new \ArrayIterator([ |
|
71 (object)['date'=>new Literal('1955', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
72 (object)['date'=>new Literal('1965', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
73 (object)['date'=>new Literal('1975', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
74 ])); |
|
75 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
76 $this->seeJsonEquals(["datestats" => [ |
|
77 "1955" => 1, |
|
78 "1965" => 1, |
|
79 "1975" => 1, |
|
80 ]]); |
|
81 } |
|
82 |
|
83 public function testIndexPeriod() { |
|
84 |
|
85 $this->sparqlClient |
|
86 ->shouldReceive('query') |
|
87 ->once() |
|
88 ->andReturn(new \ArrayIterator([ |
|
89 (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)], |
|
90 ])); |
|
91 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
92 $this->seeJsonEquals(["datestats" => [ |
|
93 "1955" => 1, |
|
94 "1956" => 1, |
|
95 "1957" => 1, |
|
96 "1958" => 1, |
|
97 "1959" => 1, |
|
98 "1960" => 1, |
|
99 "1961" => 1, |
|
100 "1962" => 1, |
|
101 "1963" => 1, |
|
102 "1964" => 1, |
|
103 "1965" => 1, |
|
104 ]]); |
|
105 } |
|
106 |
|
107 public function testIndexPeriodRemainMore() { |
|
108 |
|
109 $this->sparqlClient |
|
110 ->shouldReceive('query') |
|
111 ->once() |
|
112 ->andReturn(new \ArrayIterator([ |
|
113 (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(15)], |
|
114 ])); |
|
115 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
116 $this->seeJsonEquals(["datestats" => [ |
|
117 "1955" => 2, |
|
118 "1956" => 2, |
|
119 "1957" => 2, |
|
120 "1958" => 2, |
|
121 "1959" => 1, |
|
122 "1960" => 1, |
|
123 "1961" => 1, |
|
124 "1962" => 1, |
|
125 "1963" => 1, |
|
126 "1964" => 1, |
|
127 "1965" => 1, |
|
128 ]]); |
|
129 } |
|
130 |
|
131 public function testIndexPeriodRemainLess() { |
|
132 |
|
133 $this->sparqlClient |
|
134 ->shouldReceive('query') |
|
135 ->once() |
|
136 ->andReturn(new \ArrayIterator([ |
|
137 (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(10)], |
|
138 ])); |
|
139 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
140 $this->seeJsonEquals(["datestats" => [ |
|
141 "1955" => 1, |
|
142 "1956" => 1, |
|
143 "1957" => 1, |
|
144 "1958" => 1, |
|
145 "1959" => 1, |
|
146 "1960" => 1, |
|
147 "1961" => 1, |
|
148 "1962" => 1, |
|
149 "1963" => 1, |
|
150 "1964" => 1, |
|
151 ]]); |
|
152 } |
|
153 |
|
154 public function testIndexMix() { |
|
155 |
|
156 $this->sparqlClient |
|
157 ->shouldReceive('query') |
|
158 ->once() |
|
159 ->andReturn(new \ArrayIterator([ |
|
160 (object)['date'=>new Literal('start=1955; end=1965', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)], |
|
161 (object)['date'=>new Literal('1960', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(2)], |
|
162 ])); |
|
163 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
164 $this->seeJsonEquals(["datestats" => [ |
|
165 "1955" => 1, |
|
166 "1956" => 1, |
|
167 "1957" => 1, |
|
168 "1958" => 1, |
|
169 "1959" => 1, |
|
170 "1960" => 3, |
|
171 "1961" => 1, |
|
172 "1962" => 1, |
|
173 "1963" => 1, |
|
174 "1964" => 1, |
|
175 "1965" => 1, |
|
176 ]]); |
|
177 } |
|
178 |
|
179 public function testIndexBadDate() { |
|
180 |
|
181 $this->sparqlClient |
|
182 ->shouldReceive('query') |
|
183 ->once() |
|
184 ->andReturn(new \ArrayIterator([ |
|
185 (object)['date'=>new Literal('1955', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
186 (object)['date'=>new Literal('HELLO', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
187 (object)['date'=>new Literal('1975', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
188 ])); |
|
189 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
190 $this->seeJsonEquals(["datestats" => [ |
|
191 "1955" => 1, |
|
192 "1975" => 1, |
|
193 ]]); |
|
194 } |
|
195 |
|
196 public function testIndexBadPeriod() { |
|
197 |
|
198 $this->sparqlClient |
|
199 ->shouldReceive('query') |
|
200 ->once() |
|
201 ->andReturn(new \ArrayIterator([ |
|
202 (object)['date'=>new Literal('start=1955; end=FOO', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)], |
|
203 ])); |
|
204 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
205 $this->seeJsonEquals(["datestats" => [ |
|
206 ]]); |
|
207 } |
|
208 |
|
209 public function testIndexBadPeriodMissing() { |
|
210 |
|
211 $this->sparqlClient |
|
212 ->shouldReceive('query') |
|
213 ->once() |
|
214 ->andReturn(new \ArrayIterator([ |
|
215 (object)['date'=>new Literal('start=1955', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)], |
|
216 ])); |
|
217 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
218 $this->seeJsonEquals(["datestats" => [ |
|
219 ]]); |
|
220 } |
|
221 |
|
222 public function testIndexFullPeriod() { |
|
223 |
|
224 $this->sparqlClient |
|
225 ->shouldReceive('query') |
|
226 ->once() |
|
227 ->andReturn(new \ArrayIterator([ |
|
228 (object)['date'=>new Literal('start=1955; end=1965; scheme=v3; name=v4;', null, "http://purl.org/dc/terms/Period"), 'count' => Literal::create(11)], |
|
229 ])); |
|
230 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
231 $this->seeJsonEquals(["datestats" => [ |
|
232 "1955" => 1, |
|
233 "1956" => 1, |
|
234 "1957" => 1, |
|
235 "1958" => 1, |
|
236 "1959" => 1, |
|
237 "1960" => 1, |
|
238 "1961" => 1, |
|
239 "1962" => 1, |
|
240 "1963" => 1, |
|
241 "1964" => 1, |
|
242 "1965" => 1, |
|
243 ]]); |
|
244 } |
|
245 |
|
246 public function testIndexMultipleFormat() { |
|
247 |
|
248 $this->sparqlClient |
|
249 ->shouldReceive('query') |
|
250 ->once() |
|
251 ->andReturn(new \ArrayIterator([ |
|
252 (object)['date'=>new Literal('1975-02-05', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(1)], |
|
253 (object)['date'=>new Literal('1965-03', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(2)], |
|
254 (object)['date'=>new Literal('1955-02-12T08:30:00+00:00', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(3)], |
|
255 (object)['date'=>new Literal('1950-08-18T08:30:00Z', null, "http://purl.org/dc/terms/W3CDTF"), 'count' => Literal::create(4)], |
|
256 ])); |
|
257 $this->get('/api/v1/stats/datestats/')->assertTrue($this->response->isOk(), $this->response->content()); |
|
258 $this->seeJsonEquals(["datestats" => [ |
|
259 "1950" => 4, |
|
260 "1955" => 3, |
|
261 "1965" => 2, |
|
262 "1975" => 1, |
|
263 ]]); |
|
264 } |
|
265 |
|
266 |
|
267 |
|
268 } |
154 } |