| 
									
										
										
										
											2020-05-21 11:46:01 +02:00
										 |  |  | const ParsingContext = require("../src/services/search/parsing_context"); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | const parser = require('../src/services/search/parser'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe("Parser", () => { | 
					
						
							|  |  |  |     it("fulltext parser without content", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: ["hello", "hi"], | 
					
						
							|  |  |  |             expressionTokens: [], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext({includeNoteContent: false}) | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(rootExp.constructor.name).toEqual("NoteCacheFulltextExp"); | 
					
						
							|  |  |  |         expect(rootExp.tokens).toEqual(["hello", "hi"]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("fulltext parser with content", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: ["hello", "hi"], | 
					
						
							|  |  |  |             expressionTokens: [], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext({includeNoteContent: true}) | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(rootExp.constructor.name).toEqual("OrExp"); | 
					
						
							|  |  |  |         const [firstSub, secondSub] = rootExp.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(firstSub.constructor.name).toEqual("NoteCacheFulltextExp"); | 
					
						
							|  |  |  |         expect(firstSub.tokens).toEqual(["hello", "hi"]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(secondSub.constructor.name).toEqual("NoteContentFulltextExp"); | 
					
						
							|  |  |  |         expect(secondSub.tokens).toEqual(["hello", "hi"]); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("simple label comparison", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: [], | 
					
						
							|  |  |  |             expressionTokens: ["#mylabel", "=", "text"], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext() | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(rootExp.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(rootExp.attributeType).toEqual("label"); | 
					
						
							|  |  |  |         expect(rootExp.attributeName).toEqual("mylabel"); | 
					
						
							|  |  |  |         expect(rootExp.comparator).toBeTruthy(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("simple label AND", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: [], | 
					
						
							|  |  |  |             expressionTokens: ["#first", "=", "text", "AND", "#second", "=", "text"], | 
					
						
							| 
									
										
										
										
											2020-05-21 11:46:01 +02:00
										 |  |  |             parsingContext: new ParsingContext(true) | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(rootExp.constructor.name).toEqual("AndExp"); | 
					
						
							|  |  |  |         const [firstSub, secondSub] = rootExp.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(firstSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(firstSub.attributeName).toEqual("first"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(secondSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(secondSub.attributeName).toEqual("second"); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("simple label AND without explicit AND", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: [], | 
					
						
							|  |  |  |             expressionTokens: ["#first", "=", "text", "#second", "=", "text"], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext() | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(rootExp.constructor.name).toEqual("AndExp"); | 
					
						
							|  |  |  |         const [firstSub, secondSub] = rootExp.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(firstSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(firstSub.attributeName).toEqual("first"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(secondSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(secondSub.attributeName).toEqual("second"); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("simple label OR", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: [], | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |             expressionTokens: ["#first", "=", "text", "or", "#second", "=", "text"], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext() | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(rootExp.constructor.name).toEqual("OrExp"); | 
					
						
							|  |  |  |         const [firstSub, secondSub] = rootExp.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(firstSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(firstSub.attributeName).toEqual("first"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(secondSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(secondSub.attributeName).toEqual("second"); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("fulltext and simple label", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: ["hello"], | 
					
						
							|  |  |  |             expressionTokens: ["#mylabel", "=", "text"], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext() | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(rootExp.constructor.name).toEqual("AndExp"); | 
					
						
							|  |  |  |         const [firstSub, secondSub] = rootExp.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(firstSub.constructor.name).toEqual("NoteCacheFulltextExp"); | 
					
						
							|  |  |  |         expect(firstSub.tokens).toEqual(["hello"]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(secondSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(secondSub.attributeName).toEqual("mylabel"); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it("label sub-expression", () => { | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         const rootExp = parser({ | 
					
						
							|  |  |  |             fulltextTokens: [], | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |             expressionTokens: ["#first", "=", "text", "or", ["#second", "=", "text", "and", "#third", "=", "text"]], | 
					
						
							| 
									
										
										
										
											2020-05-21 14:05:56 +02:00
										 |  |  |             parsingContext: new ParsingContext() | 
					
						
							| 
									
										
										
										
											2020-05-21 11:18:15 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(rootExp.constructor.name).toEqual("OrExp"); | 
					
						
							|  |  |  |         const [firstSub, secondSub] = rootExp.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(firstSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(firstSub.attributeName).toEqual("first"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(secondSub.constructor.name).toEqual("AndExp"); | 
					
						
							|  |  |  |         const [firstSubSub, secondSubSub] = secondSub.subExpressions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(firstSubSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(firstSubSub.attributeName).toEqual("second"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-23 10:25:22 +02:00
										 |  |  |         expect(secondSubSub.constructor.name).toEqual("LabelComparisonExp"); | 
					
						
							| 
									
										
										
										
											2020-05-20 23:20:39 +02:00
										 |  |  |         expect(secondSubSub.attributeName).toEqual("third"); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | }); |