Ticket #3310: test_applications_with_no_models.version_2.patch
File test_applications_with_no_models.version_2.patch, 9.2 KB (added by , 18 years ago) |
---|
-
django/core/management.py
1222 1222 from django.db.models import get_app, get_apps 1223 1223 1224 1224 if len(app_labels) == 0: 1225 app_list = get_apps( )1225 app_list = get_apps(True) 1226 1226 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] 1228 1228 1229 1229 test_path = settings.TEST_RUNNER.split('.') 1230 1230 # Allow for Python 2.5 relative paths -
django/db/models/loading.py
7 7 8 8 __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models') 9 9 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? 20 23 21 def get_apps( ):22 "Returns a list of all installed modules that contain models. "24 def 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." 23 26 global _app_list 27 global _app_pckg_list 24 28 global _loaded 25 29 if not _loaded: 26 30 _loaded = True … … 30 34 except Exception, e: 31 35 # Problem importing the app 32 36 _app_errors[app_name] = e 37 if return_package: 38 return _app_pckg_list 33 39 return _app_list 34 40 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. "41 def 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." 37 43 get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish. 38 44 for app_name in settings.INSTALLED_APPS: 39 45 if app_label == app_name.split('.')[-1]: 40 mod = load_app(app_name)41 if mod is None:42 if emptyOK:43 return None46 if return_package: 47 pckg = load_app(app_name,True) 48 if pckg: 49 return pckg 44 50 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 46 57 raise ImproperlyConfigured, "App with label %s could not be found" % app_label 47 58 48 def load_app(app_name ):49 "Loads the app with the provided fully qualified name, and returns the model module. "59 def 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." 50 61 global _app_list 51 62 mod = __import__(app_name, {}, {}, ['models']) 63 if mod not in _app_pckg_list: 64 _app_pckg_list.append(mod) 52 65 if not hasattr(mod, 'models'): 66 if return_package: 67 return mod 53 68 return None 54 69 if mod.models not in _app_list: 55 70 _app_list.append(mod.models) 71 if return_package: 72 return mod 56 73 return mod.models 57 74 58 75 def get_app_errors(): -
django/test/simple.py
7 7 8 8 # The module name for tests outside models.py 9 9 TEST_MODULE = 'tests' 10 10 11 11 doctestOutputChecker = OutputChecker() 12 12 13 def build_suite(app_ module):13 def build_suite(app_package): 14 14 "Create a complete Django test suite for the provided application module" 15 15 suite = unittest.TestSuite() 16 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 16 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 28 31 # models module 29 32 try: 30 app_path = app_ module.__name__.split('.')[:-1]31 test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)32 33 app_path = app_package.__name__.split('.') 34 test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, [TEST_MODULE]) 35 33 36 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, 36 39 checker=doctestOutputChecker, 37 40 runner=DocTestRunner)) 38 41 except ValueError: … … 44 47 import os.path 45 48 from imp import find_module 46 49 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__)]) 48 51 except ImportError: 49 52 # 'tests' module doesn't exist. Move on. 50 53 pass 51 54 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 53 56 # test module itself. We don't need the module; close the file 54 57 # handle returned by find_module. 55 58 mod[0].close() 56 59 raise 57 60 58 61 return suite 59 62 60 def run_tests( module_list, verbosity=1, extra_tests=[]):63 def run_tests(package_list, verbosity=1, extra_tests=[]): 61 64 """ 62 65 Run the unit tests for all the modules in the provided list. 63 66 This testrunner will search each of the modules in the provided list, … … 66 69 will be added to the test suite. 67 70 """ 68 71 setup_test_environment() 69 70 settings.DEBUG = False 72 73 settings.DEBUG = False 71 74 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 76 79 for test in extra_tests: 77 80 suite.addTest(test) 78 81 … … 81 84 management.syncdb(verbosity, interactive=False) 82 85 unittest.TextTestRunner(verbosity=verbosity).run(suite) 83 86 destroy_test_db(old_name, verbosity) 84 87 85 88 teardown_test_environment()