mirror of
https://github.com/django/django.git
synced 2025-08-31 07:47:37 +00:00
Imported specific models in a few tests that didn't.
This commit is contained in:
parent
9b79281e31
commit
e744c7e459
4 changed files with 181 additions and 166 deletions
|
@ -29,7 +29,11 @@ from django.test import (
|
|||
from django.utils import six
|
||||
from django.utils.six.moves import range
|
||||
|
||||
from . import models
|
||||
from .models import (
|
||||
Article, Item, Object, ObjectReference, Person, Post, RawData, Reporter,
|
||||
ReporterProxy, SchoolClass, Square,
|
||||
VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ,
|
||||
)
|
||||
|
||||
|
||||
class DatabaseWrapperTests(SimpleTestCase):
|
||||
|
@ -139,7 +143,7 @@ class SQLiteTests(TestCase):
|
|||
in order to be monotonically increasing. Refs #10164.
|
||||
"""
|
||||
with connection.schema_editor(collect_sql=True) as editor:
|
||||
editor.create_model(models.Square)
|
||||
editor.create_model(Square)
|
||||
statements = editor.collected_sql
|
||||
match = re.search('"id" ([^,]+),', statements[0])
|
||||
self.assertIsNotNone(match)
|
||||
|
@ -155,13 +159,13 @@ class SQLiteTests(TestCase):
|
|||
"""
|
||||
for aggregate in (Sum, Avg, Variance, StdDev):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
models.Item.objects.all().aggregate(aggregate('time'))
|
||||
Item.objects.all().aggregate(aggregate('time'))
|
||||
with self.assertRaises(NotImplementedError):
|
||||
models.Item.objects.all().aggregate(aggregate('date'))
|
||||
Item.objects.all().aggregate(aggregate('date'))
|
||||
with self.assertRaises(NotImplementedError):
|
||||
models.Item.objects.all().aggregate(aggregate('last_modified'))
|
||||
Item.objects.all().aggregate(aggregate('last_modified'))
|
||||
with self.assertRaises(NotImplementedError):
|
||||
models.Item.objects.all().aggregate(
|
||||
Item.objects.all().aggregate(
|
||||
**{'complex': aggregate('last_modified') + aggregate('last_modified')}
|
||||
)
|
||||
|
||||
|
@ -381,8 +385,8 @@ class DateQuotingTest(TestCase):
|
|||
fields which clash with strings passed to it (e.g. 'year') (#12818).
|
||||
"""
|
||||
updated = datetime.datetime(2010, 2, 20)
|
||||
models.SchoolClass.objects.create(year=2009, last_updated=updated)
|
||||
years = models.SchoolClass.objects.dates('last_updated', 'year')
|
||||
SchoolClass.objects.create(year=2009, last_updated=updated)
|
||||
years = SchoolClass.objects.dates('last_updated', 'year')
|
||||
self.assertEqual(list(years), [datetime.date(2010, 1, 1)])
|
||||
|
||||
def test_django_date_extract(self):
|
||||
|
@ -391,8 +395,8 @@ class DateQuotingTest(TestCase):
|
|||
which clash with strings passed to it (e.g. 'day') (#12818).
|
||||
"""
|
||||
updated = datetime.datetime(2010, 2, 20)
|
||||
models.SchoolClass.objects.create(year=2009, last_updated=updated)
|
||||
classes = models.SchoolClass.objects.filter(last_updated__day=20)
|
||||
SchoolClass.objects.create(year=2009, last_updated=updated)
|
||||
classes = SchoolClass.objects.filter(last_updated__day=20)
|
||||
self.assertEqual(len(classes), 1)
|
||||
|
||||
|
||||
|
@ -408,16 +412,16 @@ class LastExecutedQueryTest(TestCase):
|
|||
connection.ops.last_executed_query(cursor, '', ())
|
||||
|
||||
def test_debug_sql(self):
|
||||
list(models.Reporter.objects.filter(first_name="test"))
|
||||
list(Reporter.objects.filter(first_name="test"))
|
||||
sql = connection.queries[-1]['sql'].lower()
|
||||
self.assertIn("select", sql)
|
||||
self.assertIn(models.Reporter._meta.db_table, sql)
|
||||
self.assertIn(Reporter._meta.db_table, sql)
|
||||
|
||||
def test_query_encoding(self):
|
||||
"""
|
||||
last_executed_query() returns an Unicode string
|
||||
"""
|
||||
data = models.RawData.objects.filter(raw_data=b'\x00\x46 \xFE').extra(select={'föö': 1})
|
||||
data = RawData.objects.filter(raw_data=b'\x00\x46 \xFE').extra(select={'föö': 1})
|
||||
sql, params = data.query.sql_with_params()
|
||||
cursor = data.query.get_compiler('default').execute_sql(CURSOR)
|
||||
last_sql = cursor.db.ops.last_executed_query(cursor, sql, params)
|
||||
|
@ -483,15 +487,15 @@ class LongNameTest(TransactionTestCase):
|
|||
|
||||
def test_sequence_name_length_limits_create(self):
|
||||
"""Test creation of model with long name and long pk name doesn't error. Ref #8901"""
|
||||
models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
|
||||
VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
|
||||
|
||||
def test_sequence_name_length_limits_m2m(self):
|
||||
"""
|
||||
An m2m save of a model with a long name and a long m2m field name
|
||||
doesn't error (#8901).
|
||||
"""
|
||||
obj = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
|
||||
rel_obj = models.Person.objects.create(first_name='Django', last_name='Reinhardt')
|
||||
obj = VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
|
||||
rel_obj = Person.objects.create(first_name='Django', last_name='Reinhardt')
|
||||
obj.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.add(rel_obj)
|
||||
|
||||
def test_sequence_name_length_limits_flush(self):
|
||||
|
@ -503,7 +507,7 @@ class LongNameTest(TransactionTestCase):
|
|||
# internals to generate the likely offending SQL and run it manually
|
||||
|
||||
# Some convenience aliases
|
||||
VLM = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
|
||||
VLM = VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
|
||||
VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
|
||||
tables = [
|
||||
VLM._meta.db_table,
|
||||
|
@ -525,17 +529,17 @@ class SequenceResetTest(TestCase):
|
|||
def test_generic_relation(self):
|
||||
"Sequence names are correct when resetting generic relations (Ref #13941)"
|
||||
# Create an object with a manually specified PK
|
||||
models.Post.objects.create(id=10, name='1st post', text='hello world')
|
||||
Post.objects.create(id=10, name='1st post', text='hello world')
|
||||
|
||||
# Reset the sequences for the database
|
||||
cursor = connection.cursor()
|
||||
commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [models.Post])
|
||||
commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [Post])
|
||||
for sql in commands:
|
||||
cursor.execute(sql)
|
||||
|
||||
# If we create a new object now, it should have a PK greater
|
||||
# than the PK we specified manually.
|
||||
obj = models.Post.objects.create(name='New post', text='goodbye world')
|
||||
obj = Post.objects.create(name='New post', text='goodbye world')
|
||||
self.assertGreater(obj.pk, 10)
|
||||
|
||||
|
||||
|
@ -608,7 +612,7 @@ class BackendTestCase(TransactionTestCase):
|
|||
|
||||
def create_squares(self, args, paramstyle, multiple):
|
||||
cursor = connection.cursor()
|
||||
opts = models.Square._meta
|
||||
opts = Square._meta
|
||||
tbl = connection.introspection.table_name_converter(opts.db_table)
|
||||
f1 = connection.ops.quote_name(opts.get_field('root').column)
|
||||
f2 = connection.ops.quote_name(opts.get_field('square').column)
|
||||
|
@ -627,67 +631,67 @@ class BackendTestCase(TransactionTestCase):
|
|||
# Test cursor.executemany #4896
|
||||
args = [(i, i ** 2) for i in range(-5, 6)]
|
||||
self.create_squares_with_executemany(args)
|
||||
self.assertEqual(models.Square.objects.count(), 11)
|
||||
self.assertEqual(Square.objects.count(), 11)
|
||||
for i in range(-5, 6):
|
||||
square = models.Square.objects.get(root=i)
|
||||
square = Square.objects.get(root=i)
|
||||
self.assertEqual(square.square, i ** 2)
|
||||
|
||||
def test_cursor_executemany_with_empty_params_list(self):
|
||||
# Test executemany with params=[] does nothing #4765
|
||||
args = []
|
||||
self.create_squares_with_executemany(args)
|
||||
self.assertEqual(models.Square.objects.count(), 0)
|
||||
self.assertEqual(Square.objects.count(), 0)
|
||||
|
||||
def test_cursor_executemany_with_iterator(self):
|
||||
# Test executemany accepts iterators #10320
|
||||
args = iter((i, i ** 2) for i in range(-3, 2))
|
||||
self.create_squares_with_executemany(args)
|
||||
self.assertEqual(models.Square.objects.count(), 5)
|
||||
self.assertEqual(Square.objects.count(), 5)
|
||||
|
||||
args = iter((i, i ** 2) for i in range(3, 7))
|
||||
with override_settings(DEBUG=True):
|
||||
# same test for DebugCursorWrapper
|
||||
self.create_squares_with_executemany(args)
|
||||
self.assertEqual(models.Square.objects.count(), 9)
|
||||
self.assertEqual(Square.objects.count(), 9)
|
||||
|
||||
@skipUnlessDBFeature('supports_paramstyle_pyformat')
|
||||
def test_cursor_execute_with_pyformat(self):
|
||||
# Support pyformat style passing of parameters #10070
|
||||
args = {'root': 3, 'square': 9}
|
||||
self.create_squares(args, 'pyformat', multiple=False)
|
||||
self.assertEqual(models.Square.objects.count(), 1)
|
||||
self.assertEqual(Square.objects.count(), 1)
|
||||
|
||||
@skipUnlessDBFeature('supports_paramstyle_pyformat')
|
||||
def test_cursor_executemany_with_pyformat(self):
|
||||
# Support pyformat style passing of parameters #10070
|
||||
args = [{'root': i, 'square': i ** 2} for i in range(-5, 6)]
|
||||
self.create_squares(args, 'pyformat', multiple=True)
|
||||
self.assertEqual(models.Square.objects.count(), 11)
|
||||
self.assertEqual(Square.objects.count(), 11)
|
||||
for i in range(-5, 6):
|
||||
square = models.Square.objects.get(root=i)
|
||||
square = Square.objects.get(root=i)
|
||||
self.assertEqual(square.square, i ** 2)
|
||||
|
||||
@skipUnlessDBFeature('supports_paramstyle_pyformat')
|
||||
def test_cursor_executemany_with_pyformat_iterator(self):
|
||||
args = iter({'root': i, 'square': i ** 2} for i in range(-3, 2))
|
||||
self.create_squares(args, 'pyformat', multiple=True)
|
||||
self.assertEqual(models.Square.objects.count(), 5)
|
||||
self.assertEqual(Square.objects.count(), 5)
|
||||
|
||||
args = iter({'root': i, 'square': i ** 2} for i in range(3, 7))
|
||||
with override_settings(DEBUG=True):
|
||||
# same test for DebugCursorWrapper
|
||||
self.create_squares(args, 'pyformat', multiple=True)
|
||||
self.assertEqual(models.Square.objects.count(), 9)
|
||||
self.assertEqual(Square.objects.count(), 9)
|
||||
|
||||
def test_unicode_fetches(self):
|
||||
# fetchone, fetchmany, fetchall return strings as unicode objects #6254
|
||||
qn = connection.ops.quote_name
|
||||
models.Person(first_name="John", last_name="Doe").save()
|
||||
models.Person(first_name="Jane", last_name="Doe").save()
|
||||
models.Person(first_name="Mary", last_name="Agnelline").save()
|
||||
models.Person(first_name="Peter", last_name="Parker").save()
|
||||
models.Person(first_name="Clark", last_name="Kent").save()
|
||||
opts2 = models.Person._meta
|
||||
Person(first_name="John", last_name="Doe").save()
|
||||
Person(first_name="Jane", last_name="Doe").save()
|
||||
Person(first_name="Mary", last_name="Agnelline").save()
|
||||
Person(first_name="Peter", last_name="Parker").save()
|
||||
Person(first_name="Clark", last_name="Kent").save()
|
||||
opts2 = Person._meta
|
||||
f3, f4 = opts2.get_field('first_name'), opts2.get_field('last_name')
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(
|
||||
|
@ -737,7 +741,7 @@ class BackendTestCase(TransactionTestCase):
|
|||
def test_duplicate_table_error(self):
|
||||
""" Creating an existing table returns a DatabaseError """
|
||||
cursor = connection.cursor()
|
||||
query = 'CREATE TABLE %s (id INTEGER);' % models.Article._meta.db_table
|
||||
query = 'CREATE TABLE %s (id INTEGER);' % Article._meta.db_table
|
||||
with self.assertRaises(DatabaseError):
|
||||
cursor.execute(query)
|
||||
|
||||
|
@ -862,14 +866,14 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
|
||||
def setUp(self):
|
||||
# Create a Reporter.
|
||||
self.r = models.Reporter.objects.create(first_name='John', last_name='Smith')
|
||||
self.r = Reporter.objects.create(first_name='John', last_name='Smith')
|
||||
|
||||
def test_integrity_checks_on_creation(self):
|
||||
"""
|
||||
Try to create a model instance that violates a FK constraint. If it
|
||||
fails it should fail with IntegrityError.
|
||||
"""
|
||||
a1 = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
|
||||
a1 = Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
|
||||
try:
|
||||
a1.save()
|
||||
except IntegrityError:
|
||||
|
@ -877,10 +881,12 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
else:
|
||||
self.skipTest("This backend does not support integrity checks.")
|
||||
# Now that we know this backend supports integrity checks we make sure
|
||||
# constraints are also enforced for proxy models. Refs #17519
|
||||
a2 = models.Article(headline='This is another test', reporter=self.r,
|
||||
pub_date=datetime.datetime(2012, 8, 3),
|
||||
reporter_proxy_id=30)
|
||||
# constraints are also enforced for proxy Refs #17519
|
||||
a2 = Article(
|
||||
headline='This is another test', reporter=self.r,
|
||||
pub_date=datetime.datetime(2012, 8, 3),
|
||||
reporter_proxy_id=30,
|
||||
)
|
||||
with self.assertRaises(IntegrityError):
|
||||
a2.save()
|
||||
|
||||
|
@ -890,9 +896,9 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
If it fails it should fail with IntegrityError.
|
||||
"""
|
||||
# Create an Article.
|
||||
models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
|
||||
Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
|
||||
# Retrieve it from the DB
|
||||
a1 = models.Article.objects.get(headline="Test article")
|
||||
a1 = Article.objects.get(headline="Test article")
|
||||
a1.reporter_id = 30
|
||||
try:
|
||||
a1.save()
|
||||
|
@ -901,14 +907,16 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
else:
|
||||
self.skipTest("This backend does not support integrity checks.")
|
||||
# Now that we know this backend supports integrity checks we make sure
|
||||
# constraints are also enforced for proxy models. Refs #17519
|
||||
# constraints are also enforced for proxy Refs #17519
|
||||
# Create another article
|
||||
r_proxy = models.ReporterProxy.objects.get(pk=self.r.pk)
|
||||
models.Article.objects.create(headline='Another article',
|
||||
pub_date=datetime.datetime(1988, 5, 15),
|
||||
reporter=self.r, reporter_proxy=r_proxy)
|
||||
r_proxy = ReporterProxy.objects.get(pk=self.r.pk)
|
||||
Article.objects.create(
|
||||
headline='Another article',
|
||||
pub_date=datetime.datetime(1988, 5, 15),
|
||||
reporter=self.r, reporter_proxy=r_proxy,
|
||||
)
|
||||
# Retrieve the second article from the DB
|
||||
a2 = models.Article.objects.get(headline='Another article')
|
||||
a2 = Article.objects.get(headline='Another article')
|
||||
a2.reporter_proxy_id = 30
|
||||
with self.assertRaises(IntegrityError):
|
||||
a2.save()
|
||||
|
@ -920,13 +928,13 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
"""
|
||||
with transaction.atomic():
|
||||
# Create an Article.
|
||||
models.Article.objects.create(
|
||||
Article.objects.create(
|
||||
headline="Test article",
|
||||
pub_date=datetime.datetime(2010, 9, 4),
|
||||
reporter=self.r,
|
||||
)
|
||||
# Retrieve it from the DB
|
||||
a = models.Article.objects.get(headline="Test article")
|
||||
a = Article.objects.get(headline="Test article")
|
||||
a.reporter_id = 30
|
||||
try:
|
||||
connection.disable_constraint_checking()
|
||||
|
@ -943,13 +951,13 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
"""
|
||||
with transaction.atomic():
|
||||
# Create an Article.
|
||||
models.Article.objects.create(
|
||||
Article.objects.create(
|
||||
headline="Test article",
|
||||
pub_date=datetime.datetime(2010, 9, 4),
|
||||
reporter=self.r,
|
||||
)
|
||||
# Retrieve it from the DB
|
||||
a = models.Article.objects.get(headline="Test article")
|
||||
a = Article.objects.get(headline="Test article")
|
||||
a.reporter_id = 30
|
||||
try:
|
||||
with connection.constraint_checks_disabled():
|
||||
|
@ -964,13 +972,13 @@ class FkConstraintsTests(TransactionTestCase):
|
|||
"""
|
||||
with transaction.atomic():
|
||||
# Create an Article.
|
||||
models.Article.objects.create(
|
||||
Article.objects.create(
|
||||
headline="Test article",
|
||||
pub_date=datetime.datetime(2010, 9, 4),
|
||||
reporter=self.r,
|
||||
)
|
||||
# Retrieve it from the DB
|
||||
a = models.Article.objects.get(headline="Test article")
|
||||
a = Article.objects.get(headline="Test article")
|
||||
a.reporter_id = 30
|
||||
with connection.constraint_checks_disabled():
|
||||
a.save()
|
||||
|
@ -1052,14 +1060,14 @@ class ThreadTests(TransactionTestCase):
|
|||
"""
|
||||
A connection can be passed from one thread to the other (#17258).
|
||||
"""
|
||||
models.Person.objects.create(first_name="John", last_name="Doe")
|
||||
Person.objects.create(first_name="John", last_name="Doe")
|
||||
|
||||
def do_thread():
|
||||
def runner(main_thread_connection):
|
||||
from django.db import connections
|
||||
connections['default'] = main_thread_connection
|
||||
try:
|
||||
models.Person.objects.get(first_name="John", last_name="Doe")
|
||||
Person.objects.get(first_name="John", last_name="Doe")
|
||||
except Exception as e:
|
||||
exceptions.append(e)
|
||||
t = threading.Thread(target=runner, args=[connections['default']])
|
||||
|
@ -1138,35 +1146,35 @@ class MySQLPKZeroTests(TestCase):
|
|||
@skipIfDBFeature('allows_auto_pk_0')
|
||||
def test_zero_as_autoval(self):
|
||||
with self.assertRaises(ValueError):
|
||||
models.Square.objects.create(id=0, root=0, square=1)
|
||||
Square.objects.create(id=0, root=0, square=1)
|
||||
|
||||
|
||||
class DBConstraintTestCase(TestCase):
|
||||
|
||||
def test_can_reference_existent(self):
|
||||
obj = models.Object.objects.create()
|
||||
ref = models.ObjectReference.objects.create(obj=obj)
|
||||
obj = Object.objects.create()
|
||||
ref = ObjectReference.objects.create(obj=obj)
|
||||
self.assertEqual(ref.obj, obj)
|
||||
|
||||
ref = models.ObjectReference.objects.get(obj=obj)
|
||||
ref = ObjectReference.objects.get(obj=obj)
|
||||
self.assertEqual(ref.obj, obj)
|
||||
|
||||
def test_can_reference_non_existent(self):
|
||||
self.assertFalse(models.Object.objects.filter(id=12345).exists())
|
||||
ref = models.ObjectReference.objects.create(obj_id=12345)
|
||||
ref_new = models.ObjectReference.objects.get(obj_id=12345)
|
||||
self.assertFalse(Object.objects.filter(id=12345).exists())
|
||||
ref = ObjectReference.objects.create(obj_id=12345)
|
||||
ref_new = ObjectReference.objects.get(obj_id=12345)
|
||||
self.assertEqual(ref, ref_new)
|
||||
|
||||
with self.assertRaises(models.Object.DoesNotExist):
|
||||
with self.assertRaises(Object.DoesNotExist):
|
||||
ref.obj
|
||||
|
||||
def test_many_to_many(self):
|
||||
obj = models.Object.objects.create()
|
||||
obj = Object.objects.create()
|
||||
obj.related_objects.create()
|
||||
self.assertEqual(models.Object.objects.count(), 2)
|
||||
self.assertEqual(Object.objects.count(), 2)
|
||||
self.assertEqual(obj.related_objects.count(), 1)
|
||||
|
||||
intermediary_model = models.Object._meta.get_field("related_objects").remote_field.through
|
||||
intermediary_model = Object._meta.get_field("related_objects").remote_field.through
|
||||
intermediary_model.objects.create(from_object_id=obj.id, to_object_id=12345)
|
||||
self.assertEqual(obj.related_objects.count(), 1)
|
||||
self.assertEqual(intermediary_model.objects.count(), 2)
|
||||
|
@ -1234,7 +1242,7 @@ class TestSqliteThreadSharing(TransactionTestCase):
|
|||
|
||||
def test_database_sharing_in_threads(self):
|
||||
def create_object():
|
||||
models.Object.objects.create()
|
||||
Object.objects.create()
|
||||
|
||||
create_object()
|
||||
|
||||
|
@ -1242,4 +1250,4 @@ class TestSqliteThreadSharing(TransactionTestCase):
|
|||
thread.start()
|
||||
thread.join()
|
||||
|
||||
self.assertEqual(models.Object.objects.count(), 2)
|
||||
self.assertEqual(Object.objects.count(), 2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue