Example #1
0
    def test_boost_equivalent(self):
        boost = Boost(Term('guide'), 5)
        equivalent = boost.children[0]
        self.assertIsInstance(equivalent, Term)
        self.assertAlmostEqual(equivalent.boost, 5)

        boost = Boost(Term('guide', boost=0.5), 5)
        equivalent = boost.children[0]
        self.assertIsInstance(equivalent, Term)
        self.assertAlmostEqual(equivalent.boost, 2.5)

        boost = Boost(Boost(Term('guide', 0.1), 3), 5)
        sub_boost = boost.children[0]
        self.assertIsInstance(sub_boost, Boost)
        sub_boost = sub_boost.children[0]
        self.assertIsInstance(sub_boost, Term)
        self.assertAlmostEqual(sub_boost.boost, 1.5)

        boost = Boost(And([Boost(Term('guide', 0.1), 3), Term('two', 2)]), 5)
        and_obj = boost.children[0]
        self.assertIsInstance(and_obj, And)
        sub_boost = and_obj.children[0]
        self.assertIsInstance(sub_boost, Boost)
        guide = sub_boost.children[0]
        self.assertIsInstance(guide, Term)
        self.assertAlmostEqual(guide.boost, 1.5)
        two = and_obj.children[1]
        self.assertIsInstance(two, Term)
        self.assertAlmostEqual(two.boost, 10)
Example #2
0
    def test_or(self):
        results = self.backend.search(Or([Term('hobbit'), Term('towers')]),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results},
                            {'The Hobbit', 'The Two Towers'})

        results = self.backend.search(Term('hobbit') | Term('towers'),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results},
                            {'The Hobbit', 'The Two Towers'})
Example #3
0
 def test_operators_combination(self):
     results = self.backend.search(
         ((Term('javascript') & ~Term('definitive')) | Term('python')
          | Term('rust')) | Term('two'), models.Book.objects.all())
     self.assertSetEqual(
         {r.title
          for r in results}, {
              'JavaScript: The good parts', 'Learning Python',
              'The Two Towers', 'The Rust Programming Language',
              'Two Scoops of Django 1.11'
          })
Example #4
0
    def test_and(self):
        results = self.backend.search(And([Term('javascript'),
                                           Term('definitive')]),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results},
                            {'JavaScript: The Definitive Guide'})

        results = self.backend.search(Term('javascript') & Term('definitive'),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results},
                            {'JavaScript: The Definitive Guide'})
Example #5
0
    def test_term(self):
        results = self.backend.search(Term('javascript'),
                                      models.Book.objects.all())

        self.assertSetEqual(
            {r.title
             for r in results},
            {'JavaScript: The Definitive Guide', 'JavaScript: The good parts'})
Example #6
0
    def test_not(self):
        all_other_titles = {
            'A Clash of Kings',
            'A Game of Thrones',
            'A Storm of Swords',
            'Foundation',
            'Learning Python',
            'The Hobbit',
            'The Two Towers',
            'The Fellowship of the Ring',
            'The Return of the King',
            'The Rust Programming Language',
            'Two Scoops of Django 1.11',
        }

        results = self.backend.search(Not(Term('javascript')),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results}, all_other_titles)

        results = self.backend.search(~Term('javascript'),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results}, all_other_titles)
Example #7
0
    def test_filter_query(self):
        results = self.backend.search(Filter(Term('javascript')),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results},
                            {'JavaScript: The Definitive Guide',
                             'JavaScript: The good parts'})

        results = self.backend.search(Filter(Term('javascript'),
                                             include=Term('definitive')),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results},
                            {'JavaScript: The Definitive Guide'})

        results = self.backend.search(Filter(Term('javascript'),
                                             include=Term('definitive'),
                                             exclude=Term('guide')),
                                      models.Book.objects.all())
        self.assertSetEqual({r.title for r in results}, set())
Example #8
0
    def test_filter_equivalent(self):
        filter = Filter(Term('javascript'))
        term = filter.child
        self.assertIsInstance(term, Term)
        self.assertEqual(term.term, 'javascript')

        filter = Filter(Term('javascript'), include=Term('definitive'))
        and_obj = filter.child
        self.assertIsInstance(and_obj, And)
        javascript = and_obj.children[0]
        self.assertIsInstance(javascript, Term)
        self.assertEqual(javascript.term, 'javascript')
        boost_obj = and_obj.children[1]
        self.assertIsInstance(boost_obj, Boost)
        self.assertEqual(boost_obj.boost, 0)
        definitive = boost_obj.child
        self.assertIsInstance(definitive, Term)
        self.assertEqual(definitive.term, 'definitive')

        filter = Filter(Term('javascript'),
                        include=Term('definitive'),
                        exclude=Term('guide'))
        and_obj1 = filter.child
        self.assertIsInstance(and_obj1, And)
        and_obj2 = and_obj1.children[0]
        javascript = and_obj2.children[0]
        self.assertIsInstance(javascript, Term)
        self.assertEqual(javascript.term, 'javascript')
        boost_obj = and_obj2.children[1]
        self.assertIsInstance(boost_obj, Boost)
        self.assertEqual(boost_obj.boost, 0)
        definitive = boost_obj.child
        self.assertIsInstance(definitive, Term)
        self.assertEqual(definitive.term, 'definitive')
        boost_obj = and_obj1.children[1]
        self.assertIsInstance(boost_obj, Boost)
        self.assertEqual(boost_obj.boost, 0)
        not_obj = boost_obj.child
        self.assertIsInstance(not_obj, Not)
        guide = not_obj.child
        self.assertEqual(guide.term, 'guide')
Example #9
0
    def test_incomplete_term(self):
        results = self.backend.search(Term('pro'), models.Book.objects.all())

        self.assertSetEqual({r.title for r in results}, set())