| 1 |
from django.db import models |
|---|
| 2 |
from django.contrib.auth.models import User |
|---|
| 3 |
from django.conf import settings |
|---|
| 4 |
import os |
|---|
| 5 |
|
|---|
| 6 |
class Animal(models.Model): |
|---|
| 7 |
name = models.CharField(max_length=150) |
|---|
| 8 |
latin_name = models.CharField(max_length=150) |
|---|
| 9 |
count = models.IntegerField() |
|---|
| 10 |
|
|---|
| 11 |
def __unicode__(self): |
|---|
| 12 |
return self.common_name |
|---|
| 13 |
|
|---|
| 14 |
def animal_pre_save_check(signal, sender, instance, **kwargs): |
|---|
| 15 |
"A signal that is used to check the type of data loaded from fixtures" |
|---|
| 16 |
print 'Count = %s (%s)' % (instance.count, type(instance.count)) |
|---|
| 17 |
|
|---|
| 18 |
class Plant(models.Model): |
|---|
| 19 |
name = models.CharField(max_length=150) |
|---|
| 20 |
|
|---|
| 21 |
class Meta: |
|---|
| 22 |
# For testing when upper case letter in app name; regression for #4057 |
|---|
| 23 |
db_table = "Fixtures_regress_plant" |
|---|
| 24 |
|
|---|
| 25 |
class Stuff(models.Model): |
|---|
| 26 |
name = models.CharField(max_length=20, null=True) |
|---|
| 27 |
owner = models.ForeignKey(User, null=True) |
|---|
| 28 |
|
|---|
| 29 |
def __unicode__(self): |
|---|
| 30 |
# Oracle doesn't distinguish between None and the empty string. |
|---|
| 31 |
# This hack makes the test case pass using Oracle. |
|---|
| 32 |
name = self.name |
|---|
| 33 |
if settings.DATABASE_ENGINE == 'oracle' and name == u'': |
|---|
| 34 |
name = None |
|---|
| 35 |
return unicode(name) + u' is owned by ' + unicode(self.owner) |
|---|
| 36 |
|
|---|
| 37 |
class Absolute(models.Model): |
|---|
| 38 |
name = models.CharField(max_length=40) |
|---|
| 39 |
|
|---|
| 40 |
load_count = 0 |
|---|
| 41 |
|
|---|
| 42 |
def __init__(self, *args, **kwargs): |
|---|
| 43 |
super(Absolute, self).__init__(*args, **kwargs) |
|---|
| 44 |
Absolute.load_count += 1 |
|---|
| 45 |
|
|---|
| 46 |
class Parent(models.Model): |
|---|
| 47 |
name = models.CharField(max_length=10) |
|---|
| 48 |
|
|---|
| 49 |
class Child(Parent): |
|---|
| 50 |
data = models.CharField(max_length=10) |
|---|
| 51 |
|
|---|
| 52 |
# Models to regresison check #7572 |
|---|
| 53 |
class Channel(models.Model): |
|---|
| 54 |
name = models.CharField(max_length=255) |
|---|
| 55 |
|
|---|
| 56 |
class Article(models.Model): |
|---|
| 57 |
title = models.CharField(max_length=255) |
|---|
| 58 |
channels = models.ManyToManyField(Channel) |
|---|
| 59 |
|
|---|
| 60 |
class Meta: |
|---|
| 61 |
ordering = ('id',) |
|---|
| 62 |
|
|---|
| 63 |
__test__ = {'API_TESTS':""" |
|---|
| 64 |
>>> from django.core import management |
|---|
| 65 |
|
|---|
| 66 |
# Load a fixture that uses PK=1 |
|---|
| 67 |
>>> management.call_command('loaddata', 'sequence', verbosity=0) |
|---|
| 68 |
|
|---|
| 69 |
# Create a new animal. Without a sequence reset, this new object |
|---|
| 70 |
# will take a PK of 1 (on Postgres), and the save will fail. |
|---|
| 71 |
# This is a regression test for ticket #3790. |
|---|
| 72 |
>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus', count=2) |
|---|
| 73 |
>>> animal.save() |
|---|
| 74 |
|
|---|
| 75 |
############################################### |
|---|
| 76 |
# Regression test for ticket #4558 -- pretty printing of XML fixtures |
|---|
| 77 |
# doesn't affect parsing of None values. |
|---|
| 78 |
|
|---|
| 79 |
# Load a pretty-printed XML fixture with Nulls. |
|---|
| 80 |
>>> management.call_command('loaddata', 'pretty.xml', verbosity=0) |
|---|
| 81 |
>>> Stuff.objects.all() |
|---|
| 82 |
[<Stuff: None is owned by None>] |
|---|
| 83 |
|
|---|
| 84 |
############################################### |
|---|
| 85 |
# Regression test for ticket #6436 -- |
|---|
| 86 |
# os.path.join will throw away the initial parts of a path if it encounters |
|---|
| 87 |
# an absolute path. This means that if a fixture is specified as an absolute path, |
|---|
| 88 |
# we need to make sure we don't discover the absolute path in every fixture directory. |
|---|
| 89 |
|
|---|
| 90 |
>>> load_absolute_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'absolute.json') |
|---|
| 91 |
>>> management.call_command('loaddata', load_absolute_path, verbosity=0) |
|---|
| 92 |
>>> Absolute.load_count |
|---|
| 93 |
1 |
|---|
| 94 |
|
|---|
| 95 |
############################################### |
|---|
| 96 |
# Test for ticket #4371 -- fixture loading fails silently in testcases |
|---|
| 97 |
# Validate that error conditions are caught correctly |
|---|
| 98 |
|
|---|
| 99 |
# redirect stderr for the next few tests... |
|---|
| 100 |
>>> import sys |
|---|
| 101 |
>>> savestderr = sys.stderr |
|---|
| 102 |
>>> sys.stderr = sys.stdout |
|---|
| 103 |
|
|---|
| 104 |
# Loading data of an unknown format should fail |
|---|
| 105 |
>>> management.call_command('loaddata', 'bad_fixture1.unkn', verbosity=0) |
|---|
| 106 |
Problem installing fixture 'bad_fixture1': unkn is not a known serialization format. |
|---|
| 107 |
|
|---|
| 108 |
# Loading a fixture file with invalid data using explicit filename |
|---|
| 109 |
>>> management.call_command('loaddata', 'bad_fixture2.xml', verbosity=0) |
|---|
| 110 |
No fixture data found for 'bad_fixture2'. (File format may be invalid.) |
|---|
| 111 |
|
|---|
| 112 |
# Loading a fixture file with invalid data without file extension |
|---|
| 113 |
>>> management.call_command('loaddata', 'bad_fixture2', verbosity=0) |
|---|
| 114 |
No fixture data found for 'bad_fixture2'. (File format may be invalid.) |
|---|
| 115 |
|
|---|
| 116 |
>>> sys.stderr = savestderr |
|---|
| 117 |
|
|---|
| 118 |
############################################### |
|---|
| 119 |
# Test for ticket #7565 -- PostgreSQL sequence resetting checks shouldn't |
|---|
| 120 |
# ascend to parent models when inheritance is used (since they are treated |
|---|
| 121 |
# individually). |
|---|
| 122 |
|
|---|
| 123 |
>>> management.call_command('loaddata', 'model-inheritance.json', verbosity=0) |
|---|
| 124 |
|
|---|
| 125 |
############################################### |
|---|
| 126 |
# Test for ticket #7572 -- MySQL has a problem if the same connection is |
|---|
| 127 |
# used to create tables, load data, and then query over that data. |
|---|
| 128 |
# To compensate, we close the connection after running loaddata. |
|---|
| 129 |
# This ensures that a new connection is opened when test queries are issued. |
|---|
| 130 |
|
|---|
| 131 |
>>> management.call_command('loaddata', 'big-fixture.json', verbosity=0) |
|---|
| 132 |
|
|---|
| 133 |
>>> articles = Article.objects.exclude(id=9) |
|---|
| 134 |
>>> articles.values_list('id', flat=True) |
|---|
| 135 |
[1, 2, 3, 4, 5, 6, 7, 8] |
|---|
| 136 |
|
|---|
| 137 |
# Just for good measure, run the same query again. Under the influence of |
|---|
| 138 |
# ticket #7572, this will give a different result to the previous call. |
|---|
| 139 |
>>> articles.values_list('id', flat=True) |
|---|
| 140 |
[1, 2, 3, 4, 5, 6, 7, 8] |
|---|
| 141 |
|
|---|
| 142 |
############################################### |
|---|
| 143 |
# Test for ticket #8298 - Field values should be coerced into the correct type |
|---|
| 144 |
# by the deserializer, not as part of the database write. |
|---|
| 145 |
|
|---|
| 146 |
>>> models.signals.pre_save.connect(animal_pre_save_check) |
|---|
| 147 |
>>> management.call_command('loaddata', 'animal.xml', verbosity=0) |
|---|
| 148 |
Count = 42 (<type 'int'>) |
|---|
| 149 |
|
|---|
| 150 |
>>> models.signals.pre_save.disconnect(animal_pre_save_check) |
|---|
| 151 |
|
|---|
| 152 |
"""} |
|---|