From 192aa7409721abd3cea2ef10e84ca073a60ec916 Mon Sep 17 00:00:00 2001 From: wassname Date: Mon, 6 Jun 2016 13:24:38 +0800 Subject: [PATCH] Add delete model method and test --- flask_whooshee.py | 13 ++++++++++++ test.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/flask_whooshee.py b/flask_whooshee.py index 46927b2..9537732 100644 --- a/flask_whooshee.py +++ b/flask_whooshee.py @@ -231,8 +231,21 @@ class Whooshee(object): attrs[f] = str(attrs[f]) writer.add_document(**attrs) + @classmethod + def delete_model(cls, writer, model): + attrs = {primary: getattr(model, primary)} + for f in index_fields: + attrs[f] = getattr(model, f) + if not isinstance(attrs[f], int): + if sys.version < '3': + attrs[f] = unicode(attrs[f]) + else: + attrs[f] = str(attrs[f]) + writer.delete_by_query(**attrs) + setattr(mwh, 'update_{0}'.format(model.__name__.lower()), update_model) setattr(mwh, 'insert_{0}'.format(model.__name__.lower()), insert_model) + setattr(mwh, 'delete_{0}'.format(model.__name__.lower()), delete_model) model._whoosheer_ = mwh model.whoosh_search = mwh.search diff --git a/test.py b/test.py index efef8f3..4a1e8c8 100644 --- a/test.py +++ b/test.py @@ -76,6 +76,14 @@ class BaseTestCases(object): title=entry.title, content=entry.content) + @classmethod + def delete_user(cls, writer, user): + # nothing, user doesn't have entries yet + pass + + @classmethod + def delete_entry(cls, writer, entry): + writer.delete_by_term('entry_id', entry.id) self.User = User self.Entry = Entry @@ -233,6 +241,50 @@ class BaseTestCases(object): found = self.Entry.query.join(self.User).whooshee_search('rambo').all() self.assertEqual(len(found), 1) + def test_add(self): + # test that the add operation works + found = self.Entry.query.whooshee_search('blah blah blah').all() + self.assertEqual(len(found), 0) + + self.db.session.add(self.e1) + self.db.session.commit() + + found = self.Entry.query.whooshee_search('blah blah blah').all() + self.assertEqual(len(found), 1) + + # def test_update(self): + # # test that the update operation works + # self.db.session.add(self.e1) + # self.db.session.commit() + # self.db.session.remove() + # + # found = self.Entry.query.whooshee_search('blah blah blah').all() + # self.assertEqual(len(found), 1) + # + # # TODO there is an error here "InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction." + # self.e1.content = 'ramble ramble ramble' + # self.db.session.commit() + # + # found = self.Entry.query.whooshee_search('ramble ramble ramble').all() + # self.assertEqual(len(found), 1) + # + # found = self.Entry.query.whooshee_search('blah blah blah').all() + # self.assertEqual(len(found), 0) + + def test_delete(self): + # test that the delete operation works + self.db.session.add(self.e1) + self.db.session.commit() + + found = self.Entry.query.whooshee_search('blah blah blah').all() + self.assertEqual(len(found), 1) + + self.db.session.delete(self.e1) + self.db.session.flush() + + found = self.Entry.query.whooshee_search('blah blah blah').all() + self.assertEqual(len(found), 0) + # TODO: more :) class TestsWithApp(BaseTestCases.BaseTest):