Monday, March 31, 2008

Nose: db setup and teardown

In the last post I noted some documentation related to nose and ORM. Well, they did not work for me because my setup was not exactly like others'. Here is what worked for me. In

tests/__init__.py
:

  • I imported
    from pylons import config
    to get the SQLAlchemy engine embedded in pylons's config variable

  • I imported
    import quickwiki.model as model
    so that I could get hold of my models and metadata

  • I created a class
    TestModel
    inheriting from
    TestCase
    to hold the setup and teardown code

  • In the
    tearDown
    method, I do
    model.metadata.drop_all(bind=engine)
    to destroy all tables

  • In the
    setUp
    method, I call
    tearDown
    to destroy tables if they have not already been cleaned up, and then call
    model.metadata.create_all(bind=engine)
    to create the tables.



Here is the final code:

"""Pylons application test package

When the test runner finds and executes tests within this directory,
this file will be loaded to setup the test environment.

It registers the root directory of the project in sys.path and
pkg_resources, in case the project hasn't been installed with
setuptools. It also initializes the application via websetup (paster
setup-app) with the project's test.ini configuration file.
"""

import os
import sys
from unittest import TestCase

import pkg_resources
import paste.fixture
import paste.script.appinstall
from paste.deploy import loadapp
from routes import url_for

__all__ = ['url_for', 'TestController']

here_dir = os.path.dirname(os.path.abspath(__file__))
conf_dir = os.path.dirname(os.path.dirname(here_dir))

sys.path.insert(0, conf_dir)
pkg_resources.working_set.add_entry(conf_dir)
pkg_resources.require('Paste')
pkg_resources.require('PasteScript')

test_file = os.path.join(conf_dir, 'test.ini')
cmd = paste.script.appinstall.SetupCommand('setup-app')
cmd.run([test_file])

from pylons import config
import quickwiki.model as model

class TestModel(TestCase):
"""
We want the database to be created from scratch before each test and dropped
after each test (thus making them unit tests).
"""

def setUp(self):
self.tearDown()
engine = config['pylons.g'].sa_engine
model.metadata.create_all(bind=engine)

page = model.Page()
page.title = 'FrontPage'
page.content = 'Welcome to the QuickWiki front page'
model.Session.save(page)
model.Session.commit()



def tearDown(self):

engine = config['pylons.g'].sa_engine
model.metadata.drop_all(bind=engine)


class TestController(TestModel):

def __init__(self, *args, **kwargs):
wsgiapp = loadapp('config:test.ini', relative_to=conf_dir)
self.app = paste.fixture.TestApp(wsgiapp)
TestCase.__init__(self, *args, **kwargs)




Well, that almost worked. I fell foul of the 'setup-app' command and 'setup-config' in websetup.py. As you can see, the 'tests/__init__.py' file loads and executes the paster 'setup-app' command. Stands to reason: the app should be 'set up' before I run tests.

My setup-app is responsible for creating the DB and populating it with some initial data. But now I can't repeat my tests, because the first time I run the tests, the db is created and initial data put in, and the next time I run the tests... poof:

IntegrityError: (IntegrityError) column title is not unique
u'INSERT INTO pages (title, content) VALUES (?, ?)' ['FrontPage',
'Welcome to the QuickWiki front page']


Well, I thought, I would just drop everything in the db in the tearDown and ensure that tearDown is called before setUp is run. No go. For some reason, it seems, the tearDown() is not working.

Well, it seems I must have at least one test, for the fixtures to be run. So I created a dummy test, and all was well.

Pylons, Paste, Nose and ORMs

Trying to do some unit tests in Pylons. Pylons uses nose. However, the Pylons Unit Testing guide is a little short on describing how to setup and teardown the database before each test. Here is all the docco to hand:


Saturday, March 29, 2008

The Open Handset Alliance, led by Google, is developing and releasing the Android mobile OS.



But Android is not the only open mobile OS in town. Another noteworthy player is the LiMo foundation. Its mission is to produce an open, Linux based software platform for mobile devices. LiMo has been around longer and there are already LiMo devices in the market.



There are other efforts towards putting Linux on mobile devices:


  • Motorola has a Linux-Java based platform called MOTOMAGX. It is not an open polatform, AFAIK. Also, Motorola's mobile division's future is uncertain rigt now.

  • Openmoko is trying to create both a software platform and hardware; they release the design to the hardware as well.

  • Ubuntu Mobile
  • .



Nokia recently joined LiMo. Does this mean a Googls vs Nokia fight in the mobile OS arean? Not to mention, Symbian OS and Windows Mobile are the current dominant players and already have application developer mindshare.

Sunday, March 16, 2008

Typing accented/european characters in Emacs

Normally I never have to type anything but ASCII in Emacs. But I have been doing some internationalization testing/development, and now and then need to type some non-ASCII characters. Right now, I am happy to be able to type accented characters, though the ultimate goal is to be able to type in a wide variety of languages.

To get emacs to display most languages and scripts:


  • I ensure than my LANG environment is UTF-8
  • I install xfonts-intl-* and emacs-intl-fonts packages
  • I do a `C-h h' to see the multilingual hello file.... the languages of my interest should be present there


To type `interesting' characters in Emacs, I have found that this works:

  • Change input method to RFC1345 by:

    C-x RET C-\ RET (select default input method RFC1345)

  • Type an accented chanracter using its RFC1345 mnemonic. E.g., to type the inverted exclamation mark used in Spanish I would type "&!I" (makes sense: '&' introduces the special character, ! is the excalamation mark, and I inverts it).


To be able to quickly lookup the RFC, I installed the `doc-rfc' meta-package. Now, to read RFC3412, for example, I do:

C-x C-f /usr/share/doc/RFC/links/rfc3412.txt.gz


and since I have installed the `emacs-goodies-el' package, which contains `rfcview', the RFC shows up nicely formatted in Emacs.

Saturday, March 15, 2008

Ticking noise from HDD

Often, my laptop's HDD makes a chirp/ticking sound. Tracked it down to this issue and this fix.

In short, the issue is that the HDD's firmware is set to save power by parking its head; but the OS is accessing the HDD quite frequently. As a result, the HDD aggressively parks its head, and then has to unpark it because the OS does a read/write. Turns out this parking/unparking could wear out the driver faster than if there was no power management.

Additionally, this happens even when the laptop is on AC... where there is no power saving advantage.

The fix linked above simply turns off the HDD's power management.

Saturday, March 1, 2008

How to choose clothes to wear


  • Choose according to your station

    • Choose according to what looks good on you

      • Exclude what you wore yesterday

        • Out of the rest, choose what you like