django-nose: Fails to see custom user model with --failed - django

I'm running tests with nose and would like to take advantage of the --failed flag. But as soon as I add it I get errors like these for user related models:
./manage.py test # works
./manage.py test --failed # fails
CommandError: One or more models did not validate:
vod.video: 'user' has a relation with model accounts.CustomUser, which has either not been installed or is abstract.
Packages:
Django==1.6.7
django-nose==1.3
nose==1.3.4
What am I doing wrong?
Update: I don't see the issue on my master branch. In the new branch I'm started to use the TenantTestCase class from django-tenant-schemas. May be the cause of the problem.
The traceback I get:
./manage.py test --failed --traceback
nosetests --failed --verbosity=1
Creating test database for alias 'default'...
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/dev/envs/py26/lib/python2.6/site-packages/south/management/commands/test.py", line 8, in handle
super(Command, self).handle(*args, **kwargs)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django_nose/runner.py", line 218, in run_tests
result = self.run_suite(nose_argv)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django_nose/runner.py", line 165, in run_suite
addplugins=plugins_to_add)
File "/home/dev/envs/py26/lib/python2.6/site-packages/nose/core.py", line 121, in __init__
**extra_args)
File "/usr/lib/python2.6/unittest.py", line 817, in __init__
self.runTests()
File "/home/dev/envs/py26/lib/python2.6/site-packages/nose/core.py", line 207, in runTests
result = self.testRunner.run(self.test)
File "/home/dev/envs/py26/lib/python2.6/site-packages/nose/core.py", line 50, in run
wrapper = self.config.plugins.prepareTest(test)
File "/home/dev/envs/py26/lib/python2.6/site-packages/nose/plugins/manager.py", line 99, in __call__
return self.call(*arg, **kw)
File "/home/dev/envs/py26/lib/python2.6/site-packages/nose/plugins/manager.py", line 167, in simple
result = meth(*arg, **kw)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django_nose/plugin.py", line 76, in prepareTest
self.old_names = self.runner.setup_databases()
File "/home/dev/envs/py26/lib/python2.6/site-packages/django_nose/runner.py", line 401, in setup_databases
return super(NoseTestSuiteRunner, self).setup_databases()
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/test/runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/test/runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/db/backends/creation.py", line 339, in create_test_db
load_initial_data=False)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/base.py", line 284, in execute
self.validate()
File "/home/dev/envs/py26/lib/python2.6/site-packages/django/core/management/base.py", line 314, in validate
raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
vod.video: 'user' has a relation with model accounts.CustomUser, which has either not been installed or is abstract.

I think it has to do with the custom user model. Check this out https://github.com/bernardopires/django-tenant-schemas/issues/237

Related

Cannot use Locust as a library in Django test framework

I want to use Locust as a library in Django's Unittest framework, because I want Django (3.1.2) to create a temporary database for me to do the testing. However, I receive the following error:
Traceback (most recent call last):
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_manage.py", line 104, in handle
failures = TestRunner(test_labels, **options)
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_runner.py", line 254, in run_tests
return DjangoTeamcityTestRunner(**options).run_tests(test_labels,
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/django_test_runner.py", line 156, in run_tests
return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/test/runner.py", line 695, in run_tests
old_config = self.setup_databases(aliases=databases)
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/test/runner.py", line 614, in setup_databases
return _setup_databases(
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/test/utils.py", line 170, in setup_databases
connection.creation.create_test_db(
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 57, in create_test_db
self.connection.close()
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 285, in close
self.validate_thread_sharing()
File "/Users/PycharmProjects/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 552, in validate_thread_sharing
raise DatabaseError(
django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 4487204352 and this is thread id 4528855696.
I don't seem to be able to even import Locust, for example, the following test_locust.py, which does nothing, can already reproduce the error:
from django.test import TransactionTestCase
from locust.env import Environment
class MyTestCase(TransactionTestCase):
def test_cart(self):
pass

Django Tests with POSTGIS Database: no attribute geo_db_type

I am trying to run django's test suite for my application with two databases: one using postgres and the other using POSTGIS. Here is my DATABASES configuration:
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
...},
'POSTGIS': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
...}
When I run python manage.py test app I am prompted to delete an old test database, and when I say yes I get the following error:
Destroying old test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/test/runner.py", line 532, in run_tests
old_config = self.setup_databases()
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/test/runner.py", line 482, in setup_databases
self.parallel, **kwargs
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/test/runner.py", line 726, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 70, in create_test_db
run_syncdb=True,
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/__init__.py", line 119, in call_command
return command.execute(*args, **defaults)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 172, in handle
self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 285, in sync_apps
editor.create_model(model)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 235, in create_model
definition, extra_params = self.column_sql(model, field)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 134, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 636, in db_parameters
type_string = self.db_type(connection)
File "/home/ubuntu/yes/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.py", line 120, in db_type
return connection.ops.geo_db_type(self)
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
Which version of Django are you using?
When you made the database changes, prior to anything else, did you use the appropriate command to update the databases and relationships to them, i.e., syncdb or migrate?
Answering those two questions will help diagnose the problem. Thanks!

How to run South migrations for two (or multiple) Django test databases?

In my apps settings.py file (then made available in django.conf.settings) I have specified:
DATABASES = {'default': dj_database_url.parse(os.environ['DATABASE']),
'secondary': dj_database_url.parse(os.environ['SECONDARY_DATABASE'])}
SOUTH_TESTS_MIGRATE = True
But running my tests using ./manage.py test app_name runs the migrations for the default database and creates some of the tables for the secondary database before failing with:
Creating test database for alias 'secondary' ('test_secondary')...
Syncing...
Creating tables ...
Creating table auth_permission
...
Creating table django_admin_log
Installing custom SQL ...
Installing indexes ...
Migrating...
Traceback (most recent call last):
File "./manage.py", line 18, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 381, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 49, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/test/simple.py", line 381, in run_tests
old_config = self.setup_databases()
File "/usr/local/venv/local/lib/python2.7/site-packages/django/test/simple.py", line 317, in setup_databases
self.verbosity, autoclobber=not self.interactive)
File "/usr/local/venv/local/lib/python2.7/site-packages/south/hacks/django_1_0.py", line 100, in wrapper
f(*args, **kwargs)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/db/backends/creation.py", line 271, in create_test_db
load_initial_data=False)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 149, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/usr/local/venv/local/lib/python2.7/site-packages/south/management/commands/syncdb.py", line 99, in handle_noargs
management.call_command('migrate', **options)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 149, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/usr/local/venv/local/lib/python2.7/site-packages/south/management/commands/migrate.py", line 108, in handle
ignore_ghosts = ignore_ghosts,
File "/usr/local/venv/local/lib/python2.7/site-packages/south/migration/__init__.py", line 171, in migrate_app
south.db.db = south.db.dbs[database]
KeyError: 'secondary'

Django unit tests spews database error - cannot commit transaction - SQL statements in progress

I'm using unittest2 together with manage.py test, and before it even seems to run any tests, it spews a horrid database error, as below. I'm in my development environment (actually on a dreamhost server for a variety of reasons), using sqlite as my database.
Even when I copy my app out of where it is for production (and edit settings.py accordingly), I still get the same error (even though it should no longer be addressing the same database file as the dev environment).
Update: Even moving all instances of my database files to a different filename still leaves me with the same error. This leads me to believe that the test environment is somehow looking at a bogus database, or something very odd is happening.
[graffias]$ python manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 14, in <module>
execute_manager(settings)
File "/home/marcintustin/django/Django-1.3/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/home/marcintustin/django/Django-1.3/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/marcintustin/django/Django-1.3/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/marcintustin/django/Django-1.3/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/management/commands/test.py", line 8, in handle
super(Command, self).handle(*args, **kwargs)
File "/home/marcintustin/django/Django-1.3/django/core/management/commands/test.py", line 37, in handle
failures = test_runner.run_tests(test_labels)
File "/home/marcintustin/django/Django-1.3/django/test/simple.py", line 359, in run_tests
old_config = self.setup_databases()
File "/home/marcintustin/django/Django-1.3/django/test/simple.py", line 296, in setup_databases
test_db_name = connection.creation.create_test_db(self.verbosity, autoclobber=not self.interactive)
File "/home/marcintustin/django/Django-1.3/django/db/backends/creation.py", line 366, in create_test_db
load_initial_data=False)
File "/home/marcintustin/django/Django-1.3/django/core/management/__init__.py", line 166, in call_command
return klass.execute(*args, **defaults)
File "/home/marcintustin/django/Django-1.3/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/marcintustin/django/Django-1.3/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/management/commands/syncdb.py", line 99, in handle_noargs
management.call_command('migrate', **options)
File "/home/marcintustin/django/Django-1.3/django/core/management/__init__.py", line 166, in call_command
return klass.execute(*args, **defaults)
File "/home/marcintustin/django/Django-1.3/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/management/commands/migrate.py", line 105, in handle
ignore_ghosts = ignore_ghosts,
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/migration/__init__.py", line 191, in migrate_app
success = migrator.migrate_many(target, workplan, database)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/migration/migrators.py", line 221, in migrate_many
result = migrator.__class__.migrate_many(migrator, target, migrations, database)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/migration/migrators.py", line 298, in migrate_many
interactive=self.interactive)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/db/generic.py", line 808, in send_pending_create_signals
interactive=interactive)
File "/home/marcintustin/django/South-0.7.3-py2.5.egg/south/db/generic.py", line 855, in really_send_create_signal
db=self.db_alias,
File "/home/marcintustin/django/Django-1.3/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/home/marcintustin/django/Django-1.3/django/contrib/auth/management/__init__.py", line 30, in create_permissions
ctype = ContentType.objects.get_for_model(klass)
File "/home/marcintustin/django/Django-1.3/django/contrib/contenttypes/models.py", line 38, in get_for_model
defaults = {'name': smart_unicode(opts.verbose_name_raw)},
File "/home/marcintustin/django/Django-1.3/django/db/models/manager.py", line 135, in get_or_create
return self.get_query_set().get_or_create(**kwargs)
File "/home/marcintustin/django/Django-1.3/django/db/models/query.py", line 385, in get_or_create
obj.save(force_insert=True, using=self.db)
File "/home/marcintustin/django/Django-1.3/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/marcintustin/django/Django-1.3/django/db/models/base.py", line 560, in save_base
transaction.commit_unless_managed(using=using)
File "/home/marcintustin/django/Django-1.3/django/db/transaction.py", line 124, in commit_unless_managed
connection.commit_unless_managed()
File "/home/marcintustin/django/Django-1.3/django/db/backends/__init__.py", line 183, in commit_unless_managed
self._commit()
File "/home/marcintustin/django/Django-1.3/django/db/backends/__init__.py", line 46, in _commit
return self.connection.commit()
pysqlite2.dbapi2.OperationalError: cannot commit transaction - SQL statements in progress
Basically, I have no idea where to start with fixing this. Any ideas?

Why do I get this error when I run manage.py validate?

Why do I get this error when I run manage.py validate?:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "C:\python26\lib\site-packages\django\core\management\__init__.py", line
438, in execute_manager
utility.execute()
File "C:\python26\lib\site-packages\django\core\management\__init__.py", line
379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\python26\lib\site-packages\django\core\management\base.py", line 191,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\python26\lib\site-packages\django\core\management\base.py", line 218,
in execute
output = self.handle(*args, **options)
File "C:\python26\lib\site-packages\django\core\management\base.py", line 347,
in handle
return self.handle_noargs(**options)
File "C:\python26\lib\site-packages\django\core\management\commands\validate.p
y", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "C:\python26\lib\site-packages\django\core\management\base.py", line 245,
in validate
num_errors = get_validation_errors(s, app)
File "C:\python26\lib\site-packages\django\core\management\validation.py", lin
e 28, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\python26\lib\site-packages\django\db\models\loading.py", line 146, in
get_app_errors
self._populate()
File "C:\python26\lib\site-packages\django\db\models\loading.py", line 61, in
_populate
self.load_app(app_name, True)
File "C:\python26\lib\site-packages\django\db\models\loading.py", line 78, in
load_app
models = import_module('.models', app_name)
File "C:\python26\lib\site-packages\django\utils\importlib.py", line 35, in im
port_module
__import__(name)
File "C:\projects\mysite\..\mysite\books\models.py", line 5, in <module>
class Publisher(models.Model):
File "C:\projects\mysite\..\mysite\books\models.py", line 6, in Publisher
name = models.CharField(maxlength=30)
File "C:\python26\lib\site-packages\django\db\models\fields\__init__.py", line
542, in __init__
super(CharField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'maxlength'
The problem is that you are using a newer version of Django now. Since 1.0 (or actually, somewhere in 0.97) Django switched to max_length instead of maxlength. So either upgrade your code or install Django 0.96 for it to work again.
It's difficult to be 100% sure, because you haven't included the code in mysite/books/models.py, but it would appear that the 'max_length' keyword argument to a CharField in the Publisher class has been misspelled 'maxlength'.
See here for the correct values:
http://docs.djangoproject.com/en/dev/ref/models/fields/#charfield