Skip to content

Commit 282f6db

Browse files
author
Cosimo Streppone
committed
delete(): improved multiple-id code and tests
Received all feedback as of PR#151 comment: <#151 (comment)> In particular, better idiomatic python code for the multiple id cases, and an improved test that will delete all documents except one, rather than deleting all in one go.
1 parent bf5145a commit 282f6db

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

pysolr.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -825,15 +825,12 @@ def delete(self, id=None, q=None, commit=True, waitFlush=None, waitSearcher=None
825825
elif id is not None and q is not None:
826826
raise ValueError('You many only specify "id" OR "q", not both.')
827827
elif id is not None:
828-
if type(id) not in (list, set):
829-
id = [ id ]
828+
if not isinstance(id, (list, set, tuple)):
829+
id = [id]
830830
else:
831-
id = filter(lambda x: x is not None, id)
832-
if len(id) > 0:
833-
m = '<delete>'
834-
for i in id:
835-
m += '<id>%s</id>' % i
836-
m += '</delete>'
831+
id = filter(None, id)
832+
if id:
833+
m = '<delete>%s</delete>' % ''.join('<id>%s</id>' % i for i in id)
837834
else:
838835
raise ValueError('The list of documents to delete was empty.')
839836
elif q is not None:

tests/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import unicode_literals
33

44
import datetime
5+
import random
56

67
from pysolr import (Solr, Results, SolrError, unescape_html, safe_urlencode,
78
force_unicode, force_bytes, sanitize, json, ET, IS_PY3,
@@ -460,7 +461,14 @@ def test_delete(self):
460461
self.solr.commit()
461462
self.assertEqual(len(self.solr.search('*:*')), len(self.docs))
462463
to_delete = [doc['id'] for doc in self.docs]
464+
# Extract a random document from the list, to later check it wasn't deleted
465+
graced_doc_id = to_delete.pop(random.randint(0, len(to_delete) - 1))
463466
self.solr.delete(id=to_delete)
467+
# There should be only one left, our graced id
468+
self.assertEqual(len(self.solr.search('*:*')), 1)
469+
self.assertEqual(len(self.solr.search('id:%s' % graced_doc_id)), 1)
470+
# Now we can wipe the graced document too. None should be left.
471+
self.solr.delete(id=graced_doc_id)
464472
self.assertEqual(len(self.solr.search('*:*')), 0)
465473

466474
# Can't delete when the list of documents is empty

0 commit comments

Comments
 (0)