blob: 1a24fb6f8a972a0b7de55f95dbc2f6235fc7b1b3 [file] [log] [blame]
Nils Diewald6ac292b2015-01-15 21:33:21 +00001/*
2Todo: In demoSpec: Create "and" on the last element of the top "or"-Group
Nils Diewald6ac292b2015-01-15 21:33:21 +00003*/
4
5
Nils Diewald0b6c0412014-12-19 03:55:57 +00006// Helper method for building factories
7buildFactory = function (objClass, defaults) {
8 return {
9 create : function (overwrites) {
10 var newObj = {};
11 for (var prop in defaults) {
12 newObj[prop] = defaults[prop];
13 };
14 for (var prop in overwrites) {
15 newObj[prop] = overwrites[prop];
16 };
Nils Diewaldf219eb82015-01-07 20:15:42 +000017 if (objClass === KorAP.VirtualCollection)
18 return objClass.render(newObj);
19 else
20 return objClass.create().fromJson(newObj);
Nils Diewald0b6c0412014-12-19 03:55:57 +000021 }
22 }
23};
24
Nils Diewald52f7eb12015-01-12 17:30:04 +000025function _andOn (obj) {
26 KorAP._and.bind(obj.element().lastChild.firstChild).apply();
27};
28
29function _orOn (obj) {
30 KorAP._or.bind(obj.element().lastChild.firstChild).apply();
31};
32
33function _delOn (obj) {
34 KorAP._delete.bind(obj.element().lastChild.firstChild).apply();
35};
36
37var demoFactory = buildFactory(KorAP.VirtualCollection, {
Nils Diewald2fe12e12015-03-06 16:47:06 +000038 "@type":"koral:docGroup",
Nils Diewald52f7eb12015-01-12 17:30:04 +000039 "operation":"operation:or",
40 "operands":[
41 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000042 "@type":"koral:docGroup",
Nils Diewald52f7eb12015-01-12 17:30:04 +000043 "operation":"operation:and",
44 "operands":[
45 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000046 "@type":"koral:doc",
Nils Diewald52f7eb12015-01-12 17:30:04 +000047 "key":"Titel",
48 "value":"Baum",
49 "match":"match:eq"
50 },
51 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000052 "@type":"koral:doc",
Nils Diewald52f7eb12015-01-12 17:30:04 +000053 "key":"Veröffentlichungsort",
54 "value":"hihi",
55 "match":"match:eq"
56 },
57 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000058 "@type":"koral:docGroup",
Nils Diewald52f7eb12015-01-12 17:30:04 +000059 "operation":"operation:or",
60 "operands":[
61 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000062 "@type":"koral:doc",
Nils Diewald52f7eb12015-01-12 17:30:04 +000063 "key":"Titel",
64 "value":"Baum",
65 "match":"match:eq"
66 },
67 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000068 "@type":"koral:doc",
Nils Diewald52f7eb12015-01-12 17:30:04 +000069 "key":"Veröffentlichungsort",
70 "value":"hihi",
71 "match":"match:eq"
72 }
73 ]
74 }
75 ]
76 },
77 {
Nils Diewald2fe12e12015-03-06 16:47:06 +000078 "@type":"koral:doc",
Nils Diewald52f7eb12015-01-12 17:30:04 +000079 "key":"Untertitel",
80 "value":"huhu",
81 "match":"match:eq"
82 }
83 ]
84});
85
86
Nils Diewald0b6c0412014-12-19 03:55:57 +000087describe('KorAP.Doc', function () {
88
89 // Create example factories
90 var stringFactory = buildFactory(KorAP.Doc, {
91 "key" : "author",
92 "value" : "Max Birkendale",
Nils Diewald2fe12e12015-03-06 16:47:06 +000093 "@type" : "koral:doc"
Nils Diewald0b6c0412014-12-19 03:55:57 +000094 });
95
96 // Create example factories
97 var dateFactory = buildFactory(KorAP.Doc, {
98 "key" : "pubDate",
99 "type" : "type:date",
100 "match" : "match:eq",
101 "value" : "2014-11-05",
Nils Diewald2fe12e12015-03-06 16:47:06 +0000102 "@type" : "koral:doc"
Nils Diewald0b6c0412014-12-19 03:55:57 +0000103 });
104
105 // Create example factories
106 var regexFactory = buildFactory(KorAP.Doc, {
107 "key" : "title",
108 "type" : "type:regex",
109 "value" : "[^b]ee.+?",
Nils Diewald2fe12e12015-03-06 16:47:06 +0000110 "@type" : "koral:doc"
Nils Diewald0b6c0412014-12-19 03:55:57 +0000111 });
112
113 it('should be initializable', function () {
114 var doc = KorAP.Doc.create();
115 expect(doc.matchop()).toEqual('eq');
116 expect(doc.key()).toBeUndefined();
117 expect(doc.value()).toBeUndefined();
118 expect(doc.type()).toEqual("string");
119 });
120
121 it('should be definable', function () {
122
123 // Empty doc
124 var doc = KorAP.Doc.create();
125
126 // Set values
127 doc.key("title");
128 doc.value("Der alte Mann");
129 expect(doc.matchop()).toEqual('eq');
130 expect(doc.key()).toEqual("title");
131 expect(doc.type()).toEqual("string");
132 expect(doc.value()).toEqual("Der alte Mann");
133 });
134
135
136 it('should deserialize JSON-LD string', function () {
137 var doc;
138
139 // String default
140 doc = stringFactory.create();
141 expect(doc.matchop()).toEqual('eq');
142 expect(doc.key()).toEqual("author");
143 expect(doc.type()).toEqual("string");
144 expect(doc.value()).toEqual("Max Birkendale");
145
146 // No valid string
147 doc = stringFactory.create({
148 value : undefined
149 });
150 expect(doc).toBeUndefined();
151
152 // No valid string
153 doc = stringFactory.create({
154 value : { "foo" : "bar" }
155 });
156 expect(doc).toBeUndefined();
157
158 // Change match type
159 doc = stringFactory.create({
160 "match" : "match:ne"
161 });
162
163 expect(doc.matchop()).toEqual('ne');
164 expect(doc.key()).toEqual("author");
165 expect(doc.type()).toEqual("string");
166 expect(doc.value()).toEqual("Max Birkendale");
167
168
169 // Invalid match type
170 doc = stringFactory.create({
171 "match" : { "foo" : "bar" }
172 });
173 expect(doc).toBeUndefined();
174 });
175
176 it('should deserialize JSON-LD regex', function () {
177 var doc = regexFactory.create();
178 expect(doc.key()).toEqual("title");
179 expect(doc.type()).toEqual("regex");
180 expect(doc.value()).toEqual("[^b]ee.+?");
181 expect(doc.matchop()).toEqual('eq');
182
183 // change matcher
184 doc = regexFactory.create({
185 match : "match:ne"
186 });
187 expect(doc.matchop()).toEqual('ne');
188
189 // Invalid matcher
190 doc = regexFactory.create({
191 match : "match:chook"
192 });
193 expect(doc).toBeUndefined();
194
195 // Invalid regex
196 doc = regexFactory.create({
197 value : "[^b"
198 });
199 expect(doc).toBeUndefined();
200 });
201
202 it('should deserialize JSON-LD date', function () {
203
204 // Normal date
205 doc = dateFactory.create({});
206
207 expect(doc.matchop()).toEqual('eq');
208 expect(doc.key()).toEqual("pubDate");
209 expect(doc.type()).toEqual("date");
210 expect(doc.value()).toEqual("2014-11-05");
211
212 // Short date 1
213 doc = dateFactory.create({
214 "value" : "2014-11"
215 });
216
217 expect(doc.matchop()).toEqual('eq');
218 expect(doc.key()).toEqual("pubDate");
219 expect(doc.type()).toEqual("date");
220 expect(doc.value()).toEqual("2014-11");
221
222 // Short date 2
223 doc = dateFactory.create({
224 "value" : "2014"
225 });
226
227 expect(doc.matchop()).toEqual('eq');
228 expect(doc.key()).toEqual("pubDate");
229 expect(doc.type()).toEqual("date");
230 expect(doc.value()).toEqual("2014");
231
232 // Invalid date!
233 doc = dateFactory.create({
234 "value" : "2014-11-050"
235 });
236 expect(doc).toBeUndefined();
237
238 // Invalid matcher!
239 doc = dateFactory.create({
240 "match" : "match:ne",
241 });
242 expect(doc).toBeUndefined();
243 });
244
Nils Diewaldf219eb82015-01-07 20:15:42 +0000245 it('should be serializale to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000246
247 // Empty doc
248 var doc = KorAP.Doc.create();
249 expect(doc.toJson()).toEqual(jasmine.any(Object));
250
251 // Serialize string
252 doc = stringFactory.create();
253 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000254 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000255 "type" : "type:string",
256 "key" : "author",
257 "value" : "Max Birkendale",
258 "match" : "match:eq"
259 }));
260
261 // Serialize regex
262 doc = regexFactory.create();
263 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000264 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000265 "type" : "type:regex",
266 "value" : "[^b]ee.+?",
267 "match" : "match:eq",
268 "key" : 'title'
269 }));
270
271 doc = regexFactory.create({
272 match: "match:ne"
273 });
274 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000275 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000276 "type" : "type:regex",
277 "value" : "[^b]ee.+?",
278 "match" : "match:ne",
279 "key" : 'title'
280 }));
281
282 doc = dateFactory.create();
283 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000284 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000285 "type" : "type:date",
286 "value" : "2014-11-05",
287 "match" : "match:eq",
288 "key" : 'pubDate'
289 }));
290
291 doc = dateFactory.create({
292 value : "2014"
293 });
294 expect(doc.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000295 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000296 "type" : "type:date",
297 "value" : "2014",
298 "match" : "match:eq",
299 "key" : 'pubDate'
300 }));
301 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000302
303 it('should be serializale to String', function () {
304
305 // Empty doc
306 var doc = KorAP.Doc.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000307 expect(doc.toQuery()).toEqual("");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000308
309 // Serialize string
310 doc = stringFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000311 expect(doc.toQuery()).toEqual('author = "Max Birkendale"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000312
313 // Serialize string with quotes
314 doc = stringFactory.create({ "value" : 'Max "Der Coole" Birkendate'});
Nils Diewaldd599d542015-01-08 20:41:34 +0000315 expect(doc.toQuery()).toEqual('author = "Max \\"Der Coole\\" Birkendate"');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000316
317 // Serialize regex
318 doc = regexFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000319 expect(doc.toQuery()).toEqual('title = /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000320
321 doc = regexFactory.create({
322 match: "match:ne"
323 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000324 expect(doc.toQuery()).toEqual('title != /[^b]ee.+?/');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000325
326 doc = dateFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000327 expect(doc.toQuery()).toEqual('pubDate in 2014-11-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000328
329 doc = dateFactory.create({
330 value : "2014"
331 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000332 expect(doc.toQuery()).toEqual('pubDate in 2014');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000333 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000334});
335
336
337describe('KorAP.DocGroup', function () {
338 // Create example factories
339 var docFactory = buildFactory(
340 KorAP.Doc,
341 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000342 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000343 "match":"match:eq",
344 "key" : "author",
345 "value" : "Max Birkendale"
346 }
347 );
348
349 var docGroupFactory = buildFactory(
350 KorAP.DocGroup, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000351 "@type" : "koral:docGroup",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000352 "operation" : "operation:and",
353 "operands" : [
354 docFactory.create().toJson(),
355 docFactory.create({
356 "key" : "pubDate",
357 "type" : "type:date",
358 "value" : "2014-12-05"
359 }).toJson()
360 ]
361 });
362
Nils Diewald0b6c0412014-12-19 03:55:57 +0000363
Nils Diewald8f4e2542014-12-19 04:42:09 +0000364 it('should be initializable', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000365 // Create empty group
366 var docGroup = KorAP.DocGroup.create();
367 expect(docGroup.operation()).toEqual('and');
368
369 // Create empty group
Nils Diewald8f4e2542014-12-19 04:42:09 +0000370 docGroup = KorAP.DocGroup.create();
371 docGroup.operation('or');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000372 expect(docGroup.operation()).toEqual('or');
373 });
374
375 it('should be definable', function () {
376
377 // Empty group
378 var docGroup = KorAP.DocGroup.create();
379 expect(docGroup.operation()).toEqual('and');
380
381 // Set values
382 docGroup.operation("or");
383 expect(docGroup.operation()).toEqual('or');
384
385 // Set invalid values
386 docGroup.operation("hui");
387 expect(docGroup.operation()).toEqual('or');
388 });
389
390 it('should be deserializable', function () {
391 var docGroup = docGroupFactory.create();
392 expect(docGroup.operation()).toEqual("and");
393 expect(docGroup.operands().length).toEqual(2);
394
395 var op1 = docGroup.getOperand(0);
396 expect(op1.type()).toEqual("string");
397 expect(op1.key()).toEqual("author");
398 expect(op1.value()).toEqual("Max Birkendale");
399 expect(op1.matchop()).toEqual("eq");
400
401 var op2 = docGroup.getOperand(1);
402 expect(op2.type()).toEqual("date");
403 expect(op2.key()).toEqual("pubDate");
404 expect(op2.value()).toEqual("2014-12-05");
405 expect(op2.matchop()).toEqual("eq");
406
Nils Diewaldf219eb82015-01-07 20:15:42 +0000407 // Append empty group
Nils Diewald966abf12014-12-20 02:27:45 +0000408 var newGroup = docGroup.append(KorAP.DocGroup.create());
Nils Diewald8f4e2542014-12-19 04:42:09 +0000409 newGroup.operation('or');
Nils Diewald966abf12014-12-20 02:27:45 +0000410 newGroup.append(docFactory.create());
411 newGroup.append(docFactory.create({
Nils Diewald0b6c0412014-12-19 03:55:57 +0000412 "type" : "type:regex",
413 "key" : "title",
414 "value" : "^e.+?$",
415 "match" : "match:ne"
416 }));
417
418 expect(docGroup.operation()).toEqual("and");
419 expect(docGroup.operands().length).toEqual(3);
420
421 var op1 = docGroup.getOperand(0);
422 expect(op1.ldType()).toEqual("doc");
423 expect(op1.type()).toEqual("string");
424 expect(op1.key()).toEqual("author");
425 expect(op1.value()).toEqual("Max Birkendale");
426 expect(op1.matchop()).toEqual("eq");
427
428 var op2 = docGroup.getOperand(1);
429 expect(op2.ldType()).toEqual("doc");
430 expect(op2.type()).toEqual("date");
431 expect(op2.key()).toEqual("pubDate");
432 expect(op2.value()).toEqual("2014-12-05");
433 expect(op2.matchop()).toEqual("eq");
434
435 var op3 = docGroup.getOperand(2);
436 expect(op3.ldType()).toEqual("docGroup");
437 expect(op3.operation()).toEqual("or");
438
439 var op4 = op3.getOperand(0);
440 expect(op4.ldType()).toEqual("doc");
441 expect(op4.type()).toEqual("string");
442 expect(op4.key()).toEqual("author");
443 expect(op4.value()).toEqual("Max Birkendale");
444 expect(op4.matchop()).toEqual("eq");
445
446 var op5 = op3.getOperand(1);
447 expect(op5.ldType()).toEqual("doc");
448 expect(op5.type()).toEqual("regex");
449 expect(op5.key()).toEqual("title");
450 expect(op5.value()).toEqual("^e.+?$");
451 expect(op5.matchop()).toEqual("ne");
452 });
453
Nils Diewaldf219eb82015-01-07 20:15:42 +0000454 it('should be serializable to JSON', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000455 var docGroup = docGroupFactory.create();
456
457 expect(docGroup.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000458 "@type" : "koral:docGroup",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000459 "operation" : "operation:and",
460 "operands" : [
461 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000462 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000463 "key" : 'author',
464 "match": 'match:eq',
465 "value": 'Max Birkendale',
466 "type": 'type:string'
467 },
468 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000469 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000470 "key": 'pubDate',
471 "match": 'match:eq',
472 "value": '2014-12-05',
473 "type": 'type:date'
474 }
475 ]
476 }));
477 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000478
479 it('should be serializable to String', function () {
480 var docGroup = docGroupFactory.create();
Nils Diewaldd599d542015-01-08 20:41:34 +0000481 expect(docGroup.toQuery()).toEqual('author = "Max Birkendale" & pubDate in 2014-12-05');
Nils Diewaldf219eb82015-01-07 20:15:42 +0000482
483 docGroup = docGroupFactory.create({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000484 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000485 "operation" : "operation:or",
486 "operands" : [
487 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000488 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000489 "key" : 'author',
490 "match": 'match:eq',
491 "value": 'Max Birkendale',
492 "type": 'type:string'
493 },
494 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000495 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000496 "operation" : "operation:and",
497 "operands" : [
498 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000499 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000500 "key": 'pubDate',
501 "match": 'match:geq',
502 "value": '2014-05-12',
503 "type": 'type:date'
504 },
505 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000506 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000507 "key": 'pubDate',
508 "match": 'match:leq',
509 "value": '2014-12-05',
510 "type": 'type:date'
511 },
512 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000513 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000514 "key": 'foo',
515 "match": 'match:ne',
516 "value": '[a]?bar',
517 "type": 'type:regex'
518 }
519 ]
520 }
521 ]
522 });
Nils Diewaldd599d542015-01-08 20:41:34 +0000523 expect(docGroup.toQuery()).toEqual(
Nils Diewaldf219eb82015-01-07 20:15:42 +0000524 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & foo != /[a]?bar/)'
525 );
526 });
Nils Diewald0b6c0412014-12-19 03:55:57 +0000527});
528
Nils Diewald966abf12014-12-20 02:27:45 +0000529describe('KorAP.UnspecifiedDoc', function () {
530 it('should be initializable', function () {
Nils Diewald8f6b6102015-01-08 18:25:33 +0000531 var doc = KorAP.UnspecifiedDoc.create();
532 var docElement = doc.element();
Nils Diewaldd599d542015-01-08 20:41:34 +0000533 expect(docElement.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000534 expect(docElement.firstChild.firstChild.data).toEqual('⋯');
Nils Diewalde15b7a22015-01-09 21:50:21 +0000535 expect(docElement.lastChild.lastChild.data).toEqual('⋯');
Nils Diewaldfda29d92015-01-22 17:28:01 +0000536 expect(doc.toQuery()).toEqual('');
Nils Diewald966abf12014-12-20 02:27:45 +0000537
Nils Diewald8f6b6102015-01-08 18:25:33 +0000538 // Only removable
Nils Diewald966abf12014-12-20 02:27:45 +0000539 expect(docElement.lastChild.children.length).toEqual(0);
540 });
541
542 it('should be removable, when no root', function () {
543 var docGroup = KorAP.DocGroup.create();
544 docGroup.operation('or');
545 expect(docGroup.operation()).toEqual('or');
546
547 docGroup.append({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000548 "@type": 'koral:doc',
Nils Diewald966abf12014-12-20 02:27:45 +0000549 "key": 'pubDate',
550 "match": 'match:eq',
551 "value": '2014-12-05',
552 "type": 'type:date'
553 });
554
Nils Diewaldf219eb82015-01-07 20:15:42 +0000555 // Add unspecified object
556 docGroup.append();
557
Nils Diewald966abf12014-12-20 02:27:45 +0000558 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
559 expect(docGroup.element().children[0].getAttribute('class')).toEqual('doc');
560
Nils Diewald966abf12014-12-20 02:27:45 +0000561 var unspec = docGroup.element().children[1];
Nils Diewaldd599d542015-01-08 20:41:34 +0000562 expect(unspec.getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000563
564 // Removable
565 expect(unspec.lastChild.children.length).toEqual(1);
566 expect(unspec.lastChild.children[0].getAttribute('class')).toEqual('delete');
567 });
Nils Diewaldfda29d92015-01-22 17:28:01 +0000568
569 it('should be replaceable by a doc', function () {
570 var doc = KorAP.UnspecifiedDoc.create();
571 expect(doc.ldType()).toEqual("non");
572 // No parent, therefor not updateable
573 expect(doc.key("baum")).toBeNull();
574
575 var docGroup = KorAP.DocGroup.create();
576 docGroup.operation('or');
577 expect(docGroup.operation()).toEqual('or');
578
579 docGroup.append({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000580 "@type": 'koral:doc',
Nils Diewaldfda29d92015-01-22 17:28:01 +0000581 "key": 'pubDate',
582 "match": 'match:eq',
583 "value": '2014-12-05',
584 "type": 'type:date'
585 });
586
587 expect(docGroup.toQuery()).toEqual("pubDate in 2014-12-05");
588 docGroup.append();
589
590 expect(docGroup.getOperand(0).ldType()).toEqual("doc");
591 expect(docGroup.getOperand(1).ldType()).toEqual("non");
592
593 var op = docGroup.getOperand(1).element().lastChild;
594 expect(op.getAttribute('class')).toEqual('operators');
595 expect(op.children[0].getAttribute('class')).toEqual('delete');
596 expect(op.children.length).toEqual(1);
597
598 // Replace unspecified doc
599 expect(docGroup.getOperand(1).key("name")).not.toBeNull();
600 expect(docGroup.getOperand(1).ldType()).toEqual("doc");
601 expect(docGroup.getOperand(1).key()).toEqual("name");
602 expect(docGroup.getOperand(1).value()).toEqual("");
603
604 op = docGroup.getOperand(1).element().lastChild;
605 expect(op.getAttribute('class')).toEqual('operators');
606 expect(op.children[0].getAttribute('class')).toEqual('and');
607 expect(op.children[1].getAttribute('class')).toEqual('or');
608 expect(op.children[2].getAttribute('class')).toEqual('delete');
609 expect(op.children.length).toEqual(3);
610
611 docGroup.getOperand(1).value("Pachelbel");
612 expect(docGroup.getOperand(1).value()).toEqual("Pachelbel");
613 expect(docGroup.getOperand(1).type()).toEqual("string");
614 expect(docGroup.getOperand(1).matchop()).toEqual("eq");
615
616 // Specified!
617 expect(docGroup.toQuery()).toEqual('pubDate in 2014-12-05 | name = "Pachelbel"');
618 });
619
620 it('should be replaceable on root', function () {
621 var vc = KorAP.VirtualCollection.render();
622 expect(vc.toQuery()).toEqual("");
623
624 expect(vc.root().ldType()).toEqual("non");
625
626 // No operators on root
627 op = vc.root().element().lastChild;
628 expect(op.lastChild.textContent).toEqual('⋯');
629
630 // Replace
631 expect(vc.root().key("baum")).not.toBeNull();
632 expect(vc.root().ldType()).toEqual("doc");
633
634 op = vc.root().element().lastChild;
635 expect(op.getAttribute('class')).toEqual('operators');
636 expect(op.children[0].getAttribute('class')).toEqual('and');
637 expect(op.children[1].getAttribute('class')).toEqual('or');
638 expect(op.children[2].getAttribute('class')).toEqual('delete');
639 expect(op.children.length).toEqual(3);
640 });
Nils Diewald966abf12014-12-20 02:27:45 +0000641});
642
Nils Diewald8f4e2542014-12-19 04:42:09 +0000643describe('KorAP.Doc element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000644 it('should be initializable', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000645 var docElement = KorAP.Doc.create(undefined, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000646 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000647 "key":"Titel",
648 "value":"Baum",
649 "match":"match:eq"
650 });
651 expect(docElement.key()).toEqual('Titel');
652 expect(docElement.matchop()).toEqual('eq');
653 expect(docElement.value()).toEqual('Baum');
654
655 var docE = docElement.element();
656 expect(docE.children[0].firstChild.data).toEqual('Titel');
657 expect(docE.children[1].firstChild.data).toEqual('eq');
658 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
659 expect(docE.children[2].firstChild.data).toEqual('Baum');
660 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
661
662 expect(docElement.toJson()).toEqual(jasmine.objectContaining({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000663 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000664 "key":"Titel",
665 "value":"Baum",
666 "match":"match:eq"
667 }));
668 });
669});
670
Nils Diewald8f4e2542014-12-19 04:42:09 +0000671describe('KorAP.DocGroup element', function () {
Nils Diewald0b6c0412014-12-19 03:55:57 +0000672 it('should be initializable', function () {
673
Nils Diewald8f4e2542014-12-19 04:42:09 +0000674 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000675 "@type" : "koral:docGroup",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000676 "operation" : "operation:and",
677 "operands" : [
678 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000679 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000680 "key" : 'author',
681 "match": 'match:eq',
682 "value": 'Max Birkendale',
683 "type": 'type:string'
684 },
685 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000686 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000687 "key": 'pubDate',
688 "match": 'match:eq',
689 "value": '2014-12-05',
690 "type": 'type:date'
691 }
692 ]
693 });
694
Nils Diewald8f4e2542014-12-19 04:42:09 +0000695 expect(docGroup.operation()).toEqual('and');
696 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000697 expect(e.getAttribute('class')).toEqual('docGroup');
698 expect(e.getAttribute('data-operation')).toEqual('and');
699
700 var first = e.children[0];
701 expect(first.getAttribute('class')).toEqual('doc');
702 expect(first.children[0].getAttribute('class')).toEqual('key');
703 expect(first.children[1].getAttribute('class')).toEqual('match');
704 expect(first.children[2].getAttribute('class')).toEqual('value');
705 expect(first.children[2].getAttribute('data-type')).toEqual('string');
706 expect(first.children[0].firstChild.data).toEqual('author');
707 expect(first.children[1].firstChild.data).toEqual('eq');
708 expect(first.children[2].firstChild.data).toEqual('Max Birkendale');
709
710 var second = e.children[1];
711 expect(second.getAttribute('class')).toEqual('doc');
712 expect(second.children[0].getAttribute('class')).toEqual('key');
713 expect(second.children[1].getAttribute('class')).toEqual('match');
714 expect(second.children[2].getAttribute('class')).toEqual('value');
715 expect(second.children[2].getAttribute('data-type')).toEqual('date');
716 expect(second.children[0].firstChild.data).toEqual('pubDate');
717 expect(second.children[1].firstChild.data).toEqual('eq');
718 expect(second.children[2].firstChild.data).toEqual('2014-12-05');
719
720 });
721
722 it('should be deserializable with nested groups', function () {
Nils Diewald8f4e2542014-12-19 04:42:09 +0000723 var docGroup = KorAP.DocGroup.create(undefined, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000724 "@type" : "koral:docGroup",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000725 "operation" : "operation:or",
726 "operands" : [
727 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000728 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000729 "key" : 'author',
730 "match": 'match:eq',
731 "value": 'Max Birkendale',
732 "type": 'type:string'
733 },
734 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000735 "@type" : "koral:docGroup",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000736 "operation" : "operation:and",
737 "operands" : [
738 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000739 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000740 "key": 'pubDate',
741 "match": 'match:geq',
742 "value": '2014-05-12',
743 "type": 'type:date'
744 },
745 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000746 "@type": 'koral:doc',
Nils Diewald0b6c0412014-12-19 03:55:57 +0000747 "key": 'pubDate',
748 "match": 'match:leq',
749 "value": '2014-12-05',
750 "type": 'type:date'
751 }
752 ]
753 }
754 ]
755 });
756
Nils Diewald8f4e2542014-12-19 04:42:09 +0000757 expect(docGroup.operation()).toEqual('or');
758 var e = docGroup.element();
Nils Diewald0b6c0412014-12-19 03:55:57 +0000759 expect(e.getAttribute('class')).toEqual('docGroup');
760 expect(e.getAttribute('data-operation')).toEqual('or');
761
Nils Diewald966abf12014-12-20 02:27:45 +0000762 expect(e.children[0].getAttribute('class')).toEqual('doc');
763 var docop = e.children[0].lastChild;
764 expect(docop.getAttribute('class')).toEqual('operators');
765 expect(docop.children[0].getAttribute('class')).toEqual('and');
766 expect(docop.children[1].getAttribute('class')).toEqual('or');
767 expect(docop.children[2].getAttribute('class')).toEqual('delete');
768
769 expect(e.children[1].getAttribute('class')).toEqual('docGroup');
770 expect(e.children[1].getAttribute('data-operation')).toEqual('and');
771
772 // This and-operation can be "or"ed or "delete"d
773 var secop = e.children[1].children[2];
774 expect(secop.getAttribute('class')).toEqual('operators');
775 expect(secop.children[0].getAttribute('class')).toEqual('or');
776 expect(secop.children[1].getAttribute('class')).toEqual('delete');
777
778 // This or-operation can be "and"ed or "delete"d
779 expect(e.children[2].getAttribute('class')).toEqual('operators');
780 expect(e.lastChild.getAttribute('class')).toEqual('operators');
781 expect(e.lastChild.children[0].getAttribute('class')).toEqual('and');
782 expect(e.lastChild.children[1].getAttribute('class')).toEqual('delete');
Nils Diewald0b6c0412014-12-19 03:55:57 +0000783
784 });
785});
786
787describe('KorAP.VirtualCollection', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000788
789 var simpleGroupFactory = buildFactory(KorAP.DocGroup, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000790 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000791 "operation" : "operation:and",
792 "operands" : [
793 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000794 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000795 "key" : 'author',
796 "match": 'match:eq',
797 "value": 'Max Birkendale',
798 "type": 'type:string'
799 },
800 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000801 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000802 "key": 'pubDate',
803 "match": 'match:eq',
804 "value": '2014-12-05',
805 "type": 'type:date'
806 }
807 ]
808 });
809
810 var nestedGroupFactory = buildFactory(KorAP.VirtualCollection, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000811 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000812 "operation" : "operation:or",
813 "operands" : [
814 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000815 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000816 "key" : 'author',
817 "match": 'match:eq',
818 "value": 'Max Birkendale',
819 "type": 'type:string'
820 },
821 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000822 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000823 "operation" : "operation:and",
824 "operands" : [
825 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000826 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000827 "key": 'pubDate',
828 "match": 'match:geq',
829 "value": '2014-05-12',
830 "type": 'type:date'
831 },
832 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000833 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000834 "key": 'pubDate',
835 "match": 'match:leq',
836 "value": '2014-12-05',
837 "type": 'type:date'
838 }
839 ]
840 }
841 ]
842 });
843
844 var flatGroupFactory = buildFactory(KorAP.VirtualCollection, {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000845 "@type" : "koral:docGroup",
Nils Diewaldf219eb82015-01-07 20:15:42 +0000846 "operation" : "operation:and",
847 "operands" : [
848 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000849 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000850 "key": 'pubDate',
851 "match": 'match:geq',
852 "value": '2014-05-12',
853 "type": 'type:date'
854 },
855 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000856 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000857 "key": 'pubDate',
858 "match": 'match:leq',
859 "value": '2014-12-05',
860 "type": 'type:date'
861 },
862 {
Nils Diewald2fe12e12015-03-06 16:47:06 +0000863 "@type": 'koral:doc',
Nils Diewaldf219eb82015-01-07 20:15:42 +0000864 "key": 'foo',
865 "match": 'match:eq',
866 "value": 'bar',
867 "type": 'type:string'
868 }
869 ]
870 });
871
Nils Diewald0b6c0412014-12-19 03:55:57 +0000872 it('should be initializable', function () {
873 var vc = KorAP.VirtualCollection.render();
874 expect(vc.element().getAttribute('class')).toEqual('vc');
Nils Diewaldd599d542015-01-08 20:41:34 +0000875 expect(vc.root().element().getAttribute('class')).toEqual('doc unspecified');
Nils Diewald966abf12014-12-20 02:27:45 +0000876
877 // Not removable
878 expect(vc.root().element().lastChild.children.length).toEqual(0);
Nils Diewald0b6c0412014-12-19 03:55:57 +0000879 });
880
881 it('should be based on a doc', function () {
882 var vc = KorAP.VirtualCollection.render({
Nils Diewald2fe12e12015-03-06 16:47:06 +0000883 "@type" : "koral:doc",
Nils Diewald0b6c0412014-12-19 03:55:57 +0000884 "key":"Titel",
885 "value":"Baum",
886 "match":"match:eq"
887 });
888
889 expect(vc.element().getAttribute('class')).toEqual('vc');
890 expect(vc.root().element().getAttribute('class')).toEqual('doc');
891 expect(vc.root().key()).toEqual('Titel');
892 expect(vc.root().value()).toEqual('Baum');
893 expect(vc.root().matchop()).toEqual('eq');
894
895 var docE = vc.root().element();
896 expect(docE.children[0].firstChild.data).toEqual('Titel');
897 expect(docE.children[1].firstChild.data).toEqual('eq');
898 expect(docE.children[1].getAttribute('data-type')).toEqual('string');
899 expect(docE.children[2].firstChild.data).toEqual('Baum');
900 expect(docE.children[2].getAttribute('data-type')).toEqual('string');
901 });
902
903 it('should be based on a docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000904 var vc = KorAP.VirtualCollection.render(simpleGroupFactory.create().toJson());
Nils Diewald0b6c0412014-12-19 03:55:57 +0000905
906 expect(vc.element().getAttribute('class')).toEqual('vc');
907 expect(vc.root().element().getAttribute('class')).toEqual('docGroup');
908 expect(vc.root().operation()).toEqual('and');
909
910 var docGroup = vc.root();
911
912 var first = docGroup.getOperand(0);
913 expect(first.key()).toEqual('author');
914 expect(first.value()).toEqual('Max Birkendale');
915 expect(first.matchop()).toEqual('eq');
916
917 var second = docGroup.getOperand(1);
918 expect(second.key()).toEqual('pubDate');
919 expect(second.value()).toEqual('2014-12-05');
920 expect(second.matchop()).toEqual('eq');
921 });
922
Nils Diewald5c817a42015-01-06 01:08:56 +0000923
Nils Diewald966abf12014-12-20 02:27:45 +0000924 it('should be based on a nested docGroup', function () {
Nils Diewaldf219eb82015-01-07 20:15:42 +0000925 var vc = nestedGroupFactory.create();
926
Nils Diewald5c817a42015-01-06 01:08:56 +0000927 expect(vc.element().getAttribute('class')).toEqual('vc');
928 expect(vc.element().firstChild.getAttribute('class')).toEqual('docGroup');
929 expect(vc.element().firstChild.children[0].getAttribute('class')).toEqual('doc');
930 var dg = vc.element().firstChild.children[1];
931 expect(dg.getAttribute('class')).toEqual('docGroup');
932 expect(dg.children[0].getAttribute('class')).toEqual('doc');
933 expect(dg.children[1].getAttribute('class')).toEqual('doc');
934 expect(dg.children[2].getAttribute('class')).toEqual('operators');
935 expect(vc.element().firstChild.children[2].getAttribute('class')).toEqual('operators');
936 });
937
Nils Diewaldf219eb82015-01-07 20:15:42 +0000938 it('should be modifiable by deletion in flat docGroups', function () {
939 var vc = flatGroupFactory.create();
Nils Diewald0297ba12015-01-05 21:56:12 +0000940 var docGroup = vc.root();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000941
942 expect(docGroup.element().getAttribute('class')).toEqual('docGroup');
943
Nils Diewald0297ba12015-01-05 21:56:12 +0000944 var doc = docGroup.getOperand(1);
945 expect(doc.key()).toEqual("pubDate");
Nils Diewaldf219eb82015-01-07 20:15:42 +0000946 expect(doc.value()).toEqual("2014-12-05");
Nils Diewald5c817a42015-01-06 01:08:56 +0000947
948 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000949 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000950 expect(doc._element).toEqual(undefined);
951
Nils Diewald0297ba12015-01-05 21:56:12 +0000952 doc = docGroup.getOperand(1);
953 expect(doc.key()).toEqual("foo");
Nils Diewald0297ba12015-01-05 21:56:12 +0000954
Nils Diewald5c817a42015-01-06 01:08:56 +0000955 // Remove operand 1
Nils Diewaldf219eb82015-01-07 20:15:42 +0000956 expect(docGroup.delOperand(doc).update()).not.toBeUndefined();
Nils Diewald5c817a42015-01-06 01:08:56 +0000957 expect(doc._element).toEqual(undefined);
Nils Diewald0297ba12015-01-05 21:56:12 +0000958
Nils Diewaldf219eb82015-01-07 20:15:42 +0000959 // Only one operand left ...
Nils Diewald5c817a42015-01-06 01:08:56 +0000960 expect(docGroup.getOperand(1)).toBeUndefined();
Nils Diewaldf219eb82015-01-07 20:15:42 +0000961 // ... but there shouldn't be a group anymore at all!
962 expect(docGroup.getOperand(0)).toBeUndefined();
963
964 var obj = vc.root();
965 expect(obj.ldType()).toEqual("doc");
966 expect(obj.key()).toEqual("pubDate");
967 expect(obj.value()).toEqual("2014-05-12");
968
969 expect(obj.element().getAttribute('class')).toEqual('doc');
Nils Diewald5c817a42015-01-06 01:08:56 +0000970 });
Nils Diewaldf219eb82015-01-07 20:15:42 +0000971
972
973 it('should be modifiable by deletion in nested docGroups (root case)', function () {
974 var vc = nestedGroupFactory.create();
975
Nils Diewaldd599d542015-01-08 20:41:34 +0000976 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +0000977 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05)'
978 );
979
Nils Diewaldf219eb82015-01-07 20:15:42 +0000980 var docGroup = vc.root();
981 expect(docGroup.ldType()).toEqual("docGroup");
982 expect(docGroup.operation()).toEqual("or");
983
984 var doc = docGroup.getOperand(0);
985 expect(doc.key()).toEqual("author");
986 expect(doc.value()).toEqual("Max Birkendale");
987
988 docGroup = docGroup.getOperand(1);
989 expect(docGroup.operation()).toEqual("and");
990
991 doc = docGroup.getOperand(0);
992 expect(doc.key()).toEqual("pubDate");
993 expect(doc.matchop()).toEqual("geq");
994 expect(doc.value()).toEqual("2014-05-12");
995 expect(doc.type()).toEqual("date");
996
997 doc = docGroup.getOperand(1);
998 expect(doc.key()).toEqual("pubDate");
999 expect(doc.matchop()).toEqual("leq");
1000 expect(doc.value()).toEqual("2014-12-05");
1001 expect(doc.type()).toEqual("date");
1002
1003 // Remove first operand so everything becomes root
1004 expect(
1005 vc.root().delOperand(
1006 vc.root().getOperand(0)
1007 ).update().ldType()
1008 ).toEqual("docGroup");
Nils Diewald8e7182e2015-01-08 15:02:07 +00001009
Nils Diewaldf219eb82015-01-07 20:15:42 +00001010 expect(vc.root().ldType()).toEqual("docGroup");
1011 expect(vc.root().operation()).toEqual("and");
Nils Diewald8e7182e2015-01-08 15:02:07 +00001012 expect(vc.root().getOperand(0).ldType()).toEqual("doc");
1013
Nils Diewaldd599d542015-01-08 20:41:34 +00001014 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +00001015 'pubDate since 2014-05-12 & pubDate until 2014-12-05'
1016 );
Nils Diewaldf219eb82015-01-07 20:15:42 +00001017 });
1018
1019 it('should be modifiable by deletion in nested docGroups (resolve group case)', function () {
1020 var vc = nestedGroupFactory.create();
1021
1022 // Get nested group
1023 var firstGroup = vc.root().getOperand(1);
1024 firstGroup.append(simpleGroupFactory.create({ "operation" : "operation:or" }));
1025
1026 // Structur is now:
1027 // or(doc, and(doc, doc, or(doc, doc)))
1028
1029 // Get nested or in and
1030 var orGroup = vc.root().getOperand(1).getOperand(2);
1031 expect(orGroup.ldType()).toEqual("docGroup");
1032 expect(orGroup.operation()).toEqual("or");
1033
1034 // Remove
1035 // Structur is now:
1036 // or(doc, and(doc, doc, doc)))
1037 expect(orGroup.delOperand(orGroup.getOperand(0)).update().operation()).toEqual("and");
1038 expect(vc.root().getOperand(1).operands().length).toEqual(3);
1039 });
1040
1041 it('should be modifiable by deletion in nested docGroups (identical group case)', function () {
1042 var vc = nestedGroupFactory.create();
1043
1044 // Get nested group
1045 var firstGroup = vc.root().getOperand(1);
Nils Diewald8e7182e2015-01-08 15:02:07 +00001046 firstGroup.append(simpleGroupFactory.create({
1047 "operation" : "operation:or"
1048 }));
1049 var oldAuthor = firstGroup.getOperand(2).getOperand(0);
1050 oldAuthor.key("title");
1051 oldAuthor.value("Der Birnbaum");
Nils Diewaldf219eb82015-01-07 20:15:42 +00001052
1053 // Structur is now:
1054 // or(doc, and(doc, doc, or(doc, doc)))
Nils Diewaldd599d542015-01-08 20:41:34 +00001055 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +00001056 'author = "Max Birkendale" | (pubDate since 2014-05-12 & pubDate until 2014-12-05 & (title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001057 );
1058
1059 var andGroup = vc.root().getOperand(1);
1060
1061 // Get leading docs in and
1062 var doc1 = andGroup.getOperand(0);
1063 expect(doc1.ldType()).toEqual("doc");
1064 expect(doc1.value()).toEqual("2014-05-12");
1065 var doc2 = andGroup.getOperand(1);
1066 expect(doc2.ldType()).toEqual("doc");
1067 expect(doc2.value()).toEqual("2014-12-05");
1068
1069 // Remove 2
1070 expect(andGroup.delOperand(doc2).update().operation()).toEqual("and");
1071 // Structur is now:
1072 // or(doc, and(doc, or(doc, doc)))
1073
Nils Diewaldd599d542015-01-08 20:41:34 +00001074 expect(vc.toQuery()).toEqual(
1075 'author = "Max Birkendale"' +
1076 ' | (pubDate since 2014-05-12 & ' +
1077 '(title = "Der Birnbaum" | pubDate in 2014-12-05))'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001078 );
1079
1080
1081 // Remove 1
1082 expect(andGroup.delOperand(doc1).update().operation()).toEqual("or");
1083 // Structur is now:
1084 // or(doc, doc, doc)
1085
Nils Diewaldd599d542015-01-08 20:41:34 +00001086 expect(vc.toQuery()).toEqual(
Nils Diewald8e7182e2015-01-08 15:02:07 +00001087 'author = "Max Birkendale" | title = "Der Birnbaum" | pubDate in 2014-12-05'
Nils Diewaldf219eb82015-01-07 20:15:42 +00001088 );
1089 });
Nils Diewald4019bd22015-01-08 19:57:50 +00001090
1091 it('should be reducible to unspecification', function () {
1092 var vc = demoFactory.create();
1093
Nils Diewaldd599d542015-01-08 20:41:34 +00001094 expect(vc.toQuery()).toEqual(vc.root().toQuery());
Nils Diewald4019bd22015-01-08 19:57:50 +00001095
Nils Diewaldd599d542015-01-08 20:41:34 +00001096 expect(vc.toQuery()).toEqual(
1097 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & ' +
1098 '(Titel = "Baum" | Veröffentlichungsort = "hihi")) ' +
1099 '| Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001100
1101 expect(vc.root().element().lastChild.children[0].firstChild.nodeValue).toEqual('and');
1102 expect(vc.root().element().lastChild.children[1].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001103 expect(vc.root().delOperand(vc.root().getOperand(0)).update()).not.toBeUndefined();
Nils Diewaldd599d542015-01-08 20:41:34 +00001104 expect(vc.toQuery()).toEqual('Untertitel = "huhu"');
Nils Diewald4019bd22015-01-08 19:57:50 +00001105
Nils Diewaldd599d542015-01-08 20:41:34 +00001106 var lc = vc.root().element().lastChild;
1107 expect(lc.children[0].firstChild.nodeValue).toEqual('and');
1108 expect(lc.children[1].firstChild.nodeValue).toEqual('or');
1109 expect(lc.children[2].firstChild.nodeValue).toEqual('×');
Nils Diewald4019bd22015-01-08 19:57:50 +00001110
1111 // Clean everything
1112 vc.clean();
Nils Diewaldfda29d92015-01-22 17:28:01 +00001113 expect(vc.toQuery()).toEqual('');
Nils Diewald4019bd22015-01-08 19:57:50 +00001114 });
Nils Diewald86dad5b2015-01-28 15:09:07 +00001115
1116 it('should flatten on import', function () {
1117 var vc = KorAP.VirtualCollection.create().render({
Nils Diewald2fe12e12015-03-06 16:47:06 +00001118 "@type":"koral:docGroup",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001119 "operation":"operation:or",
1120 "operands":[
1121 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001122 "@type":"koral:docGroup",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001123 "operation":"operation:or",
1124 "operands":[
1125 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001126 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001127 "key":"Titel",
1128 "value":"Baum",
1129 "match":"match:eq"
1130 },
1131 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001132 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001133 "key":"Veröffentlichungsort",
1134 "value":"hihi",
1135 "match":"match:eq"
1136 },
1137 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001138 "@type":"koral:docGroup",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001139 "operation":"operation:or",
1140 "operands":[
1141 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001142 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001143 "key":"Titel",
1144 "value":"Baum",
1145 "match":"match:eq"
1146 },
1147 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001148 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001149 "key":"Veröffentlichungsort",
1150 "value":"hihi",
1151 "match":"match:eq"
1152 }
1153 ]
1154 }
1155 ]
1156 },
1157 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001158 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001159 "key":"Untertitel",
1160 "value":"huhu",
1161 "match":"match:eq"
1162 }
1163 ]
1164 });
1165
1166 expect(vc.toQuery()).toEqual(
1167 'Titel = "Baum" | Veröffentlichungsort = "hihi" | Untertitel = "huhu"'
1168 );
1169
1170
1171 });
Nils Diewald0b6c0412014-12-19 03:55:57 +00001172});
1173
1174describe('KorAP.Operators', function () {
1175 it('should be initializable', function () {
1176 var op = KorAP.Operators.create(true, false, false);
1177 expect(op.and()).toBeTruthy();
1178 expect(op.or()).not.toBeTruthy();
1179 expect(op.del()).not.toBeTruthy();
1180
1181 op.and(false);
1182 expect(op.and()).not.toBeTruthy();
1183 expect(op.or()).not.toBeTruthy();
1184 expect(op.del()).not.toBeTruthy();
1185
1186 op.or(true);
1187 op.del(true);
1188 expect(op.and()).not.toBeTruthy();
1189 expect(op.or()).toBeTruthy();
1190 expect(op.del()).toBeTruthy();
1191
1192 var e = op.element();
1193 expect(e.getAttribute('class')).toEqual('operators');
1194 expect(e.children[0].getAttribute('class')).toEqual('or');
1195 expect(e.children[0].firstChild.data).toEqual('or');
1196 expect(e.children[1].getAttribute('class')).toEqual('delete');
1197 expect(e.children[1].firstChild.data).toEqual('×');
1198
1199 op.and(true);
1200 op.del(false);
1201 op.update();
1202
1203 e = op.element();
1204 expect(e.getAttribute('class')).toEqual('operators');
1205 expect(e.children[0].getAttribute('class')).toEqual('and');
1206 expect(e.children[0].firstChild.data).toEqual('and');
1207 expect(e.children[1].getAttribute('class')).toEqual('or');
1208 expect(e.children[1].firstChild.data).toEqual('or');
1209 });
1210});
Nils Diewalde15b7a22015-01-09 21:50:21 +00001211
1212describe('KorAP._delete (event)', function () {
1213 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
Nils Diewald2fe12e12015-03-06 16:47:06 +00001214 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001215 'operation' : 'operation:and',
1216 'operands' : [
1217 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001218 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001219 "key": 'pubDate',
1220 "match": 'match:eq',
1221 "value": '2014-12-05',
1222 "type": 'type:date'
1223 },
1224 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001225 "@type" : 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001226 'operation' : 'operation:or',
1227 'operands' : [
1228 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001229 '@type' : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001230 'key' : 'title',
1231 'value' : 'Hello World!'
1232 },
1233 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001234 '@type' : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001235 'key' : 'foo',
1236 'value' : 'bar'
1237 }
1238 ]
1239 }
1240 ]
1241 });
1242
1243 it('should clean on root docs', function () {
1244 var vc = KorAP.VirtualCollection.render({
Nils Diewald2fe12e12015-03-06 16:47:06 +00001245 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001246 "key": 'pubDate',
1247 "match": 'match:eq',
1248 "value": '2014-12-05',
1249 "type": 'type:date'
1250 });
1251 expect(vc.root().toQuery()).toEqual('pubDate in 2014-12-05');
1252 expect(vc.root().element().lastChild.getAttribute('class')).toEqual('operators');
1253
1254 // Clean with delete from root
1255 expect(vc.root().element().lastChild.lastChild.getAttribute('class')).toEqual('delete');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001256 _delOn(vc.root());
Nils Diewaldfda29d92015-01-22 17:28:01 +00001257 expect(vc.root().toQuery()).toEqual('');
Nils Diewalde15b7a22015-01-09 21:50:21 +00001258 expect(vc.root().element().lastChild.lastChild.data).toEqual('⋯');
1259 });
1260
1261 it ('should remove on nested docs', function () {
1262 var vc = KorAP.VirtualCollection.render(
1263 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001264 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001265 'operation' : 'operation:and',
1266 'operands' : [
1267 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001268 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001269 "key": 'pubDate',
1270 "match": 'match:eq',
1271 "value": '2014-12-05',
1272 "type": 'type:date'
1273 },
1274 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001275 "@type" : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001276 'key' : 'foo',
1277 'value' : 'bar'
1278 }
1279 ]
1280 }
1281 );
1282
1283 // Delete with direct element access
1284 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001285 _delOn(vc.root().getOperand(0));
1286
Nils Diewalde15b7a22015-01-09 21:50:21 +00001287 expect(vc.toQuery()).toEqual('foo = "bar"');
1288 expect(vc.root().ldType()).toEqual('doc');
1289 });
1290
1291 it ('should clean on doc groups', function () {
1292 var vc = KorAP.VirtualCollection.render(
1293 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001294 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001295 'operation' : 'operation:and',
1296 'operands' : [
1297 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001298 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001299 "key": 'pubDate',
1300 "match": 'match:eq',
1301 "value": '2014-12-05',
1302 "type": 'type:date'
1303 },
1304 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001305 "@type" : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001306 'key' : 'foo',
1307 'value' : 'bar'
1308 }
1309 ]
1310 }
1311 );
1312
1313 // Cleanwith direct element access
1314 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001315 _delOn(vc.root());
Nils Diewaldfda29d92015-01-22 17:28:01 +00001316 expect(vc.toQuery()).toEqual('');
Nils Diewalde15b7a22015-01-09 21:50:21 +00001317 expect(vc.root().ldType()).toEqual('non');
1318 });
1319
1320 it ('should remove on nested doc groups (case of ungrouping 1)', function () {
1321 var vc = complexVCFactory.create();
1322
1323 // Delete with direct element access
1324 expect(vc.toQuery()).toEqual(
1325 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1326 );
1327
1328 // Remove hello world:
Nils Diewald52f7eb12015-01-12 17:30:04 +00001329 _delOn(vc.root().getOperand(1).getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001330 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1331 expect(vc.root().ldType()).toEqual('docGroup');
1332 });
1333
1334 it ('should remove on nested doc groups (case of ungrouping 2)', function () {
1335 var vc = complexVCFactory.create();
1336
1337 // Delete with direct element access
1338 expect(vc.toQuery()).toEqual(
1339 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1340 );
1341
1342 // Remove bar
Nils Diewald52f7eb12015-01-12 17:30:04 +00001343 _delOn(vc.root().getOperand(1).getOperand(1));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001344 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & title = "Hello World!"');
1345 expect(vc.root().ldType()).toEqual('docGroup');
1346 expect(vc.root().operation()).toEqual('and');
1347 });
1348
1349 it ('should remove on nested doc groups (case of root changing)', function () {
1350 var vc = complexVCFactory.create();
1351
1352 // Delete with direct element access
1353 expect(vc.toQuery()).toEqual(
1354 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1355 );
1356
1357 // Remove bar
Nils Diewald52f7eb12015-01-12 17:30:04 +00001358 _delOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001359 expect(vc.toQuery()).toEqual('title = "Hello World!" | foo = "bar"');
1360 expect(vc.root().ldType()).toEqual('docGroup');
1361 expect(vc.root().operation()).toEqual('or');
1362 });
1363
1364 it ('should remove on nested doc groups (list flattening)', function () {
1365 var vc = KorAP.VirtualCollection.render(
1366 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001367 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001368 'operation' : 'operation:or',
1369 'operands' : [
1370 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001371 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001372 "key": 'pubDate',
1373 "match": 'match:eq',
1374 "value": '2014-12-05',
1375 "type": 'type:date'
1376 },
1377 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001378 "@type" : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001379 'key' : 'foo',
1380 'value' : 'bar'
1381 },
1382 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001383 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001384 'operation' : 'operation:and',
1385 'operands' : [
1386 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001387 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001388 "key": 'pubDate',
1389 "match": 'match:eq',
1390 "value": '2014-12-05',
1391 "type": 'type:date'
1392 },
1393 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001394 "@type" : 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001395 'operation' : 'operation:or',
1396 'operands' : [
1397 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001398 '@type' : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001399 'key' : 'title',
1400 'value' : 'Hello World!'
1401 },
1402 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001403 '@type' : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001404 'key' : 'yeah',
1405 'value' : 'juhu'
1406 }
1407 ]
1408 }
1409 ]
1410 }
1411 ]
1412 }
1413 );
1414
1415 // Delete with direct element access
1416 expect(vc.toQuery()).toEqual(
1417 'pubDate in 2014-12-05 | foo = "bar" | ' +
1418 '(pubDate in 2014-12-05 & ' +
1419 '(title = "Hello World!" | yeah = "juhu"))'
1420 );
1421
1422 expect(vc.root().ldType()).toEqual('docGroup');
1423 expect(vc.root().operation()).toEqual('or');
1424
1425 // Operands and operators
1426 expect(vc.element().firstChild.children.length).toEqual(4);
1427 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1428
1429 // Remove inner group and flatten
Nils Diewald52f7eb12015-01-12 17:30:04 +00001430 _delOn(vc.root().getOperand(2).getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001431
1432 expect(vc.toQuery()).toEqual(
1433 'pubDate in 2014-12-05 | foo = "bar" | title = "Hello World!" | yeah = "juhu"'
1434 );
1435 expect(vc.root().ldType()).toEqual('docGroup');
1436 expect(vc.root().operation()).toEqual('or');
1437
1438 // Operands and operators
1439 expect(vc.element().firstChild.children.length).toEqual(5);
1440 expect(vc.element().firstChild.lastChild.getAttribute('class')).toEqual('operators');
1441 });
1442});
1443
1444describe('KorAP._add (event)', function () {
1445 var complexVCFactory = buildFactory(KorAP.VirtualCollection,{
Nils Diewald2fe12e12015-03-06 16:47:06 +00001446 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001447 'operation' : 'operation:and',
1448 'operands' : [
1449 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001450 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001451 "key": 'pubDate',
1452 "match": 'match:eq',
1453 "value": '2014-12-05',
1454 "type": 'type:date'
1455 },
1456 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001457 "@type" : 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001458 'operation' : 'operation:or',
1459 'operands' : [
1460 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001461 '@type' : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001462 'key' : 'title',
1463 'value' : 'Hello World!'
1464 },
1465 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001466 '@type' : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001467 'key' : 'foo',
1468 'value' : 'bar'
1469 }
1470 ]
1471 }
1472 ]
1473 });
1474
1475 it ('should add new unspecified doc with "and"', function () {
1476 var vc = KorAP.VirtualCollection.render(
1477 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001478 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001479 'operation' : 'operation:and',
1480 'operands' : [
1481 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001482 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001483 "key": 'pubDate',
1484 "match": 'match:eq',
1485 "value": '2014-12-05',
1486 "type": 'type:date'
1487 },
1488 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001489 "@type" : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001490 'key' : 'foo',
1491 'value' : 'bar'
1492 }
1493 ]
1494 }
1495 );
1496
1497 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1498
1499 var fc = vc.element().firstChild;
1500 expect(fc.getAttribute('data-operation')).toEqual('and');
1501 expect(fc.children.length).toEqual(3);
1502 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1503 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1504 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1505
1506 // add with 'and' in the middle
Nils Diewald52f7eb12015-01-12 17:30:04 +00001507 _andOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001508 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1509
1510 fc = vc.element().firstChild;
1511 expect(fc.getAttribute('data-operation')).toEqual('and');
1512 expect(fc.children.length).toEqual(4);
1513 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1514
1515 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1516 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1517 expect(fc.children[2].getAttribute('class')).toEqual('doc');
1518 });
1519
1520 it ('should add new unspecified doc with "or"', function () {
1521 var vc = KorAP.VirtualCollection.render(
1522 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001523 "@type": 'koral:docGroup',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001524 'operation' : 'operation:and',
1525 'operands' : [
1526 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001527 "@type": 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001528 "key": 'pubDate',
1529 "match": 'match:eq',
1530 "value": '2014-12-05',
1531 "type": 'type:date'
1532 },
1533 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001534 "@type" : 'koral:doc',
Nils Diewalde15b7a22015-01-09 21:50:21 +00001535 'key' : 'foo',
1536 'value' : 'bar'
1537 }
1538 ]
1539 }
1540 );
1541
1542 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1543
1544 var fc = vc.element().firstChild;
1545 expect(fc.children.length).toEqual(3);
1546 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1547 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1548 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1549
1550 // add with 'or' in the middle
Nils Diewald52f7eb12015-01-12 17:30:04 +00001551 _orOn(vc.root().getOperand(0));
Nils Diewalde15b7a22015-01-09 21:50:21 +00001552 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
1553 fc = vc.element().firstChild;
1554
1555 expect(fc.getAttribute('data-operation')).toEqual('and');
1556 expect(fc.children.length).toEqual(3);
1557 expect(fc.children[0].getAttribute('class')).toEqual('docGroup');
1558 expect(fc.children[0].getAttribute('data-operation')).toEqual('or');
1559 expect(fc.children[1].getAttribute('class')).toEqual('doc');
1560 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1561 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1562
1563 fc = vc.element().firstChild.firstChild;
1564 expect(fc.children.length).toEqual(3);
1565 expect(fc.children[0].getAttribute('class')).toEqual('doc');
1566 expect(fc.children[1].getAttribute('class')).toEqual('doc unspecified');
1567 expect(fc.children[2].getAttribute('class')).toEqual('operators');
1568 expect(fc.lastChild.getAttribute('class')).toEqual('operators');
1569 });
1570
Nils Diewald52f7eb12015-01-12 17:30:04 +00001571 it ('should add new unspecified doc with "and" before group', function () {
1572 var vc = demoFactory.create();
1573
1574 // Wrap with direct element access
1575 expect(vc.toQuery()).toEqual(
1576 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1577 );
1578
1579 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1580 expect(vc.root().getOperand(0).operation()).toEqual('and');
1581 expect(vc.root().getOperand(0).operands().length).toEqual(3);
1582
1583 // Add unspecified on the second doc
1584 var secDoc = vc.root().getOperand(0).getOperand(1);
1585 expect(secDoc.value()).toEqual('hihi');
1586
1587 // Add
1588 _andOn(secDoc);
1589
1590 var fo = vc.root().getOperand(0);
1591
1592 expect(fo.ldType()).toEqual('docGroup');
1593 expect(fo.operation()).toEqual('and');
1594 expect(fo.operands().length).toEqual(4);
1595
1596 expect(fo.getOperand(0).ldType()).toEqual('doc');
1597 expect(fo.getOperand(1).ldType()).toEqual('doc');
1598 expect(fo.getOperand(2).ldType()).toEqual('non');
1599 expect(fo.getOperand(3).ldType()).toEqual('docGroup');
1600 });
1601
1602
1603 it ('should remove a doc with an unspecified doc in a nested group', function () {
1604 var vc = demoFactory.create();
1605
1606 // Wrap with direct element access
1607 expect(vc.toQuery()).toEqual(
1608 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1609 );
1610
1611 var fo = vc.root().getOperand(0).getOperand(0);
1612 expect(fo.key()).toEqual('Titel');
1613 expect(fo.value()).toEqual('Baum');
1614
1615 // Add unspecified on the root group
1616 _orOn(fo);
1617
1618 fo = vc.root().getOperand(0).getOperand(0);
1619
1620 expect(fo.operation()).toEqual('or');
1621 expect(fo.getOperand(0).ldType()).toEqual('doc');
1622 expect(fo.getOperand(1).ldType()).toEqual('non');
1623
1624 // Delete document
1625 _delOn(fo.getOperand(0));
1626
1627 // The operand is now non
1628 expect(vc.root().getOperand(0).getOperand(0).ldType()).toEqual('non');
1629 expect(vc.root().getOperand(0).getOperand(1).ldType()).toEqual('doc');
1630 expect(vc.root().getOperand(0).getOperand(2).ldType()).toEqual('docGroup');
1631 });
1632
1633 it ('should remove an unspecified doc with an doc in a nested group', function () {
1634 var vc = demoFactory.create();
1635
1636 // Wrap with direct element access
1637 expect(vc.toQuery()).toEqual(
1638 '(Titel = "Baum" & Veröffentlichungsort = "hihi" & (Titel = "Baum" | Veröffentlichungsort = "hihi")) | Untertitel = "huhu"'
1639 );
1640
1641 var fo = vc.root().getOperand(0).getOperand(0);
1642 expect(fo.key()).toEqual('Titel');
1643 expect(fo.value()).toEqual('Baum');
1644
1645 // Add unspecified on the root group
1646 _orOn(fo);
1647
1648 fo = vc.root().getOperand(0).getOperand(0);
1649
1650 expect(fo.operation()).toEqual('or');
1651 expect(fo.getOperand(0).ldType()).toEqual('doc');
1652 expect(fo.getOperand(1).ldType()).toEqual('non');
1653
1654 // Delete unspecified doc
1655 _delOn(fo.getOperand(1));
1656
1657 // The operand is now non
1658 fo = vc.root().getOperand(0);
1659 expect(fo.getOperand(0).ldType()).toEqual('doc');
1660 expect(fo.getOperand(0).key()).toEqual('Titel');
1661 expect(fo.getOperand(0).value()).toEqual('Baum');
1662 expect(fo.getOperand(1).ldType()).toEqual('doc');
1663 expect(fo.getOperand(2).ldType()).toEqual('docGroup');
1664 });
1665
1666
1667 it ('should add on parent group (case "and")', function () {
Nils Diewaldd5070b02015-01-11 01:44:47 +00001668 var vc = complexVCFactory.create();
1669
1670 // Wrap with direct element access
1671 expect(vc.toQuery()).toEqual(
1672 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1673 );
1674
Nils Diewald52f7eb12015-01-12 17:30:04 +00001675 expect(vc.root().operands().length).toEqual(2);
1676
1677 // Add unspecified on the root group
1678 _andOn(vc.root().getOperand(1));
Nils Diewaldd5070b02015-01-11 01:44:47 +00001679 expect(vc.toQuery()).toEqual(
1680 'pubDate in 2014-12-05 & (title = "Hello World!" | foo = "bar")'
1681 );
Nils Diewald52f7eb12015-01-12 17:30:04 +00001682
Nils Diewaldd5070b02015-01-11 01:44:47 +00001683 expect(vc.root().ldType()).toEqual('docGroup');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001684 expect(vc.root().operands().length).toEqual(3);
1685 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
Nils Diewaldd5070b02015-01-11 01:44:47 +00001686 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001687 expect(vc.root().getOperand(1).operation()).toEqual('or');
1688 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1689
1690 // Add another unspecified on the root group
1691 _andOn(vc.root().getOperand(1));
1692
1693 expect(vc.root().operands().length).toEqual(4);
1694 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1695 expect(vc.root().getOperand(1).ldType()).toEqual('docGroup');
1696 expect(vc.root().getOperand(2).ldType()).toEqual('non');
1697 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1698
1699 // Add another unspecified after the first doc
1700 _andOn(vc.root().getOperand(0));
1701
1702 expect(vc.root().operands().length).toEqual(5);
1703 expect(vc.root().getOperand(0).ldType()).toEqual('doc');
1704 expect(vc.root().getOperand(1).ldType()).toEqual('non');
1705 expect(vc.root().getOperand(2).ldType()).toEqual('docGroup');
1706 expect(vc.root().getOperand(3).ldType()).toEqual('non');
1707 expect(vc.root().getOperand(4).ldType()).toEqual('non');
Nils Diewaldd5070b02015-01-11 01:44:47 +00001708 });
1709
Nils Diewaldd5070b02015-01-11 01:44:47 +00001710 it ('should wrap on root', function () {
1711 var vc = KorAP.VirtualCollection.render(
1712 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001713 "@type": 'koral:docGroup',
Nils Diewaldd5070b02015-01-11 01:44:47 +00001714 'operation' : 'operation:and',
1715 'operands' : [
1716 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001717 "@type": 'koral:doc',
Nils Diewaldd5070b02015-01-11 01:44:47 +00001718 "key": 'pubDate',
1719 "match": 'match:eq',
1720 "value": '2014-12-05',
1721 "type": 'type:date'
1722 },
1723 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001724 "@type" : 'koral:doc',
Nils Diewaldd5070b02015-01-11 01:44:47 +00001725 'key' : 'foo',
1726 'value' : 'bar'
1727 }
1728 ]
1729 }
1730 );
1731
Nils Diewald52f7eb12015-01-12 17:30:04 +00001732 // Wrap on root
Nils Diewaldd5070b02015-01-11 01:44:47 +00001733 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05 & foo = "bar"');
Nils Diewald52f7eb12015-01-12 17:30:04 +00001734 expect(vc.root().ldType()).toEqual('docGroup');
1735 expect(vc.root().operation()).toEqual('and');
1736 _orOn(vc.root());
1737 expect(vc.root().ldType()).toEqual('docGroup');
1738 expect(vc.root().operation()).toEqual('or');
1739
1740 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1741 expect(vc.root().getOperand(0).operation()).toEqual('and');
1742 });
1743
1744 it ('should add on root (case "and")', function () {
1745 var vc = KorAP.VirtualCollection.render(
1746 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001747 "@type": 'koral:doc',
Nils Diewald52f7eb12015-01-12 17:30:04 +00001748 "key": 'pubDate',
1749 "match": 'match:eq',
1750 "value": '2014-12-05',
1751 "type": 'type:date'
1752 }
1753 );
1754
1755 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1756 expect(vc.root().ldType()).toEqual('doc');
1757 expect(vc.root().key()).toEqual('pubDate');
1758 expect(vc.root().value()).toEqual('2014-12-05');
1759
1760 // Wrap on root
1761 _andOn(vc.root());
1762 expect(vc.root().ldType()).toEqual('docGroup');
1763 expect(vc.root().operation()).toEqual('and');
1764 });
1765
1766 it ('should add on root (case "or")', function () {
1767 var vc = KorAP.VirtualCollection.render(
1768 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001769 "@type": 'koral:doc',
Nils Diewald52f7eb12015-01-12 17:30:04 +00001770 "key": 'pubDate',
1771 "match": 'match:eq',
1772 "value": '2014-12-05',
1773 "type": 'type:date'
1774 }
1775 );
1776
1777 expect(vc.toQuery()).toEqual('pubDate in 2014-12-05');
1778 expect(vc.root().key()).toEqual('pubDate');
1779 expect(vc.root().value()).toEqual('2014-12-05');
1780
1781 // Wrap on root
1782 _orOn(vc.root());
Nils Diewaldd5070b02015-01-11 01:44:47 +00001783 expect(vc.root().ldType()).toEqual('docGroup');
1784 expect(vc.root().operation()).toEqual('or');
1785 });
Nils Diewald6ac292b2015-01-15 21:33:21 +00001786
1787 it ('should support multiple sub groups per group', function () {
1788 var vc = KorAP.VirtualCollection.render(
1789 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001790 "@type": 'koral:docGroup',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001791 'operation' : 'operation:or',
1792 'operands' : [
1793 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001794 "@type": 'koral:docGroup',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001795 'operation' : 'operation:and',
1796 'operands' : [
1797 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001798 "@type": 'koral:doc',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001799 "key": 'title',
1800 "value": 't1',
1801 },
1802 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001803 "@type" : 'koral:doc',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001804 'key' : 'title',
1805 'value' : 't2'
1806 }
1807 ]
1808 },
1809 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001810 "@type": 'koral:docGroup',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001811 'operation' : 'operation:and',
1812 'operands' : [
1813 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001814 "@type": 'koral:doc',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001815 "key": 'title',
1816 "value": 't3',
1817 },
1818 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001819 "@type" : 'koral:doc',
Nils Diewald6ac292b2015-01-15 21:33:21 +00001820 'key' : 'title',
1821 'value' : 't4'
1822 }
1823 ]
1824 }
1825 ]
1826 }
1827 );
Nils Diewaldfda29d92015-01-22 17:28:01 +00001828 expect(vc.toQuery()).toEqual(
1829 '(title = "t1" & title = "t2") | (title = "t3" & title = "t4")'
1830 );
Nils Diewald6ac292b2015-01-15 21:33:21 +00001831 expect(vc.root().operation()).toEqual('or');
Nils Diewaldfda29d92015-01-22 17:28:01 +00001832 expect(vc.root().getOperand(0).toQuery()).toEqual('title = "t1" & title = "t2"');
1833 expect(vc.root().getOperand(1).toQuery()).toEqual('title = "t3" & title = "t4"');
1834
1835 _andOn(vc.root());
1836
1837 expect(vc.root().operation()).toEqual('and');
1838 expect(vc.root().getOperand(0).ldType()).toEqual('docGroup');
1839 expect(vc.root().getOperand(1).ldType()).toEqual('non');
Nils Diewald6ac292b2015-01-15 21:33:21 +00001840 });
Nils Diewalde15b7a22015-01-09 21:50:21 +00001841});
Nils Diewaldfda29d92015-01-22 17:28:01 +00001842
Nils Diewald86dad5b2015-01-28 15:09:07 +00001843describe('KorAP.Rewrite', function () {
1844
1845 it('should be initializable', function () {
1846 var rewrite = KorAP.Rewrite.create({
Nils Diewald2fe12e12015-03-06 16:47:06 +00001847 "@type" : "koral:rewrite",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001848 "operation" : "operation:modification",
1849 "src" : "querySerializer",
1850 "scope" : "tree"
1851 });
1852 expect(rewrite.toString()).toEqual('Modification of "tree" by "querySerializer"');
1853 });
1854
1855 it('should be deserialized by docs', function () {
1856 var doc = KorAP.Doc.create(undefined,
1857 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001858 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001859 "key":"Titel",
1860 "value":"Baum",
1861 "match":"match:eq"
1862 });
1863
1864 expect(doc.element().classList.contains('doc')).toBeTruthy();
1865 expect(doc.element().classList.contains('rewritten')).toBe(false);
1866
1867 doc = KorAP.Doc.create(undefined,
1868 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001869 "@type":"koral:doc",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001870 "key":"Titel",
1871 "value":"Baum",
1872 "match":"match:eq",
1873 "rewrites" : [
1874 {
Nils Diewald2fe12e12015-03-06 16:47:06 +00001875 "@type" : "koral:rewrite",
Nils Diewald86dad5b2015-01-28 15:09:07 +00001876 "operation" : "operation:modification",
1877 "src" : "querySerializer",
1878 "scope" : "tree"
1879 }
1880 ]
1881 });
1882
1883 expect(doc.element().classList.contains('doc')).toBeTruthy();
1884 expect(doc.element().classList.contains('rewritten')).toBeTruthy();
1885 });
1886/*
1887 it('should be deserialized by docGroups', function () {
1888 });
1889*/
1890});
Nils Diewald2fe12e12015-03-06 16:47:06 +00001891/*
1892describe('KorAP.DocKey', function () {
1893 it('should be initializable', function () {
1894 var docKey = KorAP.DocKey.create();
1895 expect(docKey.toString()).toEqual('...');
1896 });
1897});
1898*/