fix match() on RegexpQuery for non-string values

This commit is contained in:
Adrian Sampson 2012-09-08 16:24:39 -07:00
parent 931a6b636b
commit 90b3dba085
3 changed files with 21 additions and 0 deletions

View file

@ -452,6 +452,10 @@ class RegexpQuery(FieldQuery):
def match(self, item):
value = getattr(item, self.field) or ''
if value is None:
value = u''
elif not isinstance(value, basestring):
value = unicode(value)
return self.regexp.search(value) is not None
class BooleanQuery(MatchQuery):

View file

@ -12,6 +12,7 @@ Changelog
* Fix album queries for ``artpath`` and other non-item fields.
* Null values in the database can now be matched with the empty-string regular
expression, ``^$``.
* Regular expressions more reliably match non-string values.
* :doc:`/plugins/fetchart`: Fix a bug where cover art filenames could lack
a ``.jpg`` extension.
* :doc:`/plugins/lyrics`: Fix an exception with non-ASCII lyrics.

View file

@ -302,6 +302,22 @@ class MemoryGetTest(unittest.TestCase, AssertsMixin):
self.assert_matched(results, u'caf\xe9')
self.assert_done(results)
class MatchTest(unittest.TestCase):
def setUp(self):
self.item = _common.item()
def test_regex_match_positive(self):
q = beets.library.RegexpQuery('album', '^the album$')
self.assertTrue(q.match(self.item))
def test_regex_match_negative(self):
q = beets.library.RegexpQuery('album', '^album$')
self.assertFalse(q.match(self.item))
def test_regex_match_non_string_value(self):
q = beets.library.RegexpQuery('disc', '^6$')
self.assertTrue(q.match(self.item))
class PathQueryTest(unittest.TestCase, AssertsMixin):
def setUp(self):
self.lib = beets.library.Library(':memory:')