From b0070e702da1d7b20e19773a3800f191b1228715 Mon Sep 17 00:00:00 2001 From: Alino Date: Sun, 17 Apr 2016 13:18:18 +0200 Subject: [PATCH 1/3] Fix test by mocking real flickrapi response --- klab/public/tests.py | 25 ++++++++++++++++++++++--- klab/public/views.py | 11 +++++------ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/klab/public/tests.py b/klab/public/tests.py index 6b22938..3800492 100644 --- a/klab/public/tests.py +++ b/klab/public/tests.py @@ -1,5 +1,8 @@ import string +import mock from random import choice +from klab import flickr +from xml.etree import ElementTree from klab.tests import Helpers from klab.blog.models import Post from klab.members.models import Application, Member @@ -54,15 +57,27 @@ def setUp(self): created_by=self.user, modified_by=self.user) - def test_main_page_with_post_past_event(self): + self.data = ElementTree.fromstring(' ') + + self.mock_response = mock.Mock() + + @mock.patch('flickrapi.FlickrAPI.walk') + def test_main_page_with_post_past_event(self,mock_flickr): self.create_event(-1, "$500k Investment Lessons From Seedstars World 2016", "This Thursday meet Louis Antoine Muhire, CEO& Founder of Mergims, who is going to clear;y highlight and broadly describe the $500k Investment lessons From Seedstars World 2016.") + self.mock_response.etree.return_value = self.data + self.mock_response.status_code = 200 + mock_flickr.return_value = self.mock_response.etree.return_value response = self.client.get(reverse('public_home')) self.assertEqual(response.status_code, 200) self.assertQuerysetEqual(response.context['recent'], ['']) self.assertQuerysetEqual(response.context['upcoming'], []) - def test_main_page_with_post_event(self): + @mock.patch('flickrapi.FlickrAPI.walk') + def test_main_page_with_post_event(self,mock_flickr): self.create_event(0, "Demo Night by Skyline Digital", "This Wednesday,kLab based startup Skyline Digital that is to officially launch 2 platforms,namely rwandait.rw, which will be covering all IT news in Rwanda") + self.mock_response.etree.return_value = self.data + self.mock_response.status_code = 200 + mock_flickr.return_value = self.mock_response.etree.return_value response = self.client.get(reverse('public_home')) self.assertEqual(response.status_code, 200) self.assertQuerysetEqual(response.context['recent'], ['']) @@ -73,7 +88,11 @@ def test_event_detail(self): response = self.client.get(reverse('solo_event', args=(event.id, ))) self.assertContains(response, event.title, status_code=200) - def test_post_detail(self): + @mock.patch('flickrapi.FlickrAPI.walk') + def test_post_detail(self,mock_flickr): + self.mock_response.etree.return_value = self.data + self.mock_response.status_code = 200 + mock_flickr.return_value = self.mock_response.etree.return_value response = self.client.get(reverse('public_post', args=(self.post.id, ))) self.assertContains(response, self.post.title, status_code=200) diff --git a/klab/public/views.py b/klab/public/views.py index 0730b0f..64e6f43 100644 --- a/klab/public/views.py +++ b/klab/public/views.py @@ -33,7 +33,6 @@ class ContactForm(forms.Form): email = forms.EmailField() message = forms.CharField(widget=forms.widgets.Textarea()) - def home(request): try: @@ -100,7 +99,7 @@ def blog(request): def post(request, post_id): post = get_object_or_404(Post, pk=post_id) - + context = dict(post=post) return render(request, 'public/post.html', context) @@ -111,7 +110,7 @@ def projects(request, project_type): else: projects = Project.objects.filter(is_active=True).order_by('-created_on') - + search = request.REQUEST.get("search",None) if search: tokens = search.strip().split() @@ -144,7 +143,7 @@ def members(request, member_type): members = Member.objects.filter(is_active=True, is_alumni=True).order_by('membership_type') else: members = Member.objects.filter(is_active=True, is_alumni=False).order_by('membership_type') - + search = request.REQUEST.get("search",None) if search: tokens = search.strip().split() @@ -160,8 +159,8 @@ def members(request, member_type): def member(request, member_id): member = get_object_or_404(Member, pk=member_id) - - + + context = dict(member=member,project=project) return render(request, 'public/member.html', context) From fdfa28d4ad7c0d4a314bc216f0aafe0e7c468b5e Mon Sep 17 00:00:00 2001 From: Alino Date: Thu, 21 Apr 2016 09:23:11 +0200 Subject: [PATCH 2/3] Add space in method argument --- klab/public/tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/klab/public/tests.py b/klab/public/tests.py index 3800492..1798423 100644 --- a/klab/public/tests.py +++ b/klab/public/tests.py @@ -62,7 +62,7 @@ def setUp(self): self.mock_response = mock.Mock() @mock.patch('flickrapi.FlickrAPI.walk') - def test_main_page_with_post_past_event(self,mock_flickr): + def test_main_page_with_post_past_event(self, mock_flickr): self.create_event(-1, "$500k Investment Lessons From Seedstars World 2016", "This Thursday meet Louis Antoine Muhire, CEO& Founder of Mergims, who is going to clear;y highlight and broadly describe the $500k Investment lessons From Seedstars World 2016.") self.mock_response.etree.return_value = self.data self.mock_response.status_code = 200 @@ -73,7 +73,7 @@ def test_main_page_with_post_past_event(self,mock_flickr): self.assertQuerysetEqual(response.context['upcoming'], []) @mock.patch('flickrapi.FlickrAPI.walk') - def test_main_page_with_post_event(self,mock_flickr): + def test_main_page_with_post_event(self, mock_flickr): self.create_event(0, "Demo Night by Skyline Digital", "This Wednesday,kLab based startup Skyline Digital that is to officially launch 2 platforms,namely rwandait.rw, which will be covering all IT news in Rwanda") self.mock_response.etree.return_value = self.data self.mock_response.status_code = 200 @@ -89,7 +89,7 @@ def test_event_detail(self): self.assertContains(response, event.title, status_code=200) @mock.patch('flickrapi.FlickrAPI.walk') - def test_post_detail(self,mock_flickr): + def test_post_detail(self, mock_flickr): self.mock_response.etree.return_value = self.data self.mock_response.status_code = 200 mock_flickr.return_value = self.mock_response.etree.return_value From 3448350c6ed12e242d6c21d175b4bebf18904767 Mon Sep 17 00:00:00 2001 From: Alino Date: Thu, 21 Apr 2016 09:50:09 +0200 Subject: [PATCH 3/3] Remove mock object --- klab/public/tests.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/klab/public/tests.py b/klab/public/tests.py index 1798423..7d12ff1 100644 --- a/klab/public/tests.py +++ b/klab/public/tests.py @@ -59,14 +59,11 @@ def setUp(self): self.data = ElementTree.fromstring(' ') - self.mock_response = mock.Mock() @mock.patch('flickrapi.FlickrAPI.walk') def test_main_page_with_post_past_event(self, mock_flickr): self.create_event(-1, "$500k Investment Lessons From Seedstars World 2016", "This Thursday meet Louis Antoine Muhire, CEO& Founder of Mergims, who is going to clear;y highlight and broadly describe the $500k Investment lessons From Seedstars World 2016.") - self.mock_response.etree.return_value = self.data - self.mock_response.status_code = 200 - mock_flickr.return_value = self.mock_response.etree.return_value + mock_flickr.return_value = self.data response = self.client.get(reverse('public_home')) self.assertEqual(response.status_code, 200) self.assertQuerysetEqual(response.context['recent'], ['']) @@ -75,9 +72,7 @@ def test_main_page_with_post_past_event(self, mock_flickr): @mock.patch('flickrapi.FlickrAPI.walk') def test_main_page_with_post_event(self, mock_flickr): self.create_event(0, "Demo Night by Skyline Digital", "This Wednesday,kLab based startup Skyline Digital that is to officially launch 2 platforms,namely rwandait.rw, which will be covering all IT news in Rwanda") - self.mock_response.etree.return_value = self.data - self.mock_response.status_code = 200 - mock_flickr.return_value = self.mock_response.etree.return_value + mock_flickr.return_value = self.data response = self.client.get(reverse('public_home')) self.assertEqual(response.status_code, 200) self.assertQuerysetEqual(response.context['recent'], ['']) @@ -90,9 +85,7 @@ def test_event_detail(self): @mock.patch('flickrapi.FlickrAPI.walk') def test_post_detail(self, mock_flickr): - self.mock_response.etree.return_value = self.data - self.mock_response.status_code = 200 - mock_flickr.return_value = self.mock_response.etree.return_value + mock_flickr.return_value = self.data response = self.client.get(reverse('public_post', args=(self.post.id, ))) self.assertContains(response, self.post.title, status_code=200)