示例#1
0
 def test_or_operation(self):
     ltree = parser.parse("n_pages:360 OR edition:Lumos")
     names = auto_name(ltree)
     query = self.es_builder(ltree)
     # the one matching Lumos
     book, = list(
         self.search.filter(query).filter("term", ref="BB1").execute())
     self.assertEqual(len(book.meta.matched_queries), 1)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         '<span class="ok"><span class="ko">n_pages:360 </span>OR edition:Lumos</span>',
     )
     # the one matching n_pages
     book, = list(
         self.search.filter(query).filter("term", ref="HP8").execute())
     self.assertEqual(len(book.meta.matched_queries), 1)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         '<span class="ok">n_pages:360 OR<span class="ko"> edition:Lumos</span></span>',
     )
     # matching None
     book, = list(
         self.search.filter(Q(query) | Q("match_all")).filter(
             Q("term", ref="HP7")).execute())
     self.assertFalse(hasattr(book.meta, "matched_queries"))
示例#2
0
 def test_double_negation(self):
     ltree = parser.parse("NOT (n_pages:360 AND - edition:Lumos) AND ref:*")
     names = auto_name(ltree)
     query = self.es_builder(ltree)
     # matching Lumos double negation
     book, = list(
         self.search.filter(query).filter("term", ref="BB1").execute())
     self.assertEqual(len(book.meta.matched_queries), 2)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         '<span class="ok">NOT'
         '<span class="ko"> (n_pages:360 AND -<span class="ok"> edition:Lumos</span>) </span>'
         'AND ref:*</span>',
     )
     # not matching Lumos double negation
     book, = list(
         self.search.filter(Q(query) | Q("term", ref="HP8")).filter(
             "term", ref="HP8").execute())
     self.assertEqual(len(book.meta.matched_queries), 2)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         '<span class="ko">NOT'
         '<span class="ok"> (n_pages:360 AND -<span class="ko"> edition:Lumos</span>) </span>'
         'AND<span class="ok"> ref:*</span></span>',
     )
示例#3
0
    def _nested_test(self, query, html, ref, num_match=1):
        """scenario taking into account nested

        :param str matching_query: the query that match the book
        :param str ref: ref of expected matching book
        """
        ltree = parser.parse(query)
        names = auto_name(ltree)
        query = self.es_builder(ltree)
        queries = [query] + extract_nested_queries(query)
        matched_queries = []
        # we have to force book matching by adding condition
        for sub_query in queries:
            search = self.search.filter(Q(sub_query)
                                        | Q("term", ref=ref)).filter("term",
                                                                     ref=ref)
            book, = list(search.execute())
            self.assertEqual(book.ref, ref)
            matched_queries.extend(getattr(book.meta, "matched_queries", []))
        self.assertEqual(len(matched_queries), num_match)
        paths_ok, paths_ko = self.propagate_matching(
            ltree,
            *matching_from_names(matched_queries, names),
        )
        self.assertEqual(
            self.make_html(ltree, paths_ok, paths_ko),
            html,
        )
        return matched_queries, html
示例#4
0
 def _negation_test(self, operator):
     ltree = parser.parse(f"{operator} n_pages:360 AND edition:Lumos")
     names = auto_name(ltree)
     query = self.es_builder(ltree)
     # matching Lumos
     book, = list(
         self.search.filter(query).filter("term", ref="BB1").execute())
     self.assertEqual(len(book.meta.matched_queries), 1)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         f'<span class="ok">{operator}<span class="ko"> n_pages:360 </span>AND'
         ' edition:Lumos</span>',
     )
     # matching n_pages and not lumos
     search = self.search.filter(Q(query) | Q("term", ref="HP8")).filter(
         Q("term", ref="HP8"))
     book, = list(search.execute())
     self.assertEqual(len(book.meta.matched_queries), 1)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         f'<span class="ko">{operator}<span class="ok"> n_pages:360 </span>'
         f'AND edition:Lumos</span>',
     )
     # matching none
     search = self.search.filter(Q(query) | Q("term", ref="HP7")).filter(
         Q("term", ref="HP7"))
     book, = list(search.execute())
     self.assertFalse(hasattr(book.meta, "matched_queries"))
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names([], names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         f'<span class="ko"><span class="ok">{operator}'
         '<span class="ko"> n_pages:360 </span></span>'
         'AND edition:Lumos</span>',
     )
示例#5
0
 def test_and_operation_matching(self):
     ltree = parser.parse("n_pages:157 AND edition:Lumos")
     names = auto_name(ltree)
     query = self.es_builder(ltree)
     # matching Lumos and n_pages
     book, = list(
         self.search.filter(query).filter("term", ref="BB1").execute())
     self.assertEqual(len(book.meta.matched_queries), 2)
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         '<span class="ok">n_pages:157 AND edition:Lumos</span>',
     )
示例#6
0
    def _simple_test(self, matching_query, ref, num_match=1):
        """simple scenario

        :param str matching_query: the query that match the book
        :param str ref: ref of expected matching book
        """
        ltree = parser.parse(f"{matching_query} OR n_pages:1000")
        names = auto_name(ltree)
        query = self.es_builder(ltree)
        book, = list(self.search.filter(query).execute())
        self.assertEqual(book.ref, ref)
        self.assertEqual(len(book.meta.matched_queries), num_match)
        paths_ok, paths_ko = self.propagate_matching(
            ltree, *matching_from_names(book.meta.matched_queries, names))
        self.assertEqual(
            self.make_html(ltree, paths_ok, paths_ko),
            f'<span class="ok">{matching_query} OR<span class="ko"> n_pages:1000</span></span>',
        )
示例#7
0
 def test_keyword_naming(self):
     ltree = parser.parse("illustrators.nationality:UK")
     names = auto_name(ltree)
     query = self.es_builder(ltree)
     results = list(self.search.filter(query).execute())
     book = results[0]
     self.assertEqual(book.meta.matched_queries, ["a"])
     self.assertEqual(
         element_from_name(ltree, book.meta.matched_queries[0], names),
         ltree,
     )
     paths_ok, paths_ko = self.propagate_matching(
         ltree,
         *matching_from_names(book.meta.matched_queries, names),
     )
     self.assertEqual(
         self.make_html(ltree, paths_ok, paths_ko),
         '<span class="ok">illustrators.nationality:UK</span>',
     )
示例#8
0
 def test_matching_from_name(self):
     names = {
         "a": (0, ),
         "b": (1, ),
         "c": (0, 0),
         "d": (0, 1),
         "e": (1, 0, 1)
     }
     self.assertEqual(matching_from_names([], names), (set(), {(0, ), (1, ),
                                                               (0, 0),
                                                               (0, 1),
                                                               (1, 0, 1)}))
     self.assertEqual(matching_from_names(["a", "b"], names),
                      ({(0, ), (1, )}, {(0, 0), (0, 1), (1, 0, 1)}))
     self.assertEqual(matching_from_names(["a", "e"], names),
                      ({(0, ), (1, 0, 1)}, {(1, ), (0, 0), (0, 1)}))
     self.assertEqual(matching_from_names(["c"], names),
                      ({(0, 0)}, {(0, ), (1, ), (0, 1), (1, 0, 1)}))
     with self.assertRaises(KeyError):
         matching_from_names(["x"], names)