diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/basic/models.py | 13 | ||||
| -rw-r--r-- | tests/modeltests/fixtures/models.py | 18 | ||||
| -rw-r--r-- | tests/modeltests/test_client/models.py | 16 | ||||
| -rw-r--r-- | tests/regressiontests/datastructures/tests.py | 9 | ||||
| -rw-r--r-- | tests/regressiontests/fixtures_regress/models.py | 4 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 23 | ||||
| -rw-r--r-- | tests/regressiontests/model_regress/models.py | 6 | ||||
| -rw-r--r-- | tests/regressiontests/serializers_regress/tests.py | 8 | ||||
| -rwxr-xr-x | tests/runtests.py | 12 |
9 files changed, 92 insertions, 17 deletions
diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py index d2220320a0..0a09579761 100644 --- a/tests/modeltests/basic/models.py +++ b/tests/modeltests/basic/models.py @@ -247,6 +247,19 @@ datetime.datetime(2005, 7, 28, 0, 0) >>> (s1 | s2 | s3)[::2] [<Article: Area woman programs in Python>, <Article: Third article>] +# Slicing works with longs. +>>> Article.objects.all()[0L] +<Article: Area woman programs in Python> +>>> Article.objects.all()[1L:3L] +[<Article: Second article>, <Article: Third article>] +>>> s3 = Article.objects.filter(id__exact=3) +>>> (s1 | s2 | s3)[::2L] +[<Article: Area woman programs in Python>, <Article: Third article>] + +# And can be mixed with ints. +>>> Article.objects.all()[1:3L] +[<Article: Second article>, <Article: Third article>] + # Slices (without step) are lazy: >>> Article.objects.all()[0:5].filter() [<Article: Area woman programs in Python>, <Article: Second article>, <Article: Third article>, <Article: Article 6>, <Article: Default headline>] diff --git a/tests/modeltests/fixtures/models.py b/tests/modeltests/fixtures/models.py index b59dc82884..b59c388bbd 100644 --- a/tests/modeltests/fixtures/models.py +++ b/tests/modeltests/fixtures/models.py @@ -26,54 +26,54 @@ __test__ = {'API_TESTS': """ # Reset the database representation of this app. # This will return the database to a clean initial state. ->>> management.flush(verbosity=0, interactive=False) +>>> management.call_command('flush', verbosity=0, interactive=False) # Syncdb introduces 1 initial data object from initial_data.json. >>> Article.objects.all() [<Article: Python program becomes self aware>] # Load fixture 1. Single JSON file, with two objects. ->>> management.load_data(['fixture1.json'], verbosity=0) +>>> management.call_command('loaddata', 'fixture1.json', verbosity=0) >>> Article.objects.all() [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] # Load fixture 2. JSON file imported by default. Overwrites some existing objects ->>> management.load_data(['fixture2.json'], verbosity=0) +>>> management.call_command('loaddata', 'fixture2.json', verbosity=0) >>> Article.objects.all() [<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] # Load fixture 3, XML format. ->>> management.load_data(['fixture3.xml'], verbosity=0) +>>> management.call_command('loaddata', 'fixture3.xml', verbosity=0) >>> Article.objects.all() [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>] # Load a fixture that doesn't exist ->>> management.load_data(['unknown.json'], verbosity=0) +>>> management.call_command('loaddata', 'unknown.json', verbosity=0) # object list is unaffected >>> Article.objects.all() [<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>] # Reset the database representation of this app. This will delete all data. ->>> management.flush(verbosity=0, interactive=False) +>>> management.call_command('flush', verbosity=0, interactive=False) >>> Article.objects.all() [<Article: Python program becomes self aware>] # Load fixture 1 again, using format discovery ->>> management.load_data(['fixture1'], verbosity=0) +>>> management.call_command('loaddata', 'fixture1', verbosity=0) >>> Article.objects.all() [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] # Try to load fixture 2 using format discovery; this will fail # because there are two fixture2's in the fixtures directory ->>> management.load_data(['fixture2'], verbosity=0) # doctest: +ELLIPSIS +>>> management.call_command('loaddata', 'fixture2', verbosity=0) # doctest: +ELLIPSIS Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. >>> Article.objects.all() [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] # Dump the current contents of the database as a JSON fixture ->>> print management.dump_data(['fixtures'], format='json') +>>> management.call_command('dumpdata', 'fixtures', format='json') [{"pk": "3", "model": "fixtures.article", "fields": {"headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00"}}, {"pk": "2", "model": "fixtures.article", "fields": {"headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00"}}, {"pk": "1", "model": "fixtures.article", "fields": {"headline": "Python program becomes self aware", "pub_date": "2006-06-16 11:00:00"}}] """} diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py index 951a41d61c..98b6a808a1 100644 --- a/tests/modeltests/test_client/models.py +++ b/tests/modeltests/test_client/models.py @@ -246,6 +246,22 @@ class ClientTest(TestCase): login = self.client.login(username='inactive', password='password') self.failIf(login) + def test_logout(self): + # Log in + self.client.login(username='testclient', password='password') + + # Request a page that requires a login + response = self.client.get('/test_client/login_protected_view/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['user'].username, 'testclient') + + # Log out + self.client.logout() + + # Request a page that requires a login + response = self.client.get('/test_client/login_protected_view/') + self.assertRedirects(response, '/accounts/login/') + def test_session_modifying_view(self): "Request a page that modifies the session" # Session value isn't set initially diff --git a/tests/regressiontests/datastructures/tests.py b/tests/regressiontests/datastructures/tests.py index 18eb4fcccd..3920e1ca40 100644 --- a/tests/regressiontests/datastructures/tests.py +++ b/tests/regressiontests/datastructures/tests.py @@ -64,4 +64,13 @@ True ['Holovaty'] >>> d['person']['2']['firstname'] ['Adrian'] + +### FileDict ################################################################ + +>>> d = FileDict({'content': 'once upon a time...'}) +>>> repr(d) +"{'content': '<omitted>'}" +>>> d = FileDict({'other-key': 'once upon a time...'}) +>>> repr(d) +"{'other-key': 'once upon a time...'}" """ diff --git a/tests/regressiontests/fixtures_regress/models.py b/tests/regressiontests/fixtures_regress/models.py index 2c92839f0f..c6a50f73ce 100644 --- a/tests/regressiontests/fixtures_regress/models.py +++ b/tests/regressiontests/fixtures_regress/models.py @@ -26,7 +26,7 @@ __test__ = {'API_TESTS':""" >>> from django.core import management # Load a fixture that uses PK=1 ->>> management.load_data(['sequence'], verbosity=0) +>>> management.call_command('loaddata', 'sequence', verbosity=0) # Create a new animal. Without a sequence reset, this new object # will take a PK of 1 (on Postgres), and the save will fail. @@ -39,7 +39,7 @@ __test__ = {'API_TESTS':""" # doesn't affect parsing of None values. # Load a pretty-printed XML fixture with Nulls. ->>> management.load_data(['pretty.xml'], verbosity=0) +>>> management.call_command('loaddata', 'pretty.xml', verbosity=0) >>> Stuff.objects.all() [<Stuff: None is owned by None>] diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index d14ffc514e..6eea519cd2 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -3636,6 +3636,29 @@ True <option value="2016">2016</option> </select> +Using a SelectDateWidget in a form: + +>>> class GetDate(Form): +... mydate = DateField(widget=SelectDateWidget) +>>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'}) +>>> print a.is_valid() +True +>>> print a.cleaned_data['mydate'] +2008-04-01 + +As with any widget that implements get_value_from_datadict, +we must be prepared to accept the input from the "as_hidden" +rendering as well. + +>>> print a['mydate'].as_hidden() +<input type="hidden" name="mydate" value="2008-4-1" id="id_mydate" /> +>>> b=GetDate({'mydate':'2008-4-1'}) +>>> print b.is_valid() +True +>>> print b.cleaned_data['mydate'] +2008-04-01 + + # MultiWidget and MultiValueField ############################################# # MultiWidgets are widgets composed of other widgets. They are usually # combined with MultiValueFields - a field that is composed of other fields. diff --git a/tests/regressiontests/model_regress/models.py b/tests/regressiontests/model_regress/models.py index 0fee831212..7aa9e2a7c4 100644 --- a/tests/regressiontests/model_regress/models.py +++ b/tests/regressiontests/model_regress/models.py @@ -10,6 +10,7 @@ class Article(models.Model): headline = models.CharField(max_length=100, default='Default headline') pub_date = models.DateTimeField() status = models.IntegerField(blank=True, null=True, choices=CHOICES) + misc_data = models.CharField(max_length=100, blank=True) class Meta: ordering = ('pub_date','headline') @@ -30,5 +31,10 @@ An empty choice field should return None for the display name. >>> a.save() >>> a.get_status_display() is None True + +Empty strings should be returned as Unicode +>>> a2 = Article.objects.get(pk=a.id) +>>> a2.misc_data +u'' """ } diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py index 86dc311269..24111308d7 100644 --- a/tests/regressiontests/serializers_regress/tests.py +++ b/tests/regressiontests/serializers_regress/tests.py @@ -273,7 +273,7 @@ class SerializerTests(unittest.TestCase): def serializerTest(format, self): # Clear the database first - management.flush(verbosity=0, interactive=False) + management.call_command('flush', verbosity=0, interactive=False) # Create all the objects defined in the test data objects = [] @@ -291,7 +291,7 @@ def serializerTest(format, self): serialized_data = serializers.serialize(format, objects, indent=2) # Flush the database and recreate from the serialized data - management.flush(verbosity=0, interactive=False) + management.call_command('flush', verbosity=0, interactive=False) transaction.enter_transaction_management() transaction.managed(True) for obj in serializers.deserialize(format, serialized_data): @@ -306,7 +306,7 @@ def serializerTest(format, self): def fieldsTest(format, self): # Clear the database first - management.flush(verbosity=0, interactive=False) + management.call_command('flush', verbosity=0, interactive=False) obj = ComplexModel(field1='first',field2='second',field3='third') obj.save(raw=True) @@ -322,7 +322,7 @@ def fieldsTest(format, self): def streamTest(format, self): # Clear the database first - management.flush(verbosity=0, interactive=False) + management.call_command('flush', verbosity=0, interactive=False) obj = ComplexModel(field1='first',field2='second',field3='third') obj.save(raw=True) diff --git a/tests/runtests.py b/tests/runtests.py index 4cda2ac65d..8c3abe908e 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -51,7 +51,7 @@ class InvalidModelTestCase(unittest.TestCase): self.model_label = model_label def runTest(self): - from django.core import management + from django.core.management.validation import get_validation_errors from django.db.models.loading import load_app from cStringIO import StringIO @@ -60,8 +60,14 @@ class InvalidModelTestCase(unittest.TestCase): except Exception, e: self.fail('Unable to load invalid model module') + # Make sure sys.stdout is not a tty so that we get errors without + # coloring attached (makes matching the results easier). We restore + # sys.stderr afterwards. + orig_stdout = sys.stdout s = StringIO() - count = management.get_validation_errors(s, module) + sys.stdout = s + count = get_validation_errors(s, module) + sys.stdout = orig_stdout s.seek(0) error_log = s.read() actual = error_log.split('\n') @@ -94,6 +100,8 @@ def django_tests(verbosity, interactive, test_labels): 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.common.CommonMiddleware', ) + if not hasattr(settings, 'SITE_ID'): + settings.SITE_ID = 1 # Load all the ALWAYS_INSTALLED_APPS. # (This import statement is intentionally delayed until after we |
