diff options
author | Arthur Koziel <arthur@arthurkoziel.com> | 2010-09-13 00:04:27 +0000 |
---|---|---|
committer | Arthur Koziel <arthur@arthurkoziel.com> | 2010-09-13 00:04:27 +0000 |
commit | dd49269c7db008b2567f50cb03c4d3d9b321daa1 (patch) | |
tree | 326dd25bb045ac016cda7966b43cbdfe1f67d699 /tests/regressiontests/backends | |
parent | c9b188c4ec939abbe48dae5a371276742e64b6b8 (diff) | |
download | django-soc2010/app-loading.tar.gz |
[soc2010/app-loading] merged trunkarchive/soc2010/app-loadingsoc2010/app-loading
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13818 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/backends')
-rw-r--r-- | tests/regressiontests/backends/models.py | 36 | ||||
-rw-r--r-- | tests/regressiontests/backends/tests.py | 133 |
2 files changed, 129 insertions, 40 deletions
diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py index 423bead1ad..7315408d49 100644 --- a/tests/regressiontests/backends/models.py +++ b/tests/regressiontests/backends/models.py @@ -1,5 +1,9 @@ +from django.contrib.contenttypes import generic +from django.contrib.contenttypes.models import ContentType +from django.conf import settings from django.db import models -from django.db import connection +from django.db import connection, DEFAULT_DB_ALIAS + class Square(models.Model): root = models.IntegerField() @@ -8,6 +12,7 @@ class Square(models.Model): def __unicode__(self): return "%s ** 2 == %s" % (self.root, self.square) + class Person(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20) @@ -15,11 +20,40 @@ class Person(models.Model): def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name) + class SchoolClass(models.Model): year = models.PositiveIntegerField() day = models.CharField(max_length=9, blank=True) last_updated = models.DateTimeField() +# Unfortunately, the following model breaks MySQL hard. +# Until #13711 is fixed, this test can't be run under MySQL. +if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql': + class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model): + class Meta: + # We need to use a short actual table name or + # we hit issue #8548 which we're not testing! + verbose_name = 'model_with_long_table_name' + primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField(primary_key=True) + charfield_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.CharField(max_length=100) + m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person,blank=True) + + +class Tag(models.Model): + name = models.CharField(max_length=30) + content_type = models.ForeignKey(ContentType, related_name='backend_tags') + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') + + +class Post(models.Model): + name = models.CharField(max_length=30) + text = models.TextField() + tags = generic.GenericRelation('Tag') + + class Meta: + db_table = 'CaseSensitive_Post' + qn = connection.ops.quote_name __test__ = {'API_TESTS': """ diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index 6a26a608eb..01764135fa 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -1,13 +1,18 @@ # -*- coding: utf-8 -*- # Unit and doctests for specific database backends. import datetime -import models import unittest -from django.db import backend, connection, DEFAULT_DB_ALIAS -from django.db.backends.signals import connection_created + from django.conf import settings +from django.core import management +from django.core.management.color import no_style +from django.db import backend, connection, connections, DEFAULT_DB_ALIAS +from django.db.backends.signals import connection_created +from django.db.backends.postgresql import version as pg_version from django.test import TestCase +from regressiontests.backends import models + class Callproc(unittest.TestCase): def test_dbms_session(self): @@ -76,6 +81,7 @@ class DateQuotingTest(TestCase): classes = models.SchoolClass.objects.filter(last_updated__day=20) self.assertEqual(len(classes), 1) + class ParameterHandlingTest(TestCase): def test_bad_parameter_count(self): "An executemany call with too many/not enough parameters will raise an exception (Refs #12612)" @@ -88,46 +94,95 @@ class ParameterHandlingTest(TestCase): self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),]) self.assertRaises(Exception, cursor.executemany, query, [(1,),]) +# Unfortunately, the following tests would be a good test to run on all +# backends, but it breaks MySQL hard. Until #13711 is fixed, it can't be run +# everywhere (although it would be an effective test of #13711). +if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql': + class LongNameTest(TestCase): + """Long primary keys and model names can result in a sequence name + that exceeds the database limits, which will result in truncation + on certain databases (e.g., Postgres). The backend needs to use + the correct sequence name in last_insert_id and other places, so + check it is. Refs #8901. + """ -def connection_created_test(sender, **kwargs): - print 'connection_created signal' - -__test__ = {'API_TESTS': """ -# Check Postgres version parsing ->>> from django.db.backends.postgresql import version as pg_version - ->>> pg_version._parse_version("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)") -(8, 3, 1) - ->>> pg_version._parse_version("PostgreSQL 8.3.6") -(8, 3, 6) - ->>> pg_version._parse_version("PostgreSQL 8.3") -(8, 3, None) - ->>> pg_version._parse_version("EnterpriseDB 8.3") -(8, 3, None) - ->>> pg_version._parse_version("PostgreSQL 8.3 beta4") -(8, 3, None) + 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.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create() + + def test_sequence_name_length_limits_m2m(self): + """Test an m2m save of a model with a long name and a long m2m field name doesn't error as on Django >=1.2 this now uses object saves. Ref #8901""" + obj = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create() + rel_obj = models.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): + """Test that sequence resetting as part of a flush with model with long name and long pk name doesn't error. Ref #8901""" + # A full flush is expensive to the full test, so we dig into the + # internals to generate the likely offending SQL and run it manually + + # Some convenience aliases + VLM = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ + VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through + tables = [ + VLM._meta.db_table, + VLM_m2m._meta.db_table, + ] + sequences = [ + { + 'column': VLM._meta.pk.column, + 'table': VLM._meta.db_table + }, + ] + cursor = connection.cursor() + for statement in connection.ops.sql_flush(no_style(), tables, sequences): + cursor.execute(statement) ->>> pg_version._parse_version("PostgreSQL 8.4beta1") -(8, 4, None) +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') -"""} + # Reset the sequences for the database + cursor = connection.cursor() + commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [models.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') + self.assertTrue(obj.pk > 10) + +class PostgresVersionTest(TestCase): + def assert_parses(self, version_string, version): + self.assertEqual(pg_version._parse_version(version_string), version) + + def test_parsing(self): + self.assert_parses("PostgreSQL 8.3 beta4", (8, 3, None)) + self.assert_parses("PostgreSQL 8.3", (8, 3, None)) + self.assert_parses("EnterpriseDB 8.3", (8, 3, None)) + self.assert_parses("PostgreSQL 8.3.6", (8, 3, 6)) + self.assert_parses("PostgreSQL 8.4beta1", (8, 4, None)) + self.assert_parses("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)", (8, 3, 1)) # Unfortunately with sqlite3 the in-memory test database cannot be # closed, and so it cannot be re-opened during testing, and so we # sadly disable this test for now. -if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3': - __test__['API_TESTS'] += """ ->>> connection_created.connect(connection_created_test) ->>> connection.close() # Ensure the connection is closed ->>> cursor = connection.cursor() -connection_created signal ->>> connection_created.disconnect(connection_created_test) ->>> cursor = connection.cursor() -""" - -if __name__ == '__main__': - unittest.main() +if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] != "django.db.backends.sqlite3": + class ConnectionCreatedSignalTest(TestCase): + def test_signal(self): + data = {} + def receiver(sender, connection, **kwargs): + data["connection"] = connection + + connection_created.connect(receiver) + connection.close() + cursor = connection.cursor() + self.assertTrue(data["connection"] is connection) + + connection_created.disconnect(receiver) + data.clear() + cursor = connection.cursor() + self.assertTrue(data == {}) |