Skip to content

Commit

Permalink
more tests and moved cache to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
xsteadfastx committed Dec 15, 2016
1 parent 535472b commit 7cb6f5f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
38 changes: 3 additions & 35 deletions mopidy_emby/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import hashlib
import logging
import time

from urllib import urlencode
from urllib2 import quote
Expand All @@ -14,41 +13,10 @@

import mopidy_emby

from mopidy_emby.utils import cache

logger = logging.getLogger(__name__)


class cache(object):

def __init__(self, ctl=8, ttl=3600):
self.cache = {}
self.ctl = ctl
self.ttl = ttl
self._call_count = 1

def __call__(self, func):
def _memoized(*args):
self.func = func
now = time.time()
try:
value, last_update = self.cache[args]
age = now - last_update
if self._call_count >= self.ctl or age > self.ttl:
self._call_count = 1
raise AttributeError

self._call_count += 1
return value

except (KeyError, AttributeError):
value = self.func(*args)
self.cache[args] = (value, now)
return value

except TypeError:
return self.func(*args)

return _memoized
logger = logging.getLogger(__name__)


class EmbyHandler(object):
Expand Down Expand Up @@ -373,7 +341,7 @@ def _get_search(self, itemtype, term):
elif itemtype == 'track_name':
query = 'Audio'
else:
raise Exception('Emby search: no itemtype {}'.format())
raise Exception('Emby search: no itemtype {}'.format(itemtype))

data = self.r_get(
self.api_url(
Expand Down
40 changes: 40 additions & 0 deletions mopidy_emby/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import unicode_literals

import logging
import time


logger = logging.getLogger(__name__)


class cache(object):

def __init__(self, ctl=8, ttl=3600):
self.cache = {}
self.ctl = ctl
self.ttl = ttl
self._call_count = 1

def __call__(self, func):
def _memoized(*args):
self.func = func
now = time.time()
try:
value, last_update = self.cache[args]
age = now - last_update
if self._call_count >= self.ctl or age > self.ttl:
self._call_count = 1
raise AttributeError

self._call_count += 1
return value

except (KeyError, AttributeError):
value = self.func(*args)
self.cache[args] = (value, now)
return value

except TypeError:
return self.func(*args)

return _memoized
41 changes: 41 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,44 @@ def test_get_track(r_get_mock, data, expected, emby_client):
r_get_mock.return_value = json.load(f)

assert emby_client.get_track(0) == expected


@pytest.mark.parametrize('itemtype,url', [
(
'any',
('/Search/Hints?SearchTerm=viva%20hate'
'&IncludeItemTypes=Audio,MusicAlbum,MusicArtist')
),
(
'artist',
('/Search/Hints?SearchTerm=viva%20hate'
'&IncludeItemTypes=MusicArtist')
),
(
'album',
('/Search/Hints?SearchTerm=viva%20hate'
'&IncludeItemTypes=MusicAlbum')
),
(
'track_name',
('/Search/Hints?SearchTerm=viva%20hate'
'&IncludeItemTypes=Audio')
),
])
@mock.patch('mopidy_emby.backend.EmbyHandler.r_get')
@mock.patch('mopidy_emby.backend.EmbyHandler.api_url')
def test__get_search(api_url_mock, r_get_mock, itemtype, url, emby_client):
with open('tests/data/search_audio0.json', 'r') as f:
r_get_mock.return_value = json.load(f)

emby_client._get_search(itemtype, 'viva hate')

api_url_mock.assert_called_with(url)


@mock.patch('mopidy_emby.backend.EmbyHandler.r_get')
def test__get_search_exception(r_get_mock, emby_client):
with pytest.raises(Exception) as execinfo:
emby_client._get_search('foo', 'bar')

assert 'Emby search: no itemtype foo' in str(execinfo.value)

0 comments on commit 7cb6f5f

Please sign in to comment.