-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix test by mocking real flickrapi response #30
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('<photos page="2" pages="89" perpage="10" total="881"><photo id="2636" owner="47058503995@N01" secret="a123456" server="2" title="test_04" ispublic="1" isfriend="0" isfamily="0" /> <photo id="2635" owner="47058503995@N01" secret="b123456" server="2" title="test_03" ispublic="0" isfriend="1" isfamily="1" /><photo id="2633" owner="47058503995@N01" secret="c123456" server="2" title="test_01" ispublic="1" isfriend="0" isfamily="0" /><photo id="2610" owner="12037949754@N01" secret="d123456" server="2" title="00_tall" ispublic="1" isfriend="0" isfamily="0" /></photos>') | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we use self.data directly as return value for flickr mock? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we can, but let say if we want to change later the mock data or flickr's team update it api i.e how the etree is structured, we'll be oblige to change each line we have the return value, right now we've only 3 mocks but for the future it'll be easy for us to make one update instead of 3 updates(now ) and may be n updates (later). All approach can lead to the same result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry I meant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that approach is the best one, thanks !!!. |
||
response = self.client.get(reverse('public_home')) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertQuerysetEqual(response.context['recent'], ['<Post: Apply for Global Entrepreneurship Summit>']) | ||
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'], ['<Post: Apply for Global Entrepreneurship Summit>']) | ||
|
@@ -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) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for style one space in arguments