Ticket #14155: django-tests-fix.diff

File django-tests-fix.diff, 4.9 KB (added by Alex Gaynor, 14 years ago)
  • tests/modeltests/fixtures_model_package/models/__init__.py

    diff --git a/tests/modeltests/fixtures_model_package/models/__init__.py b/tests/modeltests/fixtures_model_package/models/__init__.py
    index 1581102..c0450b2 100644
    a b class Article(models.Model):  
    1212        app_label = 'fixtures_model_package'
    1313        ordering = ('-pub_date', 'headline')
    1414
    15 __test__ = {'API_TESTS': """
    16 >>> from django.core import management
    17 >>> from django.db.models import get_app
    18 
    19 # Reset the database representation of this app.
    20 # This will return the database to a clean initial state.
    21 >>> management.call_command('flush', verbosity=0, interactive=False)
    22 
    23 # Syncdb introduces 1 initial data object from initial_data.json.
    24 >>> Article.objects.all()
    25 [<Article: Python program becomes self aware>]
    26 
    27 # Load fixture 1. Single JSON file, with two objects.
    28 >>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
    29 >>> Article.objects.all()
    30 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
    31 
    32 # Load fixture 2. JSON file imported by default. Overwrites some existing objects
    33 >>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
    34 >>> Article.objects.all()
    35 [<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>]
    36 
    37 # Load a fixture that doesn't exist
    38 >>> management.call_command('loaddata', 'unknown.json', verbosity=0)
    39 
    40 # object list is unaffected
    41 >>> Article.objects.all()
    42 [<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>]
    43 """}
    44 
    45 
    46 from django.test import TestCase
    47 
    48 class SampleTestCase(TestCase):
    49     fixtures = ['fixture1.json', 'fixture2.json']
    50 
    51     def testClassFixtures(self):
    52         "Check that test case has installed 4 fixture objects"
    53         self.assertEqual(Article.objects.count(), 4)
    54         self.assertEquals(str(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>]")
  • new file tests/modeltests/fixtures_model_package/tests.py

    diff --git a/tests/modeltests/fixtures_model_package/tests.py b/tests/modeltests/fixtures_model_package/tests.py
    new file mode 100644
    index 0000000..0738dbd
    - +  
     1from django.core import management
     2from django.test import TestCase
     3
     4from models import Article
     5
     6
     7class SampleTestCase(TestCase):
     8    fixtures = ['fixture1.json', 'fixture2.json']
     9
     10    def testClassFixtures(self):
     11        "Check that test case has installed 4 fixture objects"
     12        self.assertEqual(Article.objects.count(), 4)
     13        self.assertQuerysetEqual(
     14            Article.objects.all(),[
     15                "Django conquers world!",
     16                "Copyright is fine the way it is",
     17                "Poker has no place on ESPN",
     18                "Python program becomes self aware"
     19            ],
     20            lambda a: a.headline
     21        )
     22
     23
     24class FixtureTestCase(TestCase):
     25    def test_initial_data(self):
     26        # Syncdb introduces 1 initial data object from initial_data.json.
     27        self.assertQuerysetEqual(
     28            Article.objects.all(), [
     29                "Python program becomes self aware"
     30            ],
     31            lambda a: a.headline
     32        )
     33   
     34    def test_loaddata(self):
     35        # Load fixture 1. Single JSON file, with two objects.
     36        management.call_command("loaddata", "fixture1.json", verbosity=0)
     37        self.assertQuerysetEqual(
     38            Article.objects.all(), [
     39                "Time to reform copyright",
     40                "Poker has no place on ESPN",
     41                "Python program becomes self aware",
     42            ],
     43            lambda a: a.headline,
     44        )
     45       
     46        # Load fixture 2. JSON file imported by default. Overwrites some
     47        # existing objects
     48        management.call_command("loaddata", "fixture2.json", verbosity=0)
     49        self.assertQuerysetEqual(
     50            Article.objects.all(), [
     51                "Django conquers world!",
     52                "Copyright is fine the way it is",
     53                "Poker has no place on ESPN",
     54                "Python program becomes self aware",
     55            ],
     56            lambda a: a.headline,
     57        )
     58
     59        # Load a fixture that doesn't exist
     60        management.call_command("loaddata", "unknown.json", verbosity=0)
     61        self.assertQuerysetEqual(
     62            Article.objects.all(), [
     63                "Django conquers world!",
     64                "Copyright is fine the way it is",
     65                "Poker has no place on ESPN",
     66                "Python program becomes self aware",
     67            ],
     68            lambda a: a.headline,
     69        )
Back to Top