def test_fts_manual(self):
        matches = lambda s: sqe.match(FTSDoc.message, s)
        messages = [FTSDoc.create(message=msg) for msg in self.messages]

        q = FTSDoc.select().where(matches('believe')).order_by(FTSDoc.id)
        self.assertMessages(q, [0, 3])

        q = FTSDoc.match('believe')
        self.assertMessages(q, [3, 0])

        q = FTSDoc.match('things')
        self.assertEqual([(x.message, x.score) for x in q], [
            (self.messages[4], 2.0 / 3),
            (self.messages[2], 1.0 / 3),
        ])
    def _test_fts_auto(self, ModelClass):
        matches = lambda s: sqe.match(ModelClass.message, s)
        posts = []
        for message in self.messages:
            posts.append(Post.create(message=message))

        # Nothing matches, index is not built.
        pq = ModelClass.select().where(matches('faith'))
        self.assertEqual(list(pq), [])

        ModelClass.rebuild()
        ModelClass.optimize()

        # it will stem faithful -> faith b/c we use the porter tokenizer
        pq = ModelClass.select().where(matches('faith')).order_by(ModelClass.id)
        self.assertMessages(pq, range(len(self.messages)))

        pq = ModelClass.select().where(matches('believe')).order_by(ModelClass.id)
        self.assertMessages(pq, [0, 3])

        pq = ModelClass.select().where(matches('thin*')).order_by(ModelClass.id)
        self.assertMessages(pq, [2, 4])

        pq = ModelClass.select().where(matches('"it is"')).order_by(ModelClass.id)
        self.assertMessages(pq, [2, 3])

        pq = (ModelClass
              .select(ModelClass, sqe.Rank(ModelClass).alias('score'))
              .where(matches('things'))
              .order_by(SQL('score').desc()))
        self.assertEqual([(x.message, x.score) for x in pq], [
            (self.messages[4], 2.0 / 3),
            (self.messages[2], 1.0 / 3),
        ])

        pq = ModelClass.select(sqe.Rank(ModelClass)).where(matches('faithful')).tuples()
        self.assertEqual([x[0] for x in pq], [.2] * 5)

        pq = ModelClass.select(ModelClass.rank()).where(matches('faithful')).tuples()
        self.assertEqual([x[0] for x in pq], [.2] * 5)