Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created February 24, 2014 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeinthehole/9187671 to your computer and use it in GitHub Desktop.
Save codeinthehole/9187671 to your computer and use it in GitHub Desktop.
Execute custom SQL before tests run (uses the Nose testrunner)
import os
from django.db import connection
def setUp():
"""
Create custom database tables before test suite runs
"""
execute_from_file('stores_table.sql')
execute_from_file('category_table.sql')
def execute_from_file(filename):
"""
Execute an SQL file
"""
filepath = os.path.realpath(os.path.join(
os.path.dirname(__file__), 'sql/', filename))
assert os.path.exists(filepath), "%s does not exist!" % filepath
cursor = connection.cursor()
cursor.execute(open(filepath, 'r').read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment