Ticket #16816: 16816.verify-exists-test-mock.4.diff

File 16816.verify-exists-test-mock.4.diff, 4.4 KB (added by Julien Phalip, 12 years ago)

Small tweak to the previous patch. Not sure where to move verify_exists_urls() to avoid doing that ugly relative import in validation/tests.py

  • tests/modeltests/validation/tests.py

    diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
    index a7e13e7..25683b4 100644
    a b from __future__ import absolute_import  
    22
    33import warnings
    44
    5 from django import forms
     5from django.forms import ModelForm
    66from django.core.exceptions import NON_FIELD_ERRORS
    77from django.test import TestCase
    88
     9# Import the verify_exists_urls from the 'forms' test app
     10from ..regressiontests.forms.tests.fields import verify_exists_urls
     11
    912from . import ValidationTestCase
    1013from .models import (Author, Article, ModelToValidate,
    1114    GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest)
     15
    1216# Import other tests for this package.
    1317from .test_custom_messages import CustomMessagesTest
    1418from .test_error_messages import ValidationMessagesTest
    class BaseModelValidationTests(ValidationTestCase):  
    7175        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=404')
    7276        self.assertFieldFailsValidationWithMessage(mtv.full_clean, 'url_verify', [u'This URL appears to be a broken link.'])
    7377
     78    @verify_exists_urls(existing_urls=('http://www.google.com/',))
    7479    def test_correct_url_value_passes(self):
    7580        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://www.google.com/')
    7681        self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
    7782
     83    @verify_exists_urls(existing_urls=('http://qa-dev.w3.org/link-testsuite/http.php?code=301',))
    7884    def test_correct_url_with_redirect(self):
    7985        mtv = ModelToValidate(number=10, name='Some Name', url_verify='http://qa-dev.w3.org/link-testsuite/http.php?code=301') #example.com is a redirect to iana.org now
    8086        self.assertEqual(None, mtv.full_clean()) # This will fail if there's no Internet connection
    class BaseModelValidationTests(ValidationTestCase):  
    8894        self.assertFailsValidation(mtv.full_clean, ['name',])
    8995
    9096
    91 class ArticleForm(forms.ModelForm):
     97class ArticleForm(ModelForm):
    9298    class Meta:
    9399        model = Article
    94100        exclude = ['author']
  • tests/regressiontests/forms/tests/fields.py

    diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
    index d37ad5e..ecb6c23 100644
    a b def fix_os_paths(x):  
    4949
    5050
    5151def verify_exists_urls(existing_urls=()):
     52    """
     53    Patches urllib to simulate the availability of some urls even when there
     54    is no Internet connection. This hack should be removed alongside with
     55    `URLField.verify_exists` in Django 1.5.
     56    """
    5257    def decorator(func):
    5358        @wraps(func)
    5459        def wrapper(*args, **kwargs):
    5560            from django.core import validators
    56             # patch urllib2
    57             original_urlopen = validators.urllib2.urlopen
    58             def urlopen(req):
    59                 url = req.get_full_url()
    60                 if url in existing_urls:
     61            # patch urllib2.OpenerDirector
     62            original_open = validators.urllib2.OpenerDirector.open
     63            def custom_open(self, req, data=None, timeout=None):
     64                if req.get_full_url() in existing_urls:
    6165                    return True
    6266                raise Exception()
    6367            try:
    64                 urllib2.urlopen = urlopen
     68                urllib2.OpenerDirector.open = custom_open
    6569                func(*args, **kwargs)
    6670            finally:
    67                 # unpatch urllib2
    68                 validators.urllib2.urlopen = original_urlopen
     71                # unpatch urllib2.OpenerDirector
     72                validators.urllib2.OpenerDirector.open = original_open
    6973        return wrapper
    7074    return decorator
    7175
    class FieldsTests(SimpleTestCase):  
    690694        except ValidationError, e:
    691695            self.assertEqual("[u'This URL appears to be a broken link.']", str(e))
    692696
     697    @verify_exists_urls((u'http://xn--hxargifdar.idn.icann.org/%CE%91%CF%81%CF%87%CE%B9%CE%BA%CE%AE_%CF%83%CE%B5%CE%BB%CE%AF%CE%B4%CE%B1',))
    693698    def test_urlfield_10(self):
    694         # UTF-8 in the domain. 
     699        # UTF-8 in the domain.
    695700        f = URLField(verify_exists=True)
    696701        url = u'http://\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac.idn.icann.org/\u0391\u03c1\u03c7\u03b9\u03ba\u03ae_\u03c3\u03b5\u03bb\u03af\u03b4\u03b1'
    697702        self.assertEqual(url, f.clean(url))
Back to Top