Changeset 74

Show
Ignore:
Timestamp:
Tue Jul 4 21:50:00 2006
Author:
jpellerin
Message:

r3492@Jason-Pellerins-Computer: jhp | 2006-07-04 21:49:43 -0500
Basics are working; tests are far behind.

Files:

Legend:

Unmodified
Added
Removed
Modified
  • nose-django/trunk/setup.py

    r45 r74  
    1   # FIXME  
      1 try:  
      2     import ez_setup  
      3     ez_setup.use_setuptools()  
      4 except ImportError:  
      5     pass  
      6  
      7 from setuptools import setup  
      8  
      9 setup(  
      10     name='NoseDjango',  
      11     version='0.1',  
      12     author='Jason Pellerin',  
      13     author_email = 'jpellerin+nose@gmail.com',  
      14     description = 'nose plugin for easy testing of django projects ' \  
      15     'and apps. Sets up a test database (or schema) and installs apps ' \  
      16     'from test settings file before tests are run, and tears the test ' \  
      17     'database (or schema) down after all tests are run.',  
      18     install_requires='nose>=0.9.0a1',  
      19     license = 'GNU LGPL',  
      20     py_modules = ['nosedjango'],  
      21     entry_points = {  
      22         'nose.plugins': [  
      23             'django = nosedjango:NoseDjango'  
      24             ]  
      25         }  
      26  
      27     )  
      28  
     
  • nose-django/trunk/nosedjango.py

    r73 r74  
    8 8  
    9 9 import atexit  
      10 import logging  
    10 11 import os  
    11 12 import re  
     
    13 14 from nose.plugins import Plugin  
    14 15  
      16 log = logging.getLogger('nose.plugins.nosedjango')  
      17  
    15 18 class NoseDjango(Plugin):  
      19     """  
      20     Enable to set up django test environment before running all tests, and  
      21     tear it down after all tests. If the django database engine in use is not  
      22     sqlite3, one or both of --django-test-db or django-test-schema must be  
      23     specified.  
      24  
      25     Note that your django project must be on PYTHONPATH for the settings file  
      26     to be loaded. The plugin will help out by placing the nose working dir  
      27     into sys.path if it isn't already there, unless the -P  
      28     (--no-path-adjustment) argument is set.  
      29     """  
    16 30     name = 'django'  
    17 31     # FIXME make this a list  
     
    31 45                           dest='django_settings', default=None,  
    32 46                           help="Set module as the DJANGO_SETTINGS_MODULE"  
    33                             " environment variable")  
      47                           " environment variable.")  
    33 47         parser.add_option('--django-test-db', action='store',  
    34 48                           dest='django_test_db',  
     
    38 52                           'drop it after tests have run. May be combined '  
    39 53                           'with --django-test-schema option for databases '  
    40                             'that support schemas [NOSE_DJANGO_TEST_DB]')  
      54                           'that support schemas. This option is not used for '  
      55                           'sqlite3 connections. PLEASE NOTE that if the test '  
      56                           'database already exists, the test run will be '  
      57                           'immediately cancelled. '  
      58                           '[NOSE_DJANGO_TEST_DB]')  
    41 59         parser.add_option('--django-test-schema', action='store',  
    42 60                           dest='django_test_schema',  
     
    46 64                           'with --django-test-db; otherwise, test schema '  
    47 65                           'will be created in the database named by '  
    48                             'DATABASE_NAME in the active django settings '  
      66                           'DATABASE_NAME in the active django settings. '  
      67                           'This option is not used for sqlite3 connections. '  
      68                           'PLEASE NOTE that if the specified schema already '  
      69                           'exists in the target database, the test run will '  
      70                           'be immediately cancelled. '  
    49 71                           '[NOSE_DJANGO_TEST_SCHEMA]')  
    50 72          
     
    65 87     def begin(self):  
    66 88         """Create the test database and schema, if needed, and switch the  
    67           connection over to that database. Then call syncdb to install all apps  
    68           listed in the loaded settings module.  
      89         connection over to that database. Then call install() to install  
      90         all apps listed in the loaded settings module.  
    69 91         """  
      92         # Add the working directory (and any package parents) to sys.path  
      93         # before trying to import django modules; otherwise, they won't be  
      94         # able to find project.settings if the working dir is project/ or  
      95         # project/..  
      96         if self.conf.addPaths:  
      97             from nose.importer import add_path  
      98             add_path(self.conf.where)  
      99          
    70 100         from django.conf import settings  
    71           from django.db import connection  
      101         from django.db import connection         
    71 101         from django.core import management  
    72            
      102         from django.db.models.loading import load_app  
    72 102         self.old_db = settings.DATABASE_NAME  
    73 103  
     
    81 111             self.test_db = ':memory:'  
    82 112         else:  
      113             if not self.test_db and not self.test_schema:  
      114                 raise Exception("When using a database engine other than "  
      115                                 "sqlite3, one or both of --django-test-db "  
      116                                 "and --django-test-schema must be specified")  
    83 117             if self.test_db:  
    84 118                 # create test db for others, if requested  
     
    86 120                 connection.connection.autocommit(1)  
    87 121                 cursor.execute("CREATE DATABASE %s" % self.test_db)  
    88                   self.created_db = self.test_db         
      122                 log.info("Created test database %s", self.test_db)  
      123                 self.created_db = self.test_db  
    89 124         # connect to the test db, if desired  
    90 125         if self.test_db:  
     
    95 130         if self.test_schema:  
    96 131             cursor = connection.cursor()  
      132             connection.connection.autocommit(1)  
    97 133             cursor.execute("CREATE SCHEMA %s" % self.test_schema)  
    98 134             cursor.execute("SET search_path TO %s,pg_catalog" %  
    99 135                            self.test_schema)  
      136             log.info("Created test schema %s", self.test_schema)  
    100 137             self.created_schema = self.test_schema  
    101 138         # sync up the db  
    102           management.syncdb()  
      139         for app in settings.INSTALLED_APPS:  
      140             log.info("Installing django app %s", app)  
      141             management.install(load_app(app))  
    103 142              
    104    
    105       def finalize(self):  
    106           """Clean up any created database and schema  
      143     def finalize(self, result=None):  
      144         """Clean up any created database and schema.  
    107 145         """  
    108 146         from django.conf import settings  
    109 147         from django.db import connection  
      148         import time  
    110 149          
    111 150         if self.created_schema:  
    112 151             cursor = connection.cursor()  
    113               cursor.execute("DROP SCHEMA %s" % self.created_schema)  
      152             connection.connection.autocommit(1)  
      153             cursor.execute("DROP SCHEMA %s CASCADE" % self.created_schema)  
      154             log.info("Dropped schema %s", self.created_schema)  
    114 155             self.created_schema = None # in case run again  
    115 156  
    116 157         if self.created_db:  
    117 158             connection.close()  
      159             time.sleep(1) # avoid "being access by other users" error  
    118 160             settings.DATABASE_NAME = self.old_db  
    119 161             cursor = connection.cursor()  
    120 162             connection.connection.autocommit(1)  
    121 163             cursor.execute("DROP DATABASE %s" % self.created_db)  
    122               self.created_db = None  
      164             log.info("Dropped database %s", self.created_db)  
      165             self.created_db = None # in case run again