Add delete model method and test

This commit is contained in:
2016-06-06 13:24:38 +08:00
parent bd9dd42e2a
commit 192aa74097
2 changed files with 65 additions and 0 deletions
+13
View File
@@ -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
+52
View File
@@ -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):