blob: 18d59b6d35e0d5151b03c1ccc2ffda51703e5dba [file] [log] [blame]
Nils Diewald0b6c0412014-12-19 03:55:57 +00001// Helper method for building factories
2buildFactory = function (objClass, defaults) {
3 return {
4 create : function (overwrites) {
5 var newObj = {};
6 for (var prop in defaults) {
7 newObj[prop] = defaults[prop];
8 };
9 for (var prop in overwrites) {
10 newObj[prop] = overwrites[prop];
11 };
12 return objClass.create().fromJson(newObj);
13 }
14 }
15};
16
17describe('KorAP.Doc', function () {
18
19 // Create example factories
20 var stringFactory = buildFactory(KorAP.Doc, {
21 "key" : "author",
22 "value" : "Max Birkendale",
23 "@type" : "korap:doc"
24 });
25
26 // Create example factories
27 var dateFactory = buildFactory(KorAP.Doc, {
28 "key" : "pubDate",
29 "type" : "type:date",
30 "match" : "match:eq",
31 "value" : "2014-11-05",
32 "@type" : "korap:doc"
33 });
34
35 // Create example factories
36 var regexFactory = buildFactory(KorAP.Doc, {
37 "key" : "title",
38 "type" : "type:regex",
39 "value" : "[^b]ee.+?",
40 "@type" : "korap:doc"
41 });
42
43 it('should be initializable', function () {
44 var doc = KorAP.Doc.create();
45 expect(doc.matchop()).toEqual('eq');
46 expect(doc.key()).toBeUndefined();
47 expect(doc.value()).toBeUndefined();
48 expect(doc.type()).toEqual("string");
49 });
50
51 it('should be definable', function () {
52
53 // Empty doc
54 var doc = KorAP.Doc.create();
55
56 // Set values
57 doc.key("title");
58 doc.value("Der alte Mann");
59 expect(doc.matchop()).toEqual('eq');
60 expect(doc.key()).toEqual("title");
61 expect(doc.type()).toEqual("string");
62 expect(doc.value()).toEqual("Der alte Mann");
63 });
64
65
66 it('should deserialize JSON-LD string', function () {
67 var doc;
68
69 // String default
70 doc = stringFactory.create();
71 expect(doc.matchop()).toEqual('eq');
72 expect(doc.key()).toEqual("author");
73 expect(doc.type()).toEqual("string");
74 expect(doc.value()).toEqual("Max Birkendale");
75
76 // No valid string
77 doc = stringFactory.create({
78 value : undefined
79 });
80 expect(doc).toBeUndefined();
81
82 // No valid string
83 doc = stringFactory.create({
84 value : { "foo" : "bar" }
85 });
86 expect(doc).toBeUndefined();
87
88 // Change match type
89 doc = stringFactory.create({
90 "match" : "match:ne"
91 });
92
93 expect(doc.matchop()).toEqual('ne');
94 expect(doc.key()).toEqual("author");
95 expect(doc.type()).toEqual("string");
96 expect(doc.value()).toEqual("Max Birkendale");
97
98
99 // Invalid match type
100 doc = stringFactory.create({
101 "match" : { "foo" : "bar" }
102 });
103 expect(doc).toBeUndefined();
104 });
105
106 it('should deserialize JSON-LD regex', function () {
107 var doc = regexFactory.create();
108 expect(doc.key()).toEqual("title");
109 expect(doc.type()).toEqual("regex");
110 expect(doc.value()).toEqual("[^b]ee.+?");
111 expect(doc.matchop()).toEqual('eq');
112
113 // change matcher
114 doc = regexFactory.create({
115 match : "match:ne"
116 });
117 expect(doc.matchop()).toEqual('ne');
118
119 // Invalid matcher
120 doc = regexFactory.create({
121 match : "match:chook"
122 });
123 expect(doc).toBeUndefined();
124
125 // Invalid regex
126 doc = regexFactory.create({
127 value : "[^b"
128 });
129 expect(doc).toBeUndefined();
130 });
131
132 it('should deserialize JSON-LD date', function () {
133
134 // Normal date
135 doc = dateFactory.create({});
136
137 expect(doc.matchop()).toEqual('eq');
138 expect(doc.key()).toEqual("pubDate");
139 expect(doc.type()).toEqual("date");
140 expect(doc.value()).toEqual("2014-11-05");
141
142 // Short date 1
143 doc = dateFactory.create({
144 "value" : "2014-11"
145 });
146
147 expect(doc.matchop()).toEqual('eq');
148 expect(doc.key()).toEqual("pubDate");
149 expect(doc.type()).toEqual("date");
150 expect(doc.value()).toEqual("2014-11");
151
152 // Short date 2
153 doc = dateFactory.create({
154 "value" : "2014"
155 });
156
157 expect(doc.matchop()).toEqual('eq');
158 expect(doc.key()).toEqual("pubDate");
159 expect(doc.type()).toEqual("date");
160 expect(doc.value()).toEqual("2014");
161
162 // Invalid date!
163 doc = dateFactory.create({
164 "value" : "2014-11-050"
165 });
166 expect(doc).toBeUndefined();
167
168 // Invalid matcher!
169 doc = dateFactory.create({
170 "match" : "match:ne",
171 });
172 expect(doc).toBeUndefined();
173 });
174
175 it('should be serializale', function () {
176
177 // Empty doc
178 var doc = KorAP.Doc.create();
179 expect(doc.toJson()).toEqual(jasmine.any(Object));
180
181 // Serialize string
182 doc = stringFactory.create();
183 expect(doc.toJson()).toEqual(jasmine.objectContaining({
184 "@type" : "korap:doc",
185 "type" : "type:string",
186 "key" : "author",
187 "value" : "Max Birkendale",
188 "match" : "match:eq"
189 }));
190
191 // Serialize regex
192 doc = regexFactory.create();
193 expect(doc.toJson()).toEqual(jasmine.objectContaining({
194 "@type" : "korap:doc",
195 "type" : "type:regex",
196 "value" : "[^b]ee.+?",
197 "match" : "match:eq",
198 "key" : 'title'
199 }));
200
201 doc = regexFactory.create({
202 match: "match:ne"
203 });
204 expect(doc.toJson()).toEqual(jasmine.objectContaining({
205 "@type" : "korap:doc",
206 "type" : "type:regex",
207 "value" : "[^b]ee.+?",
208 "match" : "match:ne",
209 "key" : 'title'
210 }));
211
212 doc = dateFactory.create();
213 expect(doc.toJson()).toEqual(jasmine.objectContaining({
214 "@type" : "korap:doc",
215 "type" : "type:date",
216 "value" : "2014-11-05",
217 "match" : "match:eq",
218 "key" : 'pubDate'
219 }));
220
221 doc = dateFactory.create({
222 value : "2014"
223 });
224 expect(doc.toJson()).toEqual(jasmine.objectContaining({
225 "@type" : "korap:doc",
226 "type" : "type:date",
227 "value" : "2014",
228 "match" : "match:eq",
229 "key" : 'pubDate'
230 }));
231 });
232});
233
234
235describe('KorAP.DocGroup', function () {
236 // Create example factories
237 var docFactory = buildFactory(
238 KorAP.Doc,
239 {
240 "@type" : "korap:doc",
241 "match":"match:eq",
242 "key" : "author",
243 "value" : "Max Birkendale"
244 }
245 );
246
247 var docGroupFactory = buildFactory(
248 KorAP.DocGroup, {
249 "@type" : "korap:docGroup",
250 "operation" : "operation:and",
251 "operands" : [
252 docFactory.create().toJson(),
253 docFactory.create({
254 "key" : "pubDate",
255 "type" : "type:date",
256 "value" : "2014-12-05"
257 }).toJson()
258 ]
259 });
260
Nils Diewald0b6c0412014-12-19 03:55:57 +0000261
Nils Diewald8f4e2542014-12-19 04:42:09 +0000262 it('should be initializable', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000263 // Create empty group
264 var docGroup = KorAP.DocGroup.create();
265 expect(docGroup.operation()).toEqual('and');
266
267 // Create empty group
Nils Diewald8f4e2542014-12-19 04:42:09 +0000268 docGroup = KorAP.DocGroup.create();
269 docGroup.operation('or');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000270 expect(docGroup.operation()).toEqual('or');
271 });
272
273 it('should be definable', function () {
274
275 // Empty group
276 var docGroup = KorAP.DocGroup.create();
277 expect(docGroup.operation()).toEqual('and');
278
279 // Set values
280 docGroup.operation("or");
281 expect(docGroup.operation()).toEqual('or');
282
283 // Set invalid values
284 docGroup.operation("hui");
285 expect(docGroup.operation()).toEqual('or');
286 });
287
288 it('should be deserializable', function () {
289 var docGroup = docGroupFactory.create();
290 expect(docGroup.operation()).toEqual("and");
291 expect(docGroup.operands().length).toEqual(2);
292
293 var op1 = docGroup.getOperand(0);
294 expect(op1.type()).toEqual("string");
295 expect(op1.key()).toEqual("author");
296 expect(op1.value()).toEqual("Max Birkendale");
297 expect(op1.matchop()).toEqual("eq");
298
299 var op2 = docGroup.getOperand(1);
300 expect(op2.type()).toEqual("date");
301 expect(op2.key()).toEqual("pubDate");
302 expect(op2.value()).toEqual("2014-12-05");
303 expect(op2.matchop()).toEqual("eq");
304
305 // Create empty group
Nils Diewald8f4e2542014-12-19 04:42:09 +0000306 var newGroup = docGroup.appendOperand(KorAP.DocGroup.create());
307 newGroup.operation('or');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000308 newGroup.appendOperand(docFactory.create());
309 newGroup.appendOperand(docFactory.create({
310 "type" : "type:regex",
311 "key" : "title",
312 "value" : "^e.+?$",
313 "match" : "match:ne"
314 }));
315
316 expect(docGroup.operation()).toEqual("and");
317 expect(docGroup.operands().length).toEqual(3);
318
319 var op1 = docGroup.getOperand(0);
320 expect(op1.ldType()).toEqual("doc");
321 expect(op1.type()).toEqual("string");
322 expect(op1.key()).toEqual("author");
323 expect(op1.value()).toEqual("Max Birkendale");
324 expect(op1.matchop()).toEqual("eq");
325
326 var op2 = docGroup.getOperand(1);
327 expect(op2.ldType()).toEqual("doc");
328 expect(op2.type()).toEqual("date");
329 expect(op2.key()).toEqual("pubDate");
330 expect(op2.value()).toEqual("2014-12-05");
331 expect(op2.matchop()).toEqual("eq");
332
333 var op3 = docGroup.getOperand(2);
334 expect(op3.ldType()).toEqual("docGroup");
335 expect(op3.operation()).toEqual("or");
336
337 var op4 = op3.getOperand(0);
338 expect(op4.ldType()).toEqual("doc");
339 expect(op4.type()).toEqual("string");
340 expect(op4.key()).toEqual("author");
341 expect(op4.value()).toEqual("Max Birkendale");
342 expect(op4.matchop()).toEqual("eq");
343
344 var op5 = op3.getOperand(1);
345 expect(op5.ldType()).toEqual("doc");
346 expect(op5.type()).toEqual("regex");
347 expect(op5.key()).toEqual("title");
348 expect(op5.value()).toEqual("^e.+?$");
349 expect(op5.matchop()).toEqual("ne");
350 });
351
352 it('should be serializable', function () {
353 var docGroup = docGroupFactory.create();
354
355 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
356 "@type" : "korap:docGroup",
357 "operation" : "operation:and",
358 "operands" : [
359 {
360 "@type": 'korap:doc',
361 "key" : 'author',
362 "match": 'match:eq',
363 "value": 'Max Birkendale',
364 "type": 'type:string'
365 },
366 {
367 "@type": 'korap:doc',
368 "key": 'pubDate',
369 "match": 'match:eq',
370 "value": '2014-12-05',
371 "type": 'type:date'
372 }
373 ]
374 }));
375 });
376});
377
Nils Diewald8f4e2542014-12-19 04:42:09 +0000378describe('KorAP.Doc element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000379 it('should be initializable', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000380 var docElement = KorAP.Doc.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000381 "@type" : "korap:doc",
382 "key":"Titel",
383 "value":"Baum",
384 "match":"match:eq"
385 });
386 expect(docElement.key()).toEqual('Titel');
387 expect(docElement.matchop()).toEqual('eq');
388 expect(docElement.value()).toEqual('Baum');
389
390 var docE = docElement.element();
391 expect(docE.children[0].firstChild.data).toEqual('Titel');
392 expect(docE.children[1].firstChild.data).toEqual('eq');
393 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
394 expect(docE.children[2].firstChild.data).toEqual('Baum');
395 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
396
397 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
398 "@type" : "korap:doc",
399 "key":"Titel",
400 "value":"Baum",
401 "match":"match:eq"
402 }));
403 });
404});
405
Nils Diewald8f4e2542014-12-19 04:42:09 +0000406describe('KorAP.DocGroup element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000407 it('should be initializable', function () {
408
Nils Diewald8f4e2542014-12-19 04:42:09 +0000409 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000410 "@type" : "korap:docGroup",
411 "operation" : "operation:and",
412 "operands" : [
413 {
414 "@type": 'korap:doc',
415 "key" : 'author',
416 "match": 'match:eq',
417 "value": 'Max Birkendale',
418 "type": 'type:string'
419 },
420 {
421 "@type": 'korap:doc',
422 "key": 'pubDate',
423 "match": 'match:eq',
424 "value": '2014-12-05',
425 "type": 'type:date'
426 }
427 ]
428 });
429
Nils Diewald8f4e2542014-12-19 04:42:09 +0000430 expect(docGroup.operation()).toEqual('and');
431 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000432 expect(e.getAttribute('class')).toEqual('docGroup');
433 expect(e.getAttribute('data-operation')).toEqual('and');
434
435 var first = e.children[0];
436 expect(first.getAttribute('class')).toEqual('doc');
437 expect(first.children[0].getAttribute('class')).toEqual('key');
438 expect(first.children[1].getAttribute('class')).toEqual('match');
439 expect(first.children[2].getAttribute('class')).toEqual('value');
440 expect(first.children[2].getAttribute('data-type')).toEqual('string');
441 expect(first.children[0].firstChild.data).toEqual('author');
442 expect(first.children[1].firstChild.data).toEqual('eq');
443 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
444
445 var second = e.children[1];
446 expect(second.getAttribute('class')).toEqual('doc');
447 expect(second.children[0].getAttribute('class')).toEqual('key');
448 expect(second.children[1].getAttribute('class')).toEqual('match');
449 expect(second.children[2].getAttribute('class')).toEqual('value');
450 expect(second.children[2].getAttribute('data-type')).toEqual('date');
451 expect(second.children[0].firstChild.data).toEqual('pubDate');
452 expect(second.children[1].firstChild.data).toEqual('eq');
453 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
454
455 });
456
457 it('should be deserializable with nested groups', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000458 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000459 "@type" : "korap:docGroup",
460 "operation" : "operation:or",
461 "operands" : [
462 {
463 "@type": 'korap:doc',
464 "key" : 'author',
465 "match": 'match:eq',
466 "value": 'Max Birkendale',
467 "type": 'type:string'
468 },
469 {
470 "@type" : "korap:docGroup",
471 "operation" : "operation:and",
472 "operands" : [
473 {
474 "@type": 'korap:doc',
475 "key": 'pubDate',
476 "match": 'match:geq',
477 "value": '2014-05-12',
478 "type": 'type:date'
479 },
480 {
481 "@type": 'korap:doc',
482 "key": 'pubDate',
483 "match": 'match:leq',
484 "value": '2014-12-05',
485 "type": 'type:date'
486 }
487 ]
488 }
489 ]
490 });
491
Nils Diewald8f4e2542014-12-19 04:42:09 +0000492 expect(docGroup.operation()).toEqual('or');
493 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000494 expect(e.getAttribute('class')).toEqual('docGroup');
495 expect(e.getAttribute('data-operation')).toEqual('or');
496
497
498 });
499});
500
501describe('KorAP.VirtualCollection', function () {
502 it('should be initializable', function () {
503 var vc = KorAP.VirtualCollection.render();
504 expect(vc.element().getAttribute('class')).toEqual('vc');
505 expect(vc.root().element().getAttribute('class')).toEqual('undefined');
506 });
507
508 it('should be based on a doc', function () {
509 var vc = KorAP.VirtualCollection.render({
510 "@type" : "korap:doc",
511 "key":"Titel",
512 "value":"Baum",
513 "match":"match:eq"
514 });
515
516 expect(vc.element().getAttribute('class')).toEqual('vc');
517 expect(vc.root().element().getAttribute('class')).toEqual('doc');
518 expect(vc.root().key()).toEqual('Titel');
519 expect(vc.root().value()).toEqual('Baum');
520 expect(vc.root().matchop()).toEqual('eq');
521
522 var docE = vc.root().element();
523 expect(docE.children[0].firstChild.data).toEqual('Titel');
524 expect(docE.children[1].firstChild.data).toEqual('eq');
525 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
526 expect(docE.children[2].firstChild.data).toEqual('Baum');
527 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
528 });
529
530 it('should be based on a docGroup', function () {
531 var vc = KorAP.VirtualCollection.render({
532 "@type" : "korap:docGroup",
533 "operation" : "operation:and",
534 "operands" : [
535 {
536 "@type": 'korap:doc',
537 "key" : 'author',
538 "match": 'match:eq',
539 "value": 'Max Birkendale',
540 "type": 'type:string'
541 },
542 {
543 "@type": 'korap:doc',
544 "key": 'pubDate',
545 "match": 'match:eq',
546 "value": '2014-12-05',
547 "type": 'type:date'
548 }
549 ]
550 });
551
552 expect(vc.element().getAttribute('class')).toEqual('vc');
553 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
554 expect(vc.root().operation()).toEqual('and');
555
556 var docGroup = vc.root();
557
558 var first = docGroup.getOperand(0);
559 expect(first.key()).toEqual('author');
560 expect(first.value()).toEqual('Max Birkendale');
561 expect(first.matchop()).toEqual('eq');
562
563 var second = docGroup.getOperand(1);
564 expect(second.key()).toEqual('pubDate');
565 expect(second.value()).toEqual('2014-12-05');
566 expect(second.matchop()).toEqual('eq');
567 });
568
569 xit('should be based on a nested docGroup', function () {
570 });
571});
572
573describe('KorAP.Operators', function () {
574 it('should be initializable', function () {
575 var op = KorAP.Operators.create(true, false, false);
576 expect(op.and()).toBeTruthy();
577 expect(op.or()).not.toBeTruthy();
578 expect(op.del()).not.toBeTruthy();
579
580 op.and(false);
581 expect(op.and()).not.toBeTruthy();
582 expect(op.or()).not.toBeTruthy();
583 expect(op.del()).not.toBeTruthy();
584
585 op.or(true);
586 op.del(true);
587 expect(op.and()).not.toBeTruthy();
588 expect(op.or()).toBeTruthy();
589 expect(op.del()).toBeTruthy();
590
591 var e = op.element();
592 expect(e.getAttribute('class')).toEqual('operators');
593 expect(e.children[0].getAttribute('class')).toEqual('or');
594 expect(e.children[0].firstChild.data).toEqual('or');
595 expect(e.children[1].getAttribute('class')).toEqual('delete');
596 expect(e.children[1].firstChild.data).toEqual('×');
597
598 op.and(true);
599 op.del(false);
600 op.update();
601
602 e = op.element();
603 expect(e.getAttribute('class')).toEqual('operators');
604 expect(e.children[0].getAttribute('class')).toEqual('and');
605 expect(e.children[0].firstChild.data).toEqual('and');
606 expect(e.children[1].getAttribute('class')).toEqual('or');
607 expect(e.children[1].firstChild.data).toEqual('or');
608 });
609});