Ticket #6961: model_folder_fixture_load_test_for_patch2.patch

File model_folder_fixture_load_test_for_patch2.patch, 5.7 KB (added by mmalone, 6 years ago)

Tests for patch 2

  • tests/modeltests/fixtures_model_package/models/__init__.py

     
     1from django.db import models
     2from django.conf import settings
     3
     4class Article(models.Model):
     5    headline = models.CharField(max_length=100, default='Default headline')
     6    pub_date = models.DateTimeField()
     7
     8    def __unicode__(self):
     9        return self.headline
     10
     11    class Meta:
     12        app_label = 'fixtures_model_package'
     13        ordering = ('-pub_date', 'headline')
     14
     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
     46from django.test import TestCase
     47
     48class 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>]")
  • tests/modeltests/fixtures_model_package/__init__.py

     
     1
     2
  • tests/modeltests/fixtures_model_package/fixtures/fixture1.json

     
     1[
     2    {
     3        "pk": "2",
     4        "model": "fixtures_model_package.article",
     5        "fields": {
     6            "headline": "Poker has no place on ESPN",
     7            "pub_date": "2006-06-16 12:00:00"
     8        }
     9    },
     10    {
     11        "pk": "3",
     12        "model": "fixtures_model_package.article",
     13        "fields": {
     14            "headline": "Time to reform copyright",
     15            "pub_date": "2006-06-16 13:00:00"
     16        }
     17    }
     18]
  • tests/modeltests/fixtures_model_package/fixtures/fixture2.json

     
     1[
     2    {
     3        "pk": "3",
     4        "model": "fixtures_model_package.article",
     5        "fields": {
     6            "headline": "Copyright is fine the way it is",
     7            "pub_date": "2006-06-16 14:00:00"
     8        }
     9    },
     10    {
     11        "pk": "4",
     12        "model": "fixtures_model_package.article",
     13        "fields": {
     14            "headline": "Django conquers world!",
     15            "pub_date": "2006-06-16 15:00:00"
     16        }
     17    }
     18]
  • tests/modeltests/fixtures_model_package/fixtures/fixture2.xml

     
     1<?xml version="1.0" encoding="utf-8"?>
     2<django-objects version="1.0">
     3    <object pk="2" model="fixtures_model_package.article">
     4        <field type="CharField" name="headline">Poker on TV is great!</field>
     5        <field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field>
     6    </object>
     7    <object pk="5" model="fixtures_model_package.article">
     8        <field type="CharField" name="headline">XML identified as leading cause of cancer</field>
     9        <field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field>
     10    </object>
     11</django-objects>
  • tests/modeltests/fixtures_model_package/fixtures/initial_data.json

     
     1[
     2    {
     3        "pk": "1",
     4        "model": "fixtures_model_package.article",
     5        "fields": {
     6            "headline": "Python program becomes self aware",
     7            "pub_date": "2006-06-16 11:00:00"
     8        }
     9    }
     10]
Back to Top