Just Trying to Test Leads to Cleaner Code

by byamabe on October 27, 2009

As I mentioned in my last post, I’ve been trying to get some unit testing going so that I can keep focused and gain a higher level of comfort with the code I’m writing. I started by trying move a business method that had crept into the model and put it in its own business class. To test the new business class I tried to use Mock to imitate the behavior of the data model, but I couldn’t find a way to get it to mimic the behavior of a Query object which is both indexed and has methods on it. I did learn that Django fixtures were supported by app-engine-patch. In fact, I learned that most of the ./manage.py functionality works. I had incorrectly assumed that app-engine-patch only allowed you run a Django app on App Engine server, not that it could be used like standard Django. After writing a test for the new business class, I created a fixture to support the test. Well, the test kept failing even though it should have passed. I did some digging, and it seems that the models in the fixture that have relationships aren’t getting loaded. I’ve got a question out to the list to see if this is actually the case, but I can’t think of another explanation.

So, I didn’t actually get any unit tests working, but while writing the test I noticed that there were model classes in my view as well as the database classes in my controller and business logic in my models. So now I’ve got it all cleaned up and the code appears to be working (no tests though). I keep seeing what kind of tests I can write and maybe someone will fix the problem with models with relationships in fixtures.

Previous post:

Next post: