blob: 3d1d0778fc4011b8dc7ebe48c7e76b6e2ff392ce [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
261 it('should be initializable', function () {
262
263 // Create empty group
264 var docGroup = KorAP.DocGroup.create();
265 expect(docGroup.operation()).toEqual('and');
266
267 // Create empty group
268 docGroup = KorAP.DocGroup.create('or');
269 expect(docGroup.operation()).toEqual('or');
270 });
271
272 it('should be definable', function () {
273
274 // Empty group
275 var docGroup = KorAP.DocGroup.create();
276 expect(docGroup.operation()).toEqual('and');
277
278 // Set values
279 docGroup.operation("or");
280 expect(docGroup.operation()).toEqual('or');
281
282 // Set invalid values
283 docGroup.operation("hui");
284 expect(docGroup.operation()).toEqual('or');
285 });
286
287 it('should be deserializable', function () {
288 var docGroup = docGroupFactory.create();
289 expect(docGroup.operation()).toEqual("and");
290 expect(docGroup.operands().length).toEqual(2);
291
292 var op1 = docGroup.getOperand(0);
293 expect(op1.type()).toEqual("string");
294 expect(op1.key()).toEqual("author");
295 expect(op1.value()).toEqual("Max Birkendale");
296 expect(op1.matchop()).toEqual("eq");
297
298 var op2 = docGroup.getOperand(1);
299 expect(op2.type()).toEqual("date");
300 expect(op2.key()).toEqual("pubDate");
301 expect(op2.value()).toEqual("2014-12-05");
302 expect(op2.matchop()).toEqual("eq");
303
304 // Create empty group
305 var newGroup = docGroup.appendOperand(KorAP.DocGroup.create("or"));
306 newGroup.appendOperand(docFactory.create());
307 newGroup.appendOperand(docFactory.create({
308 "type" : "type:regex",
309 "key" : "title",
310 "value" : "^e.+?$",
311 "match" : "match:ne"
312 }));
313
314 expect(docGroup.operation()).toEqual("and");
315 expect(docGroup.operands().length).toEqual(3);
316
317 var op1 = docGroup.getOperand(0);
318 expect(op1.ldType()).toEqual("doc");
319 expect(op1.type()).toEqual("string");
320 expect(op1.key()).toEqual("author");
321 expect(op1.value()).toEqual("Max Birkendale");
322 expect(op1.matchop()).toEqual("eq");
323
324 var op2 = docGroup.getOperand(1);
325 expect(op2.ldType()).toEqual("doc");
326 expect(op2.type()).toEqual("date");
327 expect(op2.key()).toEqual("pubDate");
328 expect(op2.value()).toEqual("2014-12-05");
329 expect(op2.matchop()).toEqual("eq");
330
331 var op3 = docGroup.getOperand(2);
332 expect(op3.ldType()).toEqual("docGroup");
333 expect(op3.operation()).toEqual("or");
334
335 var op4 = op3.getOperand(0);
336 expect(op4.ldType()).toEqual("doc");
337 expect(op4.type()).toEqual("string");
338 expect(op4.key()).toEqual("author");
339 expect(op4.value()).toEqual("Max Birkendale");
340 expect(op4.matchop()).toEqual("eq");
341
342 var op5 = op3.getOperand(1);
343 expect(op5.ldType()).toEqual("doc");
344 expect(op5.type()).toEqual("regex");
345 expect(op5.key()).toEqual("title");
346 expect(op5.value()).toEqual("^e.+?$");
347 expect(op5.matchop()).toEqual("ne");
348 });
349
350 it('should be serializable', function () {
351 var docGroup = docGroupFactory.create();
352
353 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
354 "@type" : "korap:docGroup",
355 "operation" : "operation:and",
356 "operands" : [
357 {
358 "@type": 'korap:doc',
359 "key" : 'author',
360 "match": 'match:eq',
361 "value": 'Max Birkendale',
362 "type": 'type:string'
363 },
364 {
365 "@type": 'korap:doc',
366 "key": 'pubDate',
367 "match": 'match:eq',
368 "value": '2014-12-05',
369 "type": 'type:date'
370 }
371 ]
372 }));
373 });
374});
375
376describe('KorAP.DocElement', function () {
377 it('should be initializable', function () {
378 var docElement = KorAP.DocElement.create(undefined, {
379 "@type" : "korap:doc",
380 "key":"Titel",
381 "value":"Baum",
382 "match":"match:eq"
383 });
384 expect(docElement.key()).toEqual('Titel');
385 expect(docElement.matchop()).toEqual('eq');
386 expect(docElement.value()).toEqual('Baum');
387
388 var docE = docElement.element();
389 expect(docE.children[0].firstChild.data).toEqual('Titel');
390 expect(docE.children[1].firstChild.data).toEqual('eq');
391 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
392 expect(docE.children[2].firstChild.data).toEqual('Baum');
393 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
394
395 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
396 "@type" : "korap:doc",
397 "key":"Titel",
398 "value":"Baum",
399 "match":"match:eq"
400 }));
401 });
402});
403
404describe('KorAP.DocGroupElement', function () {
405 it('should be initializable', function () {
406
407 var docGroupElement = KorAP.DocGroupElement.create(undefined, {
408 "@type" : "korap:docGroup",
409 "operation" : "operation:and",
410 "operands" : [
411 {
412 "@type": 'korap:doc',
413 "key" : 'author',
414 "match": 'match:eq',
415 "value": 'Max Birkendale',
416 "type": 'type:string'
417 },
418 {
419 "@type": 'korap:doc',
420 "key": 'pubDate',
421 "match": 'match:eq',
422 "value": '2014-12-05',
423 "type": 'type:date'
424 }
425 ]
426 });
427
428 expect(docGroupElement.operation()).toEqual('and');
429 var e = docGroupElement.element();
430 expect(e.getAttribute('class')).toEqual('docGroup');
431 expect(e.getAttribute('data-operation')).toEqual('and');
432
433 var first = e.children[0];
434 expect(first.getAttribute('class')).toEqual('doc');
435 expect(first.children[0].getAttribute('class')).toEqual('key');
436 expect(first.children[1].getAttribute('class')).toEqual('match');
437 expect(first.children[2].getAttribute('class')).toEqual('value');
438 expect(first.children[2].getAttribute('data-type')).toEqual('string');
439 expect(first.children[0].firstChild.data).toEqual('author');
440 expect(first.children[1].firstChild.data).toEqual('eq');
441 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
442
443 var second = e.children[1];
444 expect(second.getAttribute('class')).toEqual('doc');
445 expect(second.children[0].getAttribute('class')).toEqual('key');
446 expect(second.children[1].getAttribute('class')).toEqual('match');
447 expect(second.children[2].getAttribute('class')).toEqual('value');
448 expect(second.children[2].getAttribute('data-type')).toEqual('date');
449 expect(second.children[0].firstChild.data).toEqual('pubDate');
450 expect(second.children[1].firstChild.data).toEqual('eq');
451 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
452
453 });
454
455 it('should be deserializable with nested groups', function () {
456 var docGroupElement = KorAP.DocGroupElement.create(undefined, {
457 "@type" : "korap:docGroup",
458 "operation" : "operation:or",
459 "operands" : [
460 {
461 "@type": 'korap:doc',
462 "key" : 'author',
463 "match": 'match:eq',
464 "value": 'Max Birkendale',
465 "type": 'type:string'
466 },
467 {
468 "@type" : "korap:docGroup",
469 "operation" : "operation:and",
470 "operands" : [
471 {
472 "@type": 'korap:doc',
473 "key": 'pubDate',
474 "match": 'match:geq',
475 "value": '2014-05-12',
476 "type": 'type:date'
477 },
478 {
479 "@type": 'korap:doc',
480 "key": 'pubDate',
481 "match": 'match:leq',
482 "value": '2014-12-05',
483 "type": 'type:date'
484 }
485 ]
486 }
487 ]
488 });
489
490 expect(docGroupElement.operation()).toEqual('or');
491 var e = docGroupElement.element();
492 expect(e.getAttribute('class')).toEqual('docGroup');
493 expect(e.getAttribute('data-operation')).toEqual('or');
494
495
496 });
497});
498
499describe('KorAP.VirtualCollection', function () {
500 it('should be initializable', function () {
501 var vc = KorAP.VirtualCollection.render();
502 expect(vc.element().getAttribute('class')).toEqual('vc');
503 expect(vc.root().element().getAttribute('class')).toEqual('undefined');
504 });
505
506 it('should be based on a doc', function () {
507 var vc = KorAP.VirtualCollection.render({
508 "@type" : "korap:doc",
509 "key":"Titel",
510 "value":"Baum",
511 "match":"match:eq"
512 });
513
514 expect(vc.element().getAttribute('class')).toEqual('vc');
515 expect(vc.root().element().getAttribute('class')).toEqual('doc');
516 expect(vc.root().key()).toEqual('Titel');
517 expect(vc.root().value()).toEqual('Baum');
518 expect(vc.root().matchop()).toEqual('eq');
519
520 var docE = vc.root().element();
521 expect(docE.children[0].firstChild.data).toEqual('Titel');
522 expect(docE.children[1].firstChild.data).toEqual('eq');
523 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
524 expect(docE.children[2].firstChild.data).toEqual('Baum');
525 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
526 });
527
528 it('should be based on a docGroup', function () {
529 var vc = KorAP.VirtualCollection.render({
530 "@type" : "korap:docGroup",
531 "operation" : "operation:and",
532 "operands" : [
533 {
534 "@type": 'korap:doc',
535 "key" : 'author',
536 "match": 'match:eq',
537 "value": 'Max Birkendale',
538 "type": 'type:string'
539 },
540 {
541 "@type": 'korap:doc',
542 "key": 'pubDate',
543 "match": 'match:eq',
544 "value": '2014-12-05',
545 "type": 'type:date'
546 }
547 ]
548 });
549
550 expect(vc.element().getAttribute('class')).toEqual('vc');
551 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
552 expect(vc.root().operation()).toEqual('and');
553
554 var docGroup = vc.root();
555
556 var first = docGroup.getOperand(0);
557 expect(first.key()).toEqual('author');
558 expect(first.value()).toEqual('Max Birkendale');
559 expect(first.matchop()).toEqual('eq');
560
561 var second = docGroup.getOperand(1);
562 expect(second.key()).toEqual('pubDate');
563 expect(second.value()).toEqual('2014-12-05');
564 expect(second.matchop()).toEqual('eq');
565 });
566
567 xit('should be based on a nested docGroup', function () {
568 });
569});
570
571describe('KorAP.Operators', function () {
572 it('should be initializable', function () {
573 var op = KorAP.Operators.create(true, false, false);
574 expect(op.and()).toBeTruthy();
575 expect(op.or()).not.toBeTruthy();
576 expect(op.del()).not.toBeTruthy();
577
578 op.and(false);
579 expect(op.and()).not.toBeTruthy();
580 expect(op.or()).not.toBeTruthy();
581 expect(op.del()).not.toBeTruthy();
582
583 op.or(true);
584 op.del(true);
585 expect(op.and()).not.toBeTruthy();
586 expect(op.or()).toBeTruthy();
587 expect(op.del()).toBeTruthy();
588
589 var e = op.element();
590 expect(e.getAttribute('class')).toEqual('operators');
591 expect(e.children[0].getAttribute('class')).toEqual('or');
592 expect(e.children[0].firstChild.data).toEqual('or');
593 expect(e.children[1].getAttribute('class')).toEqual('delete');
594 expect(e.children[1].firstChild.data).toEqual('×');
595
596 op.and(true);
597 op.del(false);
598 op.update();
599
600 e = op.element();
601 expect(e.getAttribute('class')).toEqual('operators');
602 expect(e.children[0].getAttribute('class')).toEqual('and');
603 expect(e.children[0].firstChild.data).toEqual('and');
604 expect(e.children[1].getAttribute('class')).toEqual('or');
605 expect(e.children[1].firstChild.data).toEqual('or');
606 });
607});