Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado. Django-like testrunner and test database

I like django unit tests, cause they create and drop test database on run.

What ways to create test database for tornado exists?

UPD: I'm interested in postgresql test database creation on test run.

like image 650
Nikolay Fominyh Avatar asked May 25 '12 13:05

Nikolay Fominyh


1 Answers

I found the easiest way is just to use a SQL dump for the test database. Create a database, populate it with fixtures, and write it out to file. Simply call load_fixtures before you run your tests (or whenever you want to reset the DB). This method can certainly be improved, but it's been good enough for my needs.

import os
import unittest2

import tornado.database

settings = dict(
    db_host="127.0.0.1:3306",
    db_name="testdb",
    db_user="testdb",
    db_password="secret",
    db_fixtures_file=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fixtures.sql'),
)

def load_fixtures():
    """Fixtures are stored in an SQL dump.
    """
    os.system("psql %s --user=%s --password=%s < %s" % (settings['db_name'], 
        settings['db_user'], settings['db_password'], settings['db_fixtures_file']))

    return tornado.database.Connection(
        host=settings['db_host'], database=settings['db_name'],
        user=settings['db_user'], password=settings['db_password'])
like image 200
Cole Maclean Avatar answered Oct 03 '22 17:10

Cole Maclean