blob: 864a8ca7f203c79eb7cb057e3b2385722bac5211 [file] [log] [blame]
hebasta5e4d7542018-06-19 16:57:36 +02001/**
2 * Test suite for corpus statistic
3 *
4 * @author Helge Stallkamp
Akron18a99c02018-08-20 12:55:14 +02005 * @author Nils Diewald
hebasta5e4d7542018-06-19 16:57:36 +02006 */
7
8
hebasta6c8f09d2018-07-24 13:21:36 +02009define(['vc', 'vc/statistic', 'view/corpstatv'], function(vcClass, statClass, corpStatVClass){
hebastaa0282be2018-12-05 16:58:00 +010010
hebasta47d03a42018-06-25 10:31:58 +020011 var json = {
Akron18a99c02018-08-20 12:55:14 +020012 "@type":"koral:docGroup",
13 "operation":"operation:or",
14 "operands":[
15 {
16 "@type":"koral:docGroup",
17 "operation":"operation:and",
18 "operands":[
19 {
20 "@type":"koral:doc",
21 "key":"title",
22 "value":"Der Birnbaum",
23 "match":"match:eq"
24 },
25 {
26 "@type":"koral:doc",
27 "key":"pubPlace",
28 "value":"Mannheim",
29 "type" : "type:regex",
30 "match":"match:contains"
31 },
32 {
33 "@type":"koral:docGroup",
34 "operation":"operation:or",
35 "operands":[
36 {
37 "@type":"koral:doc",
38 "key":"subTitle",
39 "value":"Aufzucht und Pflege",
40 "match":"match:eq"
41 },
42 {
43 "@type":"koral:doc",
44 "key":"subTitle",
45 "value":"Gedichte",
46 "match":"match:eq",
47 "rewrites" : [
48 {
49 "@type": "koral:rewrite",
50 "src" : "policy",
51 "operation" : "operation:injection",
52 }
53 ]
54 }
55 ]
56 }
57 ]
58 },
59 {
60 "@type":"koral:doc",
61 "key":"pubDate",
62 "type":"type:date",
63 "value":"2015-03-05",
64 "match":"match:geq"
65 }
66 ]
67 };
hebasta5e4d7542018-06-19 16:57:36 +020068
hebasta47d03a42018-06-25 10:31:58 +020069 var preDefinedStat={
Akron18a99c02018-08-20 12:55:14 +020070 "documents":12,
71 "tokens":2323,
72 "sentences":343434,
73 "paragraphs":45454545
hebasta47d03a42018-06-25 10:31:58 +020074 };
75
76 KorAP.API.getCorpStat = function(collQu, cb){
77 return cb(preDefinedStat);
Akron18a99c02018-08-20 12:55:14 +020078 };
79
hebastaa0282be2018-12-05 16:58:00 +010080
81 generateCorpusDocGr = function(){
82 let vc = vcClass.create().fromJson({
83 "@type" : 'koral:docGroup',
84 'operation' : 'operation:or',
85 'operands' : [
86 {
87 '@type' : 'koral:doc',
88 'key' : 'title',
89 'match': 'match:eq',
90 'value' : 'Hello World!'
91 },
92 {
93 '@type' : 'koral:doc',
94 'match': 'match:eq',
95 'key' : 'foo',
96 'value' : 'bar'
97 }
98 ]
99 });
100 return vc;
101 }
102
103 generateCorpusDoc = function(){
104 let vc= vcClass.create().fromJson({
105 '@type' : 'koral:doc',
106 'key' : 'title',
107 'match': 'match:eq',
108 'value' : 'Hello World!',
109 'type' : 'type:string'
110 });
111 return vc;
112 };
113
114
115 /**
116 * Generate vc with docgroupref
117 */
118 generateCorpusRef = function(){
119 let vc = vcClass.create().fromJson({
120 "@type" : "koral:docGroupRef",
121 "ref" : "@max/myCorpus"
122 });
123 return vc;
124 };
125
126
127 /**
128 * Checks is corpus statistic is active
129 */
130 checkStatActive = function(view, div){
131 // corpus statistic exists, it is active and reloadStatButton is shown
132 if ((view.firstChild.classList.contains("stattable") === true)
hebasta48842cf2018-12-11 12:57:38 +0100133 && (view.firstChild.classList.contains("stdisabled") === false)
hebastaa0282be2018-12-05 16:58:00 +0100134 && (div.getElementsByClassName("reloadStatB").length == 0) ) {
135 return true;
136 }
137 return false;
138 };
139
140
141 /**
142 * Checks if corpus statistic is disabled
143 */
144 checkStatDisabled = function(view, div){
145 if ((view.firstChild.classList.contains("stattable") === true)
hebasta48842cf2018-12-11 12:57:38 +0100146 && (view.firstChild.classList.contains("stdisabled") === true)
hebastaa0282be2018-12-05 16:58:00 +0100147 && (div.getElementsByClassName("reloadStatB").length === 1) ) {
148 return true;
149 }
150 return false;
151 };
152
153
154 describe('KorAP.CorpusStat', function(){
hebasta47d03a42018-06-25 10:31:58 +0200155
hebasta5e4d7542018-06-19 16:57:36 +0200156 it('should be initiable', function(){
Akron18a99c02018-08-20 12:55:14 +0200157 var stat = statClass.create(preDefinedStat);
hebasta5e4d7542018-06-19 16:57:36 +0200158 expect( function() { statClass.create() }).toThrow(new Error("Missing parameter"));
159 });
160
161
Akron18a99c02018-08-20 12:55:14 +0200162 it('should be parsed in a statistic view and displayed as HTML Description List', function(){
163 var stat = statClass.create(preDefinedStat);
hebastaa0282be2018-12-05 16:58:00 +0100164 var descL = stat.element();
hebasta5e4d7542018-06-19 16:57:36 +0200165 expect(descL.tagName).toEqual('DL');
166 expect(descL.children[0].tagName).toEqual('DIV');
167 expect(descL.children[0].children[0].tagName).toEqual('DT');
168 expect(descL.children[0].children[0].attributes[0].name).toEqual('title');
169 expect(descL.children[0].children[1].tagName).toEqual('DD');
Akron18a99c02018-08-20 12:55:14 +0200170
hebasta5e4d7542018-06-19 16:57:36 +0200171 expect(descL.children[0].children[0].firstChild.nodeValue).toEqual('documents');
172 expect(descL.children[0].children[1].firstChild.nodeValue).toEqual('12');
173 expect(descL.children[0].children[0].attributes[0].value).toEqual('documents');
Akron2f979122018-07-25 17:00:23 +0200174
175 expect(descL.children[1].children[0].firstChild.nodeValue).toEqual('tokens');
176 expect(descL.children[1].children[1].firstChild.nodeValue).toEqual(new Number(2323).toLocaleString());
177 expect(descL.children[1].children[0].attributes[0].value).toEqual('tokens');
178 });
hebasta5e4d7542018-06-19 16:57:36 +0200179
hebasta47d03a42018-06-25 10:31:58 +0200180
hebasta6c8f09d2018-07-24 13:21:36 +0200181 it('should display corpus statistic after creating a corpus statistic view', function(){
Akron18a99c02018-08-20 12:55:14 +0200182 var vc = vcClass.create([
183 ['title', 'string'],
184 ['subTitle', 'string'],
185 ['pubDate', 'date'],
186 ['author', 'text']
187 ]).fromJson(json);
188
hebastaa0282be2018-12-05 16:58:00 +0100189 KorAP.vc = vc;
190
Akron18a99c02018-08-20 12:55:14 +0200191 statView = corpStatVClass.create(vc);
hebastaa0282be2018-12-05 16:58:00 +0100192 // corpStatVClass.show(vc);
Akron18a99c02018-08-20 12:55:14 +0200193
hebasta47d03a42018-06-25 10:31:58 +0200194 var testDiv = document.createElement('div');
hebasta6c8f09d2018-07-24 13:21:36 +0200195 testDiv.appendChild(statView.show());
hebastaa0282be2018-12-05 16:58:00 +0100196 // statClass.showCorpStat(testDiv, vc);
hebasta47d03a42018-06-25 10:31:58 +0200197
198 expect(testDiv.children[0].tagName).toEqual('DIV');
199 expect(testDiv.children[0].getAttribute("class")).toEqual('stattable');
200 expect(testDiv.children[0].children[0].tagName).toEqual('DL');
201 expect(testDiv.children[0].children[0].children[0].children[0].firstChild.nodeValue).toEqual('documents');
202 expect(testDiv.children[0].children[0].children[0].children[1].firstChild.nodeValue).toEqual('12');
hebasta47d03a42018-06-25 10:31:58 +0200203 });
Akron18a99c02018-08-20 12:55:14 +0200204
205 it('should display the statistics button in a panel', function () {
206 var vc = vcClass.create([
207 ['title', 'string'],
208 ['subTitle', 'string'],
209 ['pubDate', 'date'],
210 ['author', 'text']
211 ]).fromJson(json);
212
213 var show = document.createElement('div');
214
215 show.appendChild(vc.element());
216
217 var panel = show.firstChild.lastChild.firstChild;
218 expect(panel.classList.contains('panel')).toBeTruthy();
219 expect(panel.classList.contains('vcinfo')).toBeTruthy();
220 expect(panel.lastChild.classList.contains('button-group')).toBeTruthy();
221 expect(panel.lastChild.classList.contains('vcinfo')).toBeTruthy();
222 expect(panel.lastChild.children[0].tagName).toEqual('SPAN');
223 expect(panel.lastChild.children[0].textContent).toEqual('Statistics');
224 });
225
226
227 it('should display the statistics button in a panel after VC modification', function () {
228
229 var vc = vcClass.create([
230 ['title', 'string'],
231 ['subTitle', 'string'],
232 ['pubDate', 'date'],
233 ['author', 'text']
234 ]).fromJson(json);
235
236 var show = document.createElement('div');
237
238 show.appendChild(vc.element());
239
240 expect(show.querySelector(".statistic").tagName).toEqual("SPAN");
241
Akronadab5e52018-08-20 13:50:53 +0200242 var and = vc.builder().lastChild.firstChild;
Akron18a99c02018-08-20 12:55:14 +0200243
244 // Click on and() in VC
245 and.click();
246
247 // Check that statistics is there
248 expect(show.querySelector(".statistic").tagName).toEqual("SPAN");
249 });
250
251
252 it('should display and hide corpus statistics view in the panel', function () {
253
254 var vc = vcClass.create([
255 ['title', 'string'],
256 ['subTitle', 'string'],
257 ['pubDate', 'date'],
258 ['author', 'text']
259 ]).fromJson(json);
260
261 var show = document.createElement('div');
262
263 show.appendChild(vc.element());
264
265 var panel = show.firstChild.lastChild.firstChild;
266
267 // Show statistics
268 panel.lastChild.children[0].click();
269
270 // Statistics view
271 var viewE = panel.firstChild.firstChild;
272
273 expect(viewE.tagName).toEqual("DIV");
274 expect(viewE.classList.contains("view")).toBeTruthy();
275 expect(viewE.classList.contains("vcstatistic")).toBeTruthy();
276
277 expect(viewE.firstChild.classList.contains("stattable")).toBeTruthy();
278 expect(viewE.firstChild.firstChild.tagName).toEqual("DL");
279
280 // List of statistic values
281 var dl = viewE.firstChild.firstChild.firstChild;
282 expect(dl.firstChild.tagName).toEqual("DT");
283 expect(dl.firstChild.textContent).toEqual("documents");
284 expect(dl.lastChild.tagName).toEqual("DD");
285 expect(dl.lastChild.textContent).toEqual("12");
286
287 expect(viewE.children.length).toEqual(2);
288
289 expect(viewE.lastChild.classList.contains("button-group")).toBeTruthy();
290 expect(viewE.lastChild.children.length).toEqual(1);
291 expect(viewE.lastChild.firstChild.tagName).toEqual("SPAN");
292 expect(viewE.lastChild.firstChild.textContent).toEqual("Close");
293
294 // Close view
295 viewE.lastChild.firstChild.firstChild.click();
296
297 // Is gone
298 expect(panel.firstChild.children.length).toEqual(0);
299
300 // Show statistics
301 panel.lastChild.children[0].click();
302
303 // Is there
304 expect(panel.firstChild.children.length).toEqual(1);
305
306 // Only show once
307 panel.lastChild.children[0].click();
308 expect(panel.firstChild.children.length).toEqual(1);
309 });
hebastaa0282be2018-12-05 16:58:00 +0100310
311
312 });
313
314
315
316 /**
317 * Test disabling and reload of corpus statistic if vc is changed
318 * in vc builder through user-interaction
319 */
320 describe ('KorAP.CorpusStat.Disable', function(){
hebasta48842cf2018-12-11 12:57:38 +0100321
322 document.addEventListener('vcChange', function (e) {
323 KorAP.vc.checkStatActive(e.detail);
324 }, false);
325
hebastaa0282be2018-12-05 16:58:00 +0100326 /**
327 * If the user defines a new vc, the statistic should be disabled,
328 * because it is out-of-date.
329 *
330 * The user can choose to display an up-to-date corpus statistic. Here it is tested
331 * if corpus statistic is disabled after a valid change of corpus statistic and if the corpus statistic is updatable.
332 */
333 it ('should disable the corpus statistic if corpus definition is changed and display a functional reload button', function(){
334
335 KorAP.vc = generateCorpusDocGr();
336
337 //Show corpus statistic
338 let show = document.createElement('div');
339 show.appendChild(KorAP.vc.element());
340 let panel = show.firstChild.lastChild.firstChild;
341 panel.lastChild.children[0].click();
342 let view = panel.firstChild.firstChild;
343
344 //corpus statistic is active
345 expect(checkStatActive(view, show)).toBe(true);
346
347 //change vc, a line in vc builder is deleted
348 KorAP._delete.apply(KorAP.vc.root().getOperand(0));
349 expect(checkStatDisabled(view,show)).toBe(true);
350
351 //click at reload button
352 let rlbutton = show.getElementsByClassName("refresh").item(0);
353 rlbutton.click();
354
355 expect(checkStatActive(view,show)).toBe(true);
356 });
357
358
359 it('should disable corpus statistic if entries in vc builder are deleted', function(){
360 KorAP.vc = generateCorpusDocGr();
361
362 // create corpus builder and corpus statistic;
363 let show = document.createElement('div');
364 show.appendChild(KorAP.vc.element());
365 let panel = show.firstChild.lastChild.firstChild;
366 panel.lastChild.children[0].click();
367 let view = panel.firstChild.firstChild;
368
369 expect(checkStatActive(view, show)).toBe(true);
370
371 //delete foo=bar
372 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
373 expect(checkStatDisabled(view, show)).toBe(true);
374
375 //refresh corpus statistic
376 let rlbutton = show.getElementsByClassName("refresh").item(0);
377 rlbutton.click();
378 expect(checkStatActive(view,show)).toBe(true);
hebasta3f4be922018-12-11 10:41:46 +0100379
hebastaa0282be2018-12-05 16:58:00 +0100380 KorAP._delete.apply(KorAP.vc.root());
hebasta3f4be922018-12-11 10:41:46 +0100381 view = panel.firstChild.firstChild;
382 expect(checkStatDisabled(view, show)).toBe(true);
383
hebastaa0282be2018-12-05 16:58:00 +0100384 });
385
386
387 it('should disable corpus statistic if key, matchoperator or value is changed', function(){
388 /*
389 * Doc change of key, match operator and value
390 */
391 KorAP.vc= generateCorpusDoc();
392 // show vc builder and open corpus statistic
393 let show = document.createElement('div');
394 show.appendChild(KorAP.vc.element());
395 let panel = show.firstChild.lastChild.firstChild;
396 panel.lastChild.children[0].click();
397 let view = panel.firstChild.firstChild;
398 expect(checkStatActive(view, show)).toBe(true);
399
400 KorAP.vc.root().matchop("ne").update();
401 expect(checkStatDisabled(view, show)).toBe(true);
402
403 let rlbutton = show.getElementsByClassName("refresh").item(0);
404 rlbutton.click();
405
406 view = panel.firstChild.firstChild;
407 expect(checkStatActive(view, show)).toBe(true);
408 KorAP.vc.root().value("Hello tester").update();
409 expect(checkStatDisabled(view, show)).toBe(true);
410
411 //refresh corpus statistic
412 rlbutton = show.getElementsByClassName("refresh").item(0);
413 rlbutton.click();
414 view = panel.firstChild.firstChild;
415 expect(checkStatActive(view, show)).toBe(true);
416
417 KorAP.vc.root().key("author").update();
418 expect(checkStatDisabled(view, show)).toBe(true);
419
420
421 /*
422 * DocGroupRef change of value...
423 */
424 KorAP.vc = generateCorpusRef();
425 show = document.createElement('div');
426 show.appendChild(KorAP.vc.element());
427 panel = show.firstChild.lastChild.firstChild;
428 panel.lastChild.children[0].click();
429 view = panel.firstChild.firstChild;
430 expect(checkStatActive(view, show)).toBe(true);
431
432 KorAP.vc.root().ref("@anton/secondCorpus").update();
433 expect(checkStatDisabled(view, show)).toBe(true);
434 });
435
436
437 it('should not disable corpus statistic if docgroup definition is incomplete', function(){
438
439 KorAP.vc = generateCorpusDocGr();
440
441 //Show corpus statistic
442 let show = document.createElement('div');
443 show.appendChild(KorAP.vc.element());
444 let panel = show.firstChild.lastChild.firstChild;
445 panel.lastChild.children[0].click();
446 let view = panel.firstChild.firstChild;
447
448 expect(checkStatActive(view, show)).toBe(true);
449
450 KorAP._and.apply(KorAP.vc.root());
451
452 let andbuild = show.getElementsByClassName("builder");
453 expect(andbuild[0].firstChild.classList.contains('docGroup')).toBeTruthy();
454 expect(andbuild[0].firstChild.getAttribute("data-operation")).toEqual("and");
455 expect(checkStatActive(view, show)).toBe(true);
456 });
457
458
459 it('should not disable corpus statistic if doc/docref definition is incomplete', function(){
460
461 /*
462 * DOC incomplete
463 */
464 KorAP.vc = vcClass.create().fromJson();
465 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
466
467 // show vc builder and open corpus statistic
468 let show = document.createElement('div');
469 show.appendChild(KorAP.vc.element());
470 let panel = show.firstChild.lastChild.firstChild;
471 panel.lastChild.children[0].click();
472 let view = panel.firstChild.firstChild;
473
474 // corpus statistic should be shown and be up-to-date, reload button is not shown
475 expect(checkStatActive(view, show)).toBe(true);
476
477 // open the menu
478 KorAP.vc.builder().firstChild.firstChild.click();
479 KorAP._vcKeyMenu._prefix.add("author");
480 let prefElement = KorAP.vc.builder().querySelector('span.pref');
481 // add key 'author' to VC
482 prefElement.click();
483
484 expect(checkStatActive(view, show)).toBe(true);
485
486
487 /*
488 * DOCREF incomplete
489 */
490 KorAP.vc = vcClass.create().fromJson();
491 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
492
493 // show vc builder and open corpus statistic
494 show = document.createElement('div');
495 show.appendChild(KorAP.vc.element());
496 panel = show.firstChild.lastChild.firstChild;
497 panel.lastChild.children[0].click();
498 view = panel.firstChild.firstChild;
499 expect(checkStatActive(view, show)).toBe(true);
500
501 KorAP.vc.builder().firstChild.firstChild.click();
502 KorAP._vcKeyMenu._prefix.add("referTo");
503 prefElement = KorAP.vc.builder().querySelector('span.pref');
504 prefElement.click();
505 expect(checkStatActive(view, show)).toBe(true);
506 });
507 });
hebasta5e4d7542018-06-19 16:57:36 +0200508});