Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Commit

Permalink
Use patch to test shortenurl
Browse files Browse the repository at this point in the history
  • Loading branch information
giginet committed Mar 25, 2017
1 parent 6a00d01 commit 3060b50
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/kawaz/core/templatetags/templatetags/shortenurl.py
@@ -1,8 +1,6 @@
import re
from django import template
from django.utils.safestring import mark_safe
from kawaz.core.utils.shortenurl import shorten



PATTERN = re.compile(
Expand All @@ -12,6 +10,7 @@

register = template.Library()


@register.tag('shortenurl')
def do_shortenurl(parser, token):
"""
Expand All @@ -27,13 +26,15 @@ def do_shortenurl(parser, token):
parser.delete_first_token()
return ShortenURLNode(nodelist)


class ShortenURLNode(template.Node):
def __init__(self, nodelist):
self.nodelist = nodelist

def render(self, context):
value = self.nodelist.render(context)
def repl(m):
from kawaz.core.utils.shortenurl import shorten
return shorten(m.group())
replaced_value = PATTERN.sub(repl, value)
return mark_safe(replaced_value)
13 changes: 4 additions & 9 deletions src/kawaz/core/templatetags/tests/test_shortenurl.py
@@ -1,8 +1,6 @@
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from django.test import TestCase, override_settings
from django.template import Template, Context
from ..templatetags import shortenurl



URL = "http://www.kawaz.org"
Expand All @@ -17,10 +15,7 @@ def test_shortenurl_templatetag(self):
"{% endshortenurl %}"
))

mock = MagicMock()
mock.return_value = 'http://goo.gl/testurl'
shortenurl.shorten = mock

rendered = t.render(Context())

with patch('kawaz.core.utils.shortenurl.shorten') as shorten:
shorten.return_value = 'http://goo.gl/testurl'
rendered = t.render(Context())
self.assertRegex(rendered, r'http:\/\/goo\.gl\/.+')
5 changes: 3 additions & 2 deletions src/kawaz/core/utils/shortenurl.py
@@ -1,11 +1,10 @@
import json
import logging
from urllib.request import urlopen, Request
from django.conf import settings


API_URL = 'https://www.googleapis.com/urlshortener/v1/url'


def shorten(url):
"""
与えられたURLをgoo.glを使って短縮します
Expand All @@ -14,6 +13,8 @@ def shorten(url):
詳細は以下を参照してください
https://developers.google.com/url-shortener/v1/getting_started#auth
"""
from urllib.request import urlopen, Request

api_key = getattr(settings, 'GOOGLE_URL_SHORTENER_API_KEY', None)
try:
api_url = API_URL
Expand Down
16 changes: 9 additions & 7 deletions src/kawaz/core/utils/tests/test_shortenurl.py
@@ -1,12 +1,11 @@
import json
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
from django.test import TestCase, override_settings
from kawaz.core.utils import shortenurl



URL = "http://www.kawaz.org"


@override_settings(GOOGLE_URL_SHORTENER_API_KEY='key')
class ShortenURLTestCase(TestCase):
def test_shortenurl(self):
Expand All @@ -18,9 +17,10 @@ def dummy_urlopen(request):
json_string = json.dumps({'id': 'http://goo.gl/hogehoge'})
mock.read.return_value = json_string.encode('utf-8')
return mock
shortenurl.urlopen = dummy_urlopen

url = shortenurl.shorten(URL)
with patch('urllib.request.urlopen') as urlopen:
urlopen.side_effect = dummy_urlopen
url = shortenurl.shorten(URL)

self.assertRegex(url, r'^http:\/\/goo\.gl\/.+$')

Expand All @@ -31,6 +31,8 @@ def test_shortenurl_failed(self):
def dummy_urlopen(request):
raise Exception("Something went wrong")

shortenurl.urlopen = dummy_urlopen
url = shortenurl.shorten(URL)
with patch('urllib.request.urlopen') as urlopen:
urlopen.side_effect = dummy_urlopen
url = shortenurl.shorten(URL)

self.assertEqual(url, URL)

0 comments on commit 3060b50

Please sign in to comment.