{"id":2301,"date":"2012-03-05T18:41:05","date_gmt":"2012-03-05T23:41:05","guid":{"rendered":"http:\/\/blogs.terrorware.com\/geoff\/?p=2301"},"modified":"2012-03-05T19:14:27","modified_gmt":"2012-03-06T00:14:27","slug":"making-sure-south-migrations-get-run-when-using-djangos-create_test_db","status":"publish","type":"post","link":"https:\/\/blogs.terrorware.com\/geoff\/2012\/03\/05\/making-sure-south-migrations-get-run-when-using-djangos-create_test_db\/","title":{"rendered":"Making sure South migrations get run when using Django&#8217;s create_test_db()"},"content":{"rendered":"<p>I&#8217;ve been experimenting with using Lettuce for a project.\u00a0 When not using Django&#8217;s test runner, you can use the framework&#8217;s test database hooks by calling <em>create_test_db()<\/em> (<a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/topics\/testing\/#django.db.connection.creation.create_test_db\">see the Django docs for create_test_db()<\/a>) from a method in your terrain.\u00a0 <a href=\"http:\/\/cilliano.com\/blog\/2011\/02\/07\/django-bdd-with-lettuce-and-splinter\/\">Django Full Stack Testing and BDD with Lettuce and Splinter<\/a> is a great resource for seeing how to get up and running.\u00a0 But, I was having a terrible time because <em>create_test_db()<\/em> was throwing an exception because it was trying to run the <em>flush<\/em> management command to flush data on a table that hadn&#8217;t yet been created.\u00a0 <a title=\"South Unit Test Integration\" href=\"http:\/\/south.aeracode.org\/docs\/unittests.html\">According to South&#8217;s documentation<\/a>, &#8220;South\u2019s syncdb command will also apply migrations if it\u2019s run in non-interactive mode, which includes when you\u2019re running tests.&#8221;<\/p>\n<p>While South&#8217;s <em>syncdb<\/em> command was getting executed by <em>create_test_db()<\/em>, the option that tells the command to run the migrations after running <em>syncdb<\/em>, wasn&#8217;t getting properly set.\u00a0 It turns out there is a (not very well documented) workaround, you have to call<em> south.management.commands.patch_for_test_db_setup()<\/em> before your call to <em>create_test_db()<\/em>.<\/p>\n<p>So, your terrain.py might look something like this:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom lettuce import before, after, world\r\nfrom django.db import connection\r\nfrom django.test.utils import setup_test_environment, teardown_test_environment\r\nfrom south.management.commands import patch_for_test_db_setup\r\nfrom splinter.browser import Browser\r\n\r\n@before.runserver\r\ndef setup(server):\r\n# Force running migrations after syncdb.\r\n# syncdb gets run automatically by create_test_db(), and\r\n# South's syncdb (that runs migrations after the default\r\n# syncdb) normally gets called in a test environment, but\r\n# apparently not when calling create_test_db().\r\n# So, we have to use this monkey patched version.\r\npatch_for_test_db_setup()\r\nconnection.creation.create_test_db()\r\nsetup_test_environment()\r\nworld.browser = Browser('webdriver.firefox')\r\n\r\n# ...\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been experimenting with using Lettuce for a project.\u00a0 When not using Django&#8217;s test runner, you can use the framework&#8217;s test database hooks by calling create_test_db() (see the Django docs for create_test_db()) from a method in your terrain.\u00a0 Django Full Stack Testing and BDD with Lettuce and Splinter is a great resource for seeing how&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.terrorware.com\/geoff\/2012\/03\/05\/making-sure-south-migrations-get-run-when-using-djangos-create_test_db\/\">Continue reading <span class=\"screen-reader-text\">Making sure South migrations get run when using Django&#8217;s create_test_db()<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[20628,20626,20625,61,724,20624,20627],"class_list":["post-2301","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bdd","tag-lettuce","tag-migrations","tag-programming","tag-python","tag-south","tag-testing","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4wnIz-B7","_links":{"self":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/2301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/comments?post=2301"}],"version-history":[{"count":3,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/2301\/revisions"}],"predecessor-version":[{"id":2306,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/posts\/2301\/revisions\/2306"}],"wp:attachment":[{"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/media?parent=2301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/categories?post=2301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.terrorware.com\/geoff\/wp-json\/wp\/v2\/tags?post=2301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}