← Back to team overview

maas-devel team mailing list archive

Avoid database activity at import time

 

I spent a couple of hours (in front of the TV, so not quality
development time) figuring out why so many tests were failing in my
lp:~allenap/maas/repackage branch. The following diff was all I needed:

{{{
=== modified file 'pkg/maas-region/maasserver/forms.py'
--- pkg/maas-region/maasserver/forms.py 2014-01-16 13:54:59 +0000
+++ pkg/maas-region/maasserver/forms.py 2014-01-19 22:02:10 +0000
@@ -217,7 +217,7 @@

     zone = forms.ModelChoiceField(
         label="Physical zone", required=False,
-        initial=Zone.objects.get_default_zone(),
+        initial=Zone.objects.get_default_zone,
         queryset=Zone.objects.all(), to_field_name='name')

     cpu_count = forms.IntegerField(
}}}

The problem is that the original causes database activity at import
time. In tests the database is no longer available during test discovery
which is when the module is imported, and this causes the test runner to
signal an error for every test module that imports maasserver.forms,
directly or indirectly.

Django allows you to pass a callable object as the initial value, so
simply removing the braces is enough to address this issue.


Follow ups