Stefan Scherfke avatar Stefan Scherfke committed 9452486

Done with the tests.

Comments (0)

Files changed (3)

lastfm/templatetags/lastfm_widget.py

     }
 
 """
-
-
 from django import template
 from django.conf import settings
 

lastfm/tests/templatetags.py

 # encoding: utf-8
-
 """
 Tests for the last.fm app.
 
 """
-
-
 from django.test import TestCase
 import mock
 
         self.assertEqual(node.var_name, var_name)
 
     @mock.patch('django.template.loader.get_template')
-    @mock.patch('django.template.Context')
-    def test_lastfm_widget_node(self, get_template, Context):
+    def test_lastfm_widget_node(self, get_template_mock):
         """
         Test if the template node contains the correct template variables.
 
         class ContextMock(dict):
             autoescape = object()
         context = ContextMock()
-        # template = get_template.return_value
 
         var_name = 'widgets'
         node = lastfm_widget.LastfmWidgetNode(var_name)
         node.render(context)
 
+        self.assertTrue(get_template_mock.called)
         self.assertTrue(var_name in context)
         widget = context[var_name]
         self.assertTrue('title' in widget)

lastfm/tests/views.py

-from urllib import urlopen
+# encoding: utf-8
+from StringIO import StringIO
+import urllib
 
+from django.core.urlresolvers import reverse
 from django.test import TestCase
 import mock
 
 from lastfm import views
 
 
+class ViewsTest(TestCase):
+
+    @mock.patch('urllib.urlopen')
+    @mock.patch.object(views, 'LASTFM_CHART_TYPE', 'recent_tracks')
+    @mock.patch.object(views, 'LASTFM_IMG_SIZE', 'large')
+    def test_lastfm_data(self, urlopen_mock):
+        assert views.LASTFM_CHART_TYPE == 'recent_tracks'
+        assert views.urllib.urlopen is urlopen_mock
+
+        # Test last.fm error
+        urlopen_mock.return_value = StringIO('{"error": "fu"}')
+        with self.assertRaises(RuntimeError):
+            self.client.get(reverse('lastfm'))
+
+        # Test empty data
+        urlopen_mock.return_value = StringIO('{"recenttracks":{"track":{}}}')
+        response = self.client.get(reverse('lastfm'))
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.content, '[]')
+
+        # Test only one item
+        urlopen_mock.return_value = StringIO(
+           '{"recenttracks":{"track":{"artist":{"#text":"Artist1"},'
+           '"name":"Name1","url":"URL1","image":[{"#text":"small_img",'
+           '"size":"small"},{"#text":"medium_img","size":"medium"},'
+           '{"#text":"large_img","size":"large"},{"#text":'
+           '"extralarge_img","size":"extralarge"}]}}}')
+        response = self.client.get(reverse('lastfm'))
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.content, '[{"url": "URL1", "img_url":'
+            ' "large_img", "title": "Artist1 \u2013 Name1"}]')
+
+        # Test normal case
+        urlopen_mock.return_value = StringIO(
+            '{"recenttracks":{"track":[{"artist":{"#text":"Artist1"},'
+            '"name":"Name1","url":"URL1","image":[{"#text":"small_img",'
+            '"size":"small"},{"#text":"medium_img","size":"medium"},'
+            '{"#text":"large_img","size":"large"},{"#text":'
+            '"extralarge_img","size":"extralarge"}]},{"artist":'
+            '{"#text":"Artist2"},"name":"Name2","url":"URL2","image":'
+            '[{"#text":"small_img","size":"small"},{"#text":"medium_img",'
+            '"size":"medium"},{"#text":"large_img","size":"large"},'
+            '{"#text":"extralarge_img","size":"extralarge"}]}]}}')
+        response = self.client.get(reverse('lastfm'))
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.content, '[{"url": "URL1", "img_url": '
+                '"large_img", "title": "Artist1 \u2013 Name1"}, {"url": '
+                '"URL2", "img_url": "large_img", "title": "Artist2 \u2013 '
+                'Name2"}]')
+
+
+class RecentTracksTest(TestCase):
+
+    def setUp(self):
+        self.charts = views.RecentTracks()
+        self.test_data = {
+            'recenttracks': {
+                'track': [
+                    {'artist': {'#text': 'Test'}, 'name': 'Foo'},
+                ],
+            },
+        }
+
+    def test_get_data(self):
+        ret = self.charts.get_data(self.test_data)
+        self.assertEqual(ret, self.test_data['recenttracks']['track'])
+
+    def test_get_item_title(self):
+        item = self.test_data['recenttracks']['track'][0]
+        ret = self.charts.get_item_title(item)
+        self.assertEqual(ret, u'%s – %s' %
+                (item['artist']['#text'], item['name']))
+
+    def test_get_default_image(self):
+        ret = self.charts.get_default_image()
+        self.assertEqual(urllib.urlopen(ret).code, 200)
+
+
 class WeeklyTopArtistsTest(TestCase):
 
     def setUp(self):
 
     def test_get_default_image(self):
         ret = self.charts.get_default_image()
-        self.assertEqual(urlopen(ret).code, 200)
+        self.assertEqual(urllib.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
 
     def test_get_default_image(self):
         ret = self.charts.get_default_image()
-        self.assertEqual(urlopen(ret).code, 200)
+        self.assertEqual(urllib.urlopen(ret).code, 200)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.