Organizing Django Tests into Folders

Django comes with a nice testing  rig that lets you individually test each app.  By using a combination of pyunit & Django’s testing tools, you’ll be able to whip up a testing suite for each of your site’s apps (more information here: http://docs.djangoproject.com/en/dev/topics/testing/)

However there is some strangeness as to how Django expects the tests to be organized. From the Django documentation:

For a given Django application, the test runner looks
for doctests in two places:

    * The models.py file. You can define module-level doctests and/or
      a doctest for individual models. It's common practice to put
      application-level doctests in the module docstring and model-level
      doctests in the model docstrings.
    * A file called tests.py in the application directory
      -- i.e., the directory that holds models.py. This file is a
      hook for any and all doctests you want to write that aren't
      necessarily related to models.

Which sounds simple enough, until you end up with a bloated tests.py file that covers 5 different aspects of your app.

Now wouldn’t it be great if you could split them into different files and group them into a folder?

Putting your Tests into a Folder

  1. In your app’s folder, create a new folder called tests
  2. Break up your tests.py file into “logical” (what that exactly means is up to you :P ) python files. Remember to add in the import statements for each file
  3. Move your new files into the tests folder
  4. In tests, create a new file __init__.py
  5. You want to modify your __init__.py as such:
    from [Project Name].[App Name].tests.[filename] import *
    
    #starts the test suite
    __test__= {
               'mytest': [filename]
               }

    Note that the filename’s  .py extension should be excluded from [filename]!

  6. And that’s it! Just follow those basic rules to add all your new test files to __init__.py.
    You can go ahead and delete the original tests.py file now as well. Happy testing!
Posted in Django, Tech, python | Tagged , , , | Leave a comment

Django Unit Test Failure in auth Package

We’ve been using the django.contrib.auth app to handle all of our user login/logout/registration/etc. needs, and it has worked great for us so far.  However, when we tried to run the unit tests for our project we found 10 or so errors with regards to the auth package itself.

At first, I thought it was because we were missing required templates, so I implemented Change Password… to see if I could make some of the unit tests pass. That didn’t work.

So after a bit more googling, I finally found a bug related to my problem: http://code.djangoproject.com/ticket/7756

It turns out that I was missing a required app in my setup.py’s INSTALLED_APP variable. Apparently, all you have to do is add django.contrib.admin to INSTALLED_APP make all the unit tests pass.

Sigh, well at least I got a new feature in!

Posted in Django, Tech | Tagged , , , , , | Leave a comment

Confirm Password Change

I was working with Django’s view.password_change today when I encountered the dialog box above.  As far as I could tell all of my information was being passed to Django correctly; but for reasons unknown to me I was still getting this “Confirm Password Change” message.

After a bit of Googling, I found out that this is actually a Firefox-specific dialog.   It comes up when you want to change a user’s password on a site where Firefox has multiple account information.  Firefox is unable to determine which user’s password you want to change, so it shows this dialog box to ask for confirmation.

As far as I can tell, there is no way to circumvent this message.  Its probably an APD (As Poorly Designed) feature.

For more information, you can hit up these links:

http://forums.asp.net/p/1021685/1383900.aspx#1383900

http://stackoverflow.com/questions/1757510/please-confirm-which-user-you-are-changing-the-password-for

Posted in Tech | Tagged , , | Leave a comment

Incomplete Execution of Callbacks on JQuery POST

In JQuery, you can use the $.post() function to make a client-side AJAX request (more info here).

If you want to POST JSON objects to your server, you basically want to do something like this:

$.post( URL, [data], [CALLBACK FUNCTION], "json")

In the project that I’m working on, I’m rendering a table using JQuery + DataTables plugin. I wanted to embed a button in each row so that the user can selectively delete rows. My onclick function looks something like this:

var row = the tr node of the row where the delete button was clicked
$.post("myURL/",
		{key : value},
		function (jr) {
			if(jr['success'] == true) {
				oTable.fnDeleteRow(oTable.fnGetPosition(row));
			} else {
				alert("Error!!");
			}
		},
		"json");

Basically, I’m making a POST request to the server, sending it the dictionary {key : value}. Upon successful completion of the POST, the inner function will execute.

Unfortunately, when I tried to run this in Firefox, the callback function never executes the delete row line. When I debugged it with Firebug, it would always enter the callback function, but it would never execute oTable.fnDeleteRow(oTable.fnGetPosition(row));

The problem lies in fact that we’re working with asynchronous method invocations. The execution looks something like this:

Execute the onclick function -> make the POST request -> onclick function completes, so trash it -> POST response comes in from server -> execute the callback function.

We’re telling JQuery to delete row X from the table when we get a successful POST response, but the variable row will be out of the scope of the current context by this time. The onclick function would have finished executing and row would be long gone by now.

The ugly fix to this problem would be to pass the variable through to a separate function, like this:

var row = the tr node of the row where the delete button was clicked
$.post("myURL/",
		{key : value},
		function (jr) {
			if(jr['success'] == true) {
				deleteRow(row);
			} else {
				alert("Error!!");
			}
		},
		"json");

function deleteRow(row) {
	oTable.fnDeleteRow(oTable.fnGetPosition(row));
}
Posted in Javascript, Tech | Tagged , , , , , , | Leave a comment

MRU Buffer Replacement Policy

This is what we saw on Google today when we looked up the MRU Buffer Replacement policy for our CSC443 midterm:
Read more about cache algorithms here: http://en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used

Posted in General | Leave a comment