Source

django-lastfm / lastfm / tests / views.py

Full commit
from urllib import urlopen

from django.test import TestCase
import mock

from lastfm import views


class WeeklyTopArtistsTest(TestCase):

    def setUp(self):
        self.charts = views.WeeklyTopArtists()
        self.test_data = {
            'weeklyartistchart': {
                'artist': [
                    {'name': 'Test', 'playcount': '23'},
                ],
            },
        }

    def test_get_data(self):
        ret = self.charts.get_data(self.test_data)
        self.assertEqual(ret, self.test_data['weeklyartistchart']['artist'])

    def test_get_item_title(self):
        item = self.test_data['weeklyartistchart']['artist'][0]
        ret = self.charts.get_item_title(item)
        self.assertEqual(ret, '%s (%s plays)' %
                (item['name'], item['playcount']))

    def test_get_default_image(self):
        ret = self.charts.get_default_image()
        self.assertEqual(urlopen(ret).code, 200)

    @mock.patch('urllib.urlopen')
    def test_get_img_url(self, urlopen):
        from StringIO import StringIO

        item = self.test_data['weeklyartistchart']['artist'][0]

        # normal case
        json = ('{"images":{"image":{"sizes":{"size":[{"#text":'
                '"original_url","name":"original"},{"#text":"large_url",'
                '"name":"large"}]}}}}')
        urlopen.return_value = StringIO(json)
        ret = self.charts.get_img_url('large', item)
        self.assertTrue(urlopen.called)
        self.assertEqual(ret, 'large_url')

        # 'large' not in list
        json = ('{"images":{"image":{"sizes":{"size":[{"#text":'
                '"original_url","name":"original"}]}}}}')
        urlopen.return_value = StringIO(json)
        ret = self.charts.get_img_url('large', item)
        self.assertTrue(urlopen.called)
        self.assertEqual(ret, 'original_url')

        # 'large' not in list
        json = '{"images":{"image":{"sizes":{"size":[]}}}}'
        urlopen.return_value = StringIO(json)
        ret = self.charts.get_img_url('large', item)
        self.assertTrue(urlopen.called)
        self.assertEqual(ret, '')

        # Empty response from last.fm
        urlopen.return_value = StringIO('')
        ret = self.charts.get_img_url('large', item)
        self.assertTrue(urlopen.called)
        self.assertEqual(ret, '')


class TopArtistsTest(TestCase):

    def setUp(self):
        self.charts = views.TopArtists()
        self.test_data = {
            'topartists': {
                'artist': [
                    {'name': 'Test', 'playcount': '23'},
                ],
            },
        }

    def test_get_data(self):
        ret = self.charts.get_data(self.test_data)
        self.assertEqual(ret, self.test_data['topartists']['artist'])

    def test_get_item_title(self):
        item = self.test_data['topartists']['artist'][0]
        ret = self.charts.get_item_title(item)
        self.assertEqual(ret, '%s (%s plays)' %
                (item['name'], item['playcount']))

    def test_get_default_image(self):
        ret = self.charts.get_default_image()
        self.assertEqual(urlopen(ret).code, 200)