mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #18269 -- Applied unicode_literals for Python 3 compatibility.
Thanks Vinay Sajip for the support of his django3 branch and Jannis Leidel for the review.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from __future__ import absolute_import
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import datetime
|
||||
import pickle
|
||||
@@ -172,14 +172,14 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# Check that queries work across m2m joins
|
||||
self.assertEqual(list(Book.objects.using('default').filter(authors__name='Marty Alchin').values_list('title', flat=True)),
|
||||
[u'Pro Django'])
|
||||
['Pro Django'])
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='Marty Alchin').values_list('title', flat=True)),
|
||||
[])
|
||||
|
||||
self.assertEqual(list(Book.objects.using('default').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
|
||||
# Reget the objects to clear caches
|
||||
dive = Book.objects.using('other').get(title="Dive into Python")
|
||||
@@ -187,10 +187,10 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# Retrive related object by descriptor. Related objects should be database-baound
|
||||
self.assertEqual(list(dive.authors.all().values_list('name', flat=True)),
|
||||
[u'Mark Pilgrim'])
|
||||
['Mark Pilgrim'])
|
||||
|
||||
self.assertEqual(list(mark.book_set.all().values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
|
||||
def test_m2m_forward_operations(self):
|
||||
"M2M forward manipulations are all constrained to a single DB"
|
||||
@@ -211,14 +211,14 @@ class QueryTestCase(TestCase):
|
||||
|
||||
dive.authors.add(john)
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
|
||||
# Remove the second author
|
||||
dive.authors.remove(john)
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)),
|
||||
[])
|
||||
|
||||
@@ -234,7 +234,7 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Book.objects.using('other').filter(authors__name='Jane Brown').values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
|
||||
def test_m2m_reverse_operations(self):
|
||||
"M2M reverse manipulations are all constrained to a single DB"
|
||||
@@ -254,14 +254,14 @@ class QueryTestCase(TestCase):
|
||||
# Add a books to the m2m
|
||||
mark.book_set.add(grease)
|
||||
self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)),
|
||||
[u'Mark Pilgrim'])
|
||||
['Mark Pilgrim'])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True)),
|
||||
[u'Mark Pilgrim'])
|
||||
['Mark Pilgrim'])
|
||||
|
||||
# Remove a book from the m2m
|
||||
mark.book_set.remove(grease)
|
||||
self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)),
|
||||
[u'Mark Pilgrim'])
|
||||
['Mark Pilgrim'])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True)),
|
||||
[])
|
||||
|
||||
@@ -277,7 +277,7 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into HTML5').values_list('name', flat=True)),
|
||||
[u'Mark Pilgrim'])
|
||||
['Mark Pilgrim'])
|
||||
|
||||
def test_m2m_cross_database_protection(self):
|
||||
"Operations that involve sharing M2M objects across databases raise an error"
|
||||
@@ -418,14 +418,14 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# Check that queries work across foreign key joins
|
||||
self.assertEqual(list(Person.objects.using('default').filter(edited__title='Pro Django').values_list('name', flat=True)),
|
||||
[u'George Vilches'])
|
||||
['George Vilches'])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Pro Django').values_list('name', flat=True)),
|
||||
[])
|
||||
|
||||
self.assertEqual(list(Person.objects.using('default').filter(edited__title='Dive into Python').values_list('name', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)),
|
||||
[u'Chris Mills'])
|
||||
['Chris Mills'])
|
||||
|
||||
# Reget the objects to clear caches
|
||||
chris = Person.objects.using('other').get(name="Chris Mills")
|
||||
@@ -433,7 +433,7 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# Retrive related object by descriptor. Related objects should be database-baound
|
||||
self.assertEqual(list(chris.edited.values_list('title', flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
|
||||
def test_foreign_key_reverse_operations(self):
|
||||
"FK reverse manipulations are all constrained to a single DB"
|
||||
@@ -454,16 +454,16 @@ class QueryTestCase(TestCase):
|
||||
|
||||
chris.edited.add(html5)
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)),
|
||||
[u'Chris Mills'])
|
||||
['Chris Mills'])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)),
|
||||
[u'Chris Mills'])
|
||||
['Chris Mills'])
|
||||
|
||||
# Remove the second editor
|
||||
chris.edited.remove(html5)
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)),
|
||||
[u'Chris Mills'])
|
||||
['Chris Mills'])
|
||||
|
||||
# Clear all edited books
|
||||
chris.edited.clear()
|
||||
@@ -477,7 +477,7 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Water').values_list('name', flat=True)),
|
||||
[u'Chris Mills'])
|
||||
['Chris Mills'])
|
||||
self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)),
|
||||
[])
|
||||
|
||||
@@ -532,37 +532,37 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(html5._state.db, 'other')
|
||||
# ... but it isn't saved yet
|
||||
self.assertEqual(list(Person.objects.using('other').values_list('name',flat=True)),
|
||||
[u'Mark Pilgrim'])
|
||||
['Mark Pilgrim'])
|
||||
self.assertEqual(list(Book.objects.using('other').values_list('title',flat=True)),
|
||||
[u'Dive into Python'])
|
||||
['Dive into Python'])
|
||||
|
||||
# When saved (no using required), new objects goes to 'other'
|
||||
chris.save()
|
||||
html5.save()
|
||||
self.assertEqual(list(Person.objects.using('default').values_list('name',flat=True)),
|
||||
[u'Marty Alchin'])
|
||||
['Marty Alchin'])
|
||||
self.assertEqual(list(Person.objects.using('other').values_list('name',flat=True)),
|
||||
[u'Chris Mills', u'Mark Pilgrim'])
|
||||
['Chris Mills', 'Mark Pilgrim'])
|
||||
self.assertEqual(list(Book.objects.using('default').values_list('title',flat=True)),
|
||||
[u'Pro Django'])
|
||||
['Pro Django'])
|
||||
self.assertEqual(list(Book.objects.using('other').values_list('title',flat=True)),
|
||||
[u'Dive into HTML5', u'Dive into Python'])
|
||||
['Dive into HTML5', 'Dive into Python'])
|
||||
|
||||
# This also works if you assign the FK in the constructor
|
||||
water = Book(title="Dive into Water", published=datetime.date(2001, 1, 1), editor=mark)
|
||||
self.assertEqual(water._state.db, 'other')
|
||||
# ... but it isn't saved yet
|
||||
self.assertEqual(list(Book.objects.using('default').values_list('title',flat=True)),
|
||||
[u'Pro Django'])
|
||||
['Pro Django'])
|
||||
self.assertEqual(list(Book.objects.using('other').values_list('title',flat=True)),
|
||||
[u'Dive into HTML5', u'Dive into Python'])
|
||||
['Dive into HTML5', 'Dive into Python'])
|
||||
|
||||
# When saved, the new book goes to 'other'
|
||||
water.save()
|
||||
self.assertEqual(list(Book.objects.using('default').values_list('title',flat=True)),
|
||||
[u'Pro Django'])
|
||||
['Pro Django'])
|
||||
self.assertEqual(list(Book.objects.using('other').values_list('title',flat=True)),
|
||||
[u'Dive into HTML5', u'Dive into Python', u'Dive into Water'])
|
||||
['Dive into HTML5', 'Dive into Python', 'Dive into Water'])
|
||||
|
||||
def test_foreign_key_deletion(self):
|
||||
"Cascaded deletions of Foreign Key relations issue queries on the right database"
|
||||
@@ -611,14 +611,14 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# Check that queries work across joins
|
||||
self.assertEqual(list(User.objects.using('default').filter(userprofile__flavor='chocolate').values_list('username', flat=True)),
|
||||
[u'alice'])
|
||||
['alice'])
|
||||
self.assertEqual(list(User.objects.using('other').filter(userprofile__flavor='chocolate').values_list('username', flat=True)),
|
||||
[])
|
||||
|
||||
self.assertEqual(list(User.objects.using('default').filter(userprofile__flavor='crunchy frog').values_list('username', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(User.objects.using('other').filter(userprofile__flavor='crunchy frog').values_list('username', flat=True)),
|
||||
[u'bob'])
|
||||
['bob'])
|
||||
|
||||
# Reget the objects to clear caches
|
||||
alice_profile = UserProfile.objects.using('default').get(flavor='chocolate')
|
||||
@@ -666,22 +666,22 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# ... but it isn't saved yet
|
||||
self.assertEqual(list(User.objects.using('other').values_list('username',flat=True)),
|
||||
[u'bob'])
|
||||
['bob'])
|
||||
self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)),
|
||||
[u'crunchy frog'])
|
||||
['crunchy frog'])
|
||||
|
||||
# When saved (no using required), new objects goes to 'other'
|
||||
charlie.save()
|
||||
bob_profile.save()
|
||||
new_bob_profile.save()
|
||||
self.assertEqual(list(User.objects.using('default').values_list('username',flat=True)),
|
||||
[u'alice'])
|
||||
['alice'])
|
||||
self.assertEqual(list(User.objects.using('other').values_list('username',flat=True)),
|
||||
[u'bob', u'charlie'])
|
||||
['bob', 'charlie'])
|
||||
self.assertEqual(list(UserProfile.objects.using('default').values_list('flavor',flat=True)),
|
||||
[u'chocolate'])
|
||||
['chocolate'])
|
||||
self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)),
|
||||
[u'crunchy frog', u'spring surprise'])
|
||||
['crunchy frog', 'spring surprise'])
|
||||
|
||||
# This also works if you assign the O2O relation in the constructor
|
||||
denise = User.objects.db_manager('other').create_user('denise','denise@example.com')
|
||||
@@ -690,16 +690,16 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(denise_profile._state.db, 'other')
|
||||
# ... but it isn't saved yet
|
||||
self.assertEqual(list(UserProfile.objects.using('default').values_list('flavor',flat=True)),
|
||||
[u'chocolate'])
|
||||
['chocolate'])
|
||||
self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)),
|
||||
[u'crunchy frog', u'spring surprise'])
|
||||
['crunchy frog', 'spring surprise'])
|
||||
|
||||
# When saved, the new profile goes to 'other'
|
||||
denise_profile.save()
|
||||
self.assertEqual(list(UserProfile.objects.using('default').values_list('flavor',flat=True)),
|
||||
[u'chocolate'])
|
||||
['chocolate'])
|
||||
self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)),
|
||||
[u'crunchy frog', u'spring surprise', u'tofu'])
|
||||
['crunchy frog', 'spring surprise', 'tofu'])
|
||||
|
||||
def test_generic_key_separation(self):
|
||||
"Generic fields are constrained to a single database"
|
||||
@@ -728,7 +728,7 @@ class QueryTestCase(TestCase):
|
||||
|
||||
# Retrive related object by descriptor. Related objects should be database-bound
|
||||
self.assertEqual(list(dive.reviews.all().values_list('source', flat=True)),
|
||||
[u'Python Weekly'])
|
||||
['Python Weekly'])
|
||||
|
||||
def test_generic_key_reverse_operations(self):
|
||||
"Generic reverse manipulations are all constrained to a single DB"
|
||||
@@ -746,21 +746,21 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[u'Python Weekly'])
|
||||
['Python Weekly'])
|
||||
|
||||
# Add a second review
|
||||
dive.reviews.add(review2)
|
||||
self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[u'Python Monthly', u'Python Weekly'])
|
||||
['Python Monthly', 'Python Weekly'])
|
||||
|
||||
# Remove the second author
|
||||
dive.reviews.remove(review1)
|
||||
self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[u'Python Monthly'])
|
||||
['Python Monthly'])
|
||||
|
||||
# Clear all reviews
|
||||
dive.reviews.clear()
|
||||
@@ -774,7 +774,7 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[])
|
||||
self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)),
|
||||
[u'Python Daily'])
|
||||
['Python Daily'])
|
||||
|
||||
def test_generic_key_cross_database_protection(self):
|
||||
"Operations that involve sharing generic key objects across databases raise an error"
|
||||
@@ -818,16 +818,16 @@ class QueryTestCase(TestCase):
|
||||
self.assertEqual(review3._state.db, 'other')
|
||||
# ... but it isn't saved yet
|
||||
self.assertEqual(list(Review.objects.using('default').filter(object_id=pro.pk).values_list('source', flat=True)),
|
||||
[u'Python Monthly'])
|
||||
['Python Monthly'])
|
||||
self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source',flat=True)),
|
||||
[u'Python Weekly'])
|
||||
['Python Weekly'])
|
||||
|
||||
# When saved, John goes to 'other'
|
||||
review3.save()
|
||||
self.assertEqual(list(Review.objects.using('default').filter(object_id=pro.pk).values_list('source', flat=True)),
|
||||
[u'Python Monthly'])
|
||||
['Python Monthly'])
|
||||
self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source',flat=True)),
|
||||
[u'Python Daily', u'Python Weekly'])
|
||||
['Python Daily', 'Python Weekly'])
|
||||
|
||||
def test_generic_key_deletion(self):
|
||||
"Cascaded deletions of Generic Key relations issue queries on the right database"
|
||||
@@ -1103,8 +1103,8 @@ class RouterTestCase(TestCase):
|
||||
|
||||
# Related object queries stick to the same database
|
||||
# as the original object, regardless of the router
|
||||
self.assertEqual(list(pro.authors.values_list('name', flat=True)), [u'Marty Alchin'])
|
||||
self.assertEqual(pro.editor.name, u'Marty Alchin')
|
||||
self.assertEqual(list(pro.authors.values_list('name', flat=True)), ['Marty Alchin'])
|
||||
self.assertEqual(pro.editor.name, 'Marty Alchin')
|
||||
|
||||
# get_or_create is a special case. The get needs to be targeted at
|
||||
# the write database in order to avoid potential transaction
|
||||
@@ -1543,7 +1543,7 @@ class RouterTestCase(TestCase):
|
||||
str(qs.query)
|
||||
|
||||
# If you evaluate the query, it should work, running on 'other'
|
||||
self.assertEqual(list(qs.values_list('title', flat=True)), [u'Dive into Python'])
|
||||
self.assertEqual(list(qs.values_list('title', flat=True)), ['Dive into Python'])
|
||||
|
||||
class AuthTestCase(TestCase):
|
||||
multi_db = True
|
||||
|
||||
Reference in New Issue
Block a user