diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-02-28 05:35:22 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-02-28 05:35:22 +0000 |
| commit | db2a95f6f58f5593dbcee8dcf51f3e9d71449708 (patch) | |
| tree | 0732f88402e8d4b58f80d9f21c6dc3844512e746 /tests | |
| parent | b337884fcf6b2c1934d380dd99bbe485d3b88f3a (diff) | |
Fixed #5610 -- Added the ability for dumpdata to take individual model names, as well as entire applications. Thanks to David Reynolds for his work on this patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9921 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/fixtures/fixtures/fixture1.json | 30 | ||||
| -rw-r--r-- | tests/modeltests/fixtures/models.py | 45 |
2 files changed, 67 insertions, 8 deletions
diff --git a/tests/modeltests/fixtures/fixtures/fixture1.json b/tests/modeltests/fixtures/fixtures/fixture1.json index cc11a3e926..332feaef77 100644 --- a/tests/modeltests/fixtures/fixtures/fixture1.json +++ b/tests/modeltests/fixtures/fixtures/fixture1.json @@ -1,18 +1,34 @@ [ { - "pk": "2", - "model": "fixtures.article", + "pk": 1, + "model": "sites.site", "fields": { - "headline": "Poker has no place on ESPN", + "domain": "example.com", + "name": "example.com" + } + }, + { + "pk": "2", + "model": "fixtures.article", + "fields": { + "headline": "Poker has no place on ESPN", "pub_date": "2006-06-16 12:00:00" } - }, + }, { - "pk": "3", - "model": "fixtures.article", + "pk": "3", + "model": "fixtures.article", "fields": { - "headline": "Time to reform copyright", + "headline": "Time to reform copyright", "pub_date": "2006-06-16 13:00:00" } + }, + { + "pk": 1, + "model": "fixtures.category", + "fields": { + "description": "Latest news stories", + "title": "News Stories" + } } ]
\ No newline at end of file diff --git a/tests/modeltests/fixtures/models.py b/tests/modeltests/fixtures/models.py index 0a32962860..75ff99867a 100644 --- a/tests/modeltests/fixtures/models.py +++ b/tests/modeltests/fixtures/models.py @@ -11,6 +11,16 @@ in the application directory, on in one of the directories named in the from django.db import models from django.conf import settings +class Category(models.Model): + title = models.CharField(max_length=100) + description = models.TextField() + + def __unicode__(self): + return self.title + + class Meta: + ordering = ('title',) + class Article(models.Model): headline = models.CharField(max_length=100, default='Default headline') pub_date = models.DateTimeField() @@ -38,6 +48,38 @@ __test__ = {'API_TESTS': """ >>> 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 +>>> management.call_command('dumpdata', 'fixtures', format='json') +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}] + +# Try just dumping the contents of fixtures.Category +>>> management.call_command('dumpdata', 'fixtures.Category', format='json') +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}] + +# ...and just fixtures.Article +>>> management.call_command('dumpdata', 'fixtures.Article', 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"}}] + +# ...and both +>>> management.call_command('dumpdata', 'fixtures.Category', 'fixtures.Article', format='json') +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}] + +# Specify a specific model twice +>>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures.Article', 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"}}] + +# Specify a dump that specifies Article both explicitly and implicitly +>>> management.call_command('dumpdata', 'fixtures.Article', 'fixtures', format='json') +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}] + +# Same again, but specify in the reverse order +>>> management.call_command('dumpdata', 'fixtures', 'fixtures.Article', format='json') +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}] + +# Specify one model from one application, and an entire other application. +>>> management.call_command('dumpdata', 'fixtures.Category', 'sites', format='json') +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"pk": 1, "model": "sites.site", "fields": {"domain": "example.com", "name": "example.com"}}] + # Load fixture 2. JSON file imported by default. Overwrites some existing objects >>> management.call_command('loaddata', 'fixture2.json', verbosity=0) >>> Article.objects.all() @@ -82,7 +124,7 @@ Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. # Dump the current contents of the database as a JSON fixture >>> 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"}}] +[{"pk": 1, "model": "fixtures.category", "fields": {"description": "Latest news stories", "title": "News Stories"}}, {"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"}}] # Load fixture 4 (compressed), using format discovery >>> management.call_command('loaddata', 'fixture4', verbosity=0) @@ -116,6 +158,7 @@ Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. # because there are two fixture5's in the fixtures directory >>> management.call_command('loaddata', 'fixture5', verbosity=0) # doctest: +ELLIPSIS Multiple fixtures named 'fixture5' in '...fixtures'. Aborting. + """ from django.test import TestCase |
