Testing Django apps with MongoDB

April 14th, 2010 Alexander Posted in Agile, Programming, Web development 1 Comment »

I've fallen in love with #NoSQL. So I'm doing my storage with MongoDB for an application I'm working on. But since I'm also a testing zealot I had to figure out how to test my app properly, with fixtures and all.

There's already some good code out there that will help you develop your Django app using MongoDB - namely MongoKit and django-mongokit.

MongoKit is a great way to make a structure for your document. Complete with validation and all. And by combining it with django-mongokit you also get automatic test database create/drop.

models.py:

from django_mongokit.document import DjangoDocument
from django_mongokit import connection

class Country(DjangoDocument):
    structure = {
        'iso3': unicode,
        'iso2': unicode,
        'name': unicode,
    }

    required_fields = ['iso3', 'iso2', 'name']
    use_dot_notation = True

connection.register([Country])

tests.py

from django_mongokit import get_database 

from django.test import TestCase
from models import *
class ModelsTest(TestCase):
db = get_database()
def test_should_save_country(self): country = self.db.countries.Country() country.iso2 = u'GB' country.iso3 = u'GBR' country.name = u'UNITED KINGDOM' country.save() self.assertTrue(self.db.countries.find())

But the fun doesn't stop there. You can also easily add some fixtures by creating a python file to hold some dicts for you:

countries.py:

countries = [
{"iso2" : "AF", "iso3" : "AFG", "name" : "AFGHANISTAN" },
{"iso2" : "AL", "iso3" : "ALB", "name" : "ALBANIA" },
]
And add this to tests.py:
from countries import countries

class ModelsTest(TestCase):
    def setUp(self):
        db = get_database()
        db.countries.insert(countries)

    def tearDown(self):
        db = get_database()
        db.drop_collection('countries')
Your countries collection will now be populated every time TestCase is created, and dropped when the tests are done running.
AddThis Social Bookmark Button

rechargenews.com released

January 16th, 2009 Alexander Posted in Web development No Comments »

rechargelogo

I've lately been spending my working hours getting rechargenews.com up and running. And now its here! The web release was friday 9th, and the first printed newspaper came out today.

All the design was delivered to us from apt, but we implemented the design and controllers inhouse. And I'm almost a bit proud to say that this has become one of the most visually and user friendly news sites on the web.

Rechargenews will deliver news and editorials on renewable energy - an important issue in today. And it is my hope that it will influence people to make choiches that will lead to a habitable planet in the future.

Using scrum on this project has made us able to deliver on time, and with an acceptable feature set. More will surely come in the future, but the important bits are already there.

This release marked the end of my commitment for this project at this time, and I will be moving my attention to the new work that needs to be done to dn.no - the leading financial news site in Norway.

AddThis Social Bookmark Button

Remove bottlenecks and optimize the web with Javascript

December 16th, 2008 Alexander Posted in Web development 2 Comments »

speed boat

There are times when you want to embed 3rd party elements into your web pages. If you have a high traffic site which has time critical output, you might come to realize that your 3rd party vendor doesn't have the same uptime as yourself, resulting in delayed GET statements that seem to bog down your site.

Here you are left with a couple of choices.

  • Ditching the supplied elements all together, usually a very unpopular decision.
  • Caching it - which might be a problem with licensing and time critical elements.
  • Use JavaScript to fetch those elements - as explained in this article.

The goal here is to give a usable page to your visitors as quickly as possible.

Read the rest of this entry »

AddThis Social Bookmark Button

Analyzing your website for search engine optimization

June 26th, 2008 Alexander Posted in Web development No Comments »

Google Toolbar Pagerank

In accordance with my previous article about search engine optimization tips, I will in this article concentrate on how to dissect your website and figure out what needs to be done. And I'll only use free tools doing it :)

Read the rest of this entry »

AddThis Social Bookmark Button

Search Engine Optimization Tips

June 20th, 2008 Alexander Posted in Web development 1 Comment »

Optimus Prime - Prime OptimizerI'm doing search engine optimization on some of our sites now. And in that process I have collected some tips about what helps the crawler do its job.

You are looking for new readers for your glorious content, but nobody is finding their way to your site. Did you wall yourself in by an accident?

SEO is usually a lot of common sense, really, and if you have good developers on your site with a bit of time on their hands you can do the job yourself instead of contracting this out.

Read the rest of this entry »

AddThis Social Bookmark Button

AJAX event handling with prototype

January 21st, 2008 Alexander Posted in Web development No Comments »

Separating CSS and HTML has been the big fuss for a while now. So why not separate your Javascript and HTML as well? Using event handlers instead of using the old onclick="somefunction()" can make your codebase a lot more manageable and clean. I'll demonstrate this technique by using prototype.js, a very nice and lean AJAX framework.

My goal is to create two lists (ul elements) with several <li>. Each of these li elements have checkboxes. And when you click on these checkboxes they will move from one list to the other. Okay, lets get cracking...

Read the rest of this entry »

AddThis Social Bookmark Button