blob: 25e55a2905fdd020f41ec1e27967a717084d02d0 [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
hebastade595b42019-02-05 13:53:10 +0100103 /**
104 * Generate corpus doc group with more entries
105 */
106 generateBCorpusDocGr = function(){
107 let vc = vcClass.create().fromJson({
108 "@type": "koral:docGroup",
109 "operation": "operation:or",
110 "operands": [{
111 "@type" : 'koral:docGroup',
112 'operation' : 'operation:or',
113 'operands' : [
114 {
115 '@type' : 'koral:doc',
116 'key' : 'title',
117 'match': 'match:eq',
118 'value' : 'Hello World!'
119 },
120 {
121 '@type' : 'koral:doc',
122 'match': 'match:eq',
123 'key' : 'foo',
124 'value' : 'bar'
125 }
126 ]
127 },
128 {
129 '@type' : 'koral:doc',
130 'match': 'match:eq',
131 'key' : 'author',
132 'value' : 'Goethe'
133 }
134 ]
135 });
136
137 return vc;
138 }
139
hebastaa0282be2018-12-05 16:58:00 +0100140 generateCorpusDoc = function(){
141 let vc= vcClass.create().fromJson({
142 '@type' : 'koral:doc',
143 'key' : 'title',
144 'match': 'match:eq',
145 'value' : 'Hello World!',
146 'type' : 'type:string'
147 });
148 return vc;
149 };
150
151
152 /**
153 * Generate vc with docgroupref
154 */
155 generateCorpusRef = function(){
156 let vc = vcClass.create().fromJson({
157 "@type" : "koral:docGroupRef",
158 "ref" : "@max/myCorpus"
159 });
160 return vc;
161 };
162
163
164 /**
165 * Checks is corpus statistic is active
166 */
167 checkStatActive = function(view, div){
168 // corpus statistic exists, it is active and reloadStatButton is shown
169 if ((view.firstChild.classList.contains("stattable") === true)
hebasta48842cf2018-12-11 12:57:38 +0100170 && (view.firstChild.classList.contains("stdisabled") === false)
hebastaa0282be2018-12-05 16:58:00 +0100171 && (div.getElementsByClassName("reloadStatB").length == 0) ) {
172 return true;
173 }
174 return false;
175 };
176
177
178 /**
179 * Checks if corpus statistic is disabled
180 */
181 checkStatDisabled = function(view, div){
182 if ((view.firstChild.classList.contains("stattable") === true)
hebasta48842cf2018-12-11 12:57:38 +0100183 && (view.firstChild.classList.contains("stdisabled") === true)
hebastaa0282be2018-12-05 16:58:00 +0100184 && (div.getElementsByClassName("reloadStatB").length === 1) ) {
185 return true;
186 }
187 return false;
188 };
189
190
191 describe('KorAP.CorpusStat', function(){
hebasta47d03a42018-06-25 10:31:58 +0200192
hebasta5e4d7542018-06-19 16:57:36 +0200193 it('should be initiable', function(){
Akron18a99c02018-08-20 12:55:14 +0200194 var stat = statClass.create(preDefinedStat);
hebasta5e4d7542018-06-19 16:57:36 +0200195 expect( function() { statClass.create() }).toThrow(new Error("Missing parameter"));
196 });
197
198
Akron18a99c02018-08-20 12:55:14 +0200199 it('should be parsed in a statistic view and displayed as HTML Description List', function(){
200 var stat = statClass.create(preDefinedStat);
hebastaa0282be2018-12-05 16:58:00 +0100201 var descL = stat.element();
hebasta5e4d7542018-06-19 16:57:36 +0200202 expect(descL.tagName).toEqual('DL');
203 expect(descL.children[0].tagName).toEqual('DIV');
204 expect(descL.children[0].children[0].tagName).toEqual('DT');
205 expect(descL.children[0].children[0].attributes[0].name).toEqual('title');
206 expect(descL.children[0].children[1].tagName).toEqual('DD');
Akron18a99c02018-08-20 12:55:14 +0200207
hebasta5e4d7542018-06-19 16:57:36 +0200208 expect(descL.children[0].children[0].firstChild.nodeValue).toEqual('documents');
209 expect(descL.children[0].children[1].firstChild.nodeValue).toEqual('12');
210 expect(descL.children[0].children[0].attributes[0].value).toEqual('documents');
Akron2f979122018-07-25 17:00:23 +0200211
212 expect(descL.children[1].children[0].firstChild.nodeValue).toEqual('tokens');
213 expect(descL.children[1].children[1].firstChild.nodeValue).toEqual(new Number(2323).toLocaleString());
214 expect(descL.children[1].children[0].attributes[0].value).toEqual('tokens');
215 });
hebasta5e4d7542018-06-19 16:57:36 +0200216
hebasta47d03a42018-06-25 10:31:58 +0200217
hebasta6c8f09d2018-07-24 13:21:36 +0200218 it('should display corpus statistic after creating a corpus statistic view', function(){
Akron18a99c02018-08-20 12:55:14 +0200219 var vc = vcClass.create([
220 ['title', 'string'],
221 ['subTitle', 'string'],
222 ['pubDate', 'date'],
223 ['author', 'text']
224 ]).fromJson(json);
225
hebastaa0282be2018-12-05 16:58:00 +0100226 KorAP.vc = vc;
227
Akron18a99c02018-08-20 12:55:14 +0200228 statView = corpStatVClass.create(vc);
hebastaa0282be2018-12-05 16:58:00 +0100229 // corpStatVClass.show(vc);
Akron18a99c02018-08-20 12:55:14 +0200230
hebasta47d03a42018-06-25 10:31:58 +0200231 var testDiv = document.createElement('div');
hebasta6c8f09d2018-07-24 13:21:36 +0200232 testDiv.appendChild(statView.show());
hebastaa0282be2018-12-05 16:58:00 +0100233 // statClass.showCorpStat(testDiv, vc);
hebasta47d03a42018-06-25 10:31:58 +0200234
235 expect(testDiv.children[0].tagName).toEqual('DIV');
236 expect(testDiv.children[0].getAttribute("class")).toEqual('stattable');
237 expect(testDiv.children[0].children[0].tagName).toEqual('DL');
238 expect(testDiv.children[0].children[0].children[0].children[0].firstChild.nodeValue).toEqual('documents');
239 expect(testDiv.children[0].children[0].children[0].children[1].firstChild.nodeValue).toEqual('12');
hebasta47d03a42018-06-25 10:31:58 +0200240 });
Akron18a99c02018-08-20 12:55:14 +0200241
242 it('should display the statistics button in a panel', function () {
243 var vc = vcClass.create([
244 ['title', 'string'],
245 ['subTitle', 'string'],
246 ['pubDate', 'date'],
247 ['author', 'text']
248 ]).fromJson(json);
249
250 var show = document.createElement('div');
251
252 show.appendChild(vc.element());
253
254 var panel = show.firstChild.lastChild.firstChild;
255 expect(panel.classList.contains('panel')).toBeTruthy();
256 expect(panel.classList.contains('vcinfo')).toBeTruthy();
257 expect(panel.lastChild.classList.contains('button-group')).toBeTruthy();
258 expect(panel.lastChild.classList.contains('vcinfo')).toBeTruthy();
259 expect(panel.lastChild.children[0].tagName).toEqual('SPAN');
260 expect(panel.lastChild.children[0].textContent).toEqual('Statistics');
261 });
262
263
264 it('should display the statistics button in a panel after VC modification', function () {
265
266 var vc = vcClass.create([
267 ['title', 'string'],
268 ['subTitle', 'string'],
269 ['pubDate', 'date'],
270 ['author', 'text']
271 ]).fromJson(json);
272
273 var show = document.createElement('div');
274
275 show.appendChild(vc.element());
276
277 expect(show.querySelector(".statistic").tagName).toEqual("SPAN");
278
Akronadab5e52018-08-20 13:50:53 +0200279 var and = vc.builder().lastChild.firstChild;
Akron18a99c02018-08-20 12:55:14 +0200280
281 // Click on and() in VC
282 and.click();
283
284 // Check that statistics is there
285 expect(show.querySelector(".statistic").tagName).toEqual("SPAN");
286 });
287
288
289 it('should display and hide corpus statistics view in the panel', function () {
290
291 var vc = vcClass.create([
292 ['title', 'string'],
293 ['subTitle', 'string'],
294 ['pubDate', 'date'],
295 ['author', 'text']
296 ]).fromJson(json);
297
298 var show = document.createElement('div');
299
300 show.appendChild(vc.element());
301
302 var panel = show.firstChild.lastChild.firstChild;
303
304 // Show statistics
305 panel.lastChild.children[0].click();
306
307 // Statistics view
308 var viewE = panel.firstChild.firstChild;
309
310 expect(viewE.tagName).toEqual("DIV");
311 expect(viewE.classList.contains("view")).toBeTruthy();
312 expect(viewE.classList.contains("vcstatistic")).toBeTruthy();
313
314 expect(viewE.firstChild.classList.contains("stattable")).toBeTruthy();
315 expect(viewE.firstChild.firstChild.tagName).toEqual("DL");
316
317 // List of statistic values
318 var dl = viewE.firstChild.firstChild.firstChild;
319 expect(dl.firstChild.tagName).toEqual("DT");
320 expect(dl.firstChild.textContent).toEqual("documents");
321 expect(dl.lastChild.tagName).toEqual("DD");
322 expect(dl.lastChild.textContent).toEqual("12");
323
324 expect(viewE.children.length).toEqual(2);
325
326 expect(viewE.lastChild.classList.contains("button-group")).toBeTruthy();
327 expect(viewE.lastChild.children.length).toEqual(1);
328 expect(viewE.lastChild.firstChild.tagName).toEqual("SPAN");
329 expect(viewE.lastChild.firstChild.textContent).toEqual("Close");
330
331 // Close view
332 viewE.lastChild.firstChild.firstChild.click();
333
334 // Is gone
335 expect(panel.firstChild.children.length).toEqual(0);
336
337 // Show statistics
338 panel.lastChild.children[0].click();
339
340 // Is there
341 expect(panel.firstChild.children.length).toEqual(1);
342
343 // Only show once
344 panel.lastChild.children[0].click();
345 expect(panel.firstChild.children.length).toEqual(1);
346 });
hebastaa0282be2018-12-05 16:58:00 +0100347
348
349 });
350
351
352
353 /**
354 * Test disabling and reload of corpus statistic if vc is changed
355 * in vc builder through user-interaction
356 */
357 describe ('KorAP.CorpusStat.Disable', function(){
hebastade595b42019-02-05 13:53:10 +0100358
hebasta48842cf2018-12-11 12:57:38 +0100359 document.addEventListener('vcChange', function (e) {
hebastade595b42019-02-05 13:53:10 +0100360 if(KorAP.vc){
hebasta48842cf2018-12-11 12:57:38 +0100361 KorAP.vc.checkStatActive(e.detail);
hebastade595b42019-02-05 13:53:10 +0100362 }
hebasta48842cf2018-12-11 12:57:38 +0100363 }, false);
hebastade595b42019-02-05 13:53:10 +0100364
hebastaa0282be2018-12-05 16:58:00 +0100365 /**
366 * If the user defines a new vc, the statistic should be disabled,
367 * because it is out-of-date.
368 *
369 * The user can choose to display an up-to-date corpus statistic. Here it is tested
370 * if corpus statistic is disabled after a valid change of corpus statistic and if the corpus statistic is updatable.
371 */
372 it ('should disable the corpus statistic if corpus definition is changed and display a functional reload button', function(){
373
374 KorAP.vc = generateCorpusDocGr();
375
376 //Show corpus statistic
377 let show = document.createElement('div');
378 show.appendChild(KorAP.vc.element());
379 let panel = show.firstChild.lastChild.firstChild;
380 panel.lastChild.children[0].click();
381 let view = panel.firstChild.firstChild;
382
383 //corpus statistic is active
384 expect(checkStatActive(view, show)).toBe(true);
385
386 //change vc, a line in vc builder is deleted
387 KorAP._delete.apply(KorAP.vc.root().getOperand(0));
388 expect(checkStatDisabled(view,show)).toBe(true);
389
390 //click at reload button
391 let rlbutton = show.getElementsByClassName("refresh").item(0);
392 rlbutton.click();
393
394 expect(checkStatActive(view,show)).toBe(true);
395 });
396
397
398 it('should disable corpus statistic if entries in vc builder are deleted', function(){
399 KorAP.vc = generateCorpusDocGr();
400
401 // create corpus builder and corpus statistic;
402 let show = document.createElement('div');
403 show.appendChild(KorAP.vc.element());
404 let panel = show.firstChild.lastChild.firstChild;
405 panel.lastChild.children[0].click();
406 let view = panel.firstChild.firstChild;
407
408 expect(checkStatActive(view, show)).toBe(true);
409
410 //delete foo=bar
411 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
412 expect(checkStatDisabled(view, show)).toBe(true);
413
414 //refresh corpus statistic
415 let rlbutton = show.getElementsByClassName("refresh").item(0);
416 rlbutton.click();
417 expect(checkStatActive(view,show)).toBe(true);
hebasta3f4be922018-12-11 10:41:46 +0100418
hebastaa0282be2018-12-05 16:58:00 +0100419 KorAP._delete.apply(KorAP.vc.root());
hebasta3f4be922018-12-11 10:41:46 +0100420 view = panel.firstChild.firstChild;
421 expect(checkStatDisabled(view, show)).toBe(true);
hebastade595b42019-02-05 13:53:10 +0100422
423 KorAP.vc = generateBCorpusDocGr();
424 // create corpus builder and corpus statistic;
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
431 expect(checkStatActive(view, show)).toBe(true);
432 KorAP._delete.apply(KorAP.vc.root().getOperand(1));
433 view = panel.firstChild.firstChild;
434 expect(checkStatDisabled(view, show)).toBe(true);
435
hebastaa0282be2018-12-05 16:58:00 +0100436 });
437
438
439 it('should disable corpus statistic if key, matchoperator or value is changed', function(){
440 /*
441 * Doc change of key, match operator and value
442 */
443 KorAP.vc= generateCorpusDoc();
444 // show vc builder and open corpus statistic
445 let show = document.createElement('div');
446 show.appendChild(KorAP.vc.element());
447 let panel = show.firstChild.lastChild.firstChild;
448 panel.lastChild.children[0].click();
449 let view = panel.firstChild.firstChild;
450 expect(checkStatActive(view, show)).toBe(true);
451
452 KorAP.vc.root().matchop("ne").update();
453 expect(checkStatDisabled(view, show)).toBe(true);
454
455 let rlbutton = show.getElementsByClassName("refresh").item(0);
456 rlbutton.click();
457
458 view = panel.firstChild.firstChild;
459 expect(checkStatActive(view, show)).toBe(true);
460 KorAP.vc.root().value("Hello tester").update();
461 expect(checkStatDisabled(view, show)).toBe(true);
462
463 //refresh corpus statistic
464 rlbutton = show.getElementsByClassName("refresh").item(0);
465 rlbutton.click();
466 view = panel.firstChild.firstChild;
467 expect(checkStatActive(view, show)).toBe(true);
468
469 KorAP.vc.root().key("author").update();
470 expect(checkStatDisabled(view, show)).toBe(true);
471
472
473 /*
474 * DocGroupRef change of value...
475 */
476 KorAP.vc = generateCorpusRef();
477 show = document.createElement('div');
478 show.appendChild(KorAP.vc.element());
479 panel = show.firstChild.lastChild.firstChild;
480 panel.lastChild.children[0].click();
481 view = panel.firstChild.firstChild;
482 expect(checkStatActive(view, show)).toBe(true);
483
484 KorAP.vc.root().ref("@anton/secondCorpus").update();
485 expect(checkStatDisabled(view, show)).toBe(true);
486 });
487
488
489 it('should not disable corpus statistic if docgroup definition is incomplete', function(){
490
491 KorAP.vc = generateCorpusDocGr();
492
493 //Show corpus statistic
494 let show = document.createElement('div');
495 show.appendChild(KorAP.vc.element());
496 let panel = show.firstChild.lastChild.firstChild;
497 panel.lastChild.children[0].click();
498 let view = panel.firstChild.firstChild;
499
500 expect(checkStatActive(view, show)).toBe(true);
501
502 KorAP._and.apply(KorAP.vc.root());
503
504 let andbuild = show.getElementsByClassName("builder");
505 expect(andbuild[0].firstChild.classList.contains('docGroup')).toBeTruthy();
506 expect(andbuild[0].firstChild.getAttribute("data-operation")).toEqual("and");
507 expect(checkStatActive(view, show)).toBe(true);
508 });
509
510
511 it('should not disable corpus statistic if doc/docref definition is incomplete', function(){
512
513 /*
514 * DOC incomplete
515 */
516 KorAP.vc = vcClass.create().fromJson();
517 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
518
519 // show vc builder and open corpus statistic
520 let show = document.createElement('div');
521 show.appendChild(KorAP.vc.element());
522 let panel = show.firstChild.lastChild.firstChild;
523 panel.lastChild.children[0].click();
524 let view = panel.firstChild.firstChild;
525
526 // corpus statistic should be shown and be up-to-date, reload button is not shown
527 expect(checkStatActive(view, show)).toBe(true);
528
529 // open the menu
530 KorAP.vc.builder().firstChild.firstChild.click();
531 KorAP._vcKeyMenu._prefix.add("author");
532 let prefElement = KorAP.vc.builder().querySelector('span.pref');
533 // add key 'author' to VC
534 prefElement.click();
535
536 expect(checkStatActive(view, show)).toBe(true);
537
538
539 /*
540 * DOCREF incomplete
541 */
542 KorAP.vc = vcClass.create().fromJson();
543 expect(KorAP.vc.builder().firstChild.classList.contains('unspecified')).toBeTruthy();
544
545 // show vc builder and open corpus statistic
546 show = document.createElement('div');
547 show.appendChild(KorAP.vc.element());
548 panel = show.firstChild.lastChild.firstChild;
549 panel.lastChild.children[0].click();
550 view = panel.firstChild.firstChild;
551 expect(checkStatActive(view, show)).toBe(true);
552
553 KorAP.vc.builder().firstChild.firstChild.click();
554 KorAP._vcKeyMenu._prefix.add("referTo");
555 prefElement = KorAP.vc.builder().querySelector('span.pref');
556 prefElement.click();
557 expect(checkStatActive(view, show)).toBe(true);
558 });
559 });
hebasta5e4d7542018-06-19 16:57:36 +0200560});