mirror of
https://github.com/django/django.git
synced 2025-07-05 10:19:20 +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):
|
||||
title = models.CharField(max_length=100)
|
||||
published = models.DateField()
|
||||
authors = models.ManyToManyField('Author')
|
||||
|
||||
def __unicode__(self):
|
||||
return self.title
|
||||
@ -11,10 +12,17 @@ class Book(models.Model):
|
||||
class Meta:
|
||||
ordering = ('title',)
|
||||
|
||||
class Author(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
if len(settings.DATABASES) > 1:
|
||||
article_using = filter(lambda o: o != DEFAULT_DB_ALIAS, settings.DATABASES.keys())[0]
|
||||
class Article(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
author = models.ForeignKey(Author)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.title
|
||||
|
@ -5,7 +5,7 @@ from django.conf import settings
|
||||
from django.db import connections
|
||||
from django.test import TestCase
|
||||
|
||||
from models import Book
|
||||
from models import Book, Author
|
||||
|
||||
try:
|
||||
# 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:
|
||||
class MetaUsingTestCase(TestCase):
|
||||
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)
|
||||
for db in connections:
|
||||
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:
|
||||
self.assertRaises(Article.DoesNotExist,
|
||||
lambda: Article.objects.using(db).get(title="Django Rules!"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user