mirror of
https://github.com/django/django.git
synced 2025-07-05 18:29:11 +00:00
[soc2009/multidb] Added tests for using foreign keys across multipled databases, ManyToMany tests will come after the merger of my many-to-many refactor work.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11425 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1cbe183ff7
commit
0d62f50271
@ -4,6 +4,7 @@ from django.db import models, DEFAULT_DB_ALIAS
|
|||||||
class Book(models.Model):
|
class Book(models.Model):
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
published = models.DateField()
|
published = models.DateField()
|
||||||
|
authors = models.ManyToManyField('Author')
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.title
|
return self.title
|
||||||
@ -11,10 +12,17 @@ class Book(models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
ordering = ('title',)
|
ordering = ('title',)
|
||||||
|
|
||||||
|
class Author(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
if len(settings.DATABASES) > 1:
|
if len(settings.DATABASES) > 1:
|
||||||
article_using = filter(lambda o: o != DEFAULT_DB_ALIAS, settings.DATABASES.keys())[0]
|
article_using = filter(lambda o: o != DEFAULT_DB_ALIAS, settings.DATABASES.keys())[0]
|
||||||
class Article(models.Model):
|
class Article(models.Model):
|
||||||
title = models.CharField(max_length=100)
|
title = models.CharField(max_length=100)
|
||||||
|
author = models.ForeignKey(Author)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
@ -5,7 +5,7 @@ from django.conf import settings
|
|||||||
from django.db import connections
|
from django.db import connections
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from models import Book
|
from models import Book, Author
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# we only have these models if the user is using multi-db, it's safe the
|
# we only have these models if the user is using multi-db, it's safe the
|
||||||
@ -79,11 +79,14 @@ class PickleQuerySetTestCase(TestCase):
|
|||||||
if len(settings.DATABASES) > 1:
|
if len(settings.DATABASES) > 1:
|
||||||
class MetaUsingTestCase(TestCase):
|
class MetaUsingTestCase(TestCase):
|
||||||
def test_meta_using_queries(self):
|
def test_meta_using_queries(self):
|
||||||
a = Article.objects.create(title="Django Rules!")
|
auth = Author.objects.create(name="Zed Shaw")
|
||||||
|
a = Article.objects.create(title="Django Rules!", author=auth)
|
||||||
self.assertEqual(Article.objects.get(title="Django Rules!"), a)
|
self.assertEqual(Article.objects.get(title="Django Rules!"), a)
|
||||||
for db in connections:
|
for db in connections:
|
||||||
if db == article_using:
|
if db == article_using:
|
||||||
self.assertEqual(Article.objects.using(db).get(title="Django Rules!"), a)
|
a1 = Article.objects.using(db).get(title="Django Rules!")
|
||||||
|
self.assertEqual(a1, a)
|
||||||
|
self.assertEqual(a1.author, auth)
|
||||||
else:
|
else:
|
||||||
self.assertRaises(Article.DoesNotExist,
|
self.assertRaises(Article.DoesNotExist,
|
||||||
lambda: Article.objects.using(db).get(title="Django Rules!"))
|
lambda: Article.objects.using(db).get(title="Django Rules!"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user