Skip to content

Commit

Permalink
Merge a383a27 into 96633e1
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenithDK committed Nov 2, 2013
2 parents 96633e1 + a383a27 commit bc4656e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
12 changes: 8 additions & 4 deletions mopidy/backends/local/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def __init__(self, *args, **kwargs):
self._tag_cache_file = self.backend.config['local']['tag_cache_file']
self.refresh()

def _convert_to_int(self, string):
try:
return int(string)
except ValueError:
return object()

def refresh(self, uri=None):
logger.debug(
'Loading local tracks from %s using %s',
Expand Down Expand Up @@ -61,7 +67,7 @@ def find_exact(self, query=None, uris=None):
# FIXME this is bound to be slow for large libraries
for value in values:
if field == 'track_no':
q = value
q = self._convert_to_int(value)
else:
q = value.strip()

Expand All @@ -81,7 +87,6 @@ def find_exact(self, query=None, uris=None):
album_filter(t) or
artist_filter(t) or
albumartist_filter(t) or
track_no_filter(t) or
date_filter(t))

if field == 'uri':
Expand Down Expand Up @@ -119,7 +124,7 @@ def search(self, query=None, uris=None):
# FIXME this is bound to be slow for large libraries
for value in values:
if field == 'track_no':
q = value
q = self._convert_to_int(value)
else:
q = value.strip().lower()

Expand All @@ -140,7 +145,6 @@ def search(self, query=None, uris=None):
album_filter(t) or
artist_filter(t) or
albumartist_filter(t) or
track_no_filter(t) or
date_filter(t))

if field == 'uri':
Expand Down
20 changes: 13 additions & 7 deletions tests/backends/local/library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ def test_find_exact_no_hits(self):
result = self.library.find_exact(date=['1990'])
self.assertEqual(list(result[0].tracks), [])

result = self.library.find_exact(track_no=[9])
result = self.library.find_exact(track_no=['9'])
self.assertEqual(list(result[0].tracks), [])

result = self.library.find_exact(track_no=['no_match'])
self.assertEqual(list(result[0].tracks), [])

result = self.library.find_exact(uri=['fake uri'])
Expand Down Expand Up @@ -167,10 +170,10 @@ def test_find_exact_albumartist(self):
self.assertEqual(list(result[0].tracks), [self.tracks[2]])

def test_find_exact_track_no(self):
result = self.library.find_exact(track_no=[1])
result = self.library.find_exact(track_no=['1'])
self.assertEqual(list(result[0].tracks), self.tracks[:1])

result = self.library.find_exact(track_no=[2])
result = self.library.find_exact(track_no=['2'])
self.assertEqual(list(result[0].tracks), self.tracks[1:2])

def test_find_exact_date(self):
Expand Down Expand Up @@ -228,7 +231,7 @@ def test_find_exact_with_empty_query(self):
test = lambda: self.library.find_exact(album=[''])
self.assertRaises(LookupError, test)

test = lambda: self.library.find_exact(track_no=[])
test = lambda: self.library.find_exact(track_no=[''])
self.assertRaises(LookupError, test)

test = lambda: self.library.find_exact(date=[''])
Expand All @@ -247,7 +250,10 @@ def test_search_no_hits(self):
result = self.library.search(album=['unknown artist'])
self.assertEqual(list(result[0].tracks), [])

result = self.library.search(track_no=[9])
result = self.library.search(track_no=['9'])
self.assertEqual(list(result[0].tracks), [])

result = self.library.search(track_no=['no_match'])
self.assertEqual(list(result[0].tracks), [])

result = self.library.search(date=['unknown date'])
Expand Down Expand Up @@ -314,10 +320,10 @@ def test_search_date(self):
self.assertEqual(list(result[0].tracks), self.tracks[1:2])

def test_search_track_no(self):
result = self.library.search(track_no=[1])
result = self.library.search(track_no=['1'])
self.assertEqual(list(result[0].tracks), self.tracks[:1])

result = self.library.search(track_no=[2])
result = self.library.search(track_no=['2'])
self.assertEqual(list(result[0].tracks), self.tracks[1:2])

def test_search_any(self):
Expand Down

0 comments on commit bc4656e

Please sign in to comment.