Author: ramiro
Date: 2011-06-25 06:14:31 -0700 (Sat, 25 Jun 2011)
New Revision: 16451

Modified:
   django/trunk/tests/regressiontests/forms/tests/error_messages.py
   django/trunk/tests/regressiontests/forms/tests/fields.py
Log:
Fixed #6189 -- Modified test that need Internet access so they use a mock 
instead. Thanks Gregor M?\195?\188ellegger for the patch.

Modified: django/trunk/tests/regressiontests/forms/tests/error_messages.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests/error_messages.py    
2011-06-25 12:39:57 UTC (rev 16450)
+++ django/trunk/tests/regressiontests/forms/tests/error_messages.py    
2011-06-25 13:14:31 UTC (rev 16451)
@@ -4,6 +4,7 @@
 from django.test import TestCase
 from django.utils.safestring import mark_safe
 from django.utils import unittest
+from regressiontests.forms.tests.fields import verify_exists_urls
 
 class AssertFormErrorsMixin(object):
     def assertFormErrors(self, expected, the_callable, *args, **kwargs):
@@ -139,6 +140,7 @@
         self.assertFormErrors([u'EMPTY FILE'], f.clean, 
SimpleUploadedFile('name', None))
         self.assertFormErrors([u'EMPTY FILE'], f.clean, 
SimpleUploadedFile('name', ''))
 
+    @verify_exists_urls()
     def test_urlfield(self):
         e = {
             'required': 'REQUIRED',

Modified: django/trunk/tests/regressiontests/forms/tests/fields.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests/fields.py    2011-06-25 
12:39:57 UTC (rev 16450)
+++ django/trunk/tests/regressiontests/forms/tests/fields.py    2011-06-25 
13:14:31 UTC (rev 16451)
@@ -25,11 +25,11 @@
 __init__(). For example, CharField has a max_length option.
 """
 import datetime
-import time
 import re
 import os
 import urllib2
 from decimal import Decimal
+from functools import wraps
 
 from django.core.files.uploadedfile import SimpleUploadedFile
 from django.forms import *
@@ -48,6 +48,28 @@
         return x
 
 
+def verify_exists_urls(existing_urls=()):
+    def decorator(func):
+        @wraps(func)
+        def wrapper(*args, **kwargs):
+            from django.core import validators
+            # patch urllib2
+            original_urlopen = validators.urllib2.urlopen
+            def urlopen(req):
+                url = req.get_full_url()
+                if url in existing_urls:
+                    return True
+                raise Exception()
+            try:
+                urllib2.urlopen = urlopen
+                func(*args, **kwargs)
+            finally:
+                # unpatch urllib2
+                validators.urllib2.urlopen = original_urlopen
+        return wrapper
+    return decorator
+
+
 class FieldsTests(TestCase):
 
     def assertRaisesErrorWithMessage(self, error, message, callable, *args, 
**kwargs):
@@ -595,9 +617,10 @@
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid 
URL.']", f.clean, 'http://example.')
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid 
URL.']", f.clean, 'http://.com')
 
+    @verify_exists_urls(('http://www.google.com/',))
     def test_urlfield_3(self):
         f = URLField(verify_exists=True)
-        self.assertEqual(u'http://www.google.com/', 
f.clean('http://www.google.com')) # This will fail if there's no Internet 
connection
+        self.assertEqual(u'http://www.google.com/', 
f.clean('http://www.google.com'))
         self.assertRaisesErrorWithMessage(ValidationError, "[u'Enter a valid 
URL.']", f.clean, 'http://example')
         self.assertRaises(ValidationError, f.clean, 
'http://www.broken.djangoproject.com') # bad domain
         self.assertRaises(ValidationError, f.clean, 
'http://qa-dev.w3.org/link-testsuite/http.php?code=405') # Method not allowed
@@ -611,10 +634,11 @@
         except ValidationError, e:
             self.assertEqual("[u'This URL appears to be a broken link.']", 
str(e))
 
+    @verify_exists_urls(('http://www.google.com/',))
     def test_urlfield_4(self):
         f = URLField(verify_exists=True, required=False)
         self.assertEqual(u'', f.clean(''))
-        self.assertEqual(u'http://www.google.com/', 
f.clean('http://www.google.com')) # This will fail if there's no Internet 
connection
+        self.assertEqual(u'http://www.google.com/', 
f.clean('http://www.google.com'))
 
     def test_urlfield_5(self):
         f = URLField(min_length=15, max_length=20)
@@ -663,17 +687,12 @@
         except ValidationError, e:
             self.assertEqual("[u'This URL appears to be a broken link.']", 
str(e))
 
+    @verify_exists_urls(('http://xn--tr-xka.djangoproject.com/',))
     def test_urlfield_10(self):
-        # UTF-8 char in path, enclosed by a monkey-patch to make sure
-        # the encoding is passed to urllib2.urlopen
+        # UTF-8 char in path
         f = URLField(verify_exists=True)
-        try:
-            _orig_urlopen = urllib2.urlopen
-            urllib2.urlopen = lambda req: True
-            url = u'http://t\xfcr.djangoproject.com/'
-            self.assertEqual(url, f.clean(url))
-        finally:
-            urllib2.urlopen = _orig_urlopen
+        url = u'http://t\xfcr.djangoproject.com/'
+        self.assertEqual(url, f.clean(url))
 
     # BooleanField 
################################################################
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to