Python/Django: unit testing DB apps with fixtures or factory methods?
Posted by Gene at 09:12 on Fri 17 Dec 2010 -- 1 Comments
With Django, you can setup unit test data with fixtures. YAML fixtures are nicer than JSON or XML. But, honestly, setting up an object graph by looking at a flattened out text file is not fun. And using the application to setup test data, then exporting to fixtures is distracting. Examples coming soon.
Tags -
  • I generally try to just mock everything up using backend model interaction to recreate the expected conditions.

    Fixtures are OK for hard coded invariant data but that's rare. If you have a 20K row table in production and have a edge case that needs testing, generally, I want the smallest test case possible so that my test runner maintains some speed since it's all "manual" test kick offs for me.

    - Chris Green
Believe it or not you can debug Django templates with Wing4.0
Posted by Gene at 21:06 on Thu 3 Jun 2010 -- 3 Comments

Wing4.0 is a Django release. Here are the details. You should be able to find out what you need there. So I think I'll just put up some pictures.

Normal view method with a quick test class T to put in the context

Django view method

Awesome!!! Breakpoints in a template.

Django template showing breakpoints

While parked on that breakpoint, use the Debug Probe (pictured) and Call Stack (not pictured)

Wing Debug Probe window

Note the test_func actually was called

The browser rendered template
Tags -
  • With respect: you need THOUSANDS of USD to have breakpoints, simple 100USD is not enough. For THAT money I would prefer to pay Windows Server license for life and have Visual Studio with breakpoints + IronPython + MVC2. That's a joke, I just spent 30 minutes of my life checking this C R A P out and this is basically "disappointing of the decade"

    - Anna
  • Damn it, i was not aware of Wing4 ! This is what i've been looking for.

    - YngwieMalmsteen
  • very awesome

    - Daz
Model a DB with Django, Extensions and Firefox
Posted by Gene at 22:05 on Thu 27 May 2010 -- 1 Comments

Overview

The graph_models command of Django-Extensions will draw you a nice DB schema for your defined models.py. It's simple to use this to rapidly model your DB.

Steps

  1. In a Django application, make some changes to models.py
  2. Run this in an application directory (change 'yourapp') in your Django project to generate and display a graph
    python manage.py graph_models -o yourapp_models.png yourapp
  3. Now you can view the graph with a png viewer - firefox will work
    firefox myapp_models.png
  4. make changes to models.py, and repeat
You can group lines 2 and 3 like this. But, beware that if there is an error in the models, you might be looking at the old png
python manage.py graph_models -o yourapp_models.png yourapp; firefox myapp_models.png
There is one nice side affect. The models are validated when you create the graph. So, when the graph looks right , you can be assured that the models will be clean and ready for a migration. (You migrate, right? If not, try South)
$ python manage.py graph_models -o myapp.png myapp ; firefox myapp.png
Error: One or more models did not validate:
myapp.m1_to_m2: "type": CharFields require a "max_length" attribute that is a positive integer.
Comments welcome!!
Tags -
  • Great app !

    - YngwieMalmsteen
Django Apache WSGI locale and "Currency formatting is not possible using the 'C' locale."
Posted by Gene at 22:04 on Sun 25 Apr 2010 -- 2 Comments

If you want to use the python method locale.currency() to format numbers, you need to make sure you aren't set to the 'C' locale.

You can use the following and serve the code up with Django runserver with no problem. The Python documentation says that this call '...sets the locale...user’s default setting (typically specified in the LANG environment variable).'

import locale
locale.setlocale(locale.LC_ALL, '')

locale.currency(10.50)

If you use Apache, and WSGI to serve up your Django App, you need to include the locale value explicitly. Otherwise you may find the error: Currency formatting is not possible using the 'C' locale.

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

locale.currency(10.50) # $10.50 USD

Incidentally, for my code to be internationalized, I will need to set the locale based on a User preference anyway, so relying on the server environment's setting would not be correct even if it worked.

  • I've found a more complete locale example that might be worth looking at: http://djangosnippets.org/snippets/1825/

    - EyePulp
  • Just what I needed. Thank!

    - Vasili
Running Django's 'manage.py test' within WingIDE
Posted by Gene at 12:04 on Wed 14 Apr 2010 -- 1 Comments

One of the powerful features of WingIDE is that you can debug python code, even web applications and Django, with an interactive debugger that is fast and very feature full.

In this article I discuss how I use it with Django. Suppose you have a unit test that hits a relational DB. With Django you can run python manage.py test, to run these tests. When you test this way, Django will do the following

  1. Create a test database, syncdb your tables, and load any data fixtures
  2. Run the tests
  3. Drop the test database

WingIDE has a Unit Testing interface, but it only allows you to run the tests. In other words, it only runs Step 2 above. I have found no easy way to have Wing do Step 1, then run your selected tests within Wing's Testing system, then do Step 3. I'm working on a way to do this, and will blog about it someday. For now, I run the Django Test system through Wing Debug system, and not through the testing system. Like this.

  1. In your Django project, in Wing, Choose to edit the file properties for manage.py.
  2. In the 'Debug' tab of this dialog, and the 'Run Arguments' box type test (and optionally the app name to run tests just for that app).
  3. Now choose to Debug manage.py by right clicking on that file and choosing Debug Selected

This will cause WingIDE to run the Django test framework. Output will go into the Debug I/O panel. Wing will even stop at breakpoints and enter the debugger.

The advantages of this method

The tests can be run either on the commandline or within WingIDE without any changes or hacks to the source and it is simple to set up.

The disadvantages of this method

You don't get to use the rich Wing interface for running tests which would allow you to run individual tests on demand. Beware too that you might be using manage.py to run the Django runserver within WingIDE. That is also configured in the manage.py Debug dialog. You can only configure one argument to manage.py at a time.

  • Debugging unit tests is very useful.
    I do like Wing but Django's integration is too weak. Running tests should be more ergonomic and template tags should be colorized.
    btw thank you for sharing your knowledge :)

    - YngwieMalmsteen