From 291250f7b66a6b81d2fc64eecc82cb2b4bd6c090 Mon Sep 17 00:00:00 2001 From: Aaron Cannon Date: Fri, 3 May 2013 08:46:53 -0500 Subject: [PATCH 01/47] Added clarification to the docs, pointing out that unique_for_date only considers the date portion of DateTime fields. --- AUTHORS | 1 + docs/ref/models/fields.txt | 3 +++ 2 files changed, 4 insertions(+) diff --git a/AUTHORS b/AUTHORS index 8cd86d38dc..fb084f26cb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -121,6 +121,7 @@ answer newbie questions, and generally made Django that much better: Chris Cahoon Juan Manuel Caicedo Trevor Caira + Aaron Cannon Brett Cannon Ricardo Javier Cárdenes Medina Jeremy Carbaugh diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index d322904ec9..99ba78cb09 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -287,6 +287,9 @@ For example, if you have a field ``title`` that has ``unique_for_date="pub_date"``, then Django wouldn't allow the entry of two records with the same ``title`` and ``pub_date``. +Note that if you set this to point to a :class:`DateTimeField`, only the date +portion of the field will be considered. + This is enforced by model validation but not at the database level. ``unique_for_month`` From a4dec43b520fa51bf7a949576b5767242c74c982 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Fri, 10 May 2013 16:56:42 +0200 Subject: [PATCH 02/47] Fixed two admin_views tests under Oracle. Thanks Anssi for the review. --- tests/admin_views/tests.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index e0000ff6b5..39c0eb1088 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -26,6 +26,7 @@ from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.models import Group, User, Permission, UNUSABLE_PASSWORD from django.contrib.contenttypes.models import ContentType from django.core.urlresolvers import reverse +from django.db import connection from django.forms.util import ErrorList from django.template.response import TemplateResponse from django.test import TestCase @@ -3605,7 +3606,13 @@ class UserAdminTest(TestCase): # Don't depend on a warm cache, see #17377. ContentType.objects.clear_cache() - with self.assertNumQueries(10): + + expected_queries = 10 + # Oracle doesn't implement "RELEASE SAVPOINT", see #20387. + if connection.vendor == 'oracle': + expected_queries -= 1 + + with self.assertNumQueries(9): response = self.client.get('/test_admin/admin/auth/user/%s/' % u.pk) self.assertEqual(response.status_code, 200) @@ -3643,7 +3650,12 @@ class GroupAdminTest(TestCase): def test_group_permission_performance(self): g = Group.objects.create(name="test_group") - with self.assertNumQueries(8): # instead of 259! + expected_queries = 8 + # Oracle doesn't implement "RELEASE SAVPOINT", see #20387. + if connection.vendor == 'oracle': + expected_queries -= 1 + + with self.assertNumQueries(expected_queries): response = self.client.get('/test_admin/admin/auth/group/%s/' % g.pk) self.assertEqual(response.status_code, 200) From 327e362ff30969fd4ac111fa59cbfcd4c11abd38 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 10 May 2013 08:56:39 -0700 Subject: [PATCH 03/47] Fixed an obvious typo. --- tests/admin_views/tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 39c0eb1088..8e678a72b3 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -3612,7 +3612,7 @@ class UserAdminTest(TestCase): if connection.vendor == 'oracle': expected_queries -= 1 - with self.assertNumQueries(9): + with self.assertNumQueries(expected_queries): response = self.client.get('/test_admin/admin/auth/user/%s/' % u.pk) self.assertEqual(response.status_code, 200) From 92351c74c16f3f234e05cd1792e3a8d5f583301c Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 10 May 2013 15:51:14 -0700 Subject: [PATCH 04/47] Updated my bio. --- docs/internals/committers.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/internals/committers.txt b/docs/internals/committers.txt index f891bc4eb7..5886c0b6b0 100644 --- a/docs/internals/committers.txt +++ b/docs/internals/committers.txt @@ -200,7 +200,7 @@ Karen Tracey He has worked on Django's auth, admin and staticfiles apps as well as the form, core, internationalization and test systems. He currently works - as the lead engineer at Gidsy_. + at Mozilla_. Jannis lives in Berlin, Germany. @@ -208,7 +208,7 @@ Karen Tracey .. _Bauhaus-University Weimar: http://www.uni-weimar.de/ .. _virtualenv: http://www.virtualenv.org/ .. _pip: http://www.pip-installer.org/ -.. _Gidsy: http://gidsy.com/ +.. _Mozilla: http://www.mozilla.org/ `James Tauber`_ James is the lead developer of Pinax_ and the CEO and founder of From c0d8932a6d03e9326a4e407e944b09bf43cf929c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anssi=20K=C3=A4=C3=A4ri=C3=A4inen?= Date: Sat, 11 May 2013 03:48:58 +0300 Subject: [PATCH 05/47] Fixed #19939 -- generic relations + split_exclude regression Added a test, the issue was already fixed (likely by the patch for #19385). --- tests/generic_relations/models.py | 4 ++++ tests/generic_relations/tests.py | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/generic_relations/models.py b/tests/generic_relations/models.py index 34dc8d3a7d..2acb982b9a 100644 --- a/tests/generic_relations/models.py +++ b/tests/generic_relations/models.py @@ -98,3 +98,7 @@ class Gecko(models.Model): # To test fix for #11263 class Rock(Mineral): tags = generic.GenericRelation(TaggedItem) + +class ManualPK(models.Model): + id = models.IntegerField(primary_key=True) + tags = generic.GenericRelation(TaggedItem) diff --git a/tests/generic_relations/tests.py b/tests/generic_relations/tests.py index dd9dc506ca..79c7bc6184 100644 --- a/tests/generic_relations/tests.py +++ b/tests/generic_relations/tests.py @@ -6,7 +6,7 @@ from django.contrib.contenttypes.models import ContentType from django.test import TestCase from .models import (TaggedItem, ValuableTaggedItem, Comparison, Animal, - Vegetable, Mineral, Gecko, Rock) + Vegetable, Mineral, Gecko, Rock, ManualPK) class GenericRelationsTests(TestCase): @@ -75,12 +75,17 @@ class GenericRelationsTests(TestCase): "", "" ]) + # Create another fatty tagged instance with different PK to ensure + # there is a content type restriction in the generated queries below. + mpk = ManualPK.objects.create(id=lion.pk) + mpk.tags.create(tag="fatty") self.assertQuerysetEqual(Animal.objects.filter(tags__tag='fatty'), [ "" ]) self.assertQuerysetEqual(Animal.objects.exclude(tags__tag='fatty'), [ "" ]) + mpk.delete() # If you delete an object with an explicit Generic relation, the related # objects are deleted when the source object is deleted. From 9012833af857e081b515ce760685b157638efcef Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Fri, 10 May 2013 23:08:45 -0400 Subject: [PATCH 06/47] Fixed #17365, #17366, #18727 -- Switched to discovery test runner. Thanks to Preston Timmons for the bulk of the work on the patch, especially updating Django's own test suite to comply with the requirements of the new runner. Thanks also to Jannis Leidel and Mahdi Yusuf for earlier work on the patch and the discovery runner. Refs #11077, #17032, and #18670. --- .gitignore | 2 +- django/conf/global_settings.py | 2 +- django/contrib/admindocs/tests/__init__.py | 1 - django/contrib/auth/tests/__init__.py | 15 - django/contrib/auth/tests/test_handlers.py | 2 +- django/contrib/auth/tests/test_management.py | 2 +- django/contrib/flatpages/tests/__init__.py | 6 - django/contrib/formtools/tests/__init__.py | 2 - django/contrib/formtools/tests/urls.py | 2 +- django/contrib/gis/gdal/__init__.py | 7 +- django/contrib/gis/gdal/tests/__init__.py | 28 -- django/contrib/gis/gdal/tests/test_driver.py | 19 +- django/contrib/gis/gdal/tests/test_ds.py | 64 ++-- .../contrib/gis/gdal/tests/test_envelope.py | 18 +- django/contrib/gis/gdal/tests/test_geom.py | 19 +- django/contrib/gis/gdal/tests/test_srs.py | 16 +- django/contrib/gis/geoip/tests.py | 26 +- django/contrib/gis/geos/__init__.py | 24 +- django/contrib/gis/geos/tests/__init__.py | 28 -- django/contrib/gis/geos/tests/test_geos.py | 44 +-- .../gis/geos/tests/test_geos_mutation.py | 27 +- django/contrib/gis/geos/tests/test_io.py | 16 +- .../gis/geos/tests/test_mutable_list.py | 12 - django/contrib/gis/tests/__init__.py | 97 +----- django/contrib/gis/tests/distapp/tests.py | 31 +- django/contrib/gis/tests/geo3d/tests.py | 19 +- django/contrib/gis/tests/geoadmin/tests.py | 12 +- django/contrib/gis/tests/geoapp/test_feeds.py | 7 +- .../contrib/gis/tests/geoapp/test_regress.py | 7 +- .../contrib/gis/tests/geoapp/test_sitemaps.py | 7 +- django/contrib/gis/tests/geoapp/tests.py | 29 +- django/contrib/gis/tests/geogapp/tests.py | 11 +- django/contrib/gis/tests/inspectapp/tests.py | 12 +- django/contrib/gis/tests/layermap/tests.py | 20 +- django/contrib/gis/tests/relatedapp/tests.py | 15 +- .../contrib/gis/tests/test_spatialrefsys.py | 1 - django/contrib/gis/tests/utils.py | 9 + django/contrib/messages/tests/__init__.py | 5 - django/contrib/sitemaps/tests/__init__.py | 4 - django/test/_doctest.py | 7 + django/test/runner.py | 289 ++++++++++++++++++ django/test/simple.py | 217 +------------ django/test/testcases.py | 10 + django/utils/unittest/loader.py | 2 +- docs/index.txt | 1 - docs/internals/deprecation.txt | 11 +- docs/intro/tutorial05.txt | 5 +- docs/ref/contrib/gis/testing.txt | 57 +--- docs/ref/settings.txt | 7 +- docs/releases/1.2.4.txt | 2 +- docs/releases/1.3.txt | 2 +- docs/releases/1.6.txt | 57 ++++ docs/topics/testing/advanced.txt | 77 +++-- docs/topics/testing/doctests.txt | 81 ----- docs/topics/testing/index.txt | 77 +---- docs/topics/testing/overview.txt | 96 +++--- tests/admin_scripts/tests.py | 6 +- tests/generic_views/tests.py | 11 - tests/i18n/tests.py | 6 - tests/model_fields/tests.py | 5 - tests/runtests.py | 97 +++--- tests/template_tests/tests.py | 9 - .../__init__.py | 0 tests/test_discovery_sample/pattern_tests.py | 7 + .../tests}/__init__.py | 0 tests/test_discovery_sample/tests/tests.py | 7 + tests/test_discovery_sample/tests_sample.py | 22 ++ tests/test_discovery_sample2/__init__.py | 0 tests/test_discovery_sample2/tests.py | 7 + tests/test_runner/test_discover_runner.py | 68 +++++ tests/test_runner/tests.py | 25 +- tests/test_runner_deprecation_app/__init__.py | 0 .../models.py | 0 .../tests.py | 2 - .../__init__.py | 0 .../models/__init__.py | 0 .../tests/__init__.py | 0 tests/utils_tests/tests.py | 34 --- tests/validation/tests.py | 6 - 79 files changed, 959 insertions(+), 1019 deletions(-) create mode 100644 django/test/runner.py delete mode 100644 docs/topics/testing/doctests.txt delete mode 100644 tests/generic_views/tests.py rename tests/{test_runner/deprecation_app => test_discovery_sample}/__init__.py (100%) create mode 100644 tests/test_discovery_sample/pattern_tests.py rename tests/{test_runner/invalid_app/models => test_discovery_sample/tests}/__init__.py (100%) create mode 100644 tests/test_discovery_sample/tests/tests.py create mode 100644 tests/test_discovery_sample/tests_sample.py create mode 100644 tests/test_discovery_sample2/__init__.py create mode 100644 tests/test_discovery_sample2/tests.py create mode 100644 tests/test_runner/test_discover_runner.py create mode 100644 tests/test_runner_deprecation_app/__init__.py rename tests/{test_runner/deprecation_app => test_runner_deprecation_app}/models.py (100%) rename tests/{test_runner/deprecation_app => test_runner_deprecation_app}/tests.py (99%) rename tests/{test_runner/invalid_app => test_runner_invalid_app}/__init__.py (100%) create mode 100644 tests/test_runner_invalid_app/models/__init__.py rename tests/{test_runner/invalid_app => test_runner_invalid_app}/tests/__init__.py (100%) delete mode 100644 tests/utils_tests/tests.py diff --git a/.gitignore b/.gitignore index 2d028c7287..0ad4e34e23 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ MANIFEST dist/ docs/_build/ tests/coverage_html/ -tests/.coverage \ No newline at end of file +tests/.coverage diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index a9af4baa59..53aef351c0 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -576,7 +576,7 @@ DEFAULT_EXCEPTION_REPORTER_FILTER = 'django.views.debug.SafeExceptionReporterFil ########### # The name of the class to use to run the test suite -TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner' +TEST_RUNNER = 'django.test.runner.DiscoverRunner' ############ # FIXTURES # diff --git a/django/contrib/admindocs/tests/__init__.py b/django/contrib/admindocs/tests/__init__.py index 2a67f83bcb..e69de29bb2 100644 --- a/django/contrib/admindocs/tests/__init__.py +++ b/django/contrib/admindocs/tests/__init__.py @@ -1 +0,0 @@ -from .test_fields import TestFieldType diff --git a/django/contrib/auth/tests/__init__.py b/django/contrib/auth/tests/__init__.py index 2e35e33d7f..2c308642e5 100644 --- a/django/contrib/auth/tests/__init__.py +++ b/django/contrib/auth/tests/__init__.py @@ -1,16 +1 @@ -from django.contrib.auth.tests.test_custom_user import * -from django.contrib.auth.tests.test_auth_backends import * -from django.contrib.auth.tests.test_basic import * -from django.contrib.auth.tests.test_context_processors import * -from django.contrib.auth.tests.test_decorators import * -from django.contrib.auth.tests.test_forms import * -from django.contrib.auth.tests.test_remote_user import * -from django.contrib.auth.tests.test_management import * -from django.contrib.auth.tests.test_models import * -from django.contrib.auth.tests.test_handlers import * -from django.contrib.auth.tests.test_hashers import * -from django.contrib.auth.tests.test_signals import * -from django.contrib.auth.tests.test_tokens import * -from django.contrib.auth.tests.test_views import * - # The password for the fixture data users is 'password' diff --git a/django/contrib/auth/tests/test_handlers.py b/django/contrib/auth/tests/test_handlers.py index 41063aaf4a..e0d2fa2200 100644 --- a/django/contrib/auth/tests/test_handlers.py +++ b/django/contrib/auth/tests/test_handlers.py @@ -2,7 +2,7 @@ from __future__ import unicode_literals from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user from django.contrib.auth.models import User, Group -from django.contrib.auth.tests import CustomUser +from django.contrib.auth.tests.test_custom_user import CustomUser from django.contrib.auth.tests.utils import skipIfCustomUser from django.test import TransactionTestCase from django.test.utils import override_settings diff --git a/django/contrib/auth/tests/test_management.py b/django/contrib/auth/tests/test_management.py index 687a5c31cb..04fd4941ab 100644 --- a/django/contrib/auth/tests/test_management.py +++ b/django/contrib/auth/tests/test_management.py @@ -5,7 +5,7 @@ from django.contrib.auth import models, management from django.contrib.auth.management import create_permissions from django.contrib.auth.management.commands import changepassword from django.contrib.auth.models import User -from django.contrib.auth.tests import CustomUser +from django.contrib.auth.tests.test_custom_user import CustomUser from django.contrib.auth.tests.utils import skipIfCustomUser from django.core.management import call_command from django.core.management.base import CommandError diff --git a/django/contrib/flatpages/tests/__init__.py b/django/contrib/flatpages/tests/__init__.py index 3703e23cbf..e69de29bb2 100644 --- a/django/contrib/flatpages/tests/__init__.py +++ b/django/contrib/flatpages/tests/__init__.py @@ -1,6 +0,0 @@ -from django.contrib.flatpages.tests.test_csrf import * -from django.contrib.flatpages.tests.test_forms import * -from django.contrib.flatpages.tests.test_models import * -from django.contrib.flatpages.tests.test_middleware import * -from django.contrib.flatpages.tests.test_templatetags import * -from django.contrib.flatpages.tests.test_views import * diff --git a/django/contrib/formtools/tests/__init__.py b/django/contrib/formtools/tests/__init__.py index 2bbeea8c5a..e69de29bb2 100644 --- a/django/contrib/formtools/tests/__init__.py +++ b/django/contrib/formtools/tests/__init__.py @@ -1,2 +0,0 @@ -from django.contrib.formtools.tests.tests import * -from django.contrib.formtools.tests.wizard import * diff --git a/django/contrib/formtools/tests/urls.py b/django/contrib/formtools/tests/urls.py index 739095d69f..f96f89ecdf 100644 --- a/django/contrib/formtools/tests/urls.py +++ b/django/contrib/formtools/tests/urls.py @@ -5,7 +5,7 @@ This is a URLconf to be loaded by tests.py. Add any URLs needed for tests only. from __future__ import absolute_import from django.conf.urls import patterns, url -from django.contrib.formtools.tests import TestFormPreview +from django.contrib.formtools.tests.tests import TestFormPreview from django.contrib.formtools.tests.forms import TestForm diff --git a/django/contrib/gis/gdal/__init__.py b/django/contrib/gis/gdal/__init__.py index c33fbcb97a..2aa867bb69 100644 --- a/django/contrib/gis/gdal/__init__.py +++ b/django/contrib/gis/gdal/__init__.py @@ -31,6 +31,9 @@ to a non-existant file location (e.g., `GDAL_LIBRARY_PATH='/null/path'`; setting to None/False/'' will not work as a string must be given). """ +from django.contrib.gis.gdal.error import check_err, OGRException, OGRIndexError, SRSException +from django.contrib.gis.gdal.geomtype import OGRGeomType + # Attempting to import objects that depend on the GDAL library. The # HAS_GDAL flag will be set to True if the library is present on # the system. @@ -41,7 +44,7 @@ try: from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform from django.contrib.gis.gdal.geometries import OGRGeometry HAS_GDAL = True -except Exception: +except OGRException: HAS_GDAL = False try: @@ -50,5 +53,3 @@ except ImportError: # No ctypes, but don't raise an exception. pass -from django.contrib.gis.gdal.error import check_err, OGRException, OGRIndexError, SRSException -from django.contrib.gis.gdal.geomtype import OGRGeomType diff --git a/django/contrib/gis/gdal/tests/__init__.py b/django/contrib/gis/gdal/tests/__init__.py index 262d294a43..e69de29bb2 100644 --- a/django/contrib/gis/gdal/tests/__init__.py +++ b/django/contrib/gis/gdal/tests/__init__.py @@ -1,28 +0,0 @@ -""" -Module for executing all of the GDAL tests. None -of these tests require the use of the database. -""" -from __future__ import absolute_import - -from django.utils.unittest import TestSuite, TextTestRunner - -# Importing the GDAL test modules. -from . import test_driver, test_ds, test_envelope, test_geom, test_srs - -test_suites = [test_driver.suite(), - test_ds.suite(), - test_envelope.suite(), - test_geom.suite(), - test_srs.suite(), - ] - -def suite(): - "Builds a test suite for the GDAL tests." - s = TestSuite() - for test_suite in test_suites: - s.addTest(test_suite) - return s - -def run(verbosity=1): - "Runs the GDAL tests." - TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/gdal/tests/test_driver.py b/django/contrib/gis/gdal/tests/test_driver.py index 06ec93f936..c27302da72 100644 --- a/django/contrib/gis/gdal/tests/test_driver.py +++ b/django/contrib/gis/gdal/tests/test_driver.py @@ -1,5 +1,10 @@ -import unittest -from django.contrib.gis.gdal import Driver, OGRException +from django.contrib.gis.gdal import HAS_GDAL +from django.utils import unittest +from django.utils.unittest import skipUnless + +if HAS_GDAL: + from django.contrib.gis.gdal import Driver, OGRException + valid_drivers = ('ESRI Shapefile', 'MapInfo File', 'TIGER', 'S57', 'DGN', 'Memory', 'CSV', 'GML', 'KML') @@ -12,6 +17,8 @@ aliases = {'eSrI' : 'ESRI Shapefile', 'sHp' : 'ESRI Shapefile', } + +@skipUnless(HAS_GDAL, "GDAL is required") class DriverTest(unittest.TestCase): def test01_valid_driver(self): @@ -30,11 +37,3 @@ class DriverTest(unittest.TestCase): for alias, full_name in aliases.items(): dr = Driver(alias) self.assertEqual(full_name, str(dr)) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(DriverTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/gdal/tests/test_ds.py b/django/contrib/gis/gdal/tests/test_ds.py index a87a1c6c35..3664f055f2 100644 --- a/django/contrib/gis/gdal/tests/test_ds.py +++ b/django/contrib/gis/gdal/tests/test_ds.py @@ -1,32 +1,38 @@ import os -import unittest -from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError, GDAL_VERSION -from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString + +from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.geometry.test_data import get_ds_file, TestDS, TEST_DATA +from django.utils import unittest +from django.utils.unittest import skipUnless + +if HAS_GDAL: + from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError, GDAL_VERSION + from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString + + # List of acceptable data sources. + ds_list = ( + TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile', + fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, + extent=(-1.35011,0.166623,-0.524093,0.824508), # Got extent from QGIS + srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]', + field_values={'dbl' : [float(i) for i in range(1, 6)], 'int' : list(range(1, 6)), 'str' : [str(i) for i in range(1, 6)]}, + fids=range(5)), + TestDS('test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype='Point25D', driver='VRT', + fields={'POINT_X' : OFTString, 'POINT_Y' : OFTString, 'NUM' : OFTString}, # VRT uses CSV, which all types are OFTString. + extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV + field_values={'POINT_X' : ['1.0', '5.0', '100.0'], 'POINT_Y' : ['2.0', '23.0', '523.5'], 'NUM' : ['5', '17', '23']}, + fids=range(1,4)), + TestDS('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3, + driver='ESRI Shapefile', + fields={'float' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, + extent=(-1.01513,-0.558245,0.161876,0.839637), # Got extent from QGIS + srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'), + ) + +bad_ds = (TestDS('foo'),) -# List of acceptable data sources. -ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile', - fields={'dbl' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, - extent=(-1.35011,0.166623,-0.524093,0.824508), # Got extent from QGIS - srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]', - field_values={'dbl' : [float(i) for i in range(1, 6)], 'int' : list(range(1, 6)), 'str' : [str(i) for i in range(1, 6)]}, - fids=range(5)), - TestDS('test_vrt', ext='vrt', nfeat=3, nfld=3, geom='POINT', gtype='Point25D', driver='VRT', - fields={'POINT_X' : OFTString, 'POINT_Y' : OFTString, 'NUM' : OFTString}, # VRT uses CSV, which all types are OFTString. - extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV - field_values={'POINT_X' : ['1.0', '5.0', '100.0'], 'POINT_Y' : ['2.0', '23.0', '523.5'], 'NUM' : ['5', '17', '23']}, - fids=range(1,4)), - TestDS('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3, - driver='ESRI Shapefile', - fields={'float' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,}, - extent=(-1.01513,-0.558245,0.161876,0.839637), # Got extent from QGIS - srs_wkt='GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]'), - ) - -bad_ds = (TestDS('foo'), - ) - +@skipUnless(HAS_GDAL, "GDAL is required") class DataSourceTest(unittest.TestCase): def test01_valid_shp(self): @@ -236,11 +242,3 @@ class DataSourceTest(unittest.TestCase): feat = ds[0][0] # Reference value obtained using `ogrinfo`. self.assertEqual(676586997978, feat.get('ALAND10')) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(DataSourceTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/gdal/tests/test_envelope.py b/django/contrib/gis/gdal/tests/test_envelope.py index 07e12c0ca7..7518dc69aa 100644 --- a/django/contrib/gis/gdal/tests/test_envelope.py +++ b/django/contrib/gis/gdal/tests/test_envelope.py @@ -1,5 +1,9 @@ -from django.contrib.gis.gdal import Envelope, OGRException +from django.contrib.gis.gdal import HAS_GDAL from django.utils import unittest +from django.utils.unittest import skipUnless + +if HAS_GDAL: + from django.contrib.gis.gdal import Envelope, OGRException class TestPoint(object): @@ -7,11 +11,13 @@ class TestPoint(object): self.x = x self.y = y + +@skipUnless(HAS_GDAL, "GDAL is required") class EnvelopeTest(unittest.TestCase): def setUp(self): self.e = Envelope(0, 0, 5, 5) - + def test01_init(self): "Testing Envelope initilization." e1 = Envelope((0, 0, 5, 5)) @@ -85,11 +91,3 @@ class EnvelopeTest(unittest.TestCase): self.assertEqual((-1, 0, 5, 5), self.e) self.e.expand_to_include(TestPoint(10, 10)) self.assertEqual((-1, 0, 10, 10), self.e) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(EnvelopeTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/gdal/tests/test_geom.py b/django/contrib/gis/gdal/tests/test_geom.py index 9b8ae6a26b..c048d2bb82 100644 --- a/django/contrib/gis/gdal/tests/test_geom.py +++ b/django/contrib/gis/gdal/tests/test_geom.py @@ -5,12 +5,19 @@ try: except ImportError: import pickle -from django.contrib.gis.gdal import (OGRGeometry, OGRGeomType, OGRException, - OGRIndexError, SpatialReference, CoordTransform, GDAL_VERSION) +from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.geometry.test_data import TestDataMixin from django.utils.six.moves import xrange from django.utils import unittest +from django.utils.unittest import skipUnless +if HAS_GDAL: + from django.contrib.gis.gdal import (OGRGeometry, OGRGeomType, + OGRException, OGRIndexError, SpatialReference, CoordTransform, + GDAL_VERSION) + + +@skipUnless(HAS_GDAL, "GDAL is required") class OGRGeomTest(unittest.TestCase, TestDataMixin): "This tests the OGR Geometry." @@ -476,11 +483,3 @@ class OGRGeomTest(unittest.TestCase, TestDataMixin): "Testing equivalence methods with non-OGRGeometry instances." self.assertNotEqual(None, OGRGeometry('POINT(0 0)')) self.assertEqual(False, OGRGeometry('LINESTRING(0 0, 1 1)') == 3) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(OGRGeomTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/gdal/tests/test_srs.py b/django/contrib/gis/gdal/tests/test_srs.py index 3d3bba7939..363b597dae 100644 --- a/django/contrib/gis/gdal/tests/test_srs.py +++ b/django/contrib/gis/gdal/tests/test_srs.py @@ -1,5 +1,9 @@ -from django.contrib.gis.gdal import SpatialReference, CoordTransform, OGRException, SRSException +from django.contrib.gis.gdal import HAS_GDAL from django.utils import unittest +from django.utils.unittest import skipUnless + +if HAS_GDAL: + from django.contrib.gis.gdal import SpatialReference, CoordTransform, OGRException, SRSException class TestSRS: @@ -46,6 +50,8 @@ well_known = (TestSRS('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",637813 bad_srlist = ('Foobar', 'OOJCS["NAD83 / Texas South Central",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",30.28333333333333],PARAMETER["standard_parallel_2",28.38333333333333],PARAMETER["latitude_of_origin",27.83333333333333],PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],PARAMETER["false_northing",4000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32140"]]',) + +@skipUnless(HAS_GDAL, "GDAL is required") class SpatialRefTest(unittest.TestCase): def test01_wkt(self): @@ -155,11 +161,3 @@ class SpatialRefTest(unittest.TestCase): self.assertEqual('EPSG', s1['AUTHORITY']) self.assertEqual(4326, int(s1['AUTHORITY', 1])) self.assertEqual(None, s1['FOOBAR']) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(SpatialRefTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/geoip/tests.py b/django/contrib/gis/geoip/tests.py index bb4a3e7e23..3fa64bf6be 100644 --- a/django/contrib/gis/geoip/tests.py +++ b/django/contrib/gis/geoip/tests.py @@ -3,16 +3,28 @@ from __future__ import unicode_literals import os from django.conf import settings -from django.contrib.gis.geos import GEOSGeometry -from django.contrib.gis.geoip import GeoIP, GeoIPException +from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.geoip import HAS_GEOIP from django.utils import unittest +from django.utils.unittest import skipUnless from django.utils import six +if HAS_GEOIP: + from . import GeoIP, GeoIPException + +if HAS_GEOS: + from ..geos import GEOSGeometry + + # Note: Requires use of both the GeoIP country and city datasets. # The GEOIP_DATA path should be the only setting set (the directory # should contain links or the actual database files 'GeoIP.dat' and # 'GeoLiteCity.dat'. + + +@skipUnless(HAS_GEOIP and getattr(settings, "GEOIP_PATH", None), + "GeoIP is required along with the GEOIP_DATA setting.") class GeoIPTest(unittest.TestCase): def test01_init(self): @@ -70,6 +82,7 @@ class GeoIPTest(unittest.TestCase): self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'}, g.country(query)) + @skipUnless(HAS_GEOS, "Geos is required") def test04_city(self): "Testing GeoIP city querying methods." g = GeoIP(country='') @@ -105,12 +118,3 @@ class GeoIPTest(unittest.TestCase): g = GeoIP() d = g.city("www.osnabrueck.de") self.assertEqual('Osnabrück', d['city']) - - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(GeoIPTest)) - return s - -def run(verbosity=1): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/geos/__init__.py b/django/contrib/gis/geos/__init__.py index 5885a30bef..945f561fb7 100644 --- a/django/contrib/gis/geos/__init__.py +++ b/django/contrib/gis/geos/__init__.py @@ -3,12 +3,18 @@ The GeoDjango GEOS module. Please consult the GeoDjango documentation for more details: http://geodjango.org/docs/geos.html """ -from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex -from django.contrib.gis.geos.point import Point -from django.contrib.gis.geos.linestring import LineString, LinearRing -from django.contrib.gis.geos.polygon import Polygon -from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon -from django.contrib.gis.geos.error import GEOSException, GEOSIndexError -from django.contrib.gis.geos.io import WKTReader, WKTWriter, WKBReader, WKBWriter -from django.contrib.gis.geos.factory import fromfile, fromstr -from django.contrib.gis.geos.libgeos import geos_version, geos_version_info, GEOS_PREPARE +try: + from .libgeos import geos_version, geos_version_info, GEOS_PREPARE + HAS_GEOS = True +except ImportError: + HAS_GEOS = False + +if HAS_GEOS: + from .geometry import GEOSGeometry, wkt_regex, hex_regex + from .point import Point + from .linestring import LineString, LinearRing + from .polygon import Polygon + from .collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon + from .error import GEOSException, GEOSIndexError + from .io import WKTReader, WKTWriter, WKBReader, WKBWriter + from .factory import fromfile, fromstr diff --git a/django/contrib/gis/geos/tests/__init__.py b/django/contrib/gis/geos/tests/__init__.py index 6b715d8c59..e69de29bb2 100644 --- a/django/contrib/gis/geos/tests/__init__.py +++ b/django/contrib/gis/geos/tests/__init__.py @@ -1,28 +0,0 @@ -""" -GEOS Testing module. -""" -from __future__ import absolute_import - -from django.utils.unittest import TestSuite, TextTestRunner -from . import test_geos, test_io, test_geos_mutation, test_mutable_list - -test_suites = [ - test_geos.suite(), - test_io.suite(), - test_geos_mutation.suite(), - test_mutable_list.suite(), - ] - -def suite(): - "Builds a test suite for the GEOS tests." - s = TestSuite() - for suite in test_suites: - s.addTest(suite) - return s - -def run(verbosity=1): - "Runs the GEOS tests." - TextTestRunner(verbosity=verbosity).run(suite()) - -if __name__ == '__main__': - run(2) diff --git a/django/contrib/gis/geos/tests/test_geos.py b/django/contrib/gis/geos/tests/test_geos.py index 66d890d8cb..c7fe5b2321 100644 --- a/django/contrib/gis/geos/tests/test_geos.py +++ b/django/contrib/gis/geos/tests/test_geos.py @@ -6,20 +6,28 @@ import random from binascii import a2b_hex, b2a_hex from io import BytesIO +from django.contrib.gis.gdal import HAS_GDAL + from django.contrib.gis import memoryview -from django.contrib.gis.geos import (GEOSException, GEOSIndexError, GEOSGeometry, - GeometryCollection, Point, MultiPoint, Polygon, MultiPolygon, LinearRing, - LineString, MultiLineString, fromfile, fromstr, geos_version_info) -from django.contrib.gis.geos.base import gdal, numpy, GEOSBase -from django.contrib.gis.geos.libgeos import GEOS_PREPARE from django.contrib.gis.geometry.test_data import TestDataMixin from django.utils.encoding import force_bytes from django.utils import six from django.utils.six.moves import xrange from django.utils import unittest +from django.utils.unittest import skipUnless + +from .. import HAS_GEOS + +if HAS_GEOS: + from .. import (GEOSException, GEOSIndexError, GEOSGeometry, + GeometryCollection, Point, MultiPoint, Polygon, MultiPolygon, LinearRing, + LineString, MultiLineString, fromfile, fromstr, geos_version_info, + GEOS_PREPARE) + from ..base import gdal, numpy, GEOSBase +@skipUnless(HAS_GEOS, "Geos is required.") class GEOSTest(unittest.TestCase, TestDataMixin): @property @@ -198,7 +206,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertEqual(srid, poly.shell.srid) self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export - @unittest.skipUnless(gdal.HAS_GDAL, "gdal is required") + @skipUnless(HAS_GDAL, "GDAL is required.") def test_json(self): "Testing GeoJSON input/output (via GDAL)." for g in self.geometries.json_geoms: @@ -662,6 +670,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): p3 = fromstr(p1.hex, srid=-1) # -1 is intended. self.assertEqual(-1, p3.srid) + @skipUnless(HAS_GDAL, "GDAL is required.") def test_custom_srid(self): """ Test with a srid unknown from GDAL """ pnt = Point(111200, 220900, srid=999999) @@ -851,7 +860,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): # And, they should be equal. self.assertEqual(gc1, gc2) - @unittest.skipUnless(gdal.HAS_GDAL, "gdal is required") + @skipUnless(HAS_GDAL, "GDAL is required.") def test_gdal(self): "Testing `ogr` and `srs` properties." g1 = fromstr('POINT(5 23)') @@ -878,7 +887,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertNotEqual(poly._ptr, cpy1._ptr) self.assertNotEqual(poly._ptr, cpy2._ptr) - @unittest.skipUnless(gdal.HAS_GDAL, "gdal is required to transform geometries") + @skipUnless(HAS_GDAL, "GDAL is required to transform geometries") def test_transform(self): "Testing `transform` method." orig = GEOSGeometry('POINT (-104.609 38.255)', 4326) @@ -903,7 +912,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertAlmostEqual(trans.x, p.x, prec) self.assertAlmostEqual(trans.y, p.y, prec) - @unittest.skipUnless(gdal.HAS_GDAL, "gdal is required to transform geometries") + @skipUnless(HAS_GDAL, "GDAL is required to transform geometries") def test_transform_3d(self): p3d = GEOSGeometry('POINT (5 23 100)', 4326) p3d.transform(2774) @@ -912,6 +921,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): else: self.assertIsNone(p3d.z) + @skipUnless(HAS_GDAL, "GDAL is required.") def test_transform_noop(self): """ Testing `transform` method (SRID match) """ # transform() should no-op if source & dest SRIDs match, @@ -962,6 +972,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1) self.assertRaises(GEOSException, g.transform, 2774, clone=True) + @skipUnless(HAS_GDAL, "GDAL is required.") def test_transform_nogdal(self): """ Testing `transform` method (GDAL not available) """ old_has_gdal = gdal.HAS_GDAL @@ -1016,7 +1027,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertEqual(geom, tmpg) if not no_srid: self.assertEqual(geom.srid, tmpg.srid) - @unittest.skipUnless(GEOS_PREPARE, "geos >= 3.1.0 is required") + @skipUnless(HAS_GEOS and GEOS_PREPARE, "geos >= 3.1.0 is required") def test_prepared(self): "Testing PreparedGeometry support." # Creating a simple multipolygon and getting a prepared version. @@ -1043,7 +1054,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): for geom, merged in zip(ref_geoms, ref_merged): self.assertEqual(merged, geom.merged) - @unittest.skipUnless(GEOS_PREPARE, "geos >= 3.1.0 is required") + @skipUnless(HAS_GEOS and GEOS_PREPARE, "geos >= 3.1.0 is required") def test_valid_reason(self): "Testing IsValidReason support" @@ -1058,7 +1069,7 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertIsInstance(g.valid_reason, six.string_types) self.assertTrue(g.valid_reason.startswith("Too few points in geometry component")) - @unittest.skipUnless(geos_version_info()['version'] >= '3.2.0', "geos >= 3.2.0 is required") + @skipUnless(HAS_GEOS and geos_version_info()['version'] >= '3.2.0', "geos >= 3.2.0 is required") def test_linearref(self): "Testing linear referencing" @@ -1091,12 +1102,3 @@ class GEOSTest(unittest.TestCase, TestDataMixin): self.assertTrue(m, msg="Unable to parse the version string '%s'" % v_init) self.assertEqual(m.group('version'), v_geos) self.assertEqual(m.group('capi_version'), v_capi) - - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(GEOSTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/geos/tests/test_geos_mutation.py b/django/contrib/gis/geos/tests/test_geos_mutation.py index 0c69b2bf4f..40b708a0ef 100644 --- a/django/contrib/gis/geos/tests/test_geos_mutation.py +++ b/django/contrib/gis/geos/tests/test_geos_mutation.py @@ -2,15 +2,23 @@ # Modified from original contribution by Aryeh Leib Taurog, which was # released under the New BSD license. -from django.contrib.gis.geos import * -from django.contrib.gis.geos.error import GEOSIndexError from django.utils import unittest +from django.utils.unittest import skipUnless + +from .. import HAS_GEOS + +if HAS_GEOS: + from .. import * + from ..error import GEOSIndexError + def getItem(o,i): return o[i] def delItem(o,i): del o[i] def setItem(o,i,v): o[i] = v -def api_get_distance(x): return x.distance(Point(-200,-200)) +if HAS_GEOS: + def api_get_distance(x): return x.distance(Point(-200,-200)) + def api_get_buffer(x): return x.buffer(10) def api_get_geom_typeid(x): return x.geom_typeid def api_get_num_coords(x): return x.num_coords @@ -29,6 +37,8 @@ geos_function_tests = [ val for name, val in vars().items() if hasattr(val, '__call__') and name.startswith('api_get_') ] + +@skipUnless(HAS_GEOS, "Geos is required.") class GEOSMutationTest(unittest.TestCase): """ Tests Pythonic Mutability of Python GEOS geometry wrappers @@ -122,14 +132,3 @@ class GEOSMutationTest(unittest.TestCase): lsa = MultiPoint(*map(Point,((5,5),(3,-2),(8,1)))) for f in geos_function_tests: self.assertEqual(f(lsa), f(mp), 'MultiPoint ' + f.__name__) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(GEOSMutationTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) - -if __name__ == '__main__': - run() diff --git a/django/contrib/gis/geos/tests/test_io.py b/django/contrib/gis/geos/tests/test_io.py index 45a9a220b1..38ca2e0923 100644 --- a/django/contrib/gis/geos/tests/test_io.py +++ b/django/contrib/gis/geos/tests/test_io.py @@ -4,10 +4,16 @@ import binascii import unittest from django.contrib.gis import memoryview -from django.contrib.gis.geos import GEOSGeometry, WKTReader, WKTWriter, WKBReader, WKBWriter, geos_version_info from django.utils import six +from django.utils.unittest import skipUnless + +from ..import HAS_GEOS + +if HAS_GEOS: + from .. import GEOSGeometry, WKTReader, WKTWriter, WKBReader, WKBWriter, geos_version_info +@skipUnless(HAS_GEOS, "Geos is required.") class GEOSIOTest(unittest.TestCase): def test01_wktreader(self): @@ -109,11 +115,3 @@ class GEOSIOTest(unittest.TestCase): wkb_w.srid = True self.assertEqual(hex3d_srid, wkb_w.write_hex(g)) self.assertEqual(wkb3d_srid, wkb_w.write(g)) - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(GEOSIOTest)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) diff --git a/django/contrib/gis/geos/tests/test_mutable_list.py b/django/contrib/gis/geos/tests/test_mutable_list.py index 988d8417a2..a4a56f2e5f 100644 --- a/django/contrib/gis/geos/tests/test_mutable_list.py +++ b/django/contrib/gis/geos/tests/test_mutable_list.py @@ -395,15 +395,3 @@ class ListMixinTest(unittest.TestCase): class ListMixinTestSingle(ListMixinTest): listType = UserListB - -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(ListMixinTest)) - s.addTest(unittest.makeSuite(ListMixinTestSingle)) - return s - -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) - -if __name__ == '__main__': - run() diff --git a/django/contrib/gis/tests/__init__.py b/django/contrib/gis/tests/__init__.py index 765c03018b..1703f3f1b3 100644 --- a/django/contrib/gis/tests/__init__.py +++ b/django/contrib/gis/tests/__init__.py @@ -1,13 +1,4 @@ -from django.conf import settings -from django.test.simple import build_suite, DjangoTestSuiteRunner -from django.utils import unittest - -from .test_geoforms import GeometryFieldTest -from .test_measure import DistanceTest, AreaTest -from .test_spatialrefsys import SpatialRefSysTest - - -def geo_apps(namespace=True, runtests=False): +def geo_apps(): """ Returns a list of GeoDjango test applications that reside in `django.contrib.gis.tests` that can be used with the current @@ -36,88 +27,4 @@ def geo_apps(namespace=True, runtests=False): # 3D apps use LayerMapping, which uses GDAL and require GEOS 3.1+. if connection.ops.postgis and GEOS_PREPARE: apps.append('geo3d') - if runtests: - return [('django.contrib.gis.tests', app) for app in apps] - elif namespace: - return ['django.contrib.gis.tests.%s' % app - for app in apps] - else: - return apps - - -def geodjango_suite(apps=True): - """ - Returns a TestSuite consisting only of GeoDjango tests that can be run. - """ - import sys - from django.db.models import get_app - - suite = unittest.TestSuite() - - # Adding the GEOS tests. - from django.contrib.gis.geos import tests as geos_tests - suite.addTest(geos_tests.suite()) - - # Adding GDAL tests, and any test suite that depends on GDAL, to the - # suite if GDAL is available. - from django.contrib.gis.gdal import HAS_GDAL - if HAS_GDAL: - from django.contrib.gis.gdal import tests as gdal_tests - suite.addTest(gdal_tests.suite()) - else: - sys.stderr.write('GDAL not available - no tests requiring GDAL will be run.\n') - - # Add GeoIP tests to the suite, if the library and data is available. - from django.contrib.gis.geoip import HAS_GEOIP - if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): - from django.contrib.gis.geoip import tests as geoip_tests - suite.addTest(geoip_tests.suite()) - - # Finally, adding the suites for each of the GeoDjango test apps. - if apps: - for app_name in geo_apps(namespace=False): - suite.addTest(build_suite(get_app(app_name))) - - return suite - - -class GeoDjangoTestSuiteRunner(DjangoTestSuiteRunner): - - def setup_test_environment(self, **kwargs): - super(GeoDjangoTestSuiteRunner, self).setup_test_environment(**kwargs) - - # Saving original values of INSTALLED_APPS, ROOT_URLCONF, and SITE_ID. - self.old_installed = getattr(settings, 'INSTALLED_APPS', None) - self.old_root_urlconf = getattr(settings, 'ROOT_URLCONF', '') - self.old_site_id = getattr(settings, 'SITE_ID', None) - - # Constructing the new INSTALLED_APPS, and including applications - # within the GeoDjango test namespace. - new_installed = [ - 'django.contrib.sites', - 'django.contrib.sitemaps', - 'django.contrib.gis', - ] - - # Calling out to `geo_apps` to get GeoDjango applications supported - # for testing. - new_installed.extend(geo_apps()) - settings.INSTALLED_APPS = list(self.old_installed) + new_installed - - # SITE_ID needs to be set - settings.SITE_ID = 1 - - # ROOT_URLCONF needs to be set, else `AttributeErrors` are raised - # when TestCases are torn down that have `urls` defined. - settings.ROOT_URLCONF = '' - - - def teardown_test_environment(self, **kwargs): - super(GeoDjangoTestSuiteRunner, self).teardown_test_environment(**kwargs) - settings.INSTALLED_APPS = self.old_installed - settings.ROOT_URLCONF = self.old_root_urlconf - settings.SITE_ID = self.old_site_id - - - def build_suite(self, test_labels, extra_tests=None, **kwargs): - return geodjango_suite() + return [('django.contrib.gis.tests', app) for app in apps] diff --git a/django/contrib/gis/tests/distapp/tests.py b/django/contrib/gis/tests/distapp/tests.py index 5574b42738..2e778d3fce 100644 --- a/django/contrib/gis/tests/distapp/tests.py +++ b/django/contrib/gis/tests/distapp/tests.py @@ -2,24 +2,33 @@ from __future__ import absolute_import from django.db import connection from django.db.models import Q -from django.contrib.gis.geos import GEOSGeometry, LineString +from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.measure import D # alias for Distance -from django.contrib.gis.tests.utils import oracle, postgis, spatialite, no_oracle, no_spatialite +from django.contrib.gis.tests.utils import ( + HAS_SPATIAL_DB, oracle, postgis, spatialite, no_oracle, no_spatialite +) from django.test import TestCase +from django.utils.unittest import skipUnless -from .models import (AustraliaCity, Interstate, SouthTexasInterstate, - SouthTexasCity, SouthTexasCityFt, CensusZipcode, SouthTexasZipcode) +if HAS_GEOS and HAS_SPATIAL_DB: + from django.contrib.gis.geos import GEOSGeometry, LineString + + from .models import (AustraliaCity, Interstate, SouthTexasInterstate, + SouthTexasCity, SouthTexasCityFt, CensusZipcode, SouthTexasZipcode) +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, + "Geos and spatial db are required.") class DistanceTest(TestCase): - # A point we are testing distances with -- using a WGS84 - # coordinate that'll be implicitly transormed to that to - # the coordinate system of the field, EPSG:32140 (Texas South Central - # w/units in meters) - stx_pnt = GEOSGeometry('POINT (-95.370401017314293 29.704867409475465)', 4326) - # Another one for Australia - au_pnt = GEOSGeometry('POINT (150.791 -34.4919)', 4326) + if HAS_GEOS and HAS_SPATIAL_DB: + # A point we are testing distances with -- using a WGS84 + # coordinate that'll be implicitly transormed to that to + # the coordinate system of the field, EPSG:32140 (Texas South Central + # w/units in meters) + stx_pnt = GEOSGeometry('POINT (-95.370401017314293 29.704867409475465)', 4326) + # Another one for Australia + au_pnt = GEOSGeometry('POINT (150.791 -34.4919)', 4326) def get_names(self, qs): cities = [c.name for c in qs] diff --git a/django/contrib/gis/tests/geo3d/tests.py b/django/contrib/gis/tests/geo3d/tests.py index 6b40164422..c51c0decd7 100644 --- a/django/contrib/gis/tests/geo3d/tests.py +++ b/django/contrib/gis/tests/geo3d/tests.py @@ -3,14 +3,22 @@ from __future__ import absolute_import, unicode_literals import os import re -from django.contrib.gis.db.models import Union, Extent3D -from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon -from django.contrib.gis.utils import LayerMapping, LayerMapError +from django.contrib.gis.gdal import HAS_GDAL +from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB from django.test import TestCase from django.utils._os import upath +from django.utils.unittest import skipUnless -from .models import (City3D, Interstate2D, Interstate3D, InterstateProj2D, - InterstateProj3D, Point2D, Point3D, MultiPoint3D, Polygon2D, Polygon3D) +if HAS_GEOS: + from django.contrib.gis.db.models import Union, Extent3D + from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon + + from .models import (City3D, Interstate2D, Interstate3D, InterstateProj2D, + InterstateProj3D, Point2D, Point3D, MultiPoint3D, Polygon2D, Polygon3D) + +if HAS_GDAL: + from django.contrib.gis.utils import LayerMapping, LayerMapError data_path = os.path.realpath(os.path.join(os.path.dirname(upath(__file__)), '..', 'data')) @@ -54,6 +62,7 @@ bbox_data = ( ) +@skipUnless(HAS_GEOS and HAS_GDAL and HAS_SPATIAL_DB, "Geos, GDAL and spatial db are required.") class Geo3DTest(TestCase): """ Only a subset of the PostGIS routines are 3D-enabled, and this TestCase diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py index 669914bdea..99dcdf0768 100644 --- a/django/contrib/gis/tests/geoadmin/tests.py +++ b/django/contrib/gis/tests/geoadmin/tests.py @@ -1,12 +1,18 @@ from __future__ import absolute_import from django.test import TestCase -from django.contrib.gis import admin -from django.contrib.gis.geos import GEOSGeometry, Point +from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB +from django.utils.unittest import skipUnless -from .models import City +if HAS_GEOS and HAS_SPATIAL_DB: + from django.contrib.gis import admin + from django.contrib.gis.geos import Point + + from .models import City +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoAdminTest(TestCase): urls = 'django.contrib.gis.tests.geoadmin.urls' diff --git a/django/contrib/gis/tests/geoapp/test_feeds.py b/django/contrib/gis/tests/geoapp/test_feeds.py index c9cf6362c1..778cadc9d4 100644 --- a/django/contrib/gis/tests/geoapp/test_feeds.py +++ b/django/contrib/gis/tests/geoapp/test_feeds.py @@ -4,11 +4,16 @@ from xml.dom import minidom from django.conf import settings from django.contrib.sites.models import Site +from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB from django.test import TestCase +from django.utils.unittest import skipUnless -from .models import City +if HAS_GEOS: + from .models import City +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoFeedTest(TestCase): urls = 'django.contrib.gis.tests.geoapp.urls' diff --git a/django/contrib/gis/tests/geoapp/test_regress.py b/django/contrib/gis/tests/geoapp/test_regress.py index a27b2d40f6..d6f3c8d698 100644 --- a/django/contrib/gis/tests/geoapp/test_regress.py +++ b/django/contrib/gis/tests/geoapp/test_regress.py @@ -3,14 +3,19 @@ from __future__ import absolute_import, unicode_literals from datetime import datetime +from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.tests.utils import no_mysql, no_spatialite from django.contrib.gis.shortcuts import render_to_kmz +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB from django.db.models import Count, Min from django.test import TestCase +from django.utils.unittest import skipUnless -from .models import City, PennsylvaniaCity, State, Truth +if HAS_GEOS: + from .models import City, PennsylvaniaCity, State, Truth +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoRegressionTests(TestCase): def test_update(self): diff --git a/django/contrib/gis/tests/geoapp/test_sitemaps.py b/django/contrib/gis/tests/geoapp/test_sitemaps.py index aa2d97032c..337b4b75c6 100644 --- a/django/contrib/gis/tests/geoapp/test_sitemaps.py +++ b/django/contrib/gis/tests/geoapp/test_sitemaps.py @@ -5,12 +5,17 @@ from xml.dom import minidom import zipfile from django.conf import settings +from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB from django.contrib.sites.models import Site from django.test import TestCase +from django.utils.unittest import skipUnless -from .models import City, Country +if HAS_GEOS: + from .models import City, Country +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoSitemapTest(TestCase): urls = 'django.contrib.gis.tests.geoapp.urls' diff --git a/django/contrib/gis/tests/geoapp/tests.py b/django/contrib/gis/tests/geoapp/tests.py index 672d78c1dd..f6de0670c7 100644 --- a/django/contrib/gis/tests/geoapp/tests.py +++ b/django/contrib/gis/tests/geoapp/tests.py @@ -3,26 +3,31 @@ from __future__ import absolute_import import re from django.db import connection -from django.db.utils import DatabaseError from django.contrib.gis import gdal -from django.contrib.gis.geos import (fromstr, GEOSGeometry, - Point, LineString, LinearRing, Polygon, GeometryCollection) +from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.tests.utils import ( - no_mysql, no_oracle, no_spatialite, + HAS_SPATIAL_DB, no_mysql, no_oracle, no_spatialite, mysql, oracle, postgis, spatialite) from django.test import TestCase from django.utils import six, unittest +from django.utils.unittest import skipUnless -from .models import Country, City, PennsylvaniaCity, State, Track +if HAS_GEOS: + from django.contrib.gis.geos import (fromstr, GEOSGeometry, + Point, LineString, LinearRing, Polygon, GeometryCollection) -from .test_feeds import GeoFeedTest -from .test_regress import GeoRegressionTests -from .test_sitemaps import GeoSitemapTest + from .models import Country, City, PennsylvaniaCity, State, Track - -if not spatialite: +if HAS_GEOS and not spatialite: from .models import Feature, MinusOneSRID + +def postgis_bug_version(): + spatial_version = getattr(connection.ops, "spatial_version", (0,0,0)) + return spatial_version and (2, 0, 0) <= spatial_version <= (2, 0, 1) + + +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoModelTest(TestCase): def test_fixtures(self): @@ -197,6 +202,7 @@ class GeoModelTest(TestCase): self.assertTrue(isinstance(cities2[0].point, Point)) +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoLookupTest(TestCase): @no_mysql @@ -297,7 +303,7 @@ class GeoLookupTest(TestCase): # The left/right lookup tests are known failures on PostGIS 2.0/2.0.1 # http://trac.osgeo.org/postgis/ticket/2035 - if connection.ops.postgis and (2, 0, 0) <= connection.ops.spatial_version <= (2, 0, 1): + if postgis_bug_version(): test_left_right_lookups = unittest.expectedFailure(test_left_right_lookups) def test_equals_lookups(self): @@ -382,6 +388,7 @@ class GeoLookupTest(TestCase): self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, intersects_mask)).name) +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeoQuerySetTest(TestCase): # Please keep the tests in GeoQuerySet method's alphabetic order diff --git a/django/contrib/gis/tests/geogapp/tests.py b/django/contrib/gis/tests/geogapp/tests.py index a8c607c502..8d3e96b0a9 100644 --- a/django/contrib/gis/tests/geogapp/tests.py +++ b/django/contrib/gis/tests/geogapp/tests.py @@ -5,14 +5,19 @@ from __future__ import absolute_import import os -from django.contrib.gis import gdal +from django.contrib.gis.gdal import HAS_GDAL +from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.measure import D +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB from django.test import TestCase from django.utils._os import upath +from django.utils.unittest import skipUnless -from .models import City, County, Zipcode +if HAS_GEOS: + from .models import City, County, Zipcode +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class GeographyTest(TestCase): def test01_fixture_load(self): @@ -54,11 +59,11 @@ class GeographyTest(TestCase): htown = City.objects.get(name='Houston') self.assertRaises(ValueError, City.objects.get, point__exact=htown.point) + @skipUnless(HAS_GDAL, "GDAL is required.") def test05_geography_layermapping(self): "Testing LayerMapping support on models with geography fields." # There is a similar test in `layermap` that uses the same data set, # but the County model here is a bit different. - if not gdal.HAS_GDAL: return from django.contrib.gis.utils import LayerMapping # Getting the shapefile and mapping dictionary. diff --git a/django/contrib/gis/tests/inspectapp/tests.py b/django/contrib/gis/tests/inspectapp/tests.py index d28bdf0290..668b87ba86 100644 --- a/django/contrib/gis/tests/inspectapp/tests.py +++ b/django/contrib/gis/tests/inspectapp/tests.py @@ -4,13 +4,19 @@ import os from django.db import connections from django.test import TestCase -from django.contrib.gis.gdal import Driver +from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.geometry.test_data import TEST_DATA -from django.contrib.gis.utils.ogrinspect import ogrinspect +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB +from django.utils.unittest import skipUnless -from .models import AllOGRFields +if HAS_GDAL: + from django.contrib.gis.gdal import Driver + from django.contrib.gis.utils.ogrinspect import ogrinspect + + from .models import AllOGRFields +@skipUnless(HAS_GDAL and HAS_SPATIAL_DB, "GDAL and spatial db are required.") class OGRInspectTest(TestCase): maxDiff = 1024 diff --git a/django/contrib/gis/tests/layermap/tests.py b/django/contrib/gis/tests/layermap/tests.py index 470e5be216..8379311a2b 100644 --- a/django/contrib/gis/tests/layermap/tests.py +++ b/django/contrib/gis/tests/layermap/tests.py @@ -5,19 +5,23 @@ import os from copy import copy from decimal import Decimal -from django.contrib.gis.gdal import DataSource -from django.contrib.gis.tests.utils import mysql -from django.contrib.gis.utils.layermapping import (LayerMapping, LayerMapError, - InvalidDecimal, MissingForeignKey) +from django.contrib.gis.gdal import HAS_GDAL +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB, mysql from django.db import router from django.conf import settings from django.test import TestCase from django.utils import unittest +from django.utils.unittest import skipUnless from django.utils._os import upath -from .models import ( - City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State, - city_mapping, co_mapping, cofeat_mapping, inter_mapping) +if HAS_GDAL: + from django.contrib.gis.utils.layermapping import (LayerMapping, + LayerMapError, InvalidDecimal, MissingForeignKey) + from django.contrib.gis.gdal import DataSource + + from .models import ( + City, County, CountyFeat, Interstate, ICity1, ICity2, Invalid, State, + city_mapping, co_mapping, cofeat_mapping, inter_mapping) shp_path = os.path.realpath(os.path.join(os.path.dirname(upath(__file__)), os.pardir, 'data')) @@ -32,6 +36,7 @@ NUMS = [1, 2, 1, 19, 1] # Number of polygons for each. STATES = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado'] +@skipUnless(HAS_GDAL and HAS_SPATIAL_DB, "GDAL and spatial db are required.") class LayerMapTest(TestCase): def test_init(self): @@ -310,6 +315,7 @@ class OtherRouter(object): return True +@skipUnless(HAS_GDAL and HAS_SPATIAL_DB, "GDAL and spatial db are required.") class LayerMapRouterTest(TestCase): def setUp(self): diff --git a/django/contrib/gis/tests/relatedapp/tests.py b/django/contrib/gis/tests/relatedapp/tests.py index 93a8ff4efd..6320edcff5 100644 --- a/django/contrib/gis/tests/relatedapp/tests.py +++ b/django/contrib/gis/tests/relatedapp/tests.py @@ -2,15 +2,20 @@ from __future__ import absolute_import from datetime import date -from django.contrib.gis.geos import GEOSGeometry, Point, MultiPoint -from django.contrib.gis.db.models import Collect, Count, Extent, F, Union -from django.contrib.gis.geometry.backend import Geometry -from django.contrib.gis.tests.utils import mysql, oracle, no_mysql, no_oracle, no_spatialite +from django.contrib.gis.geos import HAS_GEOS +from django.contrib.gis.tests.utils import HAS_SPATIAL_DB, mysql, oracle, no_mysql, no_oracle, no_spatialite from django.test import TestCase +from django.utils.unittest import skipUnless -from .models import City, Location, DirectoryEntry, Parcel, Book, Author, Article +if HAS_GEOS: + from django.contrib.gis.db.models import Collect, Count, Extent, F, Union + from django.contrib.gis.geometry.backend import Geometry + from django.contrib.gis.geos import GEOSGeometry, Point, MultiPoint + + from .models import City, Location, DirectoryEntry, Parcel, Book, Author, Article +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") class RelatedGeoModelTest(TestCase): def test02_select_related(self): diff --git a/django/contrib/gis/tests/test_spatialrefsys.py b/django/contrib/gis/tests/test_spatialrefsys.py index 257e06a3f8..f33883e9f1 100644 --- a/django/contrib/gis/tests/test_spatialrefsys.py +++ b/django/contrib/gis/tests/test_spatialrefsys.py @@ -1,4 +1,3 @@ -from django.db import connection from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.tests.utils import (no_mysql, oracle, postgis, spatialite, HAS_SPATIALREFSYS, SpatialRefSys) diff --git a/django/contrib/gis/tests/utils.py b/django/contrib/gis/tests/utils.py index 8355b27fd7..a09a3ab531 100644 --- a/django/contrib/gis/tests/utils.py +++ b/django/contrib/gis/tests/utils.py @@ -35,3 +35,12 @@ elif spatialite: else: HAS_SPATIALREFSYS = False SpatialRefSys = None + + +def has_spatial_db(): + # All databases must have spatial backends to run GeoDjango tests. + spatial_dbs = [name for name, db_dict in settings.DATABASES.items() + if db_dict['ENGINE'].startswith('django.contrib.gis')] + return len(spatial_dbs) == len(settings.DATABASES) + +HAS_SPATIAL_DB = has_spatial_db() diff --git a/django/contrib/messages/tests/__init__.py b/django/contrib/messages/tests/__init__.py index e57594dec9..e69de29bb2 100644 --- a/django/contrib/messages/tests/__init__.py +++ b/django/contrib/messages/tests/__init__.py @@ -1,5 +0,0 @@ -from django.contrib.messages.tests.test_cookie import CookieTest -from django.contrib.messages.tests.test_fallback import FallbackTest -from django.contrib.messages.tests.test_middleware import MiddlewareTest -from django.contrib.messages.tests.test_session import SessionTest -from django.contrib.messages.tests.test_mixins import SuccessMessageMixinTests diff --git a/django/contrib/sitemaps/tests/__init__.py b/django/contrib/sitemaps/tests/__init__.py index fdbb9696d5..e69de29bb2 100644 --- a/django/contrib/sitemaps/tests/__init__.py +++ b/django/contrib/sitemaps/tests/__init__.py @@ -1,4 +0,0 @@ -from .test_flatpages import FlatpagesSitemapTests -from .test_generic import GenericViewsSitemapTests -from .test_http import HTTPSitemapTests -from .test_https import HTTPSSitemapTests, HTTPSDetectionSitemapTests diff --git a/django/test/_doctest.py b/django/test/_doctest.py index 9b8ddc9696..5381cff2f0 100644 --- a/django/test/_doctest.py +++ b/django/test/_doctest.py @@ -49,6 +49,13 @@ files containing doctests. There are also many ways to override parts of doctest's default behaviors. See the Library Reference Manual for details. """ +import warnings + +warnings.warn( + "The django.test._doctest module is deprecated; " + "use the doctest module from the Python standard library instead.", + PendingDeprecationWarning) + __docformat__ = 'reStructuredText en' diff --git a/django/test/runner.py b/django/test/runner.py new file mode 100644 index 0000000000..e753e365fa --- /dev/null +++ b/django/test/runner.py @@ -0,0 +1,289 @@ +import os +from optparse import make_option + +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured +from django.test import TestCase +from django.test.utils import setup_test_environment, teardown_test_environment +from django.utils import unittest +from django.utils.unittest import TestSuite, defaultTestLoader + + +class DiscoverRunner(object): + """ + A Django test runner that uses unittest2 test discovery. + """ + + test_loader = defaultTestLoader + reorder_by = (TestCase, ) + option_list = ( + make_option('-t', '--top-level-directory', + action='store', dest='top_level', default=None, + help='Top level of project for unittest discovery.'), + make_option('-p', '--pattern', action='store', dest='pattern', + default="test*.py", + help='The test matching pattern. Defaults to test*.py.'), + ) + + def __init__(self, pattern=None, top_level=None, + verbosity=1, interactive=True, failfast=False, + **kwargs): + + self.pattern = pattern + self.top_level = top_level + + self.verbosity = verbosity + self.interactive = interactive + self.failfast = failfast + + def setup_test_environment(self, **kwargs): + setup_test_environment() + settings.DEBUG = False + unittest.installHandler() + + def build_suite(self, test_labels=None, extra_tests=None, **kwargs): + suite = TestSuite() + test_labels = test_labels or ['.'] + extra_tests = extra_tests or [] + + discover_kwargs = {} + if self.pattern is not None: + discover_kwargs['pattern'] = self.pattern + if self.top_level is not None: + discover_kwargs['top_level_dir'] = self.top_level + + for label in test_labels: + kwargs = discover_kwargs.copy() + tests = None + + label_as_path = os.path.abspath(label) + + # if a module, or "module.ClassName[.method_name]", just run those + if not os.path.exists(label_as_path): + tests = self.test_loader.loadTestsFromName(label) + elif os.path.isdir(label_as_path) and not self.top_level: + # Try to be a bit smarter than unittest about finding the + # default top-level for a given directory path, to avoid + # breaking relative imports. (Unittest's default is to set + # top-level equal to the path, which means relative imports + # will result in "Attempted relative import in non-package."). + + # We'd be happy to skip this and require dotted module paths + # (which don't cause this problem) instead of file paths (which + # do), but in the case of a directory in the cwd, which would + # be equally valid if considered as a top-level module or as a + # directory path, unittest unfortunately prefers the latter. + + top_level = label_as_path + while True: + init_py = os.path.join(top_level, '__init__.py') + if os.path.exists(init_py): + try_next = os.path.dirname(top_level) + if try_next == top_level: + # __init__.py all the way down? give up. + break + top_level = try_next + continue + break + kwargs['top_level_dir'] = top_level + + + if not (tests and tests.countTestCases()): + # if no tests found, it's probably a package; try discovery + tests = self.test_loader.discover(start_dir=label, **kwargs) + + # make unittest forget the top-level dir it calculated from this + # run, to support running tests from two different top-levels. + self.test_loader._top_level_dir = None + + suite.addTests(tests) + + for test in extra_tests: + suite.addTest(test) + + return reorder_suite(suite, self.reorder_by) + + def setup_databases(self, **kwargs): + return setup_databases(self.verbosity, self.interactive, **kwargs) + + def run_suite(self, suite, **kwargs): + return unittest.TextTestRunner( + verbosity=self.verbosity, + failfast=self.failfast, + ).run(suite) + + def teardown_databases(self, old_config, **kwargs): + """ + Destroys all the non-mirror databases. + """ + old_names, mirrors = old_config + for connection, old_name, destroy in old_names: + if destroy: + connection.creation.destroy_test_db(old_name, self.verbosity) + + def teardown_test_environment(self, **kwargs): + unittest.removeHandler() + teardown_test_environment() + + def suite_result(self, suite, result, **kwargs): + return len(result.failures) + len(result.errors) + + def run_tests(self, test_labels, extra_tests=None, **kwargs): + """ + Run the unit tests for all the test labels in the provided list. + + Test labels should be dotted Python paths to test modules, test + classes, or test methods. + + A list of 'extra' tests may also be provided; these tests + will be added to the test suite. + + Returns the number of tests that failed. + """ + self.setup_test_environment() + suite = self.build_suite(test_labels, extra_tests) + old_config = self.setup_databases() + result = self.run_suite(suite) + self.teardown_databases(old_config) + self.teardown_test_environment() + return self.suite_result(suite, result) + + +def dependency_ordered(test_databases, dependencies): + """ + Reorder test_databases into an order that honors the dependencies + described in TEST_DEPENDENCIES. + """ + ordered_test_databases = [] + resolved_databases = set() + + # Maps db signature to dependencies of all it's aliases + dependencies_map = {} + + # sanity check - no DB can depend on it's own alias + for sig, (_, aliases) in test_databases: + all_deps = set() + for alias in aliases: + all_deps.update(dependencies.get(alias, [])) + if not all_deps.isdisjoint(aliases): + raise ImproperlyConfigured( + "Circular dependency: databases %r depend on each other, " + "but are aliases." % aliases) + dependencies_map[sig] = all_deps + + while test_databases: + changed = False + deferred = [] + + # Try to find a DB that has all it's dependencies met + for signature, (db_name, aliases) in test_databases: + if dependencies_map[signature].issubset(resolved_databases): + resolved_databases.update(aliases) + ordered_test_databases.append((signature, (db_name, aliases))) + changed = True + else: + deferred.append((signature, (db_name, aliases))) + + if not changed: + raise ImproperlyConfigured( + "Circular dependency in TEST_DEPENDENCIES") + test_databases = deferred + return ordered_test_databases + + +def reorder_suite(suite, classes): + """ + Reorders a test suite by test type. + + `classes` is a sequence of types + + All tests of type classes[0] are placed first, then tests of type + classes[1], etc. Tests with no match in classes are placed last. + """ + class_count = len(classes) + bins = [unittest.TestSuite() for i in range(class_count+1)] + partition_suite(suite, classes, bins) + for i in range(class_count): + bins[0].addTests(bins[i+1]) + return bins[0] + + +def partition_suite(suite, classes, bins): + """ + Partitions a test suite by test type. + + classes is a sequence of types + bins is a sequence of TestSuites, one more than classes + + Tests of type classes[i] are added to bins[i], + tests with no match found in classes are place in bins[-1] + """ + for test in suite: + if isinstance(test, unittest.TestSuite): + partition_suite(test, classes, bins) + else: + for i in range(len(classes)): + if isinstance(test, classes[i]): + bins[i].addTest(test) + break + else: + bins[-1].addTest(test) + + +def setup_databases(verbosity, interactive, **kwargs): + from django.db import connections, DEFAULT_DB_ALIAS + + # First pass -- work out which databases actually need to be created, + # and which ones are test mirrors or duplicate entries in DATABASES + mirrored_aliases = {} + test_databases = {} + dependencies = {} + for alias in connections: + connection = connections[alias] + if connection.settings_dict['TEST_MIRROR']: + # If the database is marked as a test mirror, save + # the alias. + mirrored_aliases[alias] = ( + connection.settings_dict['TEST_MIRROR']) + else: + # Store a tuple with DB parameters that uniquely identify it. + # If we have two aliases with the same values for that tuple, + # we only need to create the test database once. + item = test_databases.setdefault( + connection.creation.test_db_signature(), + (connection.settings_dict['NAME'], set()) + ) + item[1].add(alias) + + if 'TEST_DEPENDENCIES' in connection.settings_dict: + dependencies[alias] = ( + connection.settings_dict['TEST_DEPENDENCIES']) + else: + if alias != DEFAULT_DB_ALIAS: + dependencies[alias] = connection.settings_dict.get( + 'TEST_DEPENDENCIES', [DEFAULT_DB_ALIAS]) + + # Second pass -- actually create the databases. + old_names = [] + mirrors = [] + + for signature, (db_name, aliases) in dependency_ordered( + test_databases.items(), dependencies): + test_db_name = None + # Actually create the database for the first connection + + for alias in aliases: + connection = connections[alias] + old_names.append((connection, db_name, True)) + if test_db_name is None: + test_db_name = connection.creation.create_test_db( + verbosity, autoclobber=not interactive) + else: + connection.settings_dict['NAME'] = test_db_name + + for alias, mirror_alias in mirrored_aliases.items(): + mirrors.append((alias, connections[alias].settings_dict['NAME'])) + connections[alias].settings_dict['NAME'] = ( + connections[mirror_alias].settings_dict['NAME']) + + return old_names, mirrors diff --git a/django/test/simple.py b/django/test/simple.py index 6804b4958c..5117c6452f 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -1,10 +1,15 @@ -import unittest as real_unittest +""" +This module is pending deprecation as of Django 1.6 and will be removed in +version 1.8. + +""" + +import unittest as real_unittest +import warnings -from django.conf import settings -from django.core.exceptions import ImproperlyConfigured from django.db.models import get_app, get_apps from django.test import _doctest as doctest -from django.test.utils import setup_test_environment, teardown_test_environment +from django.test import runner from django.test.testcases import OutputChecker, DocTestRunner from django.utils import unittest from django.utils.importlib import import_module @@ -12,6 +17,11 @@ from django.utils.module_loading import module_has_submodule __all__ = ('DjangoTestSuiteRunner',) +warnings.warn( + "The django.test.simple module and DjangoTestSuiteRunner are deprecated; " + "use django.test.runner.DiscoverRunner instead.", + PendingDeprecationWarning) + # The module name for tests outside models.py TEST_MODULE = 'tests' @@ -154,97 +164,7 @@ def build_test(label): return unittest.TestSuite(tests) -def partition_suite(suite, classes, bins): - """ - Partitions a test suite by test type. - - classes is a sequence of types - bins is a sequence of TestSuites, one more than classes - - Tests of type classes[i] are added to bins[i], - tests with no match found in classes are place in bins[-1] - """ - for test in suite: - if isinstance(test, unittest.TestSuite): - partition_suite(test, classes, bins) - else: - for i in range(len(classes)): - if isinstance(test, classes[i]): - bins[i].addTest(test) - break - else: - bins[-1].addTest(test) - - -def reorder_suite(suite, classes): - """ - Reorders a test suite by test type. - - `classes` is a sequence of types - - All tests of type classes[0] are placed first, then tests of type - classes[1], etc. Tests with no match in classes are placed last. - """ - class_count = len(classes) - bins = [unittest.TestSuite() for i in range(class_count+1)] - partition_suite(suite, classes, bins) - for i in range(class_count): - bins[0].addTests(bins[i+1]) - return bins[0] - - -def dependency_ordered(test_databases, dependencies): - """ - Reorder test_databases into an order that honors the dependencies - described in TEST_DEPENDENCIES. - """ - ordered_test_databases = [] - resolved_databases = set() - - # Maps db signature to dependencies of all it's aliases - dependencies_map = {} - - # sanity check - no DB can depend on it's own alias - for sig, (_, aliases) in test_databases: - all_deps = set() - for alias in aliases: - all_deps.update(dependencies.get(alias, [])) - if not all_deps.isdisjoint(aliases): - raise ImproperlyConfigured( - "Circular dependency: databases %r depend on each other, " - "but are aliases." % aliases) - dependencies_map[sig] = all_deps - - while test_databases: - changed = False - deferred = [] - - # Try to find a DB that has all it's dependencies met - for signature, (db_name, aliases) in test_databases: - if dependencies_map[signature].issubset(resolved_databases): - resolved_databases.update(aliases) - ordered_test_databases.append((signature, (db_name, aliases))) - changed = True - else: - deferred.append((signature, (db_name, aliases))) - - if not changed: - raise ImproperlyConfigured( - "Circular dependency in TEST_DEPENDENCIES") - test_databases = deferred - return ordered_test_databases - - -class DjangoTestSuiteRunner(object): - def __init__(self, verbosity=1, interactive=True, failfast=True, **kwargs): - self.verbosity = verbosity - self.interactive = interactive - self.failfast = failfast - - def setup_test_environment(self, **kwargs): - setup_test_environment() - settings.DEBUG = False - unittest.installHandler() +class DjangoTestSuiteRunner(runner.DiscoverRunner): def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() @@ -264,109 +184,4 @@ class DjangoTestSuiteRunner(object): for test in extra_tests: suite.addTest(test) - return reorder_suite(suite, (unittest.TestCase,)) - - def setup_databases(self, **kwargs): - from django.db import connections, DEFAULT_DB_ALIAS - - # First pass -- work out which databases actually need to be created, - # and which ones are test mirrors or duplicate entries in DATABASES - mirrored_aliases = {} - test_databases = {} - dependencies = {} - for alias in connections: - connection = connections[alias] - if connection.settings_dict['TEST_MIRROR']: - # If the database is marked as a test mirror, save - # the alias. - mirrored_aliases[alias] = ( - connection.settings_dict['TEST_MIRROR']) - else: - # Store a tuple with DB parameters that uniquely identify it. - # If we have two aliases with the same values for that tuple, - # we only need to create the test database once. - item = test_databases.setdefault( - connection.creation.test_db_signature(), - (connection.settings_dict['NAME'], set()) - ) - item[1].add(alias) - - if 'TEST_DEPENDENCIES' in connection.settings_dict: - dependencies[alias] = ( - connection.settings_dict['TEST_DEPENDENCIES']) - else: - if alias != DEFAULT_DB_ALIAS: - dependencies[alias] = connection.settings_dict.get( - 'TEST_DEPENDENCIES', [DEFAULT_DB_ALIAS]) - - # Second pass -- actually create the databases. - old_names = [] - mirrors = [] - - for signature, (db_name, aliases) in dependency_ordered( - test_databases.items(), dependencies): - test_db_name = None - # Actually create the database for the first connection - - for alias in aliases: - connection = connections[alias] - old_names.append((connection, db_name, True)) - if test_db_name is None: - test_db_name = connection.creation.create_test_db( - self.verbosity, autoclobber=not self.interactive) - else: - connection.settings_dict['NAME'] = test_db_name - - for alias, mirror_alias in mirrored_aliases.items(): - mirrors.append((alias, connections[alias].settings_dict['NAME'])) - connections[alias].settings_dict['NAME'] = ( - connections[mirror_alias].settings_dict['NAME']) - - return old_names, mirrors - - def run_suite(self, suite, **kwargs): - return unittest.TextTestRunner( - verbosity=self.verbosity, failfast=self.failfast).run(suite) - - def teardown_databases(self, old_config, **kwargs): - """ - Destroys all the non-mirror databases. - """ - old_names, mirrors = old_config - for connection, old_name, destroy in old_names: - if destroy: - connection.creation.destroy_test_db(old_name, self.verbosity) - - def teardown_test_environment(self, **kwargs): - unittest.removeHandler() - teardown_test_environment() - - def suite_result(self, suite, result, **kwargs): - return len(result.failures) + len(result.errors) - - def run_tests(self, test_labels, extra_tests=None, **kwargs): - """ - Run the unit tests for all the test labels in the provided list. - Labels must be of the form: - - app.TestClass.test_method - Run a single specific test method - - app.TestClass - Run all the test methods in a given class - - app - Search for doctests and unittests in the named application. - - When looking for tests, the test runner will look in the models and - tests modules for the application. - - A list of 'extra' tests may also be provided; these tests - will be added to the test suite. - - Returns the number of tests that failed. - """ - self.setup_test_environment() - suite = self.build_suite(test_labels, extra_tests) - old_config = self.setup_databases() - result = self.run_suite(suite) - self.teardown_databases(old_config) - self.teardown_test_environment() - return self.suite_result(suite, result) + return runner.reorder_suite(suite, (unittest.TestCase,)) diff --git a/django/test/testcases.py b/django/test/testcases.py index a9fcc2bab6..6fe6b9c397 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -97,6 +97,12 @@ def assert_and_parse_html(self, html, user_msg, msg): class OutputChecker(doctest.OutputChecker): + def __init__(self): + warnings.warn( + "The django.test.testcases.OutputChecker class is deprecated; " + "use the doctest module from the Python standard library instead.", + PendingDeprecationWarning) + def check_output(self, want, got, optionflags): """ The entry method for doctest output checking. Defers to a sequence of @@ -151,6 +157,10 @@ class OutputChecker(doctest.OutputChecker): class DocTestRunner(doctest.DocTestRunner): def __init__(self, *args, **kwargs): + warnings.warn( + "The django.test.testcases.DocTestRunner class is deprecated; " + "use the doctest module from the Python standard library instead.", + PendingDeprecationWarning) doctest.DocTestRunner.__init__(self, *args, **kwargs) self.optionflags = doctest.ELLIPSIS diff --git a/django/utils/unittest/loader.py b/django/utils/unittest/loader.py index 0bdd106d39..695bac40ef 100644 --- a/django/utils/unittest/loader.py +++ b/django/utils/unittest/loader.py @@ -125,7 +125,7 @@ class TestLoader(unittest.TestLoader): return self.loadTestsFromTestCase(obj) elif (isinstance(obj, types.UnboundMethodType) and isinstance(parent, type) and - issubclass(parent, case.TestCase)): + issubclass(parent, unittest.TestCase)): return self.suiteClass([parent(obj.__name__)]) elif isinstance(obj, unittest.TestSuite): return obj diff --git a/docs/index.txt b/docs/index.txt index 6473aa3168..3b5e74f1a2 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -186,7 +186,6 @@ testing of Django applications: :doc:`Introduction ` | :doc:`Writing and running tests ` | :doc:`Advanced topics ` | - :doc:`Doctests ` * **Deployment:** :doc:`Overview ` | diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 1533e25dc8..862907b2a8 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -73,7 +73,7 @@ these changes. ``django.utils.formats.get_format()`` to get the appropriate formats. -* The ability to use a function-based test runners will be removed, +* The ability to use a function-based test runner will be removed, along with the ``django.test.simple.run_tests()`` test runner. * The ``views.feed()`` view and ``feeds.Feed`` class in @@ -375,6 +375,15 @@ these changes. * ``django.forms.widgets.RadioInput`` will be removed in favor of ``django.forms.widgets.RadioChoiceInput``. +* The module ``django.test.simple`` and the class + ``django.test.simple.DjangoTestSuiteRunner`` will be removed. Instead use + ``django.test.runner.DiscoverRunner``. + +* The module ``django.test._doctest`` and the classes + ``django.test.testcases.DocTestRunner`` and + ``django.test.testcases.OutputChecker`` will be removed. Instead use the + doctest module from the Python standard library. + 2.0 --- diff --git a/docs/intro/tutorial05.txt b/docs/intro/tutorial05.txt index 97d1d96ad7..67a40aba40 100644 --- a/docs/intro/tutorial05.txt +++ b/docs/intro/tutorial05.txt @@ -156,8 +156,9 @@ Create a test to expose the bug What we've just done in the shell to test for the problem is exactly what we can do in an automated test, so let's turn that into an automated test. -The best place for an application's tests is in the application's ``tests.py`` -file - the testing system will look there for tests automatically. +A conventional place for an application's tests is in the application's +``tests.py`` file; the testing system will automatically find tests in any file +whose name begins with ``test``. Put the following in the ``tests.py`` file in the ``polls`` application:: diff --git a/docs/ref/contrib/gis/testing.txt b/docs/ref/contrib/gis/testing.txt index 2a6dcef46f..fca6675345 100644 --- a/docs/ref/contrib/gis/testing.txt +++ b/docs/ref/contrib/gis/testing.txt @@ -134,57 +134,14 @@ your settings:: GeoDjango tests =============== -GeoDjango's test suite may be run in one of two ways, either by itself or -with the rest of :ref:`Django's unit tests `. +To have the GeoDjango tests executed when :ref:`running the Django test suite +` with ``runtests.py`` all of the databases in the settings +file must be using one of the :ref:`spatial database backends +`. -Run only GeoDjango tests ------------------------- - -.. class:: django.contrib.gis.tests.GeoDjangoTestSuiteRunner - -To run *only* the tests for GeoDjango, the :setting:`TEST_RUNNER` -setting must be changed to use the -:class:`~django.contrib.gis.tests.GeoDjangoTestSuiteRunner`:: - - TEST_RUNNER = 'django.contrib.gis.tests.GeoDjangoTestSuiteRunner' Example -^^^^^^^ - -First, you'll need a bare-bones settings file, like below, that is -customized with your spatial database name and user:: - - TEST_RUNNER = 'django.contrib.gis.tests.GeoDjangoTestSuiteRunner' - - DATABASES = { - 'default': { - 'ENGINE': 'django.contrib.gis.db.backends.postgis', - 'NAME': 'a_spatial_database', - 'USER': 'db_user' - } - } - -Assuming the above is in a file called ``postgis.py`` that is in the -the same directory as ``manage.py`` of your Django project, then -you may run the tests with the following command:: - - $ python manage.py test --settings=postgis - -Run with ``runtests.py`` ------------------------- - -To have the GeoDjango tests executed when -:ref:`running the Django test suite ` with ``runtests.py`` -all of the databases in the settings file must be using one of the -:ref:`spatial database backends `. - -.. warning:: - - Do not change the :setting:`TEST_RUNNER` setting - when running the GeoDjango tests with ``runtests.py``. - -Example -^^^^^^^ +------- The following is an example bare-bones settings file with spatial backends that can be used to run the entire Django test suite, including those @@ -208,3 +165,7 @@ directory as ``runtests.py``, then all Django and GeoDjango tests would be performed when executing the command:: $ ./runtests.py --settings=postgis + +To run only the GeoDjango test suite, specify ``django.contrib.gis``:: + + $ ./runtests.py --settings=postgis django.contrib.gis diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt index 04b42aeeb2..eb470cdd14 100644 --- a/docs/ref/settings.txt +++ b/docs/ref/settings.txt @@ -1725,11 +1725,16 @@ misspelled) variables. See :ref:`invalid-template-variables`.. TEST_RUNNER ----------- -Default: ``'django.test.simple.DjangoTestSuiteRunner'`` +Default: ``'django.test.runner.DiscoverRunner'`` The name of the class to use for starting the test suite. See :ref:`other-testing-frameworks`. +.. versionchanged:: 1.6 + + Previously the default ``TEST_RUNNER`` was + ``django.test.simple.DjangoTestSuiteRunner``. + .. setting:: THOUSAND_SEPARATOR THOUSAND_SEPARATOR diff --git a/docs/releases/1.2.4.txt b/docs/releases/1.2.4.txt index b74ea9aef2..ae15ea6f7c 100644 --- a/docs/releases/1.2.4.txt +++ b/docs/releases/1.2.4.txt @@ -78,7 +78,7 @@ GeoDjango The function-based :setting:`TEST_RUNNER` previously used to execute the GeoDjango test suite, ``django.contrib.gis.tests.run_gis_tests``, was finally deprecated in favor of a class-based test runner, -:class:`django.contrib.gis.tests.GeoDjangoTestSuiteRunner`, added in this +``django.contrib.gis.tests.GeoDjangoTestSuiteRunner``, added in this release. In addition, the GeoDjango test suite is now included when diff --git a/docs/releases/1.3.txt b/docs/releases/1.3.txt index 9a41f903f8..89cece941b 100644 --- a/docs/releases/1.3.txt +++ b/docs/releases/1.3.txt @@ -799,7 +799,7 @@ GeoDjango * The function-based :setting:`TEST_RUNNER` previously used to execute the GeoDjango test suite, ``django.contrib.gis.tests.run_gis_tests``, was deprecated for the class-based runner, - :class:`django.contrib.gis.tests.GeoDjangoTestSuiteRunner`. + ``django.contrib.gis.tests.GeoDjangoTestSuiteRunner``. * Previously, calling :meth:`~django.contrib.gis.geos.GEOSGeometry.transform` would diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index 9cce36aac3..5780229eb5 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -69,6 +69,29 @@ This avoids the overhead of re-establishing a connection at the beginning of each request. For backwards compatibility, this feature is disabled by default. See :ref:`persistent-database-connections` for details. +Discovery of tests in any test module +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Django 1.6 ships with a new test runner that allows more flexibility in the +location of tests. The previous runner +(``django.test.simple.DjangoTestSuiteRunner``) found tests only in the +``models.py`` and ``tests.py`` modules of a Python package in +:setting:`INSTALLED_APPS`. + +The new runner (``django.test.runner.DjangoTestDiscoverRunner``) uses the test +discovery features built into unittest2 (the version of unittest in the Python +2.7+ standard library, and bundled with Django). With test discovery, tests can +be located in any module whose name matches the pattern ``test*.py``. + +In addition, the test labels provided to ``./manage.py test`` to nominate +specific tests to run must now be full Python dotted paths (or directory +paths), rather than ``applabel.TestCase.test_method_name`` pseudo-paths. This +allows running tests located anywhere in your codebase, rather than only in +:setting:`INSTALLED_APPS`. For more details, see :doc:`/topics/testing/index`. + +This change is backwards-incompatible; see the :ref:`backwards-incompatibility +notes`. + Time zone aware aggregation ~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -238,6 +261,40 @@ In previous versions, database-level autocommit was only an option for PostgreSQL, and it was disabled by default. This option is now :ref:`ignored ` and can be removed. +.. _new-test-runner: + +New test runner +~~~~~~~~~~~~~~~ + +In order to maintain greater consistency with Python's unittest module, the new +test runner (``django.test.runner.DiscoverRunner``) does not automatically +support some types of tests that were supported by the previous runner: + +* Tests in ``models.py`` and ``tests/__init__.py`` files will no longer be + found and run. Move them to a file whose name begins with ``test``. + +* Doctests will no longer be automatically discovered. To integrate doctests in + your test suite, follow the `recommendations in the Python documentation`_. + +Django bundles a modified version of the :mod:`doctest` module from the Python +standard library (in ``django.test._doctest``) in order to allow passing in a +custom ``DocTestRunner`` when instantiating a ``DocTestSuite``, and includes +some additional doctest utilities (``django.test.testcases.DocTestRunner`` +turns on the ``ELLIPSIS`` option by default, and +``django.test.testcases.OutputChecker`` provides better matching of XML, JSON, +and numeric data types). + +These utilities are deprecated and will be removed in Django 1.8; doctest +suites should be updated to work with the standard library's doctest module (or +converted to unittest-compatible tests). + +If you wish to delay updates to your test suite, you can set your +:setting:`TEST_RUNNER` setting to ``django.test.simple.DjangoTestSuiteRunner`` +to fully restore the old test behavior. ``DjangoTestSuiteRunner`` is +deprecated but will not be removed from Django until version 1.8. + +.. _recommendations in the Python documentation: http://docs.python.org/2/library/doctest.html#unittest-api + Addition of ``QuerySet.datetimes()`` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt index 5f2fa65bed..cefb770469 100644 --- a/docs/topics/testing/advanced.txt +++ b/docs/topics/testing/advanced.txt @@ -165,7 +165,7 @@ environment first. Django provides a convenience method to do this:: :func:`~django.test.utils.setup_test_environment` puts several Django features into modes that allow for repeatable testing, but does not create the test -databases; :func:`django.test.simple.DjangoTestSuiteRunner.setup_databases` +databases; :func:`django.test.runner.DiscoverRunner.setup_databases` takes care of that. The call to :func:`~django.test.utils.setup_test_environment` is made @@ -178,27 +178,27 @@ tests via Django's test runner. Using different testing frameworks ================================== -Clearly, :mod:`doctest` and :mod:`unittest` are not the only Python testing -frameworks. While Django doesn't provide explicit support for alternative -frameworks, it does provide a way to invoke tests constructed for an -alternative framework as if they were normal Django tests. +Clearly, :mod:`unittest` is not the only Python testing framework. While Django +doesn't provide explicit support for alternative frameworks, it does provide a +way to invoke tests constructed for an alternative framework as if they were +normal Django tests. When you run ``./manage.py test``, Django looks at the :setting:`TEST_RUNNER` setting to determine what to do. By default, :setting:`TEST_RUNNER` points to -``'django.test.simple.DjangoTestSuiteRunner'``. This class defines the default Django +``'django.test.runner.DiscoverRunner'``. This class defines the default Django testing behavior. This behavior involves: #. Performing global pre-test setup. -#. Looking for unit tests and doctests in the ``models.py`` and - ``tests.py`` files in each installed application. +#. Looking for tests in any file below the current directory whose name matches + the pattern ``test*.py``. #. Creating the test databases. #. Running ``syncdb`` to install models and initial data into the test databases. -#. Running the unit tests and doctests that are found. +#. Running the tests that were found. #. Destroying the test databases. @@ -215,15 +215,22 @@ process to satisfy whatever testing requirements you may have. Defining a test runner ---------------------- -.. currentmodule:: django.test.simple +.. currentmodule:: django.test.runner A test runner is a class defining a ``run_tests()`` method. Django ships -with a ``DjangoTestSuiteRunner`` class that defines the default Django +with a ``DiscoverRunner`` class that defines the default Django testing behavior. This class defines the ``run_tests()`` entry point, plus a selection of other methods that are used to by ``run_tests()`` to set up, execute and tear down the test suite. -.. class:: DjangoTestSuiteRunner(verbosity=1, interactive=True, failfast=True, **kwargs) +.. class:: DiscoverRunner(pattern='test*.py', top_level=None, verbosity=1, interactive=True, failfast=True, **kwargs) + + ``DiscoverRunner`` will search for tests in any file matching ``pattern``. + + ``top_level`` can be used to specify the directory containing your + top-level Python modules. Usually Django can figure this out automatically, + so it's not necessary to specify this option. If specified, it should + generally be the directory containing your ``manage.py`` file. ``verbosity`` determines the amount of notification and debug information that will be printed to the console; ``0`` is no output, ``1`` is normal @@ -238,11 +245,10 @@ set up, execute and tear down the test suite. If ``failfast`` is ``True``, the test suite will stop running after the first test failure is detected. - Django will, from time to time, extend the capabilities of - the test runner by adding new arguments. The ``**kwargs`` declaration - allows for this expansion. If you subclass ``DjangoTestSuiteRunner`` or - write your own test runner, ensure accept and handle the ``**kwargs`` - parameter. + Django may, from time to time, extend the capabilities of the test runner + by adding new arguments. The ``**kwargs`` declaration allows for this + expansion. If you subclass ``DiscoverRunner`` or write your own test + runner, ensure it accepts ``**kwargs``. Your test runner may also define additional command-line options. If you add an ``option_list`` attribute to a subclassed test runner, @@ -252,7 +258,7 @@ set up, execute and tear down the test suite. Attributes ~~~~~~~~~~ -.. attribute:: DjangoTestSuiteRunner.option_list +.. attribute:: DiscoverRunner.option_list This is the tuple of ``optparse`` options which will be fed into the management command's ``OptionParser`` for parsing arguments. See the @@ -261,20 +267,25 @@ Attributes Methods ~~~~~~~ -.. method:: DjangoTestSuiteRunner.run_tests(test_labels, extra_tests=None, **kwargs) +.. method:: DiscoverRunner.run_tests(test_labels, extra_tests=None, **kwargs) Run the test suite. ``test_labels`` is a list of strings describing the tests to be run. A test - label can take one of three forms: + label can take one of four forms: - * ``app.TestCase.test_method`` -- Run a single test method in a test + * ``path.to.test_module.TestCase.test_method`` -- Run a single test method + in a test case. + * ``path.to.test_module.TestCase`` -- Run all the test methods in a test case. - * ``app.TestCase`` -- Run all the test methods in a test case. - * ``app`` -- Search for and run all tests in the named application. + * ``path.to.module`` -- Search for and run all tests in the named Python + package or module. + * ``path/to/directory`` -- Search for and run all tests below the named + directory. - If ``test_labels`` has a value of ``None``, the test runner should run - search for tests in all the applications in :setting:`INSTALLED_APPS`. + If ``test_labels`` has a value of ``None``, the test runner will search for + tests in all files below the current directory whose names match its + ``pattern`` (see above). ``extra_tests`` is a list of extra ``TestCase`` instances to add to the suite that is executed by the test runner. These extra tests are run @@ -282,13 +293,13 @@ Methods This method should return the number of tests that failed. -.. method:: DjangoTestSuiteRunner.setup_test_environment(**kwargs) +.. method:: DiscoverRunner.setup_test_environment(**kwargs) Sets up the test environment by calling :func:`~django.test.utils.setup_test_environment` and setting :setting:`DEBUG` to ``False``. -.. method:: DjangoTestSuiteRunner.build_suite(test_labels, extra_tests=None, **kwargs) +.. method:: DiscoverRunner.build_suite(test_labels, extra_tests=None, **kwargs) Constructs a test suite that matches the test labels provided. @@ -309,7 +320,7 @@ Methods Returns a ``TestSuite`` instance ready to be run. -.. method:: DjangoTestSuiteRunner.setup_databases(**kwargs) +.. method:: DiscoverRunner.setup_databases(**kwargs) Creates the test databases. @@ -317,13 +328,13 @@ Methods that have been made. This data will be provided to the ``teardown_databases()`` function at the conclusion of testing. -.. method:: DjangoTestSuiteRunner.run_suite(suite, **kwargs) +.. method:: DiscoverRunner.run_suite(suite, **kwargs) Runs the test suite. Returns the result produced by the running the test suite. -.. method:: DjangoTestSuiteRunner.teardown_databases(old_config, **kwargs) +.. method:: DiscoverRunner.teardown_databases(old_config, **kwargs) Destroys the test databases, restoring pre-test conditions. @@ -331,11 +342,11 @@ Methods database configuration that need to be reversed. It is the return value of the ``setup_databases()`` method. -.. method:: DjangoTestSuiteRunner.teardown_test_environment(**kwargs) +.. method:: DiscoverRunner.teardown_test_environment(**kwargs) Restores the pre-test environment. -.. method:: DjangoTestSuiteRunner.suite_result(suite, result, **kwargs) +.. method:: DiscoverRunner.suite_result(suite, result, **kwargs) Computes and returns a return code based on a test suite, and the result from that test suite. @@ -402,7 +413,7 @@ can be useful during testing. ``old_database_name``. The ``verbosity`` argument has the same behavior as for - :class:`~django.test.simple.DjangoTestSuiteRunner`. + :class:`~django.test.runner.DiscoverRunner`. .. _topics-testing-code-coverage: diff --git a/docs/topics/testing/doctests.txt b/docs/topics/testing/doctests.txt deleted file mode 100644 index 5036e946a9..0000000000 --- a/docs/topics/testing/doctests.txt +++ /dev/null @@ -1,81 +0,0 @@ -=================== -Django and doctests -=================== - -Doctests use Python's standard :mod:`doctest` module, which searches your -docstrings for statements that resemble a session of the Python interactive -interpreter. A full explanation of how :mod:`doctest` works is out of the scope -of this document; read Python's official documentation for the details. - -.. admonition:: What's a **docstring**? - - A good explanation of docstrings (and some guidelines for using them - effectively) can be found in :pep:`257`: - - A docstring is a string literal that occurs as the first statement in - a module, function, class, or method definition. Such a docstring - becomes the ``__doc__`` special attribute of that object. - - For example, this function has a docstring that describes what it does:: - - def add_two(num): - "Return the result of adding two to the provided number." - return num + 2 - - Because tests often make great documentation, putting tests directly in - your docstrings is an effective way to document *and* test your code. - -As with unit tests, for a given Django application, the test runner looks for -doctests in two places: - -* The ``models.py`` file. You can define module-level doctests and/or a - doctest for individual models. It's common practice to put - application-level doctests in the module docstring and model-level - doctests in the model docstrings. - -* A file called ``tests.py`` in the application directory -- i.e., the - directory that holds ``models.py``. This file is a hook for any and all - doctests you want to write that aren't necessarily related to models. - -This example doctest is equivalent to the example given in the unittest section -above:: - - # models.py - - from django.db import models - - class Animal(models.Model): - """ - An animal that knows how to make noise - - # Create some animals - >>> lion = Animal.objects.create(name="lion", sound="roar") - >>> cat = Animal.objects.create(name="cat", sound="meow") - - # Make 'em speak - >>> lion.speak() - 'The lion says "roar"' - >>> cat.speak() - 'The cat says "meow"' - """ - name = models.CharField(max_length=20) - sound = models.CharField(max_length=20) - - def speak(self): - return 'The %s says "%s"' % (self.name, self.sound) - -When you :ref:`run your tests `, the test runner will find this -docstring, notice that portions of it look like an interactive Python session, -and execute those lines while checking that the results match. - -In the case of model tests, note that the test runner takes care of creating -its own test database. That is, any test that accesses a database -- by -creating and saving model instances, for example -- will not affect your -production database. However, the database is not refreshed between doctests, -so if your doctest requires a certain state you should consider flushing the -database or loading a fixture. (See the section on :ref:`fixtures -` for more on this.) Note that to use this feature, -the database user Django is connecting as must have ``CREATE DATABASE`` -rights. - -For more details about :mod:`doctest`, see the Python documentation. diff --git a/docs/topics/testing/index.txt b/docs/topics/testing/index.txt index 94e88bdf04..1a99a399b4 100644 --- a/docs/topics/testing/index.txt +++ b/docs/topics/testing/index.txt @@ -6,7 +6,6 @@ Testing in Django :hidden: overview - doctests advanced Automated testing is an extremely useful bug-killing tool for the modern @@ -29,83 +28,13 @@ it should be doing. The best part is, it's really easy. -Unit tests v. doctests -====================== - -There are two primary ways to write tests with Django, corresponding to the -two test frameworks that ship in the Python standard library. The two -frameworks are: - -* **Unit tests** -- tests that are expressed as methods on a Python class - that subclasses :class:`unittest.TestCase` or Django's customized - :class:`~django.test.TestCase`. For example:: - - import unittest - - class MyFuncTestCase(unittest.TestCase): - def testBasic(self): - a = ['larry', 'curly', 'moe'] - self.assertEqual(my_func(a, 0), 'larry') - self.assertEqual(my_func(a, 1), 'curly') - -* **Doctests** -- tests that are embedded in your functions' docstrings and - are written in a way that emulates a session of the Python interactive - interpreter. For example:: - - def my_func(a_list, idx): - """ - >>> a = ['larry', 'curly', 'moe'] - >>> my_func(a, 0) - 'larry' - >>> my_func(a, 1) - 'curly' - """ - return a_list[idx] - -Which should I use? -------------------- - -Because Django supports both of the standard Python test frameworks, it's up to -you and your tastes to decide which one to use. You can even decide to use -*both*. - -For developers new to testing, however, this choice can seem confusing. Here, -then, are a few key differences to help you decide which approach is right for -you: - -* If you've been using Python for a while, :mod:`doctest` will probably feel - more "pythonic". It's designed to make writing tests as easy as possible, - so it requires no overhead of writing classes or methods. You simply put - tests in docstrings. This has the added advantage of serving as - documentation (and correct documentation, at that!). However, while - doctests are good for some simple example code, they are not very good if - you want to produce either high quality, comprehensive tests or high - quality documentation. Test failures are often difficult to debug - as it can be unclear exactly why the test failed. Thus, doctests should - generally be avoided and used primarily for documentation examples only. - -* The :mod:`unittest` framework will probably feel very familiar to - developers coming from Java. :mod:`unittest` is inspired by Java's JUnit, - so you'll feel at home with this method if you've used JUnit or any test - framework inspired by JUnit. - -* If you need to write a bunch of tests that share similar code, then - you'll appreciate the :mod:`unittest` framework's organization around - classes and methods. This makes it easy to abstract common tasks into - common methods. The framework also supports explicit setup and/or cleanup - routines, which give you a high level of control over the environment - in which your test cases are run. - -* If you're writing tests for Django itself, you should use :mod:`unittest`. - Where to go from here ===================== -As unit tests are preferred in Django, we treat them in detail in the +The preferred way to write tests in Django is using the :mod:`unittest` module +built in to the Python standard library. This is covered in detail in the :doc:`overview` document. -:doc:`doctests` describes Django-specific features when using doctests. - -You can also use any *other* Python test framework, Django provides an API and +You can also use any *other* Python test framework; Django provides an API and tools for that kind of integration. They are described in the :ref:`other-testing-frameworks` section of :doc:`advanced`. diff --git a/docs/topics/testing/overview.txt b/docs/topics/testing/overview.txt index 9228a07b31..5023e099aa 100644 --- a/docs/topics/testing/overview.txt +++ b/docs/topics/testing/overview.txt @@ -17,7 +17,7 @@ Writing tests ============= Django's unit tests use a Python standard library module: :mod:`unittest`. This -module defines tests in class-based approach. +module defines tests using a class-based approach. .. admonition:: unittest2 @@ -46,16 +46,6 @@ module defines tests in class-based approach. .. _unittest2: http://pypi.python.org/pypi/unittest2 -For a given Django application, the test runner looks for unit tests in two -places: - -* The ``models.py`` file. The test runner looks for any subclass of - :class:`unittest.TestCase` in this module. - -* A file called ``tests.py`` in the application directory -- i.e., the - directory that holds ``models.py``. Again, the test runner looks for any - subclass of :class:`unittest.TestCase` in this module. - Here is an example :class:`unittest.TestCase` subclass:: from django.utils import unittest @@ -71,22 +61,19 @@ Here is an example :class:`unittest.TestCase` subclass:: self.assertEqual(self.lion.speak(), 'The lion says "roar"') self.assertEqual(self.cat.speak(), 'The cat says "meow"') -When you :ref:`run your tests `, the default behavior of the test -utility is to find all the test cases (that is, subclasses of -:class:`unittest.TestCase`) in ``models.py`` and ``tests.py``, automatically -build a test suite out of those test cases, and run that suite. +When you :ref:`run your tests `, the default behavior of the +test utility is to find all the test cases (that is, subclasses of +:class:`unittest.TestCase`) in any file whose name begins with ``test``, +automatically build a test suite out of those test cases, and run that suite. -There is a second way to define the test suite for a module: if you define a -function called ``suite()`` in either ``models.py`` or ``tests.py``, the -Django test runner will use that function to construct the test suite for that -module. This follows the `suggested organization`_ for unit tests. See the -Python documentation for more details on how to construct a complex test -suite. +.. versionchanged:: 1.6 + + Previously, Django's default test runner only discovered tests in + ``tests.py`` and ``models.py`` files within a Python package listed in + :setting:`INSTALLED_APPS`. For more details about :mod:`unittest`, see the Python documentation. -.. _suggested organization: http://docs.python.org/library/unittest.html#organizing-tests - .. warning:: If your tests rely on database access such as creating or querying models, @@ -101,6 +88,7 @@ For more details about :mod:`unittest`, see the Python documentation. .. _running-tests: + Running tests ============= @@ -109,46 +97,47 @@ your project's ``manage.py`` utility:: $ ./manage.py test -By default, this will run every test in every application in -:setting:`INSTALLED_APPS`. If you only want to run tests for a particular -application, add the application name to the command line. For example, if your -:setting:`INSTALLED_APPS` contains ``'myproject.polls'`` and -``'myproject.animals'``, you can run the ``myproject.animals`` unit tests alone -with this command:: +Test discovery is based on the unittest module's `built-in test discovery`. By +default, this will discover tests in any file named "test*.py" under the +current working directory. +.. _built-in test discovery: http://docs.python.org/2/library/unittest.html#test-discovery + +You can specify particular tests to run by supplying any number of "test +labels" to ``./manage.py test``. Each test label can be a full Python dotted +path to a package, module, ``TestCase`` subclass, or test method. For instance:: + + # Run all the tests in the animals.tests module + $ ./manage.py test animals.tests + + # Run all the tests found within the 'animals' package $ ./manage.py test animals -Note that we used ``animals``, not ``myproject.animals``. + # Run just one test case + $ ./manage.py test animals.tests.AnimalTestCase -You can be even *more* specific by naming an individual test case. To -run a single test case in an application (for example, the -``AnimalTestCase`` described in the "Writing unit tests" section), add -the name of the test case to the label on the command line:: + # Run just one test method + $ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak - $ ./manage.py test animals.AnimalTestCase +You can also provide a path to a directory to discover tests below that +directory:: -And it gets even more granular than that! To run a *single* test -method inside a test case, add the name of the test method to the -label:: + $ ./manage.py test animals/ - $ ./manage.py test animals.AnimalTestCase.test_animals_can_speak +You can specify a custom filename pattern match using the ``-p`` (or +``--pattern``) option, if your test files are named differently from the +``test*.py`` pattern:: -You can use the same rules if you're using doctests. Django will use the -test label as a path to the test method or class that you want to run. -If your ``models.py`` or ``tests.py`` has a function with a doctest, or -class with a class-level doctest, you can invoke that test by appending the -name of the test method or class to the label:: + $ ./manage.py test --pattern="tests_*.py" - $ ./manage.py test animals.classify +.. versionchanged:: 1.6 -If you want to run the doctest for a specific method in a class, add the -name of the method to the label:: - - $ ./manage.py test animals.Classifier.run - -If you're using a ``__test__`` dictionary to specify doctests for a -module, Django will use the label as a key in the ``__test__`` dictionary -for defined in ``models.py`` and ``tests.py``. + Previously, test labels were in the form ``applabel``, + ``applabel.TestCase``, or ``applabel.TestCase.test_method``, rather than + being true Python dotted paths, and tests could only be found within + ``tests.py`` or ``models.py`` files within a Python package listed in + :setting:`INSTALLED_APPS`. The ``--pattern`` option and file paths as test + labels are new in 1.6. If you press ``Ctrl-C`` while the tests are running, the test runner will wait for the currently running test to complete and then exit gracefully. @@ -173,6 +162,7 @@ be reported, and any test databases created by the run will not be destroyed. flag areas in your code that aren't strictly wrong but could benefit from a better implementation. + .. _the-test-database: The test database diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index 5071977b2d..c8986770f3 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -19,9 +19,9 @@ from django import conf, get_version from django.conf import settings from django.core.management import BaseCommand, CommandError from django.db import connection -from django.test.simple import DjangoTestSuiteRunner +from django.test.runner import DiscoverRunner from django.utils import unittest -from django.utils.encoding import force_str, force_text +from django.utils.encoding import force_text from django.utils._os import upath from django.utils.six import StringIO from django.test import LiveServerTestCase @@ -1090,7 +1090,7 @@ class ManageValidate(AdminScriptTestCase): self.assertOutput(out, '0 errors found') -class CustomTestRunner(DjangoTestSuiteRunner): +class CustomTestRunner(DiscoverRunner): def __init__(self, *args, **kwargs): assert 'liveserver' not in kwargs diff --git a/tests/generic_views/tests.py b/tests/generic_views/tests.py deleted file mode 100644 index 244aa3c701..0000000000 --- a/tests/generic_views/tests.py +++ /dev/null @@ -1,11 +0,0 @@ -from __future__ import absolute_import - -from .test_base import (ViewTest, TemplateViewTest, RedirectViewTest, - GetContextDataTest) -from .test_dates import (ArchiveIndexViewTests, YearArchiveViewTests, - MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, - DateDetailViewTests) -from .test_detail import DetailViewTest -from .test_edit import (FormMixinTests, BasicFormTests, ModelFormMixinTests, - CreateViewTests, UpdateViewTests, DeleteViewTests) -from .test_list import ListViewTests diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index d26a201efa..1022c8d2f1 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -43,14 +43,8 @@ if can_run_compilation_tests: from .commands.compilation import (PoFileTests, PoFileContentsTests, PercentRenderingTests, MultipleLocaleCompilationTests, CompilationErrorHandling) -from .contenttypes.tests import ContentTypeTests from .forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm from .models import Company, TestModel -from .patterns.tests import (URLRedirectWithoutTrailingSlashTests, - URLTranslationTests, URLDisabledTests, URLTagTests, URLTestCaseBase, - URLRedirectWithoutTrailingSlashSettingTests, URLNamespaceTests, - URLPrefixTests, URLResponseTests, URLRedirectTests, PathUnusedTests, - URLVaryAcceptLanguageTests) here = os.path.dirname(os.path.abspath(upath(__file__))) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index 035a5c2ae3..e1e38d0ec7 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -15,11 +15,6 @@ from .models import (Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, DataModel, Document, RenamedField, VerboseNameField, FksToBooleans) -from .test_imagefield import (ImageFieldTests, ImageFieldTwoDimensionsTests, - TwoImageFieldTests, ImageFieldNoDimensionsTests, - ImageFieldOneDimensionTests, ImageFieldDimensionsFirstTests, - ImageFieldUsingFileTests) - class BasicFieldTests(test.TestCase): def test_show_hidden_initial(self): diff --git a/tests/runtests.py b/tests/runtests.py index ee2488c4a1..cb124f2541 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -10,16 +10,23 @@ from django import contrib from django.utils._os import upath from django.utils import six -CONTRIB_DIR_NAME = 'django.contrib' +CONTRIB_MODULE_PATH = 'django.contrib' TEST_TEMPLATE_DIR = 'templates' RUNTESTS_DIR = os.path.abspath(os.path.dirname(upath(__file__))) CONTRIB_DIR = os.path.dirname(upath(contrib.__file__)) + TEMP_DIR = tempfile.mkdtemp(prefix='django_') os.environ['DJANGO_TEST_TEMP_DIR'] = TEMP_DIR -SUBDIRS_TO_SKIP = ['templates'] +SUBDIRS_TO_SKIP = [ + 'templates', + 'test_discovery_sample', + 'test_discovery_sample2', + 'test_runner_deprecation_app', + 'test_runner_invalid_app', +] ALWAYS_INSTALLED_APPS = [ 'shared_models', @@ -40,17 +47,12 @@ ALWAYS_INSTALLED_APPS = [ 'staticfiles_tests.apps.no_label', ] -def geodjango(settings): - # All databases must have spatial backends to run GeoDjango tests. - spatial_dbs = [name for name, db_dict in settings.DATABASES.items() - if db_dict['ENGINE'].startswith('django.contrib.gis')] - return len(spatial_dbs) == len(settings.DATABASES) def get_test_modules(): modules = [] - for loc, dirpath in ( + for modpath, dirpath in ( (None, RUNTESTS_DIR), - (CONTRIB_DIR_NAME, CONTRIB_DIR)): + (CONTRIB_MODULE_PATH, CONTRIB_DIR)): for f in os.listdir(dirpath): if ('.' in f or # Python 3 byte code dirs (PEP 3147) @@ -59,9 +61,14 @@ def get_test_modules(): os.path.basename(f) in SUBDIRS_TO_SKIP or os.path.isfile(f)): continue - modules.append((loc, f)) + modules.append((modpath, f)) return modules +def get_installed(): + from django.db.models.loading import get_apps + return [app.__name__.rsplit('.', 1)[0] for app in get_apps()] + + def setup(verbosity, test_labels): from django.conf import settings from django.db.models.loading import get_apps, load_app @@ -95,25 +102,45 @@ def setup(verbosity, test_labels): get_apps() # Load all the test model apps. - test_labels_set = set([label.split('.')[0] for label in test_labels]) test_modules = get_test_modules() + # Reduce given test labels to just the app module path + test_labels_set = set() + for label in test_labels: + bits = label.split('.') + if bits[:2] == ['django', 'contrib']: + bits = bits[:3] + else: + bits = bits[:1] + test_labels_set.add('.'.join(bits)) + # If GeoDjango, then we'll want to add in the test applications # that are a part of its test suite. - if geodjango(settings): + from django.contrib.gis.tests.utils import HAS_SPATIAL_DB + if HAS_SPATIAL_DB: from django.contrib.gis.tests import geo_apps - test_modules.extend(geo_apps(runtests=True)) + test_modules.extend(geo_apps()) settings.INSTALLED_APPS.extend(['django.contrib.gis', 'django.contrib.sitemaps']) - for module_dir, module_name in test_modules: - if module_dir: - module_label = '.'.join([module_dir, module_name]) + for modpath, module_name in test_modules: + if modpath: + module_label = '.'.join([modpath, module_name]) else: module_label = module_name - # if the module was named on the command line, or + # if the module (or an ancestor) was named on the command line, or # no modules were named (i.e., run all), import - # this module and add it to the list to test. - if not test_labels or module_name in test_labels_set: + # this module and add it to INSTALLED_APPS. + if not test_labels: + module_found_in_labels = True + else: + match = lambda label: ( + module_label == label or # exact match + module_label.startswith(label + '.') # ancestor match + ) + + module_found_in_labels = any(match(l) for l in test_labels_set) + + if module_found_in_labels: if verbosity >= 2: print("Importing application %s" % module_name) mod = load_app(module_label) @@ -139,21 +166,16 @@ def django_tests(verbosity, interactive, failfast, test_labels): state = setup(verbosity, test_labels) extra_tests = [] - # If GeoDjango is used, add it's tests that aren't a part of - # an application (e.g., GEOS, GDAL, Distance objects). - if geodjango(settings) and (not test_labels or 'gis' in test_labels): - from django.contrib.gis.tests import geodjango_suite - extra_tests.append(geodjango_suite(apps=False)) - # Run the test suite, including the extra validation tests. - from django.test.utils import get_runner - if not hasattr(settings, 'TEST_RUNNER'): - settings.TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner' - TestRunner = get_runner(settings) + from django.test.runner import DiscoverRunner - test_runner = TestRunner(verbosity=verbosity, interactive=interactive, - failfast=failfast) - failures = test_runner.run_tests(test_labels, extra_tests=extra_tests) + test_runner = DiscoverRunner( + verbosity=verbosity, + interactive=interactive, + failfast=failfast, + ) + failures = test_runner.run_tests( + test_labels or get_installed(), extra_tests=extra_tests) teardown(state) return failures @@ -162,10 +184,7 @@ def django_tests(verbosity, interactive, failfast, test_labels): def bisect_tests(bisection_label, options, test_labels): state = setup(int(options.verbosity), test_labels) - if not test_labels: - # Get the full list of test labels to use for bisection - from django.db.models.loading import get_apps - test_labels = [app.__name__.split('.')[-2] for app in get_apps()] + test_labels = test_labels or get_installed() print('***** Bisecting test suite: %s' % ' '.join(test_labels)) @@ -222,11 +241,7 @@ def bisect_tests(bisection_label, options, test_labels): def paired_tests(paired_test, options, test_labels): state = setup(int(options.verbosity), test_labels) - if not test_labels: - print("") - # Get the full list of test labels to use for bisection - from django.db.models.loading import get_apps - test_labels = [app.__name__.split('.')[-2] for app in get_apps()] + test_labels = test_labels or get_installed() print('***** Trying paired execution') diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index da1562d2d5..2aeaee9464 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -36,15 +36,6 @@ from django.utils.safestring import mark_safe from django.utils import six from django.utils.tzinfo import LocalTimezone -from .test_callables import CallableVariablesTests -from .test_context import ContextTests -from .test_custom import CustomTagTests, CustomFilterTests -from .test_parser import ParserTests -from .test_unicode import UnicodeTests -from .test_nodelist import NodelistTest, ErrorIndexTest -from .test_smartif import SmartIfTests -from .test_response import (TemplateResponseTest, CacheMiddlewareTest, - SimpleTemplateResponseTest, CustomURLConfTest) try: from .loaders import RenderToStringTest, EggLoaderTest diff --git a/tests/test_runner/deprecation_app/__init__.py b/tests/test_discovery_sample/__init__.py similarity index 100% rename from tests/test_runner/deprecation_app/__init__.py rename to tests/test_discovery_sample/__init__.py diff --git a/tests/test_discovery_sample/pattern_tests.py b/tests/test_discovery_sample/pattern_tests.py new file mode 100644 index 0000000000..f62a233d75 --- /dev/null +++ b/tests/test_discovery_sample/pattern_tests.py @@ -0,0 +1,7 @@ +from unittest import TestCase + + +class Test(TestCase): + + def test_sample(self): + self.assertEqual(1, 1) diff --git a/tests/test_runner/invalid_app/models/__init__.py b/tests/test_discovery_sample/tests/__init__.py similarity index 100% rename from tests/test_runner/invalid_app/models/__init__.py rename to tests/test_discovery_sample/tests/__init__.py diff --git a/tests/test_discovery_sample/tests/tests.py b/tests/test_discovery_sample/tests/tests.py new file mode 100644 index 0000000000..58bd4e7f1e --- /dev/null +++ b/tests/test_discovery_sample/tests/tests.py @@ -0,0 +1,7 @@ +from unittest import TestCase + + +class Test(TestCase): + + def test_sample(self): + pass diff --git a/tests/test_discovery_sample/tests_sample.py b/tests/test_discovery_sample/tests_sample.py new file mode 100644 index 0000000000..c541bc4cd6 --- /dev/null +++ b/tests/test_discovery_sample/tests_sample.py @@ -0,0 +1,22 @@ +from unittest import TestCase as UnitTestCase + +from django.test import TestCase as DjangoTestCase +from django.utils.unittest import TestCase as UT2TestCase + + +class TestVanillaUnittest(UnitTestCase): + + def test_sample(self): + self.assertEqual(1, 1) + + +class TestUnittest2(UT2TestCase): + + def test_sample(self): + self.assertEqual(1, 1) + + +class TestDjangoTestCase(DjangoTestCase): + + def test_sample(self): + self.assertEqual(1, 1) diff --git a/tests/test_discovery_sample2/__init__.py b/tests/test_discovery_sample2/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/test_discovery_sample2/tests.py b/tests/test_discovery_sample2/tests.py new file mode 100644 index 0000000000..232054a9f1 --- /dev/null +++ b/tests/test_discovery_sample2/tests.py @@ -0,0 +1,7 @@ +from django.test import TestCase + + +class Test(TestCase): + + def test_sample(self): + pass diff --git a/tests/test_runner/test_discover_runner.py b/tests/test_runner/test_discover_runner.py new file mode 100644 index 0000000000..3dc364b351 --- /dev/null +++ b/tests/test_runner/test_discover_runner.py @@ -0,0 +1,68 @@ +from django.test import TestCase +from django.test.runner import DiscoverRunner + + +class DiscoverRunnerTest(TestCase): + + def test_dotted_test_module(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample"], + ).countTestCases() + + self.assertEqual(count, 3) + + def test_dotted_test_class_vanilla_unittest(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample.TestVanillaUnittest"], + ).countTestCases() + + self.assertEqual(count, 1) + + def test_dotted_test_class_unittest2(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample.TestUnittest2"], + ).countTestCases() + + self.assertEqual(count, 1) + + def test_dotted_test_class_django_testcase(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample.TestDjangoTestCase"], + ).countTestCases() + + self.assertEqual(count, 1) + + def test_dotted_test_method_vanilla_unittest(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample.TestVanillaUnittest.test_sample"], + ).countTestCases() + + self.assertEqual(count, 1) + + def test_dotted_test_method_unittest2(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample.TestUnittest2.test_sample"], + ).countTestCases() + + self.assertEqual(count, 1) + + def test_dotted_test_method_django_testcase(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample.tests_sample.TestDjangoTestCase.test_sample"], + ).countTestCases() + + self.assertEqual(count, 1) + + def test_pattern(self): + count = DiscoverRunner( + pattern="*_tests.py", + ).build_suite(["test_discovery_sample"]).countTestCases() + + self.assertEqual(count, 1) + + def test_file_path(self): + count = DiscoverRunner().build_suite( + ["test_discovery_sample/"], + ).countTestCases() + + self.assertEqual(count, 4) diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index 9f0cc794b7..95ccf0242a 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -9,7 +9,7 @@ from optparse import make_option from django.core.exceptions import ImproperlyConfigured from django.core.management import call_command from django import db -from django.test import simple, TransactionTestCase, skipUnlessDBFeature +from django.test import runner, TransactionTestCase, skipUnlessDBFeature from django.test.simple import DjangoTestSuiteRunner, get_tests from django.test.testcases import connections_support_transactions from django.utils import unittest @@ -20,7 +20,7 @@ from .models import Person TEST_APP_OK = 'test_runner.valid_app.models' -TEST_APP_ERROR = 'test_runner.invalid_app.models' +TEST_APP_ERROR = 'test_runner_invalid_app.models' class DependencyOrderingTests(unittest.TestCase): @@ -36,7 +36,7 @@ class DependencyOrderingTests(unittest.TestCase): 'bravo': ['charlie'], } - ordered = simple.dependency_ordered(raw, dependencies=dependencies) + ordered = runner.dependency_ordered(raw, dependencies=dependencies) ordered_sigs = [sig for sig,value in ordered] self.assertIn('s1', ordered_sigs) @@ -56,7 +56,7 @@ class DependencyOrderingTests(unittest.TestCase): 'bravo': ['charlie'], } - ordered = simple.dependency_ordered(raw, dependencies=dependencies) + ordered = runner.dependency_ordered(raw, dependencies=dependencies) ordered_sigs = [sig for sig,value in ordered] self.assertIn('s1', ordered_sigs) @@ -83,7 +83,7 @@ class DependencyOrderingTests(unittest.TestCase): 'delta': ['charlie'], } - ordered = simple.dependency_ordered(raw, dependencies=dependencies) + ordered = runner.dependency_ordered(raw, dependencies=dependencies) ordered_sigs = [sig for sig,aliases in ordered] self.assertIn('s1', ordered_sigs) @@ -110,7 +110,7 @@ class DependencyOrderingTests(unittest.TestCase): 'alpha': ['bravo'], } - self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies) + self.assertRaises(ImproperlyConfigured, runner.dependency_ordered, raw, dependencies=dependencies) def test_own_alias_dependency(self): raw = [ @@ -121,7 +121,7 @@ class DependencyOrderingTests(unittest.TestCase): } with self.assertRaises(ImproperlyConfigured): - simple.dependency_ordered(raw, dependencies=dependencies) + runner.dependency_ordered(raw, dependencies=dependencies) # reordering aliases shouldn't matter raw = [ @@ -129,7 +129,7 @@ class DependencyOrderingTests(unittest.TestCase): ] with self.assertRaises(ImproperlyConfigured): - simple.dependency_ordered(raw, dependencies=dependencies) + runner.dependency_ordered(raw, dependencies=dependencies) class MockTestRunner(object): @@ -156,7 +156,7 @@ class ManageCommandTests(unittest.TestCase): testrunner='test_runner.NonExistentRunner') -class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner): +class CustomOptionsTestRunner(runner.DiscoverRunner): option_list = ( make_option('--option_a','-a', action='store', dest='option_a', default='1'), make_option('--option_b','-b', action='store', dest='option_b', default='2'), @@ -289,15 +289,16 @@ class DummyBackendTest(unittest.TestCase): class DeprecationDisplayTest(AdminScriptTestCase): # tests for 19546 def setUp(self): - settings = {'INSTALLED_APPS': '("test_runner.deprecation_app",)', - 'DATABASES': '{"default": {"ENGINE":"django.db.backends.sqlite3", "NAME":":memory:"}}' } + settings = { + 'DATABASES': '{"default": {"ENGINE":"django.db.backends.sqlite3", "NAME":":memory:"}}' + } self.write_settings('settings.py', sdict=settings) def tearDown(self): self.remove_settings('settings.py') def test_runner_deprecation_verbosity_default(self): - args = ['test', '--settings=test_project.settings'] + args = ['test', '--settings=test_project.settings', 'test_runner_deprecation_app'] out, err = self.run_django_admin(args) self.assertIn("DeprecationWarning: warning from test", err) self.assertIn("DeprecationWarning: module-level warning from deprecation_app", err) diff --git a/tests/test_runner_deprecation_app/__init__.py b/tests/test_runner_deprecation_app/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/test_runner/deprecation_app/models.py b/tests/test_runner_deprecation_app/models.py similarity index 100% rename from tests/test_runner/deprecation_app/models.py rename to tests/test_runner_deprecation_app/models.py diff --git a/tests/test_runner/deprecation_app/tests.py b/tests/test_runner_deprecation_app/tests.py similarity index 99% rename from tests/test_runner/deprecation_app/tests.py rename to tests/test_runner_deprecation_app/tests.py index 6dee0888ec..24d716f2bb 100644 --- a/tests/test_runner/deprecation_app/tests.py +++ b/tests/test_runner_deprecation_app/tests.py @@ -7,5 +7,3 @@ warnings.warn("module-level warning from deprecation_app", DeprecationWarning) class DummyTest(TestCase): def test_warn(self): warnings.warn("warning from test", DeprecationWarning) - - diff --git a/tests/test_runner/invalid_app/__init__.py b/tests/test_runner_invalid_app/__init__.py similarity index 100% rename from tests/test_runner/invalid_app/__init__.py rename to tests/test_runner_invalid_app/__init__.py diff --git a/tests/test_runner_invalid_app/models/__init__.py b/tests/test_runner_invalid_app/models/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/test_runner/invalid_app/tests/__init__.py b/tests/test_runner_invalid_app/tests/__init__.py similarity index 100% rename from tests/test_runner/invalid_app/tests/__init__.py rename to tests/test_runner_invalid_app/tests/__init__.py diff --git a/tests/utils_tests/tests.py b/tests/utils_tests/tests.py deleted file mode 100644 index 8d7fda4800..0000000000 --- a/tests/utils_tests/tests.py +++ /dev/null @@ -1,34 +0,0 @@ -""" -Tests for django.utils. -""" -from __future__ import absolute_import - -from .test_archive import TestBzip2Tar, TestGzipTar, TestTar, TestZip -from .test_baseconv import TestBaseConv -from .test_checksums import TestUtilsChecksums -from .test_crypto import TestUtilsCryptoMisc, TestUtilsCryptoPBKDF2 -from .test_datastructures import (DictWrapperTests, ImmutableListTests, - MergeDictTests, MultiValueDictTests, SortedDictTests) -from .test_dateformat import DateFormatTests -from .test_dateparse import DateParseTests -from .test_datetime_safe import DatetimeTests -from .test_decorators import DecoratorFromMiddlewareTests -from .test_encoding import TestEncodingUtils -from .test_feedgenerator import FeedgeneratorTest -from .test_functional import FunctionalTestCase -from .test_html import TestUtilsHtml -from .test_http import TestUtilsHttp, ETagProcessingTests, HttpDateProcessingTests -from .test_itercompat import TestIsIterator -from .test_ipv6 import TestUtilsIPv6 -from .test_jslex import JsToCForGettextTest, JsTokensTest -from .test_module_loading import (CustomLoader, DefaultLoader, EggLoader, - ModuleImportTestCase) -from .test_numberformat import TestNumberFormat -from .test_os_utils import SafeJoinTests -from .test_regex_helper import NormalizeTests -from .test_simplelazyobject import TestUtilsSimpleLazyObject -from .test_termcolors import TermColorTests -from .test_text import TestUtilsText -from .test_timesince import TimesinceTests -from .test_timezone import TimezoneTests -from .test_tzinfo import TzinfoTests diff --git a/tests/validation/tests.py b/tests/validation/tests.py index ead1b57981..b571e0c298 100644 --- a/tests/validation/tests.py +++ b/tests/validation/tests.py @@ -8,12 +8,6 @@ from . import ValidationTestCase from .models import (Author, Article, ModelToValidate, GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest) -# Import other tests for this package. -from .test_custom_messages import CustomMessagesTest -from .test_error_messages import ValidationMessagesTest -from .test_unique import GetUniqueCheckTests, PerformUniqueChecksTest -from .test_validators import TestModelsWithValidators - class BaseModelValidationTests(ValidationTestCase): From 1172bef998734ef78fc69927bed57364a868c44d Mon Sep 17 00:00:00 2001 From: zhongqi Date: Sat, 11 May 2013 15:53:54 +0800 Subject: [PATCH 07/47] Update customizing.txt The origin statement "which could be ... or whatever" **misguides** many newbies like me. In fact, the ``login`` function in ``contrib.auth`` stores ``user.pk`` in session, then ``get_user`` function in ``contrib.auth`` gets ``user.pk`` in session and then passes it to your custom ``get_user`` as ``user_id``. Which means, ``user_id`` prarameter in your custom ``get_user`` has to be the primary key of ``User`` object, too. --- docs/topics/auth/customizing.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/topics/auth/customizing.txt b/docs/topics/auth/customizing.txt index b53bbe8211..56f3e60350 100644 --- a/docs/topics/auth/customizing.txt +++ b/docs/topics/auth/customizing.txt @@ -95,7 +95,8 @@ An authentication backend is a class that implements two required methods: optional permission related :ref:`authorization methods `. The ``get_user`` method takes a ``user_id`` -- which could be a username, -database ID or whatever -- and returns a ``User`` object. +database ID or whatever, but has to be the primary key of your ``User`` object +-- and returns a ``User`` object. The ``authenticate`` method takes credentials as keyword arguments. Most of the time, it'll just look like this:: From e23a5f9a4730ddecb8f3950ee2936716f458c506 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Sat, 11 May 2013 15:47:40 +0200 Subject: [PATCH 08/47] Fixed a regression in the test runner loading of runtests.py. Refs #17365, #17366, #18727. --- tests/runtests.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/runtests.py b/tests/runtests.py index cb124f2541..a18324f676 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -167,9 +167,12 @@ def django_tests(verbosity, interactive, failfast, test_labels): extra_tests = [] # Run the test suite, including the extra validation tests. - from django.test.runner import DiscoverRunner + from django.test.utils import get_runner + if not hasattr(settings, 'TEST_RUNNER'): + settings.TEST_RUNNER = 'django.test.runner.DiscoverRunner' + TestRunner = get_runner(settings) - test_runner = DiscoverRunner( + test_runner = TestRunner( verbosity=verbosity, interactive=interactive, failfast=failfast, From 3070e8f711f891aa3bf6e9d1e123047094bf1bb0 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Sat, 11 May 2013 01:33:10 -0400 Subject: [PATCH 09/47] Properly force bytes or str for bcrypt on Python3 --- django/contrib/auth/hashers.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/django/contrib/auth/hashers.py b/django/contrib/auth/hashers.py index 092cccedde..2e0fb0a034 100644 --- a/django/contrib/auth/hashers.py +++ b/django/contrib/auth/hashers.py @@ -9,7 +9,7 @@ from django.conf import settings from django.test.signals import setting_changed from django.utils import importlib from django.utils.datastructures import SortedDict -from django.utils.encoding import force_bytes, force_str +from django.utils.encoding import force_bytes, force_str, force_text from django.core.exceptions import ImproperlyConfigured from django.utils.crypto import ( pbkdf2, constant_time_compare, get_random_string) @@ -291,7 +291,7 @@ class BCryptSHA256PasswordHasher(BasePasswordHasher): password = force_bytes(password) data = bcrypt.hashpw(password, salt) - return "%s$%s" % (self.algorithm, data) + return "%s$%s" % (self.algorithm, force_text(data)) def verify(self, password, encoded): algorithm, data = encoded.split('$', 1) @@ -307,6 +307,9 @@ class BCryptSHA256PasswordHasher(BasePasswordHasher): else: password = force_bytes(password) + # Ensure that our data is a bytestring + data = force_bytes(data) + return constant_time_compare(data, bcrypt.hashpw(password, data)) def safe_summary(self, encoded): From 2bf403ecbd958bfb269794b36e61b69f0aede4cf Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Sat, 11 May 2013 18:29:08 +0200 Subject: [PATCH 10/47] Fixed a regression from e23a5f9a4730ddecb8f3950ee2936716f458c506. Excluded postgis specific gis tests from other spatial databases. Refs #17365, #17366, #18727. --- django/contrib/gis/tests/distapp/tests.py | 6 +++--- django/contrib/gis/tests/geo3d/tests.py | 4 ++-- django/contrib/gis/tests/geoapp/tests.py | 8 ++++---- django/contrib/gis/tests/geogapp/tests.py | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/django/contrib/gis/tests/distapp/tests.py b/django/contrib/gis/tests/distapp/tests.py index 2e778d3fce..2ed17a03bd 100644 --- a/django/contrib/gis/tests/distapp/tests.py +++ b/django/contrib/gis/tests/distapp/tests.py @@ -5,7 +5,7 @@ from django.db.models import Q from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.measure import D # alias for Distance from django.contrib.gis.tests.utils import ( - HAS_SPATIAL_DB, oracle, postgis, spatialite, no_oracle, no_spatialite + HAS_SPATIAL_DB, mysql, oracle, postgis, spatialite, no_oracle, no_spatialite ) from django.test import TestCase from django.utils.unittest import skipUnless @@ -17,8 +17,8 @@ if HAS_GEOS and HAS_SPATIAL_DB: SouthTexasCity, SouthTexasCityFt, CensusZipcode, SouthTexasZipcode) -@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, - "Geos and spatial db are required.") +@skipUnless(HAS_GEOS and HAS_SPATIAL_DB and not mysql, + "Geos and spatial db (not mysql) are required.") class DistanceTest(TestCase): if HAS_GEOS and HAS_SPATIAL_DB: diff --git a/django/contrib/gis/tests/geo3d/tests.py b/django/contrib/gis/tests/geo3d/tests.py index c51c0decd7..df9f35690b 100644 --- a/django/contrib/gis/tests/geo3d/tests.py +++ b/django/contrib/gis/tests/geo3d/tests.py @@ -5,7 +5,7 @@ import re from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.geos import HAS_GEOS -from django.contrib.gis.tests.utils import HAS_SPATIAL_DB +from django.contrib.gis.tests.utils import postgis from django.test import TestCase from django.utils._os import upath from django.utils.unittest import skipUnless @@ -62,7 +62,7 @@ bbox_data = ( ) -@skipUnless(HAS_GEOS and HAS_GDAL and HAS_SPATIAL_DB, "Geos, GDAL and spatial db are required.") +@skipUnless(HAS_GEOS and HAS_GDAL and postgis, "Geos, GDAL and postgis are required.") class Geo3DTest(TestCase): """ Only a subset of the PostGIS routines are 3D-enabled, and this TestCase diff --git a/django/contrib/gis/tests/geoapp/tests.py b/django/contrib/gis/tests/geoapp/tests.py index f6de0670c7..cf6e316919 100644 --- a/django/contrib/gis/tests/geoapp/tests.py +++ b/django/contrib/gis/tests/geoapp/tests.py @@ -6,7 +6,7 @@ from django.db import connection from django.contrib.gis import gdal from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.tests.utils import ( - HAS_SPATIAL_DB, no_mysql, no_oracle, no_spatialite, + no_mysql, no_oracle, no_spatialite, mysql, oracle, postgis, spatialite) from django.test import TestCase from django.utils import six, unittest @@ -27,7 +27,7 @@ def postgis_bug_version(): return spatial_version and (2, 0, 0) <= spatial_version <= (2, 0, 1) -@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") +@skipUnless(HAS_GEOS and postgis, "Geos and postgis are required.") class GeoModelTest(TestCase): def test_fixtures(self): @@ -202,7 +202,7 @@ class GeoModelTest(TestCase): self.assertTrue(isinstance(cities2[0].point, Point)) -@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") +@skipUnless(HAS_GEOS and postgis, "Geos and postgis are required.") class GeoLookupTest(TestCase): @no_mysql @@ -388,7 +388,7 @@ class GeoLookupTest(TestCase): self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, intersects_mask)).name) -@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") +@skipUnless(HAS_GEOS and postgis, "Geos and postgis are required.") class GeoQuerySetTest(TestCase): # Please keep the tests in GeoQuerySet method's alphabetic order diff --git a/django/contrib/gis/tests/geogapp/tests.py b/django/contrib/gis/tests/geogapp/tests.py index 8d3e96b0a9..ed54999f90 100644 --- a/django/contrib/gis/tests/geogapp/tests.py +++ b/django/contrib/gis/tests/geogapp/tests.py @@ -8,7 +8,7 @@ import os from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.geos import HAS_GEOS from django.contrib.gis.measure import D -from django.contrib.gis.tests.utils import HAS_SPATIAL_DB +from django.contrib.gis.tests.utils import postgis from django.test import TestCase from django.utils._os import upath from django.utils.unittest import skipUnless @@ -17,7 +17,7 @@ if HAS_GEOS: from .models import City, County, Zipcode -@skipUnless(HAS_GEOS and HAS_SPATIAL_DB, "Geos and spatial db are required.") +@skipUnless(HAS_GEOS and postgis, "Geos and postgis are required.") class GeographyTest(TestCase): def test01_fixture_load(self): From 01820466ca1e2425c6a5535402b36251c5e5dae5 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Sat, 11 May 2013 22:41:34 +0200 Subject: [PATCH 11/47] Don't hardcode primary keys in gis tests. --- django/contrib/gis/tests/geoapp/test_regress.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/tests/geoapp/test_regress.py b/django/contrib/gis/tests/geoapp/test_regress.py index d6f3c8d698..f8f2e0de3a 100644 --- a/django/contrib/gis/tests/geoapp/test_regress.py +++ b/django/contrib/gis/tests/geoapp/test_regress.py @@ -77,8 +77,8 @@ class GeoRegressionTests(TestCase): t1 = Truth.objects.create(val=True) t2 = Truth.objects.create(val=False) - val1 = Truth.objects.get(pk=1).val - val2 = Truth.objects.get(pk=2).val + val1 = Truth.objects.get(pk=t1).val + val2 = Truth.objects.get(pk=t2).val # verify types -- should't be 0/1 self.assertIsInstance(val1, bool) self.assertIsInstance(val2, bool) From a6edde326057c0bf2e7cf8929ba8f238a7fd2b41 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Sat, 11 May 2013 22:57:01 +0200 Subject: [PATCH 12/47] Fixed embarrassing typo. --- django/contrib/gis/tests/geoapp/test_regress.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/tests/geoapp/test_regress.py b/django/contrib/gis/tests/geoapp/test_regress.py index f8f2e0de3a..43dbcfd852 100644 --- a/django/contrib/gis/tests/geoapp/test_regress.py +++ b/django/contrib/gis/tests/geoapp/test_regress.py @@ -77,8 +77,8 @@ class GeoRegressionTests(TestCase): t1 = Truth.objects.create(val=True) t2 = Truth.objects.create(val=False) - val1 = Truth.objects.get(pk=t1).val - val2 = Truth.objects.get(pk=t2).val + val1 = Truth.objects.get(pk=t1.pk).val + val2 = Truth.objects.get(pk=t2.pk).val # verify types -- should't be 0/1 self.assertIsInstance(val1, bool) self.assertIsInstance(val2, bool) From 679a2ac843567d32c95ccc46a215bc453ccfa2d0 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 11 May 2013 19:08:57 -0400 Subject: [PATCH 13/47] Fixed #20249 - Removed a "feature" in the tutorial that doesn't actually work. Thanks bmispelon for the report and draft patch. --- docs/intro/tutorial04.txt | 70 +++++++++++++++++++++-------------- docs/intro/tutorial05.txt | 77 ++++++++++++++++++--------------------- 2 files changed, 77 insertions(+), 70 deletions(-) diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt index 87d8e584ad..9f54243a3e 100644 --- a/docs/intro/tutorial04.txt +++ b/docs/intro/tutorial04.txt @@ -185,7 +185,7 @@ conversion. We will: 2. Delete some of the old, unneeded views. -3. Fix up URL handling for the new views. +3. Introduce new views based on Django's generic views. Read on for details. @@ -205,32 +205,51 @@ Amend URLconf First, open the ``polls/urls.py`` URLconf and change it like so:: from django.conf.urls import patterns, url - from django.views.generic import DetailView, ListView - from polls.models import Poll + + from polls import views urlpatterns = patterns('', - url(r'^$', - ListView.as_view( - queryset=Poll.objects.order_by('-pub_date')[:5], - context_object_name='latest_poll_list', - template_name='polls/index.html'), - name='index'), - url(r'^(?P\d+)/$', - DetailView.as_view( - model=Poll, - template_name='polls/detail.html'), - name='detail'), - url(r'^(?P\d+)/results/$', - DetailView.as_view( - model=Poll, - template_name='polls/results.html'), - name='results'), - url(r'^(?P\d+)/vote/$', 'polls.views.vote', name='vote'), + url(r'^$', views.IndexView.as_view(), name='index'), + url(r'^(?P\d+)/$', views.DetailView.as_view(), name='detail'), + url(r'^(?P\d+)/results/$', views.ResultsView.as_view(), name='results'), + url(r'^(?P\d+)/vote/$', views.vote, name='vote'), ) Amend views ----------- +Next, we're going to remove our old ``index``, ``detail``, and ``results`` +views and use Django's generic views instead. To do so, open the +``polls/views.py`` file and change it like so:: + + from django.shortcuts import get_object_or_404, render + from django.http import HttpResponseRedirect + from django.core.urlresolvers import reverse + from django.views import generic + + from polls.models import Choice, Poll + + class IndexView(generic.ListView): + template_name = 'polls/index.html' + context_object_name = 'latest_poll_list' + + def get_queryset(self): + """Return the last five published polls.""" + return Poll.objects.order_by('-pub_date')[:5] + + + class DetailView(generic.DetailView): + model = Poll + template_name = 'polls/detail.html' + + + class ResultsView(generic.DetailView): + model = Poll + template_name = 'polls/results.html' + + def vote(request, poll_id): + .... + We're using two generic views here: :class:`~django.views.generic.list.ListView` and :class:`~django.views.generic.detail.DetailView`. Respectively, those @@ -238,7 +257,7 @@ two views abstract the concepts of "display a list of objects" and "display a detail page for a particular type of object." * Each generic view needs to know what model it will be acting - upon. This is provided using the ``model`` parameter. + upon. This is provided using the ``model`` attribute. * The :class:`~django.views.generic.detail.DetailView` generic view expects the primary key value captured from the URL to be called @@ -248,7 +267,7 @@ two views abstract the concepts of "display a list of objects" and By default, the :class:`~django.views.generic.detail.DetailView` generic view uses a template called ``/_detail.html``. In our case, it'll use the template ``"polls/poll_detail.html"``. The -``template_name`` argument is used to tell Django to use a specific +``template_name`` attribute is used to tell Django to use a specific template name instead of the autogenerated default template name. We also specify the ``template_name`` for the ``results`` list view -- this ensures that the results view and the detail view have a @@ -268,16 +287,11 @@ automatically -- since we're using a Django model (``Poll``), Django is able to determine an appropriate name for the context variable. However, for ListView, the automatically generated context variable is ``poll_list``. To override this we provide the ``context_object_name`` -option, specifying that we want to use ``latest_poll_list`` instead. +attribute, specifying that we want to use ``latest_poll_list`` instead. As an alternative approach, you could change your templates to match the new default context variables -- but it's a lot easier to just tell Django to use the variable you want. -You can now delete the ``index()``, ``detail()`` and ``results()`` views from -``polls/views.py``. We don't need them anymore -- they have been replaced by -generic views. You can also delete the import for ``HttpResponse``, which is no -longer required. - Run the server, and use your new polling app based on generic views. For full details on generic views, see the :doc:`generic views documentation diff --git a/docs/intro/tutorial05.txt b/docs/intro/tutorial05.txt index 67a40aba40..a276763d67 100644 --- a/docs/intro/tutorial05.txt +++ b/docs/intro/tutorial05.txt @@ -378,45 +378,40 @@ Improving our view The list of polls shows polls that aren't published yet (i.e. those that have a ``pub_date`` in the future). Let's fix that. -In :doc:`Tutorial 4 ` we deleted the view functions from -``views.py`` in favor of a :class:`~django.views.generic.list.ListView` in -``urls.py``:: +In :doc:`Tutorial 4 ` we introduced a class-based view, +based on :class:`~django.views.generic.list.ListView`:: - url(r'^$', - ListView.as_view( - queryset=Poll.objects.order_by('-pub_date')[:5], - context_object_name='latest_poll_list', - template_name='polls/index.html'), - name='index'), + class IndexView(generic.ListView): + template_name = 'polls/index.html' + context_object_name = 'latest_poll_list' + + def get_queryset(self): + """Return the last five published polls.""" + return Poll.objects.order_by('-pub_date')[:5] ``response.context_data['latest_poll_list']`` extracts the data this view places into the context. -We need to amend the line that gives us the ``queryset``:: - - queryset=Poll.objects.order_by('-pub_date')[:5], - -Let's change the queryset so that it also checks the date by comparing it with -``timezone.now()``. First we need to add an import:: +We need to amend the ``get_queryset`` method and change it so that it also +checks the date by comparing it with ``timezone.now()``. First we need to add +an import:: from django.utils import timezone -and then we must amend the existing ``url`` function to:: +and then we must amend the ``get_queryset`` method like so:: - url(r'^$', - ListView.as_view( - queryset=Poll.objects.filter(pub_date__lte=timezone.now) \ - .order_by('-pub_date')[:5], - context_object_name='latest_poll_list', - template_name='polls/index.html'), - name='index'), + def get_queryset(self): + """ + Return the last five published polls (not including those set to be + published in the future). + """ + return Poll.objects.filter( + pub_date__lte=timezone.now() + ).order_by('-pub_date')[:5] -``Poll.objects.filter(pub_date__lte=timezone.now)`` returns a queryset +``Poll.objects.filter(pub_date__lte=timezone.now())`` returns a queryset containing Polls whose ``pub_date`` is less than or equal to - that is, earlier -than or equal to - ``timezone.now``. Notice that we use a callable queryset -argument, ``timezone.now``, which will be evaluated at request time. If we had -included the parentheses, ``timezone.now()`` would be evaluated just once when -the web server is started. +than or equal to - ``timezone.now``. Testing our new view -------------------- @@ -527,20 +522,18 @@ Testing the ``DetailView`` What we have works well; however, even though future polls don't appear in the *index*, users can still reach them if they know or guess the right URL. So we -need similar constraints in the ``DetailViews``, by adding:: +need to add a similar constraint to ``DetailView``:: - queryset=Poll.objects.filter(pub_date__lte=timezone.now) -to them - for example:: + class DetailView(generic.DetailView): + ... + def get_queryset(self): + """ + Excludes any polls that aren't published yet. + """ + return Poll.objects.filter(pub_date__lte=timezone.now()) - url(r'^(?P\d+)/$', - DetailView.as_view( - queryset=Poll.objects.filter(pub_date__lte=timezone.now), - model=Poll, - template_name='polls/detail.html'), - name='detail'), - -and of course, we will add some tests, to check that a ``Poll`` whose +And of course, we will add some tests, to check that a ``Poll`` whose ``pub_date`` is in the past can be displayed, and that one with a ``pub_date`` in the future is not:: @@ -566,9 +559,9 @@ in the future is not:: Ideas for more tests -------------------- -We ought to add similar ``queryset`` arguments to the other ``DetailView`` -URLs, and create a new test class for each view. They'll be very similar to -what we have just created; in fact there will be a lot of repetition. +We ought to add a similar ``get_queryset`` method to ``ResultsView`` and +create a new test class for that view. It'll be very similar to what we have +just created; in fact there will be a lot of repetition. We could also improve our application in other ways, adding tests along the way. For example, it's silly that ``Polls`` can be published on the site that From 2c62a509deb50e39375b0cd44cfc85a743978fdc Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 11 May 2013 19:34:02 -0400 Subject: [PATCH 14/47] Fixed #20136 - Fixed and expanded the docs for loaddata and model signals. Thanks brandon@ and Anssi for the report. --- django/core/serializers/base.py | 4 +--- docs/ref/django-admin.txt | 41 ++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index 294934a04a..1e78026c40 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -161,9 +161,7 @@ class DeserializedObject(object): def save(self, save_m2m=True, using=None): # Call save on the Model baseclass directly. This bypasses any # model-defined save. The save is also forced to be raw. - # This ensures that the data that is deserialized is literally - # what came from the file, not post-processed by pre_save/save - # methods. + # raw=True is passed to any pre/post_save signals. models.Model.save_base(self.object, using=using, raw=True) if self.m2m_data and save_m2m: for accessor_name, object_list in self.m2m_data.items(): diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index ec49705add..2f2880679c 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -373,7 +373,46 @@ application, ``/foo/bar/mydata.json`` for each directory in :setting:`FIXTURE_DIRS`, and the literal path ``foo/bar/mydata.json``. When fixture files are processed, the data is saved to the database as is. -Model defined ``save`` methods and ``pre_save`` signals are not called. +Model defined :meth:`~django.db.models.Model.save` methods are not called, and +any :data:`~django.db.models.signals.pre_save` or +:data:`~django.db.models.signals.post_save` signals will be called with +``raw=True`` since the instance only contains attributes that are local to the +model. You may, for example, want to disable handlers that access +related fields that aren't present during fixture loading and would otherwise +raise an exception:: + + from django.db.models.signals import post_save + from .models import MyModel + + def my_handler(**kwargs): + # disable the handler during fixture loading + if kwargs['raw']: + return + ... + + post_save.connect(my_handler, sender=MyModel) + +You could also write a simple decorator to encapsulate this logic:: + + from functools import wraps + + def disable_for_loaddata(signal_handler): + """ + Decorator that turns off signal handlers when loading fixture data. + """ + @wraps(signal_handler) + def wrapper(*args, **kwargs): + if kwargs['raw']: + return + signal_handler(*args, **kwargs) + return wrapper + + @disable_for_loaddata + def my_handler(**kwargs): + ... + +Just be aware that this logic will disable the signals whenever fixtures are +deserialized, not just during ``loaddata``. Note that the order in which fixture files are processed is undefined. However, all fixture data is installed as a single transaction, so data in From b9efc03e6d9cf602203d4808d4e171e19014a25f Mon Sep 17 00:00:00 2001 From: Simeon Visser Date: Sun, 12 May 2013 20:24:48 +0100 Subject: [PATCH 15/47] Fixed #20397 - Cleaned up issue with quotation marks in documentation --- docs/ref/models/querysets.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index d27214a66c..96e189fa74 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -328,7 +328,7 @@ Use the ``reverse()`` method to reverse the order in which a queryset's elements are returned. Calling ``reverse()`` a second time restores the ordering back to the normal direction. -To retrieve the ''last'' five items in a queryset, you could do this:: +To retrieve the "last" five items in a queryset, you could do this:: my_queryset.reverse()[:5] From 956973ca6c47219d5dc3c645bc34af8fb58b8f24 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sun, 12 May 2013 17:29:34 -0300 Subject: [PATCH 16/47] Updated test failure example. --- docs/index.txt | 2 +- docs/topics/testing/overview.txt | 20 +++++--------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/docs/index.txt b/docs/index.txt index 3b5e74f1a2..7f9d1bd032 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -185,7 +185,7 @@ testing of Django applications: * **Testing:** :doc:`Introduction ` | :doc:`Writing and running tests ` | - :doc:`Advanced topics ` | + :doc:`Advanced topics ` * **Deployment:** :doc:`Overview ` | diff --git a/docs/topics/testing/overview.txt b/docs/topics/testing/overview.txt index 5023e099aa..2a8b4613a7 100644 --- a/docs/topics/testing/overview.txt +++ b/docs/topics/testing/overview.txt @@ -280,25 +280,15 @@ If there are test failures, however, you'll see full details about which tests failed:: ====================================================================== - FAIL: Doctest: ellington.core.throttle.models + FAIL: test_was_published_recently_with_future_poll (polls.tests.PollMethodTests) ---------------------------------------------------------------------- Traceback (most recent call last): - File "/dev/django/test/doctest.py", line 2153, in runTest - raise self.failureException(self.format_failure(new.getvalue())) - AssertionError: Failed doctest test for myapp.models - File "/dev/myapp/models.py", line 0, in models + File "/dev/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_poll + self.assertEqual(future_poll.was_published_recently(), False) + AssertionError: True != False ---------------------------------------------------------------------- - File "/dev/myapp/models.py", line 14, in myapp.models - Failed example: - throttle.check("actor A", "action one", limit=2, hours=1) - Expected: - True - Got: - False - - ---------------------------------------------------------------------- - Ran 2 tests in 0.048s + Ran 1 test in 0.003s FAILED (failures=1) From 897e4eab6531cdbaf27c1b2a6716c86eb6ee1f5d Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 13 May 2013 13:43:28 -0400 Subject: [PATCH 17/47] Fixed #20398 - Added language selection code to example in documentation Thanks ggbaker for the suggestion and Simeon Visser for the patch. --- docs/topics/i18n/translation.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index 2ce9d8d2bc..5b4ffea528 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -1437,7 +1437,9 @@ Here's example HTML template code: From 8f0a4665d67868dce2e204dd592b0f133edf7943 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Mon, 13 May 2013 23:39:50 -0400 Subject: [PATCH 18/47] Recommend using the bcrypt library instead of py-bcrypt * py-bcrypt has not been updated in some time * py-bcrypt does not support Python3 * py3k-bcrypt, a port of py-bcrypt to python3 is not compatible with Django * bcrypt is supported on all versions of Python that Django supports --- django/contrib/auth/hashers.py | 6 +++--- django/contrib/auth/tests/test_hashers.py | 4 ++-- docs/topics/auth/passwords.txt | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/django/contrib/auth/hashers.py b/django/contrib/auth/hashers.py index 2e0fb0a034..6abdb5f476 100644 --- a/django/contrib/auth/hashers.py +++ b/django/contrib/auth/hashers.py @@ -263,13 +263,13 @@ class BCryptSHA256PasswordHasher(BasePasswordHasher): Secure password hashing using the bcrypt algorithm (recommended) This is considered by many to be the most secure algorithm but you - must first install the py-bcrypt library. Please be warned that + must first install the bcrypt library. Please be warned that this library depends on native C code and might cause portability issues. """ algorithm = "bcrypt_sha256" digest = hashlib.sha256 - library = ("py-bcrypt", "bcrypt") + library = ("bcrypt", "bcrypt") rounds = 12 def salt(self): @@ -329,7 +329,7 @@ class BCryptPasswordHasher(BCryptSHA256PasswordHasher): Secure password hashing using the bcrypt algorithm This is considered by many to be the most secure algorithm but you - must first install the py-bcrypt library. Please be warned that + must first install the bcrypt library. Please be warned that this library depends on native C code and might cause portability issues. diff --git a/django/contrib/auth/tests/test_hashers.py b/django/contrib/auth/tests/test_hashers.py index 9253fcbc43..d49fdc412e 100644 --- a/django/contrib/auth/tests/test_hashers.py +++ b/django/contrib/auth/tests/test_hashers.py @@ -92,7 +92,7 @@ class TestUtilsHashPass(unittest.TestCase): self.assertFalse(check_password('lètmeiz', encoded)) self.assertEqual(identify_hasher(encoded).algorithm, "crypt") - @skipUnless(bcrypt, "py-bcrypt not installed") + @skipUnless(bcrypt, "bcrypt not installed") def test_bcrypt_sha256(self): encoded = make_password('lètmein', hasher='bcrypt_sha256') self.assertTrue(is_password_usable(encoded)) @@ -108,7 +108,7 @@ class TestUtilsHashPass(unittest.TestCase): self.assertTrue(check_password(password, encoded)) self.assertFalse(check_password(password[:72], encoded)) - @skipUnless(bcrypt, "py-bcrypt not installed") + @skipUnless(bcrypt, "bcrypt not installed") def test_bcrypt(self): encoded = make_password('lètmein', hasher='bcrypt') self.assertTrue(is_password_usable(encoded)) diff --git a/docs/topics/auth/passwords.txt b/docs/topics/auth/passwords.txt index 2193e6a3c7..206e7d856c 100644 --- a/docs/topics/auth/passwords.txt +++ b/docs/topics/auth/passwords.txt @@ -76,8 +76,8 @@ use it Django supports bcrypt with minimal effort. To use Bcrypt as your default storage algorithm, do the following: -1. Install the `py-bcrypt`_ library (probably by running ``sudo pip install - py-bcrypt``, or downloading the library and installing it with ``python +1. Install the `bcrypt library`_ (probably by running ``sudo pip install + bcrypt``, or downloading the library and installing it with ``python setup.py install``). 2. Modify :setting:`PASSWORD_HASHERS` to list ``BCryptSHA256PasswordHasher`` @@ -185,7 +185,7 @@ mentioned algorithms won't be able to upgrade. .. _pbkdf2: http://en.wikipedia.org/wiki/PBKDF2 .. _nist: http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf .. _bcrypt: http://en.wikipedia.org/wiki/Bcrypt -.. _py-bcrypt: http://pypi.python.org/pypi/py-bcrypt/ +.. _`bcrypt library`: https://pypi.python.org/pypi/bcrypt/ Manually managing a user's password From d258cce48238678a606b82287c245576249147cb Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Tue, 14 May 2013 11:40:33 +0100 Subject: [PATCH 19/47] Fixing a minor spelling mistake in the queryset documentation --- docs/ref/models/querysets.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 96e189fa74..ffada19082 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -1486,7 +1486,7 @@ internally so that repeated evaluations do not result in additional queries. In contrast, ``iterator()`` will read results directly, without doing any caching at the ``QuerySet`` level (internally, the default iterator calls ``iterator()`` and caches the return value). For a ``QuerySet`` which returns a large number of -objects that you only need to access once, this can results in better +objects that you only need to access once, this can result in better performance and a significant reduction in memory. Note that using ``iterator()`` on a ``QuerySet`` which has already been From 33793f7c3edd8ff144ff2e9434367267c20af26a Mon Sep 17 00:00:00 2001 From: Daniel Lindsley Date: Tue, 14 May 2013 19:31:16 -0700 Subject: [PATCH 20/47] Fixed #19934 - Use of Pillow is now preferred over PIL. This starts the deprecation period for PIL (support to end in 1.8). --- django/core/files/images.py | 8 +- django/core/management/validation.py | 8 +- django/forms/fields.py | 16 +-- django/utils/image.py | 146 ++++++++++++++++++++++++++ docs/faq/contributing.txt | 2 +- docs/internals/deprecation.txt | 6 ++ docs/ref/forms/fields.txt | 12 ++- docs/releases/1.6.txt | 7 ++ tests/file_storage/tests.py | 20 ++-- tests/model_fields/models.py | 17 ++- tests/model_fields/test_imagefield.py | 8 +- tests/model_forms/models.py | 12 +-- tests/serializers_regress/models.py | 2 +- 13 files changed, 198 insertions(+), 66 deletions(-) create mode 100644 django/utils/image.py diff --git a/django/core/files/images.py b/django/core/files/images.py index 0d87ae853e..e1d6091658 100644 --- a/django/core/files/images.py +++ b/django/core/files/images.py @@ -1,7 +1,7 @@ """ Utility functions for handling images. -Requires PIL, as you might imagine. +Requires Pillow (or PIL), as you might imagine. """ import zlib @@ -35,11 +35,7 @@ def get_image_dimensions(file_or_path, close=False): 'close' to True to close the file at the end if it is initially in an open state. """ - # Try to import PIL in either of the two ways it can end up installed. - try: - from PIL import ImageFile as PILImageFile - except ImportError: - import ImageFile as PILImageFile + from django.utils.image import ImageFile as PILImageFile p = PILImageFile.Parser() if hasattr(file_or_path, 'read'): diff --git a/django/core/management/validation.py b/django/core/management/validation.py index 94d604346b..0f0eade569 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -105,14 +105,10 @@ def get_validation_errors(outfile, app=None): if isinstance(f, models.FileField) and not f.upload_to: e.add(opts, '"%s": FileFields require an "upload_to" attribute.' % f.name) if isinstance(f, models.ImageField): - # Try to import PIL in either of the two ways it can end up installed. try: - from PIL import Image + from django.utils.image import Image except ImportError: - try: - import Image - except ImportError: - e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name) + e.add(opts, '"%s": To use ImageFields, you need to install Pillow. Get it at https://pypi.python.org/pypi/Pillow.' % f.name) if isinstance(f, models.BooleanField) and getattr(f, 'null', False): e.add(opts, '"%s": BooleanFields do not accept null values. Use a NullBooleanField instead.' % f.name) if isinstance(f, models.FilePathField) and not (f.allow_files or f.allow_folders): diff --git a/django/forms/fields.py b/django/forms/fields.py index 146a10d635..4ce57d34a3 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -602,13 +602,9 @@ class ImageField(FileField): if f is None: return None - # Try to import PIL in either of the two ways it can end up installed. - try: - from PIL import Image - except ImportError: - import Image + from django.utils.image import Image - # We need to get a file object for PIL. We might have a path or we might + # We need to get a file object for Pillow. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): file = data.temporary_file_path() @@ -623,12 +619,8 @@ class ImageField(FileField): # image in memory, which is a DoS vector. See #3848 and #18520. # verify() must be called immediately after the constructor. Image.open(file).verify() - except ImportError: - # Under PyPy, it is possible to import PIL. However, the underlying - # _imaging C module isn't available, so an ImportError will be - # raised. Catch and re-raise. - raise - except Exception: # Python Imaging Library doesn't recognize it as an image + except Exception: + # Pillow (or PIL) doesn't recognize it as an image. six.reraise(ValidationError, ValidationError(self.error_messages['invalid_image']), sys.exc_info()[2]) if hasattr(f, 'seek') and callable(f.seek): f.seek(0) diff --git a/django/utils/image.py b/django/utils/image.py new file mode 100644 index 0000000000..ed9b210973 --- /dev/null +++ b/django/utils/image.py @@ -0,0 +1,146 @@ +# -*- coding: utf-8 -*- +""" +To provide a shim layer over Pillow/PIL situation until the PIL support is +removed. + + +Combinations To Account For +=========================== + +* Pillow: + + * never has ``_imaging`` under any Python + * has the ``Image.alpha_composite``, which may aid in detection + +* PIL + + * CPython 2.x may have _imaging (& work) + * CPython 2.x may *NOT* have _imaging (broken & needs a error message) + * CPython 3.x doesn't work + * PyPy will *NOT* have _imaging (but works?) + +Restated, that looks like: + +* If we're on Python 2.x, it could be either Pillow or PIL: + + * If ``import _imaging`` results in ``ImportError``, either they have a + working Pillow installation or a broken PIL installation, so we need to + detect further: + + * To detect, we first ``import Image``. + * If ``Image`` has a ``alpha_composite`` attribute present, only Pillow + has this, so we assume it's working. + * If ``Image`` DOES NOT have a ``alpha_composite``attribute, it must be + PIL & is a broken (likely C compiler-less) install, which we need to + warn the user about. + + * If ``import _imaging`` works, it must be PIL & is a working install. + +* Python 3.x + + * If ``import Image`` works, it must be Pillow, since PIL isn't Python 3.x + compatible. + +* PyPy + + * If ``import _imaging`` results in ``ImportError``, it could be either + Pillow or PIL, both of which work without it on PyPy, so we're fine. + + +Approach +======== + +* Attempt to import ``Image`` + + * ``ImportError`` - nothing is installed, toss an exception + * Either Pillow or the PIL is installed, so continue detecting + +* Attempt to ``hasattr(Image, 'alpha_composite')`` + + * If it works, it's Pillow & working + * If it fails, we've got a PIL install, continue detecting + + * The only option here is that we're on Python 2.x or PyPy, of which + we only care about if we're on CPython. + * If we're on CPython, attempt to ``import _imaging`` + + * ``ImportError`` - Bad install, toss an exception + +""" +import warnings + +from django.core.exceptions import ImproperlyConfigured +from django.utils.translation import ugettext_lazy as _ + + +Image = None +_imaging = None +ImageFile = None + + +def _detect_image_library(): + global Image + global _imaging + global ImageFile + + # Skip re-attempting to import if we've already run detection. + if Image is not None: + return Image, _imaging, ImageFile + + # Assume it's not there. + PIL_imaging = False + + try: + # Try from the Pillow (or one variant of PIL) install location first. + from PIL import Image as PILImage + except ImportError as err: + try: + # If that failed, try the alternate import syntax for PIL. + import Image as PILImage + except ImportError as err: + # Neither worked, so it's likely not installed. + raise ImproperlyConfigured( + _(u"Neither Pillow nor PIL could be imported: %s" % err) + ) + + # ``Image.alpha_composite`` was added to Pillow in SHA: e414c6 & is not + # available in any version of the PIL. + if hasattr(PILImage, u'alpha_composite'): + PIL_imaging = False + else: + # We're dealing with the PIL. Determine if we're on CPython & if + # ``_imaging`` is available. + import platform + + # This is the Alex Approved™ way. + # See http://mail.python.org/pipermail//pypy-dev/2011-November/008739.html + if platform.python_implementation().lower() == u'cpython': + # We're on CPython (likely 2.x). Since a C compiler is needed to + # produce a fully-working PIL & will create a ``_imaging`` module, + # we'll attempt to import it to verify their kit works. + try: + import _imaging as PIL_imaging + except ImportError as err: + raise ImproperlyConfigured( + _(u"The '_imaging' module for the PIL could not be " + + u"imported: %s" % err) + ) + + # Try to import ImageFile as well. + try: + from PIL import ImageFile as PILImageFile + except ImportError: + import ImageFile as PILImageFile + + # Finally, warn about deprecation... + if PIL_imaging is not False: + warnings.warn( + "Support for the PIL will be removed in Django 1.8. Please " + + "uninstall it & install Pillow instead.", + PendingDeprecationWarning + ) + + return PILImage, PIL_imaging, PILImageFile + + +Image, _imaging, ImageFile = _detect_image_library() diff --git a/docs/faq/contributing.txt b/docs/faq/contributing.txt index 6f2dfd906f..20950e88c5 100644 --- a/docs/faq/contributing.txt +++ b/docs/faq/contributing.txt @@ -27,7 +27,7 @@ to make it dead easy, even for someone who may not be intimately familiar with that area of the code, to understand the problem and verify the fix: * Are there clear instructions on how to reproduce the bug? If this - touches a dependency (such as PIL), a contrib module, or a specific + touches a dependency (such as Pillow/PIL), a contrib module, or a specific database, are those instructions clear enough even for someone not familiar with it? diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index 862907b2a8..774de2a2fd 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -365,6 +365,12 @@ these changes. * ``django.conf.urls.shortcut`` and ``django.views.defaults.shortcut`` will be removed. +* Support for the Python Imaging Library (PIL) module will be removed, as it + no longer appears to be actively maintained & does not work on Python 3. + You are advised to install `Pillow`_, which should be used instead. + +.. _`Pillow`: https://pypi.python.org/pypi/Pillow + * The following private APIs will be removed: - ``django.db.close_connection()`` diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 29f889445d..054f45c430 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -608,19 +608,21 @@ For each field, we describe the default widget used if you don't specify * Normalizes to: An ``UploadedFile`` object that wraps the file content and file name into a single object. * Validates that file data has been bound to the form, and that the - file is of an image format understood by PIL. + file is of an image format understood by Pillow/PIL. * Error message keys: ``required``, ``invalid``, ``missing``, ``empty``, ``invalid_image`` - Using an ``ImageField`` requires that the `Python Imaging Library`_ (PIL) - is installed and supports the image formats you use. If you encounter a - ``corrupt image`` error when you upload an image, it usually means PIL + Using an ``ImageField`` requires that either `Pillow`_ (recommended) or the + `Python Imaging Library`_ (PIL) are installed and supports the image + formats you use. If you encounter a ``corrupt image`` error when you + upload an image, it usually means either Pillow or PIL doesn't understand its format. To fix this, install the appropriate - library and reinstall PIL. + library and reinstall Pillow or PIL. When you use an ``ImageField`` on a form, you must also remember to :ref:`bind the file data to the form `. +.. _Pillow: http://python-imaging.github.io/Pillow/ .. _Python Imaging Library: http://www.pythonware.com/products/pil/ ``IntegerField`` diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index 5780229eb5..7469783659 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -220,6 +220,13 @@ Minor features * Added ``BCryptSHA256PasswordHasher`` to resolve the password truncation issue with bcrypt. +* `Pillow`_ is now the preferred image manipulation library to use with Django. + `PIL`_ is pending deprecation (support to be removed in Django 1.8). + To upgrade, you should **first** uninstall PIL, **then** install Pillow. + +.. _`Pillow`: https://pypi.python.org/pypi/Pillow +.. _`PIL`: https://pypi.python.org/pypi/PIL + Backwards incompatible changes in 1.6 ===================================== diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 5e6adee894..e4b71dba82 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -29,16 +29,10 @@ from django.utils._os import upath from django.test.utils import override_settings from servers.tests import LiveServerBase -# Try to import PIL in either of the two ways it can end up installed. -# Checking for the existence of Image is enough for CPython, but -# for PyPy, you need to check for the underlying modules try: - from PIL import Image, _imaging -except ImportError: - try: - import Image, _imaging - except ImportError: - Image = None + from django.utils.image import Image +except ImproperlyConfigured: + Image = None class GetStorageClassTests(SimpleTestCase): @@ -494,7 +488,7 @@ class DimensionClosingBug(unittest.TestCase): """ Test that get_image_dimensions() properly closes files (#8817) """ - @unittest.skipUnless(Image, "PIL not installed") + @unittest.skipUnless(Image, "Pillow/PIL not installed") def test_not_closing_of_files(self): """ Open files passed into get_image_dimensions() should stay opened. @@ -505,7 +499,7 @@ class DimensionClosingBug(unittest.TestCase): finally: self.assertTrue(not empty_io.closed) - @unittest.skipUnless(Image, "PIL not installed") + @unittest.skipUnless(Image, "Pillow/PIL not installed") def test_closing_of_filenames(self): """ get_image_dimensions() called with a filename should closed the file. @@ -542,7 +536,7 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase): Test that get_image_dimensions() works properly after various calls using a file handler (#11158) """ - @unittest.skipUnless(Image, "PIL not installed") + @unittest.skipUnless(Image, "Pillow/PIL not installed") def test_multiple_calls(self): """ Multiple calls of get_image_dimensions() should return the same size. @@ -556,7 +550,7 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase): self.assertEqual(image_pil.size, size_1) self.assertEqual(size_1, size_2) - @unittest.skipUnless(Image, "PIL not installed") + @unittest.skipUnless(Image, "Pillow/PIL not installed") def test_bug_19457(self): """ Regression test for #19457 diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index c3b2f7fccb..2d602d6412 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -1,17 +1,12 @@ import os import tempfile -# Try to import PIL in either of the two ways it can end up installed. -# Checking for the existence of Image is enough for CPython, but for PyPy, -# you need to check for the underlying modules. +from django.core.exceptions import ImproperlyConfigured try: - from PIL import Image, _imaging -except ImportError: - try: - import Image, _imaging - except ImportError: - Image = None + from django.utils.image import Image +except ImproperlyConfigured: + Image = None from django.core.files.storage import FileSystemStorage from django.db import models @@ -87,7 +82,7 @@ class VerboseNameField(models.Model): field9 = models.FileField("verbose field9", upload_to="unused") field10 = models.FilePathField("verbose field10") field11 = models.FloatField("verbose field11") - # Don't want to depend on PIL in this test + # Don't want to depend on Pillow/PIL in this test #field_image = models.ImageField("verbose field") field12 = models.IntegerField("verbose field12") field13 = models.IPAddressField("verbose field13") @@ -119,7 +114,7 @@ class Document(models.Model): ############################################################################### # ImageField -# If PIL available, do these tests. +# If Pillow/PIL available, do these tests. if Image: class TestImageFieldFile(ImageFieldFile): """ diff --git a/tests/model_fields/test_imagefield.py b/tests/model_fields/test_imagefield.py index df0215db3d..457892ddb8 100644 --- a/tests/model_fields/test_imagefield.py +++ b/tests/model_fields/test_imagefield.py @@ -3,20 +3,24 @@ from __future__ import absolute_import import os import shutil +from django.core.exceptions import ImproperlyConfigured from django.core.files import File from django.core.files.images import ImageFile from django.test import TestCase from django.utils._os import upath from django.utils.unittest import skipIf -from .models import Image +try: + from .models import Image +except ImproperlyConfigured: + Image = None if Image: from .models import (Person, PersonWithHeight, PersonWithHeightAndWidth, PersonDimensionsFirst, PersonTwoImages, TestImageFieldFile) from .models import temp_storage_dir else: - # PIL not available, create dummy classes (tests will be skipped anyway) + # Pillow not available, create dummy classes (tests will be skipped anyway) class Person(): pass PersonWithHeight = PersonWithHeightAndWidth = PersonDimensionsFirst = Person diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index 25c780f1c2..a79d9b8c5b 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -11,6 +11,7 @@ from __future__ import unicode_literals import os import tempfile +from django.core.exceptions import ImproperlyConfigured from django.core.files.storage import FileSystemStorage from django.db import models from django.utils import six @@ -91,14 +92,7 @@ class TextFile(models.Model): return self.description try: - # If PIL is available, try testing ImageFields. Checking for the existence - # of Image is enough for CPython, but for PyPy, you need to check for the - # underlying modules If PIL is not available, ImageField tests are omitted. - # Try to import PIL in either of the two ways it can end up installed. - try: - from PIL import Image, _imaging - except ImportError: - import Image, _imaging + from django.utils.image import Image test_images = True @@ -137,7 +131,7 @@ try: def __str__(self): return self.description -except ImportError: +except ImproperlyConfigured: test_images = False @python_2_unicode_compatible diff --git a/tests/serializers_regress/models.py b/tests/serializers_regress/models.py index 21fe073122..21a3448a8e 100644 --- a/tests/serializers_regress/models.py +++ b/tests/serializers_regress/models.py @@ -2,7 +2,7 @@ A test spanning all the capabilities of all the serializers. This class sets up a model for each model field type -(except for image types, because of the PIL dependency). +(except for image types, because of the Pillow/PIL dependency). """ from django.db import models From 4ecc6da20b00b2d00a8cc158056a10bf0a259d07 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Wed, 15 May 2013 09:00:09 +0200 Subject: [PATCH 21/47] Removed unicode literals from PIL compat. --- django/utils/image.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/django/utils/image.py b/django/utils/image.py index ed9b210973..54c11adfee 100644 --- a/django/utils/image.py +++ b/django/utils/image.py @@ -67,6 +67,8 @@ Approach * ``ImportError`` - Bad install, toss an exception """ +from __future__ import unicode_literals + import warnings from django.core.exceptions import ImproperlyConfigured @@ -100,12 +102,12 @@ def _detect_image_library(): except ImportError as err: # Neither worked, so it's likely not installed. raise ImproperlyConfigured( - _(u"Neither Pillow nor PIL could be imported: %s" % err) + _("Neither Pillow nor PIL could be imported: %s" % err) ) # ``Image.alpha_composite`` was added to Pillow in SHA: e414c6 & is not # available in any version of the PIL. - if hasattr(PILImage, u'alpha_composite'): + if hasattr(PILImage, 'alpha_composite'): PIL_imaging = False else: # We're dealing with the PIL. Determine if we're on CPython & if @@ -114,7 +116,7 @@ def _detect_image_library(): # This is the Alex Approved™ way. # See http://mail.python.org/pipermail//pypy-dev/2011-November/008739.html - if platform.python_implementation().lower() == u'cpython': + if platform.python_implementation().lower() == 'cpython': # We're on CPython (likely 2.x). Since a C compiler is needed to # produce a fully-working PIL & will create a ``_imaging`` module, # we'll attempt to import it to verify their kit works. @@ -122,8 +124,8 @@ def _detect_image_library(): import _imaging as PIL_imaging except ImportError as err: raise ImproperlyConfigured( - _(u"The '_imaging' module for the PIL could not be " + - u"imported: %s" % err) + _("The '_imaging' module for the PIL could not be " + + "imported: %s" % err) ) # Try to import ImageFile as well. From 84d8b247d20aafb376368dc458f277ad2cd71ecd Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 14 May 2013 20:25:45 -0400 Subject: [PATCH 22/47] Fixed #20165 - Updated testing example to use django.test.TestCase. Thanks Lorin Hochstein. --- docs/topics/testing/overview.txt | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/docs/topics/testing/overview.txt b/docs/topics/testing/overview.txt index 2a8b4613a7..fc2b393898 100644 --- a/docs/topics/testing/overview.txt +++ b/docs/topics/testing/overview.txt @@ -46,20 +46,24 @@ module defines tests using a class-based approach. .. _unittest2: http://pypi.python.org/pypi/unittest2 -Here is an example :class:`unittest.TestCase` subclass:: +Here is an example which subclasses from :class:`django.test.TestCase`, +which is a subclass of :class:`unittest.TestCase` that runs each test inside a +transaction to provide isolation:: - from django.utils import unittest + from django.test import TestCase from myapp.models import Animal - class AnimalTestCase(unittest.TestCase): + class AnimalTestCase(TestCase): def setUp(self): - self.lion = Animal(name="lion", sound="roar") - self.cat = Animal(name="cat", sound="meow") + Animal.objects.create(name="lion", sound="roar") + Animal.objects.create(name="cat", sound="meow") def test_animals_can_speak(self): """Animals that can speak are correctly identified""" - self.assertEqual(self.lion.speak(), 'The lion says "roar"') - self.assertEqual(self.cat.speak(), 'The cat says "meow"') + lion = Animal.objects.get(name="lion") + cat = Animal.objects.get(name="cat") + self.assertEqual(lion.speak(), 'The lion says "roar"') + self.assertEqual(cat.speak(), 'The cat says "meow"') When you :ref:`run your tests `, the default behavior of the test utility is to find all the test cases (that is, subclasses of @@ -80,11 +84,11 @@ For more details about :mod:`unittest`, see the Python documentation. be sure to create your test classes as subclasses of :class:`django.test.TestCase` rather than :class:`unittest.TestCase`. - In the example above, we instantiate some models but do not save them to - the database. Using :class:`unittest.TestCase` avoids the cost of running - each test in a transaction and flushing the database, but for most - applications the scope of tests you will be able to write this way will - be fairly limited, so it's easiest to use :class:`django.test.TestCase`. + Using :class:`unittest.TestCase` avoids the cost of running each test in a + transaction and flushing the database, but if your tests interact with + the database their behavior will vary based on the order that the test + runner executes them. This can lead to unit tests that pass when run in + isolation but fail when run in a suite. .. _running-tests: From c8dcee9a423fa3f31a2303e7ec20a924775730a2 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Wed, 15 May 2013 16:43:39 +0200 Subject: [PATCH 23/47] Improved the timezone middleware example slightly. This change avoids having the timezone leak from a request to the next. --- docs/topics/i18n/timezones.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/topics/i18n/timezones.txt b/docs/topics/i18n/timezones.txt index 22a0edb073..e4a043b08f 100644 --- a/docs/topics/i18n/timezones.txt +++ b/docs/topics/i18n/timezones.txt @@ -189,6 +189,8 @@ Add the following middleware to :setting:`MIDDLEWARE_CLASSES`:: tz = request.session.get('django_timezone') if tz: timezone.activate(tz) + else: + timezone.deactivate() Create a view that can set the current timezone:: From f6d1ca56c9d2583c894a44841d109a597ac019bd Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Wed, 15 May 2013 16:45:07 +0200 Subject: [PATCH 24/47] Don't unregister OSMGeoAdmin, other tests rely on it. --- django/contrib/gis/tests/geoadmin/tests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py index 99dcdf0768..124e05b722 100644 --- a/django/contrib/gis/tests/geoadmin/tests.py +++ b/django/contrib/gis/tests/geoadmin/tests.py @@ -30,8 +30,7 @@ class GeoAdminTest(TestCase): result) def test_olmap_WMS_rendering(self): - admin.site.unregister(City) - admin.site.register(City, admin.GeoModelAdmin) + geoadmin = admin.GeoModelAdmin(City, admin.site) geoadmin = admin.site._registry[City] result = geoadmin.get_map_widget(City._meta.get_field('point'))( From ebfb71c64a786620947c9d598fd1ebae2958acff Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Wed, 15 May 2013 16:50:33 +0200 Subject: [PATCH 25/47] Fixed previous commit. (Don't commit from DjangCon!) --- django/contrib/gis/tests/geoadmin/tests.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py index 124e05b722..15874758be 100644 --- a/django/contrib/gis/tests/geoadmin/tests.py +++ b/django/contrib/gis/tests/geoadmin/tests.py @@ -31,8 +31,6 @@ class GeoAdminTest(TestCase): def test_olmap_WMS_rendering(self): geoadmin = admin.GeoModelAdmin(City, admin.site) - - geoadmin = admin.site._registry[City] result = geoadmin.get_map_widget(City._meta.get_field('point'))( ).render('point', Point(-79.460734, 40.18476)) self.assertIn( From 74f3884ae04ea57baee9b04ab0b5658a97cfd296 Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Mon, 13 May 2013 12:40:39 -0700 Subject: [PATCH 26/47] Fixed #20413 - Respect Query.get_meta() --- django/contrib/gis/db/models/sql/compiler.py | 4 ++-- django/db/models/query.py | 2 +- django/db/models/sql/compiler.py | 24 ++++++++++---------- django/db/models/sql/query.py | 10 ++++---- django/db/models/sql/subqueries.py | 10 ++++---- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/django/contrib/gis/db/models/sql/compiler.py b/django/contrib/gis/db/models/sql/compiler.py index b488f59362..1f81dbd376 100644 --- a/django/contrib/gis/db/models/sql/compiler.py +++ b/django/contrib/gis/db/models/sql/compiler.py @@ -121,7 +121,7 @@ class GeoSQLCompiler(compiler.SQLCompiler): """ result = [] if opts is None: - opts = self.query.model._meta + opts = self.get_meta() aliases = set() only_load = self.deferred_to_columns() seen = self.query.included_inherited_models.copy() @@ -247,7 +247,7 @@ class GeoSQLCompiler(compiler.SQLCompiler): used. If `column` is specified, it will be used instead of the value in `field.column`. """ - if table_alias is None: table_alias = self.query.model._meta.db_table + if table_alias is None: table_alias = self.get_meta().db_table return "%s.%s" % (self.quote_name_unless_alias(table_alias), self.connection.ops.quote_name(column or field.column)) diff --git a/django/db/models/query.py b/django/db/models/query.py index 337049e2ff..d3763d3934 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -869,7 +869,7 @@ class QuerySet(object): """ if self.query.extra_order_by or self.query.order_by: return True - elif self.query.default_ordering and self.query.model._meta.ordering: + elif self.query.default_ordering and self.query.get_meta().ordering: return True else: return False diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 018fc098ea..7b7c430420 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -32,7 +32,7 @@ class SQLCompiler(object): # cleaned. We are not using a clone() of the query here. """ if not self.query.tables: - self.query.join((None, self.query.model._meta.db_table, None)) + self.query.join((None, self.query.get_meta().db_table, None)) if (not self.query.select and self.query.default_cols and not self.query.included_inherited_models): self.query.setup_inherited_models() @@ -260,7 +260,7 @@ class SQLCompiler(object): """ result = [] if opts is None: - opts = self.query.model._meta + opts = self.query.get_meta() qn = self.quote_name_unless_alias qn2 = self.connection.ops.quote_name aliases = set() @@ -309,7 +309,7 @@ class SQLCompiler(object): qn = self.quote_name_unless_alias qn2 = self.connection.ops.quote_name result = [] - opts = self.query.model._meta + opts = self.query.get_meta() for name in self.query.distinct_fields: parts = name.split(LOOKUP_SEP) @@ -338,7 +338,7 @@ class SQLCompiler(object): ordering = self.query.order_by else: ordering = (self.query.order_by - or self.query.model._meta.ordering + or self.query.get_meta().ordering or []) qn = self.quote_name_unless_alias qn2 = self.connection.ops.quote_name @@ -388,7 +388,7 @@ class SQLCompiler(object): # 'col' is of the form 'field' or 'field1__field2' or # '-field1__field2__field', etc. for table, cols, order in self.find_ordering_name(field, - self.query.model._meta, default_order=asc): + self.query.get_meta(), default_order=asc): for col in cols: if (table, col) not in processed_pairs: elt = '%s.%s' % (qn(table), qn2(col)) @@ -556,10 +556,10 @@ class SQLCompiler(object): select_cols = self.query.select + self.query.related_select_cols # Just the column, not the fields. select_cols = [s[0] for s in select_cols] - if (len(self.query.model._meta.concrete_fields) == len(self.query.select) + if (len(self.query.get_meta().concrete_fields) == len(self.query.select) and self.connection.features.allows_group_by_pk): self.query.group_by = [ - (self.query.model._meta.db_table, self.query.model._meta.pk.column) + (self.query.get_meta().db_table, self.query.get_meta().pk.column) ] select_cols = [] seen = set() @@ -716,14 +716,14 @@ class SQLCompiler(object): if self.query.select: fields = [f.field for f in self.query.select] else: - fields = self.query.model._meta.concrete_fields + fields = self.query.get_meta().concrete_fields fields = fields + [f.field for f in self.query.related_select_cols] # If the field was deferred, exclude it from being passed # into `resolve_columns` because it wasn't selected. only_load = self.deferred_to_columns() if only_load: - db_table = self.query.model._meta.db_table + db_table = self.query.get_meta().db_table fields = [f for f in fields if db_table in only_load and f.column in only_load[db_table]] row = self.resolve_columns(row, fields) @@ -825,7 +825,7 @@ class SQLInsertCompiler(SQLCompiler): # We don't need quote_name_unless_alias() here, since these are all # going to be column names (so we can avoid the extra overhead). qn = self.connection.ops.quote_name - opts = self.query.model._meta + opts = self.query.get_meta() result = ['INSERT INTO %s' % qn(opts.db_table)] has_fields = bool(self.query.fields) @@ -887,7 +887,7 @@ class SQLInsertCompiler(SQLCompiler): if self.connection.features.can_return_id_from_insert: return self.connection.ops.fetch_returned_insert_id(cursor) return self.connection.ops.last_insert_id(cursor, - self.query.model._meta.db_table, self.query.model._meta.pk.column) + self.query.get_meta().db_table, self.query.get_meta().pk.column) class SQLDeleteCompiler(SQLCompiler): @@ -992,7 +992,7 @@ class SQLUpdateCompiler(SQLCompiler): query.bump_prefix() query.extra = {} query.select = [] - query.add_fields([query.model._meta.pk.name]) + query.add_fields([query.get_meta().pk.name]) # Recheck the count - it is possible that fiddling with the select # fields above removes tables from the query. Refs #18304. count = query.count_active_tables() diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index acb9f248d6..6db2bf6e12 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -552,7 +552,7 @@ class Query(object): field_names, defer = self.deferred_loading if not field_names: return - orig_opts = self.model._meta + orig_opts = self.get_meta() seen = {} must_include = {orig_opts.concrete_model: set([orig_opts.pk])} for field_name in field_names: @@ -818,7 +818,7 @@ class Query(object): alias = self.tables[0] self.ref_alias(alias) else: - alias = self.join((None, self.model._meta.db_table, None)) + alias = self.join((None, self.get_meta().db_table, None)) return alias def count_active_tables(self): @@ -906,7 +906,7 @@ class Query(object): whereas column determination is a later part, and side-effect, of as_sql()). """ - opts = self.model._meta + opts = self.get_meta() root_alias = self.tables[0] seen = {None: root_alias} @@ -1624,7 +1624,7 @@ class Query(object): "Cannot add count col with multiple cols in 'select': %r" % self.select count = self.aggregates_module.Count(self.select[0].col) else: - opts = self.model._meta + opts = self.get_meta() if not self.select: count = self.aggregates_module.Count( (self.join((None, opts.db_table, None)), opts.pk.column), @@ -1732,7 +1732,7 @@ class Query(object): field_names = set(field_names) if 'pk' in field_names: field_names.remove('pk') - field_names.add(self.model._meta.pk.name) + field_names.add(self.get_meta().pk.name) if defer: # Remove any existing deferred names from the current set before diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index 78727e394a..fae42b4be2 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -41,12 +41,12 @@ class DeleteQuery(Query): lot of values in pk_list. """ if not field: - field = self.model._meta.pk + field = self.get_meta().pk for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): where = self.where_class() where.add((Constraint(None, field.column, field), 'in', pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]), AND) - self.do_query(self.model._meta.db_table, where, using=using) + self.do_query(self.get_meta().db_table, where, using=using) def delete_qs(self, query, using): """ @@ -112,7 +112,7 @@ class UpdateQuery(Query): related_updates=self.related_updates.copy(), **kwargs) def update_batch(self, pk_list, values, using): - pk_field = self.model._meta.pk + pk_field = self.get_meta().pk self.add_update_values(values) for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): self.where = self.where_class() @@ -129,7 +129,7 @@ class UpdateQuery(Query): """ values_seq = [] for name, val in six.iteritems(values): - field, model, direct, m2m = self.model._meta.get_field_by_name(name) + field, model, direct, m2m = self.get_meta().get_field_by_name(name) if not direct or m2m: raise FieldError('Cannot update model field %r (only non-relations and foreign keys permitted).' % field) if model: @@ -236,7 +236,7 @@ class DateQuery(Query): ) except FieldError: raise FieldDoesNotExist("%s has no field named '%s'" % ( - self.model._meta.object_name, field_name + self.get_meta().object_name, field_name )) field = result[0] self._check_field(field) # overridden in DateTimeQuery From 31887751746088d99b2f607f929c0c4a02978740 Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Wed, 15 May 2013 14:07:34 -0700 Subject: [PATCH 27/47] Fix bug introduced in contrib.gis in 74f3884ae0 --- django/contrib/gis/db/models/sql/compiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django/contrib/gis/db/models/sql/compiler.py b/django/contrib/gis/db/models/sql/compiler.py index 1f81dbd376..b2befd44b2 100644 --- a/django/contrib/gis/db/models/sql/compiler.py +++ b/django/contrib/gis/db/models/sql/compiler.py @@ -121,7 +121,7 @@ class GeoSQLCompiler(compiler.SQLCompiler): """ result = [] if opts is None: - opts = self.get_meta() + opts = self.query.get_meta() aliases = set() only_load = self.deferred_to_columns() seen = self.query.included_inherited_models.copy() @@ -247,7 +247,7 @@ class GeoSQLCompiler(compiler.SQLCompiler): used. If `column` is specified, it will be used instead of the value in `field.column`. """ - if table_alias is None: table_alias = self.get_meta().db_table + if table_alias is None: table_alias = self.query.get_meta().db_table return "%s.%s" % (self.quote_name_unless_alias(table_alias), self.connection.ops.quote_name(column or field.column)) From 746d3166d6fec63795ba7a3805fc2fa022367810 Mon Sep 17 00:00:00 2001 From: Stefan hr Berder Date: Thu, 16 May 2013 15:21:18 +0800 Subject: [PATCH 28/47] small typo, example pk=27 but description pk=24 --- docs/topics/serialization.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/topics/serialization.txt b/docs/topics/serialization.txt index cb34117997..e88e16029e 100644 --- a/docs/topics/serialization.txt +++ b/docs/topics/serialization.txt @@ -203,7 +203,7 @@ Foreign keys and other relational fields are treated a little bit differently:: -In this example we specify that the auth.Permission object with the PK 24 has +In this example we specify that the auth.Permission object with the PK 27 has a foreign key to the contenttypes.ContentType instance with the PK 9. ManyToMany-relations are exported for the model that binds them. For instance, From cde820bd31e810d61cc2c9b09cc2ec7d5ddbf165 Mon Sep 17 00:00:00 2001 From: Marc Tamlyn Date: Thu, 16 May 2013 10:06:42 +0200 Subject: [PATCH 29/47] Make the example usage of import_by_path less confusing. Using import_by_path to import import_by_path is a really odd use case and makes the code example difficult to read. --- docs/ref/utils.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index a7d5b6690e..14ae9aa9b8 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -659,11 +659,11 @@ Functions for working with Python modules. wrong. For example:: from django.utils.module_loading import import_by_path - import_by_path = import_by_path('django.utils.module_loading.import_by_path') + ImproperlyConfigured = import_by_path('django.core.exceptions.ImproperlyConfigured') is equivalent to:: - from django.utils.module_loading import import_by_path + from django.core.exceptions import ImproperlyConfigured ``django.utils.safestring`` =========================== From 0a29057ebea4335d503e373e4526b1c62c15bcdc Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 16 May 2013 13:48:38 +0200 Subject: [PATCH 30/47] Fixed #20415 -- Ensured srid is not localized in openlayers template Thanks pierremarc07 at gmail.com for the report. --- django/contrib/gis/templates/gis/admin/openlayers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/contrib/gis/templates/gis/admin/openlayers.js b/django/contrib/gis/templates/gis/admin/openlayers.js index 924621ea49..4425fee27e 100644 --- a/django/contrib/gis/templates/gis/admin/openlayers.js +++ b/django/contrib/gis/templates/gis/admin/openlayers.js @@ -12,7 +12,7 @@ OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857", OpenLayers.Layer.Sp {{ module }}.is_point = {{ is_point|yesno:"true,false" }}; {% endblock %} {{ module }}.get_ewkt = function(feat){ - return 'SRID={{ srid }};' + {{ module }}.wkt_f.write(feat); + return 'SRID={{ srid|unlocalize }};' + {{ module }}.wkt_f.write(feat); }; {{ module }}.read_wkt = function(wkt){ // OpenLayers cannot handle EWKT -- we make sure to strip it out. From b5b63079bd758c4492ab6b6935edec3346016f25 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 16 May 2013 05:55:21 -0400 Subject: [PATCH 31/47] Fixed #19712 - Clarified admindocs regarding which attributes appear. Thanks Daniele Procida for the report. --- docs/ref/contrib/admin/admindocs.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/ref/contrib/admin/admindocs.txt b/docs/ref/contrib/admin/admindocs.txt index b3e26eca48..394d078e5b 100644 --- a/docs/ref/contrib/admin/admindocs.txt +++ b/docs/ref/contrib/admin/admindocs.txt @@ -57,9 +57,10 @@ Model reference =============== The **models** section of the ``admindocs`` page describes each model in the -system along with all the fields and methods available on it. Relationships to -other models appear as hyperlinks. Descriptions are pulled from ``help_text`` -attributes on fields or from docstrings on model methods. +system along with all the fields and methods (without any arguments) available +on it. While model properties don't have any arguments, they are not listed. +Relationships to other models appear as hyperlinks. Descriptions are pulled +from ``help_text`` attributes on fields or from docstrings on model methods. A model with useful documentation might look like this:: From 79715f267c7b301dc246b35182ede3e269a15aa2 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 16 May 2013 08:37:20 -0400 Subject: [PATCH 32/47] Fixed misleading heading on admindocs models page; refs #19712. --- django/contrib/admindocs/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py index 6b1bea15f3..348727eb21 100644 --- a/django/contrib/admindocs/views.py +++ b/django/contrib/admindocs/views.py @@ -267,7 +267,7 @@ def model_detail(request, app_label, model_name): return render_to_response('admin_doc/model_detail.html', { 'root_path': urlresolvers.reverse('admin:index'), 'name': '%s.%s' % (opts.app_label, opts.object_name), - 'summary': _("Fields on %s objects") % opts.object_name, + 'summary': _("Attributes on %s objects") % opts.object_name, 'description': model.__doc__, 'fields': fields, }, context_instance=RequestContext(request)) From 9ef4d177d1d501be44b6e1c87197fef1e26900f1 Mon Sep 17 00:00:00 2001 From: Shai Berger Date: Thu, 16 May 2013 15:01:39 +0300 Subject: [PATCH 33/47] Fixed #20388 -- Test failures under Oracle. Add "FROM DUAL" to SQL selecting constants in tests for Oracle. --- tests/backends/tests.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/backends/tests.py b/tests/backends/tests.py index a426926cef..81b2851403 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -367,14 +367,19 @@ class EscapingChecks(TestCase): All tests in this test case are also run with settings.DEBUG=True in EscapingChecksDebug test case, to also test CursorDebugWrapper. """ + + # For Oracle, when you want to select a value, you need to specify the + # special pseudo-table 'dual'; a select with no from clause is invalid. + bare_select_suffix = " FROM DUAL" if connection.vendor == 'oracle' else "" + def test_paramless_no_escaping(self): cursor = connection.cursor() - cursor.execute("SELECT '%s'") + cursor.execute("SELECT '%s'" + self.bare_select_suffix) self.assertEqual(cursor.fetchall()[0][0], '%s') def test_parameter_escaping(self): cursor = connection.cursor() - cursor.execute("SELECT '%%', %s", ('%d',)) + cursor.execute("SELECT '%%', %s" + self.bare_select_suffix, ('%d',)) self.assertEqual(cursor.fetchall()[0], ('%', '%d')) @unittest.skipUnless(connection.vendor == 'sqlite', From 289afc3156356a60bb7d2dcb7084add0aa03b2df Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 16 May 2013 09:56:30 -0400 Subject: [PATCH 34/47] Fixed #20421 - Typo in class-based view docs. --- docs/ref/class-based-views/base.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ref/class-based-views/base.txt b/docs/ref/class-based-views/base.txt index 3ba7c38c43..ee0bf0f225 100644 --- a/docs/ref/class-based-views/base.txt +++ b/docs/ref/class-based-views/base.txt @@ -9,7 +9,7 @@ required for projects, in which case there are Mixins and Generic class-based views. Many of Django's built-in class-based views inherit from other class-based -views or various mixins. Because this inheritence chain is very important, the +views or various mixins. Because this inheritance chain is very important, the ancestor classes are documented under the section title of **Ancestors (MRO)**. MRO is an acronym for Method Resolution Order. From 0732c8e8c6e156d4d9a4a1cc02d631fe41342bf8 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Mon, 6 May 2013 18:45:56 +0800 Subject: [PATCH 35/47] Fixed #20357 -- Allow empty username field label in `AuthentificationForm`. --- django/contrib/auth/forms.py | 2 +- django/contrib/auth/tests/test_forms.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index e44e7a703e..0e08d8ef31 100644 --- a/django/contrib/auth/forms.py +++ b/django/contrib/auth/forms.py @@ -171,7 +171,7 @@ class AuthenticationForm(forms.Form): # Set the label for the "username" field. UserModel = get_user_model() self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD) - if not self.fields['username'].label: + if self.fields['username'].label is None: self.fields['username'].label = capfirst(self.username_field.verbose_name) def clean(self): diff --git a/django/contrib/auth/tests/test_forms.py b/django/contrib/auth/tests/test_forms.py index 2c8f6e4faf..0b998105af 100644 --- a/django/contrib/auth/tests/test_forms.py +++ b/django/contrib/auth/tests/test_forms.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals import os + +from django.contrib.auth import get_user_model from django.contrib.auth.models import User from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm, PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm, @@ -13,6 +15,7 @@ from django.test.utils import override_settings from django.utils.encoding import force_text from django.utils._os import upath from django.utils import translation +from django.utils.text import capfirst from django.utils.translation import ugettext as _ @@ -146,6 +149,24 @@ class AuthenticationFormTest(TestCase): form = CustomAuthenticationForm() self.assertEqual(form['username'].label, "Name") + def test_username_field_label_not_set(self): + + class CustomAuthenticationForm(AuthenticationForm): + username = CharField() + + form = CustomAuthenticationForm() + UserModel = get_user_model() + username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD) + self.assertEqual(form.fields['username'].label, capfirst(username_field.verbose_name)) + + def test_username_field_label_empty_string(self): + + class CustomAuthenticationForm(AuthenticationForm): + username = CharField(label='') + + form = CustomAuthenticationForm() + self.assertEqual(form.fields['username'].label, "") + @skipIfCustomUser @override_settings(USE_TZ=False, PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) From 1bc6e18b4e8e35a0a2d28cff8a0eb88bb99ee0e8 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Thu, 16 May 2013 10:36:50 -0700 Subject: [PATCH 36/47] Updated my bio. --- docs/internals/committers.txt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/internals/committers.txt b/docs/internals/committers.txt index 5886c0b6b0..22dd86c260 100644 --- a/docs/internals/committers.txt +++ b/docs/internals/committers.txt @@ -30,13 +30,10 @@ Journal-World`_ of Lawrence, Kansas, USA. Simon lives in Brighton, England. `Jacob Kaplan-Moss`_ - Jacob is a partner at `Revolution Systems`_ which provides support services - around Django and related open source technologies. A good deal of Jacob's - work time is devoted to working on Django. Jacob previously worked at World - Online, where Django was invented, where he was the lead developer of - Ellington, a commercial Web publishing platform for media companies. - - Jacob lives in Lawrence, Kansas, USA. + Jacob is Director of Platform Security at Heroku_. He worked at World + Online for four years, where he helped open source Django and found + the Django Software Foundation. Jacob lives on a hobby farm outside of + Lawrence where he spends his weekends playing with dirt and power tools. `Wilson Miner`_ Wilson's design-fu is what makes Django look so nice. He designed the @@ -55,6 +52,7 @@ Journal-World`_ of Lawrence, Kansas, USA. .. _jacob kaplan-moss: http://jacobian.org/ .. _revolution systems: http://revsys.com/ .. _wilson miner: http://wilsonminer.com/ +.. _heroku: http://heroku.com/ Current developers ================== From d4d114563280b6e202b8459be0aa471621c7b563 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Thu, 16 May 2013 10:37:48 -0700 Subject: [PATCH 37/47] Removed "specialists". The distinction's not really one we make any more, so updated the docs accordingly. --- docs/internals/committers.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/internals/committers.txt b/docs/internals/committers.txt index 22dd86c260..06562ca777 100644 --- a/docs/internals/committers.txt +++ b/docs/internals/committers.txt @@ -453,9 +453,6 @@ Jeremy Dunck .. _`Daniel Lindsley`: http://toastdriven.com/ .. _`Amazon Web Services`: https://aws.amazon.com/ -Specialists ------------ - `James Bennett`_ James is Django's release manager, and also contributes to the documentation and provide the occasional bugfix. From b16b72d415808073da0418de93bf32f71ead959d Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 16 Mar 2013 11:39:18 +0100 Subject: [PATCH 38/47] Fixed #5472 --Added OpenLayers-based widgets in contrib.gis Largely inspired from django-floppyforms. Designed to not depend on OpenLayers at code level. --- django/contrib/gis/db/models/fields.py | 13 +- django/contrib/gis/forms/__init__.py | 5 +- django/contrib/gis/forms/fields.py | 35 +- django/contrib/gis/forms/widgets.py | 112 ++++++ .../contrib/gis/static/gis/js/OLMapWidget.js | 371 ++++++++++++++++++ .../gis/templates/gis/openlayers-osm.html | 17 + .../contrib/gis/templates/gis/openlayers.html | 34 ++ django/contrib/gis/tests/test_geoforms.py | 189 ++++++++- docs/ref/contrib/gis/forms-api.txt | 165 ++++++++ docs/ref/contrib/gis/index.txt | 1 + docs/ref/forms/fields.txt | 2 + docs/releases/1.6.txt | 7 + 12 files changed, 931 insertions(+), 20 deletions(-) create mode 100644 django/contrib/gis/forms/widgets.py create mode 100644 django/contrib/gis/static/gis/js/OLMapWidget.js create mode 100644 django/contrib/gis/templates/gis/openlayers-osm.html create mode 100644 django/contrib/gis/templates/gis/openlayers.html create mode 100644 docs/ref/contrib/gis/forms-api.txt diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py index 249617f771..2e221b7477 100644 --- a/django/contrib/gis/db/models/fields.py +++ b/django/contrib/gis/db/models/fields.py @@ -44,6 +44,7 @@ class GeometryField(Field): # The OpenGIS Geometry name. geom_type = 'GEOMETRY' + form_class = forms.GeometryField # Geodetic units. geodetic_units = ('Decimal Degree', 'degree') @@ -201,11 +202,14 @@ class GeometryField(Field): return connection.ops.geo_db_type(self) def formfield(self, **kwargs): - defaults = {'form_class' : forms.GeometryField, + defaults = {'form_class' : self.form_class, 'geom_type' : self.geom_type, 'srid' : self.srid, } defaults.update(kwargs) + if (self.dim > 2 and not 'widget' in kwargs and + not getattr(defaults['form_class'].widget, 'supports_3d', False)): + defaults['widget'] = forms.Textarea return super(GeometryField, self).formfield(**defaults) def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): @@ -267,28 +271,35 @@ class GeometryField(Field): # The OpenGIS Geometry Type Fields class PointField(GeometryField): geom_type = 'POINT' + form_class = forms.PointField description = _("Point") class LineStringField(GeometryField): geom_type = 'LINESTRING' + form_class = forms.LineStringField description = _("Line string") class PolygonField(GeometryField): geom_type = 'POLYGON' + form_class = forms.PolygonField description = _("Polygon") class MultiPointField(GeometryField): geom_type = 'MULTIPOINT' + form_class = forms.MultiPointField description = _("Multi-point") class MultiLineStringField(GeometryField): geom_type = 'MULTILINESTRING' + form_class = forms.MultiLineStringField description = _("Multi-line string") class MultiPolygonField(GeometryField): geom_type = 'MULTIPOLYGON' + form_class = forms.MultiPolygonField description = _("Multi polygon") class GeometryCollectionField(GeometryField): geom_type = 'GEOMETRYCOLLECTION' + form_class = forms.GeometryCollectionField description = _("Geometry collection") diff --git a/django/contrib/gis/forms/__init__.py b/django/contrib/gis/forms/__init__.py index 82971da6be..93a2d3847b 100644 --- a/django/contrib/gis/forms/__init__.py +++ b/django/contrib/gis/forms/__init__.py @@ -1,2 +1,5 @@ from django.forms import * -from django.contrib.gis.forms.fields import GeometryField +from .fields import (GeometryField, GeometryCollectionField, PointField, + MultiPointField, LineStringField, MultiLineStringField, PolygonField, + MultiPolygonField) +from .widgets import BaseGeometryWidget, OpenLayersWidget, OSMWidget diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py index d3feac83e7..6e2cbd59f5 100644 --- a/django/contrib/gis/forms/fields.py +++ b/django/contrib/gis/forms/fields.py @@ -9,6 +9,7 @@ from django.utils.translation import ugettext_lazy as _ # While this couples the geographic forms to the GEOS library, # it decouples from database (by not importing SpatialBackend). from django.contrib.gis.geos import GEOSException, GEOSGeometry, fromstr +from .widgets import OpenLayersWidget class GeometryField(forms.Field): @@ -17,7 +18,8 @@ class GeometryField(forms.Field): accepted by GEOSGeometry is accepted by this form. By default, this includes WKT, HEXEWKB, WKB (in a buffer), and GeoJSON. """ - widget = forms.Textarea + widget = OpenLayersWidget + geom_type = 'GEOMETRY' default_error_messages = { 'required' : _('No geometry value provided.'), @@ -31,12 +33,13 @@ class GeometryField(forms.Field): # Pop out attributes from the database field, or use sensible # defaults (e.g., allow None). self.srid = kwargs.pop('srid', None) - self.geom_type = kwargs.pop('geom_type', 'GEOMETRY') + self.geom_type = kwargs.pop('geom_type', self.geom_type) if 'null' in kwargs: kwargs.pop('null', True) warnings.warn("Passing 'null' keyword argument to GeometryField is deprecated.", DeprecationWarning, stacklevel=2) super(GeometryField, self).__init__(**kwargs) + self.widget.attrs['geom_type'] = self.geom_type def to_python(self, value): """ @@ -98,3 +101,31 @@ class GeometryField(forms.Field): else: # Check for change of state of existence return bool(initial) != bool(data) + + +class GeometryCollectionField(GeometryField): + geom_type = 'GEOMETRYCOLLECTION' + + +class PointField(GeometryField): + geom_type = 'POINT' + + +class MultiPointField(GeometryField): + geom_type = 'MULTIPOINT' + + +class LineStringField(GeometryField): + geom_type = 'LINESTRING' + + +class MultiLineStringField(GeometryField): + geom_type = 'MULTILINESTRING' + + +class PolygonField(GeometryField): + geom_type = 'POLYGON' + + +class MultiPolygonField(GeometryField): + geom_type = 'MULTIPOLYGON' diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py new file mode 100644 index 0000000000..d50c7c005a --- /dev/null +++ b/django/contrib/gis/forms/widgets.py @@ -0,0 +1,112 @@ +from __future__ import unicode_literals + +import logging + +from django.conf import settings +from django.contrib.gis import gdal +from django.contrib.gis.geos import GEOSGeometry, GEOSException +from django.forms.widgets import Widget +from django.template import loader +from django.utils import six +from django.utils import translation + +logger = logging.getLogger('django.contrib.gis') + + +class BaseGeometryWidget(Widget): + """ + The base class for rich geometry widgets. + Renders a map using the WKT of the geometry. + """ + geom_type = 'GEOMETRY' + map_srid = 4326 + map_width = 600 + map_height = 400 + display_wkt = False + + supports_3d = False + template_name = '' # set on subclasses + + def __init__(self, attrs=None): + self.attrs = {} + for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_wkt'): + self.attrs[key] = getattr(self, key) + if attrs: + self.attrs.update(attrs) + + def render(self, name, value, attrs=None): + # If a string reaches here (via a validation error on another + # field) then just reconstruct the Geometry. + if isinstance(value, six.string_types): + try: + value = GEOSGeometry(value) + except (GEOSException, ValueError) as err: + logger.error( + "Error creating geometry from value '%s' (%s)" % ( + value, err) + ) + value = None + + wkt = '' + if value: + # Check that srid of value and map match + if value.srid != self.map_srid: + try: + ogr = value.ogr + ogr.transform(self.map_srid) + wkt = ogr.wkt + except gdal.OGRException as err: + logger.error( + "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( + value.srid, self.map_srid, err) + ) + else: + wkt = value.wkt + + context = self.build_attrs(attrs, + name=name, + module='geodjango_%s' % name.replace('-','_'), # JS-safe + wkt=wkt, + geom_type=gdal.OGRGeomType(self.attrs['geom_type']), + STATIC_URL=settings.STATIC_URL, + LANGUAGE_BIDI=translation.get_language_bidi(), + ) + return loader.render_to_string(self.template_name, context) + + +class OpenLayersWidget(BaseGeometryWidget): + template_name = 'gis/openlayers.html' + class Media: + js = ( + 'http://openlayers.org/api/2.11/OpenLayers.js', + 'gis/js/OLMapWidget.js', + ) + + +class OSMWidget(BaseGeometryWidget): + """ + An OpenLayers/OpenStreetMap-based widget. + """ + template_name = 'gis/openlayers-osm.html' + default_lon = 5 + default_lat = 47 + + class Media: + js = ( + 'http://openlayers.org/api/2.11/OpenLayers.js', + 'http://www.openstreetmap.org/openlayers/OpenStreetMap.js', + 'gis/js/OLMapWidget.js', + ) + + @property + def map_srid(self): + # Use the official spherical mercator projection SRID on versions + # of GDAL that support it; otherwise, fallback to 900913. + if gdal.HAS_GDAL and gdal.GDAL_VERSION >= (1, 7): + return 3857 + else: + return 900913 + + def render(self, name, value, attrs=None): + return super(self, OSMWidget).render(name, value, + {'default_lon': self.default_lon, 'default_lat': self.default_lat}) diff --git a/django/contrib/gis/static/gis/js/OLMapWidget.js b/django/contrib/gis/static/gis/js/OLMapWidget.js new file mode 100644 index 0000000000..252196b369 --- /dev/null +++ b/django/contrib/gis/static/gis/js/OLMapWidget.js @@ -0,0 +1,371 @@ +(function() { +/** + * Transforms an array of features to a single feature with the merged + * geometry of geom_type + */ +OpenLayers.Util.properFeatures = function(features, geom_type) { + if (features.constructor == Array) { + var geoms = []; + for (var i=0; i + */ + +OpenLayers.Format.DjangoWKT = OpenLayers.Class(OpenLayers.Format.WKT, { + initialize: function(options) { + OpenLayers.Format.WKT.prototype.initialize.apply(this, [options]); + this.regExes.justComma = /\s*,\s*/; + }, + + parse: { + 'point': function(str) { + var coords = OpenLayers.String.trim(str).split(this.regExes.spaces); + return new OpenLayers.Feature.Vector( + new OpenLayers.Geometry.Point(coords[0], coords[1]) + ); + }, + + 'multipoint': function(str) { + var point; + var points = OpenLayers.String.trim(str).split(this.regExes.justComma); + var components = []; + for(var i=0, len=points.length; i0) { + pieces.push(','); + } + pieces.push(this.extractGeometry(collection[i])); + } + pieces.push(')'); + } else { + pieces.push(this.extractGeometry(features.geometry)); + } + return pieces.join(''); + }, + + CLASS_NAME: "OpenLayers.Format.DjangoWKT" +}); + +function MapWidget(options) { + this.map = null; + this.controls = null; + this.panel = null; + this.layers = {}; + this.wkt_f = new OpenLayers.Format.DjangoWKT(); + + // Mapping from OGRGeomType name to OpenLayers.Geometry name + if (options['geom_name'] == 'Unknown') options['geom_type'] = OpenLayers.Geometry; + else if (options['geom_name'] == 'GeometryCollection') options['geom_type'] = OpenLayers.Geometry.Collection; + else options['geom_type'] = eval('OpenLayers.Geometry' + options['geom_name']); + + // Default options + this.options = { + color: 'ee9900', + default_lat: 0, + default_lon: 0, + default_zoom: 4, + is_collection: options['geom_type'] instanceof OpenLayers.Geometry.Collection, + layerswitcher: false, + map_options: {}, + map_srid: 4326, + modifiable: true, + mouse_position: false, + opacity: 0.4, + point_zoom: 12, + scale_text: false, + scrollable: true + }; + + // Altering using user-provied options + for (var property in options) { + if (options.hasOwnProperty(property)) { + this.options[property] = options[property]; + } + } + + this.map = new OpenLayers.Map(this.options.map_id, this.options.map_options); + if (this.options.base_layer) this.layers.base = this.options.base_layer; + else this.layers.base = new OpenLayers.Layer.WMS('OpenLayers WMS', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers: 'basic'}); + this.map.addLayer(this.layers.base); + + var defaults_style = { + 'fillColor': '#' + this.options.color, + 'fillOpacity': this.options.opacity, + 'strokeColor': '#' + this.options.color, + }; + if (this.options.geom_name == 'LineString') { + defaults_style['strokeWidth'] = 3; + } + var styleMap = new OpenLayers.StyleMap({'default': OpenLayers.Util.applyDefaults(defaults_style, OpenLayers.Feature.Vector.style['default'])}); + this.layers.vector = new OpenLayers.Layer.Vector(" " + this.options.name, {styleMap: styleMap}); + this.map.addLayer(this.layers.vector); + wkt = document.getElementById(this.options.id).value; + if (wkt) { + var feat = OpenLayers.Util.properFeatures(this.read_wkt(wkt), this.options.geom_type); + this.write_wkt(feat); + if (this.options.is_collection) { + for (var i=0; i 1) { + old_feats = [this.layers.vector.features[0]]; + this.layers.vector.removeFeatures(old_feats); + this.layers.vector.destroyFeatures(old_feats); + } + this.write_wkt(event.feature); + } +}; + +MapWidget.prototype.modify_wkt = function(event) { + if (this.options.is_collection) { + if (this.options.geom_name == 'MultiPoint') { + this.add_wkt(event); + return; + } else { + var feat = new OpenLayers.Feature.Vector(new this.options.geom_type()); + for (var i=0; i{% block map_css %} + #{{ id }}_map { width: {{ map_width }}px; height: {{ map_height }}px; } + #{{ id }}_map .aligned label { float: inherit; } + #{{ id }}_div_map { position: relative; vertical-align: top; float: {{ LANGUAGE_BIDI|yesno:"right,left" }}; } + {% if not display_wkt %}#{{ id }} { display: none; }{% endif %} + .olControlEditingToolbar .olControlModifyFeatureItemActive { + background-image: url("{{ STATIC_URL }}admin/img/gis/move_vertex_on.png"); + background-repeat: no-repeat; + } + .olControlEditingToolbar .olControlModifyFeatureItemInactive { + background-image: url("{{ STATIC_URL }}admin/img/gis/move_vertex_off.png"); + background-repeat: no-repeat; + }{% endblock %} + + +
+
+ Delete all Features + {% if display_wkt %}

WKT debugging window:

{% endif %} + + +
diff --git a/django/contrib/gis/tests/test_geoforms.py b/django/contrib/gis/tests/test_geoforms.py index 24bb50c6bc..402d9b944b 100644 --- a/django/contrib/gis/tests/test_geoforms.py +++ b/django/contrib/gis/tests/test_geoforms.py @@ -1,24 +1,25 @@ from django.forms import ValidationError from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.tests.utils import HAS_SPATIALREFSYS +from django.test import SimpleTestCase from django.utils import six -from django.utils import unittest +from django.utils.unittest import skipUnless if HAS_SPATIALREFSYS: from django.contrib.gis import forms from django.contrib.gis.geos import GEOSGeometry -@unittest.skipUnless(HAS_GDAL and HAS_SPATIALREFSYS, "GeometryFieldTest needs gdal support and a spatial database") -class GeometryFieldTest(unittest.TestCase): +@skipUnless(HAS_GDAL and HAS_SPATIALREFSYS, "GeometryFieldTest needs gdal support and a spatial database") +class GeometryFieldTest(SimpleTestCase): - def test00_init(self): + def test_init(self): "Testing GeometryField initialization with defaults." fld = forms.GeometryField() for bad_default in ('blah', 3, 'FoO', None, 0): self.assertRaises(ValidationError, fld.clean, bad_default) - def test01_srid(self): + def test_srid(self): "Testing GeometryField with a SRID set." # Input that doesn't specify the SRID is assumed to be in the SRID # of the input field. @@ -34,7 +35,7 @@ class GeometryFieldTest(unittest.TestCase): cleaned_geom = fld.clean('SRID=4326;POINT (-95.363151 29.763374)') self.assertTrue(xform_geom.equals_exact(cleaned_geom, tol)) - def test02_null(self): + def test_null(self): "Testing GeometryField's handling of null (None) geometries." # Form fields, by default, are required (`required=True`) fld = forms.GeometryField() @@ -46,7 +47,7 @@ class GeometryFieldTest(unittest.TestCase): fld = forms.GeometryField(required=False) self.assertIsNone(fld.clean(None)) - def test03_geom_type(self): + def test_geom_type(self): "Testing GeometryField's handling of different geometry types." # By default, all geometry types are allowed. fld = forms.GeometryField() @@ -60,7 +61,7 @@ class GeometryFieldTest(unittest.TestCase): # but rejected by `clean` self.assertRaises(forms.ValidationError, pnt_fld.clean, 'LINESTRING(0 0, 1 1)') - def test04_to_python(self): + def test_to_python(self): """ Testing to_python returns a correct GEOSGeometry object or a ValidationError @@ -74,13 +75,169 @@ class GeometryFieldTest(unittest.TestCase): self.assertRaises(forms.ValidationError, fld.to_python, wkt) -def suite(): - s = unittest.TestSuite() - s.addTest(unittest.makeSuite(GeometryFieldTest)) - return s +@skipUnless(HAS_GDAL and HAS_SPATIALREFSYS, + "SpecializedFieldTest needs gdal support and a spatial database") +class SpecializedFieldTest(SimpleTestCase): + def setUp(self): + self.geometries = { + 'point': GEOSGeometry("SRID=4326;POINT(9.052734375 42.451171875)"), + 'multipoint': GEOSGeometry("SRID=4326;MULTIPOINT(" + "(13.18634033203125 14.504356384277344)," + "(13.207969665527 14.490966796875)," + "(13.177070617675 14.454917907714))"), + 'linestring': GEOSGeometry("SRID=4326;LINESTRING(" + "-8.26171875 -0.52734375," + "-7.734375 4.21875," + "6.85546875 3.779296875," + "5.44921875 -3.515625)"), + 'multilinestring': GEOSGeometry("SRID=4326;MULTILINESTRING(" + "(-16.435546875 -2.98828125," + "-17.2265625 2.98828125," + "-0.703125 3.515625," + "-1.494140625 -3.33984375)," + "(-8.0859375 -5.9765625," + "8.525390625 -8.7890625," + "12.392578125 -0.87890625," + "10.01953125 7.646484375))"), + 'polygon': GEOSGeometry("SRID=4326;POLYGON(" + "(-1.669921875 6.240234375," + "-3.8671875 -0.615234375," + "5.9765625 -3.955078125," + "18.193359375 3.955078125," + "9.84375 9.4921875," + "-1.669921875 6.240234375))"), + 'multipolygon': GEOSGeometry("SRID=4326;MULTIPOLYGON(" + "((-17.578125 13.095703125," + "-17.2265625 10.8984375," + "-13.974609375 10.1953125," + "-13.359375 12.744140625," + "-15.732421875 13.7109375," + "-17.578125 13.095703125))," + "((-8.525390625 5.537109375," + "-8.876953125 2.548828125," + "-5.888671875 1.93359375," + "-5.09765625 4.21875," + "-6.064453125 6.240234375," + "-8.525390625 5.537109375)))"), + 'geometrycollection': GEOSGeometry("SRID=4326;GEOMETRYCOLLECTION(" + "POINT(5.625 -0.263671875)," + "POINT(6.767578125 -3.603515625)," + "POINT(8.525390625 0.087890625)," + "POINT(8.0859375 -2.13134765625)," + "LINESTRING(" + "6.273193359375 -1.175537109375," + "5.77880859375 -1.812744140625," + "7.27294921875 -2.230224609375," + "7.657470703125 -1.25244140625))"), + } -def run(verbosity=2): - unittest.TextTestRunner(verbosity=verbosity).run(suite()) + def assertMapWidget(self, form_instance): + """ + Make sure the MapWidget js is passed in the form media and a MapWidget + is actually created + """ + self.assertTrue(form_instance.is_valid()) + rendered = form_instance.as_p() + self.assertIn('new MapWidget(options);', rendered) + self.assertIn('gis/js/OLMapWidget.js', str(form_instance.media)) -if __name__=="__main__": - run() + def assertTextarea(self, geom, rendered): + """Makes sure the wkt and a textarea are in the content""" + + self.assertIn('