Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions klab/public/tests.py
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
Expand Down Expand Up @@ -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):
Copy link
Contributor

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

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I meant mock_flickr.return_value = self.data without the self.mock_response above

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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>'])
Expand All @@ -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)

Expand Down
11 changes: 5 additions & 6 deletions klab/public/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ContactForm(forms.Form):
email = forms.EmailField()
message = forms.CharField(widget=forms.widgets.Textarea())


def home(request):

try:
Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down