1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

[soc2009/multidb] Updated testing services to handle multiple databases better. Includes extra tests (some failing) for multiple database support. Patch from Russell Keith-Magee.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11764 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor
2009-11-23 16:42:56 +00:00
parent 0c167ae0ff
commit f2604c331d
14 changed files with 375 additions and 131 deletions

View File

@@ -0,0 +1,10 @@
[
{
"pk": 1,
"model": "multiple_database.book",
"fields": {
"title": "The Definitive Guide to Django",
"published": "2009-7-8"
}
}
]

View File

@@ -0,0 +1,10 @@
[
{
"pk": 2,
"model": "multiple_database.book",
"fields": {
"title": "Dive into Python",
"published": "2009-5-4"
}
}
]

View File

@@ -0,0 +1,10 @@
[
{
"pk": 2,
"model": "multiple_database.book",
"fields": {
"title": "Pro Django",
"published": "2008-12-16"
}
}
]

View File

@@ -14,19 +14,7 @@ class Book(models.Model):
class Author(models.Model):
name = models.CharField(max_length=100)
favourite_book = models.ForeignKey(Book, null=True, related_name='favourite_of')
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
class Meta:
ordering = ('title',)
using = article_using

View File

@@ -14,82 +14,243 @@ try:
except ImportError:
pass
class ConnectionHandlerTestCase(TestCase):
def test_alias_for_connection(self):
for db in connections:
self.assertEqual(db, connections.alias_for_connection(connections[db]))
class QueryTestCase(TestCase):
multi_db = True
def test_default_creation(self):
"Objects created on the default database don't leak onto other databases"
# Create a book on the default database using create()
Book.objects.create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
# Create a book on the default database using a save
pro = Book()
pro.title="Pro Django"
pro.published = datetime.date(2008, 12, 16)
pro.save()
# Check that book exists on the default database, but not on other database
try:
Book.objects.get(title="Dive into Python")
Book.objects.using('default').get(title="Dive into Python")
except Book.DoesNotExist:
self.fail('"Dive Into Python" should exist on default database')
self.assertRaises(Book.DoesNotExist,
Book.objects.using('other').get,
title="Dive into Python"
)
try:
Book.objects.get(title="Pro Django")
Book.objects.using('default').get(title="Pro Django")
except Book.DoesNotExist:
self.fail('"Pro Django" should exist on default database')
self.assertRaises(Book.DoesNotExist,
Book.objects.using('other').get,
title="Pro Django"
)
def test_other_creation(self):
"Objects created on another database don't leak onto the default database"
# Create a book on the second database
Book.objects.using('other').create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
# Create a book on the default database using a save
pro = Book()
pro.title="Pro Django"
pro.published = datetime.date(2008, 12, 16)
pro.save(using='other')
# Check that book exists on the default database, but not on other database
try:
Book.objects.using('other').get(title="Dive into Python")
except Book.DoesNotExist:
self.fail('"Dive Into Python" should exist on other database')
self.assertRaises(Book.DoesNotExist,
Book.objects.get,
title="Dive into Python"
)
self.assertRaises(Book.DoesNotExist,
Book.objects.using('default').get,
title="Dive into Python"
)
try:
Book.objects.using('other').get(title="Pro Django")
except Book.DoesNotExist:
self.fail('"Pro Django" should exist on other database')
self.assertRaises(Book.DoesNotExist,
Book.objects.get,
title="Pro Django"
)
self.assertRaises(Book.DoesNotExist,
Book.objects.using('default').get,
title="Pro Django"
)
def test_basic_queries(self):
for db in connections:
self.assertRaises(Book.DoesNotExist,
lambda: Book.objects.using(db).get(title="Dive into Python"))
Book.objects.using(db).create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
"Queries are constrained to a single database"
dive = Book.objects.using('other').create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
for db in connections:
books = Book.objects.all().using(db)
self.assertEqual(books.count(), 1)
self.assertEqual(len(books), 1)
self.assertEqual(books[0].title, "Dive into Python")
self.assertEqual(books[0].published, datetime.date(2009, 5, 4))
dive = Book.objects.using('other').get(published=datetime.date(2009, 5, 4))
self.assertEqual(dive.title, "Dive into Python")
self.assertRaises(Book.DoesNotExist, Book.objects.using('default').get, published=datetime.date(2009, 5, 4))
for db in connections:
self.assertRaises(Book.DoesNotExist,
lambda: Book.objects.using(db).get(title="Pro Django"))
book = Book(title="Pro Django", published=datetime.date(2008, 12, 16))
book.save(using=db)
dive = Book.objects.using('other').get(title__icontains="dive")
self.assertEqual(dive.title, "Dive into Python")
self.assertRaises(Book.DoesNotExist, Book.objects.using('default').get, title__icontains="dive")
for db in connections:
books = Book.objects.all().using(db)
self.assertEqual(books.count(), 2)
self.assertEqual(len(books), 2)
self.assertEqual(books[0].title, "Dive into Python")
self.assertEqual(books[1].title, "Pro Django")
dive = Book.objects.using('other').get(title__iexact="dive INTO python")
self.assertEqual(dive.title, "Dive into Python")
self.assertRaises(Book.DoesNotExist, Book.objects.using('default').get, title__iexact="dive INTO python")
pro = Book.objects.using(db).get(published=datetime.date(2008, 12, 16))
self.assertEqual(pro.title, "Pro Django")
dive = Book.objects.using('other').get(published__year=2009)
self.assertEqual(dive.title, "Dive into Python")
self.assertEqual(dive.published, datetime.date(2009, 5, 4))
self.assertRaises(Book.DoesNotExist, Book.objects.using('default').get, published__year=2009)
dive = Book.objects.using(db).get(title__icontains="dive")
self.assertEqual(dive.title, "Dive into Python")
years = Book.objects.using('other').dates('published', 'year')
self.assertEqual([o.year for o in years], [2009])
years = Book.objects.using('default').dates('published', 'year')
self.assertEqual([o.year for o in years], [])
dive = Book.objects.using(db).get(title__iexact="dive INTO python")
self.assertEqual(dive.title, "Dive into Python")
months = Book.objects.using('other').dates('published', 'month')
self.assertEqual([o.month for o in months], [5])
months = Book.objects.using('default').dates('published', 'month')
self.assertEqual([o.month for o in months], [])
pro = Book.objects.using(db).get(published__year=2008)
self.assertEqual(pro.title, "Pro Django")
self.assertEqual(pro.published, datetime.date(2008, 12, 16))
def test_m2m(self):
"M2M fields are constrained to a single database"
# Create a book and author on the default database
dive = Book.objects.create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
years = Book.objects.using(db).dates('published', 'year')
self.assertEqual([o.year for o in years], [2008, 2009])
mark = Author.objects.create(name="Mark Pilgrim")
# Create a book and author on the other database
pro = Book.objects.using('other').create(title="Pro Django",
published=datetime.date(2008, 12, 16))
marty = Author.objects.using('other').create(name="Marty Alchin")
# Save the author relations
dive.authors = [mark]
pro.authors = [marty]
# Inspect the m2m tables directly.
# There should be 1 entry in each database
self.assertEquals(Book.authors.through.objects.using('default').count(), 1)
self.assertEquals(Book.authors.through.objects.using('other').count(), 1)
# Check that queries work across m2m joins
self.assertEquals(Book.objects.using('default').filter(authors__name='Mark Pilgrim').values_list('title', flat=True),
['Dive into Python'])
self.assertEquals(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True),
[])
self.assertEquals(Book.objects.using('default').filter(authors__name='Marty Alchin').values_list('title', flat=True),
[])
self.assertEquals(Book.objects.using('other').filter(authors__name='Marty Alchin').values_list('title', flat=True),
['Pro Django'])
def test_foreign_key(self):
"FK fields are constrained to a single database"
# Create a book and author on the default database
dive = Book.objects.create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
mark = Author.objects.create(name="Mark Pilgrim")
# Create a book and author on the other database
pro = Book.objects.using('other').create(title="Pro Django",
published=datetime.date(2008, 12, 16))
marty = Author.objects.using('other').create(name="Marty Alchin")
# Save the author's favourite books
mark.favourite_book = dive
mark.save()
marty.favourite_book = pro
marty.save() # FIXME Should this be save(using=alias)?
mark = Author.objects.using('default').get(name="Mark Pilgrim")
self.assertEquals(mark.favourite_book.title, "Dive into Python")
marty = Author.objects.using('other').get(name='Marty Alchin')
self.assertEquals(marty.favourite_book.title, "Dive into Python")
try:
mark.favourite_book = marty
self.fail("Shouldn't be able to assign across databases")
except Exception: # FIXME - this should be more explicit
pass
# Check that queries work across foreign key joins
self.assertEquals(Book.objects.using('default').filter(favourite_of__name='Mark Pilgrim').values_list('title', flat=True),
['Dive into Python'])
self.assertEquals(Book.objects.using('other').filter(favourite_of__name='Mark Pilgrim').values_list('title', flat=True),
[])
self.assertEquals(Book.objects.using('default').filter(favourite_of__name='Marty Alchin').values_list('title', flat=True),
[])
self.assertEquals(Book.objects.using('other').filter(favourite_of__name='Marty Alchin').values_list('title', flat=True),
['Pro Django'])
class FixtureTestCase(TestCase):
multi_db = True
fixtures = ['multidb-common', 'multidb']
def test_fixture_loading(self):
"Multi-db fixtures are loaded correctly"
# Check that "Dive into Python" exists on the default database, but not on other database
try:
Book.objects.get(title="Dive into Python")
Book.objects.using('default').get(title="Dive into Python")
except Book.DoesNotExist:
self.fail('"Dive Into Python" should exist on default database')
self.assertRaises(Book.DoesNotExist,
Book.objects.using('other').get,
title="Dive into Python"
)
# Check that "Pro Django" exists on the default database, but not on other database
try:
Book.objects.using('other').get(title="Pro Django")
except Book.DoesNotExist:
self.fail('"Pro Django" should exist on other database')
self.assertRaises(Book.DoesNotExist,
Book.objects.get,
title="Pro Django"
)
self.assertRaises(Book.DoesNotExist,
Book.objects.using('default').get,
title="Pro Django"
)
# Check that "Definitive Guide" exists on the both databases
try:
Book.objects.get(title="The Definitive Guide to Django")
Book.objects.using('default').get(title="The Definitive Guide to Django")
Book.objects.using('other').get(title="The Definitive Guide to Django")
except Book.DoesNotExist:
self.fail('"The Definitive Guide to Django" should exist on both databases')
months = Book.objects.dates('published', 'month').using(db)
self.assertEqual(sorted(o.month for o in months), [5, 12])
class PickleQuerySetTestCase(TestCase):
multi_db = True
def test_pickling(self):
for db in connections:
Book.objects.using(db).create(title='Pro Django', published=datetime.date(2008, 12, 16))
qs = Book.objects.all()
self.assertEqual(qs._using, pickle.loads(pickle.dumps(qs))._using)
if len(settings.DATABASES) > 1:
class MetaUsingTestCase(TestCase):
def test_meta_using_queries(self):
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:
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!"))
a.delete()
self.assertRaises(Article.DoesNotExist,
lambda: Article.objects.get(title="Django Rules!"))
self.assertRaises(ValueError,
lambda: list(Article.objects.get(pk__in=Article.objects.using('default'))))