2 from django
.core
.management
import call_command
4 from StringIO
import StringIO
6 class CommandsTest(TestCase
):
8 self
.stdout
= sys
.stdout
11 sys
.stdout
= self
.stdout
13 def test_that_syncdb_raises_exception_based_on_setting(self
):
14 from django
.conf
import settings
15 old_setting
= getattr(settings
, 'DISABLE_SYNCDB', False)
17 settings
.DISABLE_SYNCDB
= True
18 self
.assertRaises(SystemExit, call_command
, 'syncdb')
20 settings
.DISABLE_SYNCDB
= False
21 self
.assert_(not self
.pipe_command('syncdb'))
23 settings
.DISABLE_SYNCDB
= old_setting
25 def pipe_command(self
, *args
, **kwargs
):
26 sys
.stdout
= StringIO()
27 call_command(*args
, **kwargs
)
28 res
= sys
.stdout
.getvalue()
29 sys
.stdout
= self
.stdout
32 def test_dmigration(self
):
33 from django
.conf
import settings
34 if 'django.contrib.sessions' not in settings
.INSTALLED_APPS
:
35 print "WARNING: Skipping ./manage.py dmigration tests, " \
36 "add django.contrib.sessions to INSTALLED_APPS to run them"
39 # ./manage.py dmigration addcolumn sessions session session_key
40 actual
= self
.pipe_command(
41 'dmigration', "addcolumn", "sessions", "session", "session_key",
44 expected
= """from dmigrations.mysql import migrations as m\nimport datetime\nmigration = m.AddColumn('sessions', 'session', 'session_key', 'varchar(40) NOT NULL PRIMARY KEY')\n\n"""
45 self
.assert_equal(expected
, actual
)
47 # ./manage.py dmigration migration new something
48 actual
= self
.pipe_command(
49 'dmigration', "new", "something", output
=True
51 expected
= """from dmigrations.mysql import migrations as m
53 class CustomMigration(m.Migration):
57 super(MyMigration, self).__init__(sql_up=sql_up, sql_down=sql_down)
58 # Or override the up() and down() methods\n\nmigration = CustomMigration()\n\n"""
59 self
.assert_equal(expected
, actual
)
61 # ./manage.py dmigration addindex sessions session expire_date
62 actual
= self
.pipe_command(
63 'dmigration', "addindex", "sessions", "session", "expire_date",
66 expected
= """from dmigrations.mysql import migrations as m\nimport datetime\nmigration = m.AddIndex('sessions', 'session', 'expire_date')\n\n"""
67 self
.assert_equal(expected
, actual
)
69 # Simply check they don't raise an exception
70 actual
= self
.pipe_command(
71 'dmigration', "app", "sessions", output
=True
74 actual
= self
.pipe_command(
75 'dmigration', "addtable", "sessions", "session", output
=True
78 #actual = self.pipe_command(
79 # 'dmigration', "insert", "sessions", "session", output=True