Ticket #3310: test_applications_with_no_models.patch

File test_applications_with_no_models.patch, 9.0 KB (added by medhat, 18 years ago)
  • django/core/management.py

     
    12221222    from django.db.models import get_app, get_apps
    12231223
    12241224    if len(app_labels) == 0:
    1225         app_list = get_apps()
     1225        app_list = get_apps(True)
    12261226    else:
    1227         app_list = [get_app(app_label) for app_label in app_labels]
     1227        app_list = [get_app(app_label,return_package=True) for app_label in app_labels]
    12281228
    12291229    test_path = settings.TEST_RUNNER.split('.')
    12301230    # Allow for Python 2.5 relative paths
  • django/db/models/loading.py

     
    77
    88__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models')
    99
    10 _app_list = []   # Cache of installed apps.
    11                  # Entry is not placed in app_list cache until entire app is loaded.
    12 _app_models = {} # Dictionary of models against app label
    13                  # Each value is a dictionary of model name: model class
    14                  # Applabel and Model entry exists in cache when individual model is loaded.
    15 _app_errors = {} # Dictionary of errors that were experienced when loading the INSTALLED_APPS
    16                  # Key is the app_name of the model, value is the exception that was raised
    17                  # during model loading.
    18 _loaded = False  # Has the contents of settings.INSTALLED_APPS been loaded?
    19                  # i.e., has get_apps() been called?
     10_app_list = []      # Cache of installed apps.
     11                    # Entry is not placed in app_list cache until entire app is loaded.
     12_app_pckg_list = [] # Similar to _app_list but will have the application package insted of
     13                    # the models module. Unlike _app_list, list will also include applications
     14                    # that do not define models.
     15_app_models = {}    # Dictionary of models against app label
     16                    # Each value is a dictionary of model name: model class
     17                    # Applabel and Model entry exists in cache when individual model is loaded.
     18_app_errors = {}    # Dictionary of errors that were experienced when loading the INSTALLED_APPS
     19                    # Key is the app_name of the model, value is the exception that was raised
     20                    # during model loading.
     21_loaded = False     # Has the contents of settings.INSTALLED_APPS been loaded?
     22                    # i.e., has get_apps() been called?
    2023
    21 def get_apps():
    22     "Returns a list of all installed modules that contain models."
     24def get_apps(return_package=False):
     25    "Returns a list of all installed modules that contain models. If return_package is True it will return all application packages in INSTALLED_APPS."
    2326    global _app_list
     27    global _app_pckg_list
    2428    global _loaded
    2529    if not _loaded:
    2630        _loaded = True
     
    3034            except Exception, e:
    3135                # Problem importing the app
    3236                _app_errors[app_name] = e
     37    if return_package:
     38        return _app_pckg_list
    3339    return _app_list
    3440
    35 def get_app(app_label, emptyOK=False):
    36     "Returns the module containing the models for the given app_label. If the app has no models in it and 'emptyOK' is True, returns None."
     41def get_app(app_label, emptyOK=False, return_package=False):
     42    "Returns the module containing the models for the given app_label. If the app has no models in it and 'emptyOK' is True, returns None. Otherwise, if return_package is True, will return the package of the application."
    3743    get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.
    3844    for app_name in settings.INSTALLED_APPS:
    3945        if app_label == app_name.split('.')[-1]:
    40             mod = load_app(app_name)
    41             if mod is None:
    42                 if emptyOK:
    43                     return None
     46            if return_package:
     47                pckg = load_app(app_name,True)
     48                if pckg:
     49                    return pckg
    4450            else:
    45                 return mod
     51                mod = load_app(app_name)
     52                if mod is None:
     53                    if emptyOK:
     54                        return None
     55                else:
     56                    return mod
    4657    raise ImproperlyConfigured, "App with label %s could not be found" % app_label
    4758
    48 def load_app(app_name):
    49     "Loads the app with the provided fully qualified name, and returns the model module."
     59def load_app(app_name, return_package=False):
     60    "Loads the app with the provided fully qualified name, and returns the model module. If return_package is True, will return the application package instead."
    5061    global _app_list
    5162    mod = __import__(app_name, {}, {}, ['models'])
     63    if mod not in _app_pckg_list:
     64        _app_pckg_list.append(mod)
    5265    if not hasattr(mod, 'models'):
     66        if return_package:
     67            return mod
    5368        return None
    5469    if mod.models not in _app_list:
    5570        _app_list.append(mod.models)
     71    if return_package:
     72        return mod
    5673    return mod.models
    5774
    5875def get_app_errors():
  • django/test/simple.py

     
    77
    88# The module name for tests outside models.py
    99TEST_MODULE = 'tests'
    10    
     10
    1111doctestOutputChecker = OutputChecker()
    1212
    13 def build_suite(app_module):
     13def build_suite(app_package):
    1414    "Create a complete Django test suite for the provided application module"
    1515    suite = unittest.TestSuite()
    1616   
    17     # Load unit and doctests in the models.py file
    18     suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module))
    19     try:
    20         suite.addTest(doctest.DocTestSuite(app_module,
    21                                            checker=doctestOutputChecker,
    22                                            runner=DocTestRunner))
    23     except ValueError:
    24         # No doc tests in models.py
    25         pass
    26    
    27     # Check to see if a separate 'tests' module exists parallel to the
     17    if hasattr(app_package,'models'):
     18        app_module = app_package.models
     19
     20        # Load unit and doctests in the models.py file
     21        suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module))
     22        try:
     23            suite.addTest(doctest.DocTestSuite(app_module,
     24                                               checker=doctestOutputChecker,
     25                                               runner=DocTestRunner))
     26        except ValueError:
     27            # No doc tests in models.py
     28            pass
     29
     30    # Check to see if a separate 'tests' module exists parallel to the
    2831    # models module
    2932    try:
    30         app_path = app_module.__name__.split('.')[:-1]
     33        app_path = app_package.__name__.split('.')[:-1]
    3134        test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
    32        
     35
    3336        suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module))
    34         try:           
    35             suite.addTest(doctest.DocTestSuite(test_module, 
     37        try:
     38            suite.addTest(doctest.DocTestSuite(test_module,
    3639                                               checker=doctestOutputChecker,
    3740                                               runner=DocTestRunner))
    3841        except ValueError:
     
    4447        import os.path
    4548        from imp import find_module
    4649        try:
    47             mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
     50            mod = find_module(TEST_MODULE, [os.path.dirname(app_package.__file__)])
    4851        except ImportError:
    4952            # 'tests' module doesn't exist. Move on.
    5053            pass
    5154        else:
    52             # The module exists, so there must be an import error in the 
     55            # The module exists, so there must be an import error in the
    5356            # test module itself. We don't need the module; close the file
    5457            # handle returned by find_module.
    5558            mod[0].close()
    5659            raise
    57            
     60
    5861    return suite
    5962
    60 def run_tests(module_list, verbosity=1, extra_tests=[]):
     63def run_tests(package_list, verbosity=1, extra_tests=[]):
    6164    """
    6265    Run the unit tests for all the modules in the provided list.
    6366    This testrunner will search each of the modules in the provided list,
     
    6669    will be added to the test suite.
    6770    """
    6871    setup_test_environment()
    69    
    70     settings.DEBUG = False   
     72
     73    settings.DEBUG = False
    7174    suite = unittest.TestSuite()
    72      
    73     for module in module_list:
    74         suite.addTest(build_suite(module))
    75    
     75
     76    for package in package_list:
     77        suite.addTest(build_suite(package))
     78
    7679    for test in extra_tests:
    7780        suite.addTest(test)
    7881
     
    8184    management.syncdb(verbosity, interactive=False)
    8285    unittest.TextTestRunner(verbosity=verbosity).run(suite)
    8386    destroy_test_db(old_name, verbosity)
    84    
     87
    8588    teardown_test_environment()
Back to Top