diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2012-06-19 13:25:22 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2012-06-19 13:25:22 +0100 |
| commit | 959a3f9791d780062c4efe8765404a8ef95e87f0 (patch) | |
| tree | 4f9aee7ad1ce74a48ea9c82664f6a2fe103b6ef6 /tests | |
| parent | 8ba5bf31986fa746ecc81683c64999dcea4f8e0a (diff) | |
Add some field schema alteration methods and tests.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/modeltests/schema/tests.py | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/tests/modeltests/schema/tests.py b/tests/modeltests/schema/tests.py index 6d5d27cdf1..83b2dabd45 100644 --- a/tests/modeltests/schema/tests.py +++ b/tests/modeltests/schema/tests.py @@ -2,8 +2,9 @@ from __future__ import absolute_import import copy import datetime from django.test import TestCase -from django.db.models.loading import cache from django.db import connection, DatabaseError, IntegrityError +from django.db.models.fields import IntegerField, TextField +from django.db.models.loading import cache from .models import Author, Book @@ -18,6 +19,8 @@ class SchemaTests(TestCase): models = [Author, Book] + # Utility functions + def setUp(self): # Make sure we're in manual transaction mode connection.commit_unless_managed() @@ -51,6 +54,18 @@ class SchemaTests(TestCase): cache.app_store = self.old_app_store cache._get_models_cache = {} + def column_classes(self, model): + cursor = connection.cursor() + return dict( + (d[0], (connection.introspection.get_field_type(d[1], d), d)) + for d in connection.introspection.get_table_description( + cursor, + model._meta.db_table, + ) + ) + + # Tests + def test_creation_deletion(self): """ Tries creating a model's table, and then deleting it. @@ -100,3 +115,60 @@ class SchemaTests(TestCase): pub_date = datetime.datetime.now(), ) connection.commit() + + def test_create_field(self): + """ + Tests adding fields to models + """ + # Create the table + editor = connection.schema_editor() + editor.start() + editor.create_model(Author) + editor.commit() + # Ensure there's no age field + columns = self.column_classes(Author) + self.assertNotIn("age", columns) + # Alter the name field to a TextField + new_field = IntegerField(null=True) + new_field.set_attributes_from_name("age") + editor = connection.schema_editor() + editor.start() + editor.create_field( + Author, + new_field, + ) + editor.commit() + # Ensure the field is right afterwards + columns = self.column_classes(Author) + self.assertEqual(columns['age'][0], "IntegerField") + self.assertEqual(columns['age'][1][6], True) + + def test_alter(self): + """ + Tests simple altering of fields + """ + # Create the table + editor = connection.schema_editor() + editor.start() + editor.create_model(Author) + editor.commit() + # Ensure the field is right to begin with + columns = self.column_classes(Author) + self.assertEqual(columns['name'][0], "CharField") + self.assertEqual(columns['name'][1][3], 255) + self.assertEqual(columns['name'][1][6], False) + # Alter the name field to a TextField + new_field = TextField(null=True) + new_field.set_attributes_from_name("name") + editor = connection.schema_editor() + editor.start() + editor.alter_field( + Author, + Author._meta.get_field_by_name("name")[0], + new_field, + ) + editor.commit() + # Ensure the field is right afterwards + columns = self.column_classes(Author) + self.assertEqual(columns['name'][0], "TextField") + self.assertEqual(columns['name'][1][6], True) |
