如何在django tests.py中创建管理员用户

时间:2010-08-16 16:20:06

标签: django functional-testing

我正在尝试创建一个管理员用户作为我的tests.py的一部分来检查持久性。

更新: tests.py是子类TestCase的标准格式,下面的代码在setUp()函数中调用。

我可以创建普通用户但不能创建管理员用户。如果我试试这个:

    self.adminuser = User.objects.create_user('admin', 'admin@test.com', 'pass')
    self.adminuser.save()
    self.adminuser.is_staff = True
    self.adminuser.save()

OR         self.adminuser = User.objects.create_superuser('admin','admin @ test.com','pass')         self.adminuser.save()

我明白了:

Warning: Data truncated for column 'name' at row 1

如果我删除了is_staff行一切都很好(除了我无法进行测试!)

我是否必须将管理员用户作为固定装置加载?

UserProfile定义如下:

class UserProfile(models.Model):

    user = models.ForeignKey(User, unique=True)
    organisation = models.ForeignKey(Organisation, null=True, blank=True)
    telephone = models.CharField(max_length=20, null=True, blank=True)

和完整错误回溯是:

    Traceback (most recent call last):
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 242, in __call__
        self._pre_setup()
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 217, in _pre_setup
        self._fixture_setup()
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 440, in _fixture_setup
        return super(TestCase, self)._fixture_setup()
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/test/testcases.py", line 222, in _fixture_setup
        call_command('flush', verbosity=0, interactive=False)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/core/management/__init__.py", line 166, in call_command
        return klass.execute(*args, **defaults)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/core/management/base.py", line 222, in execute
        output = self.handle(*args, **options)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/core/management/base.py", line 351, in handle
        return self.handle_noargs(**options)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/core/management/commands/flush.py", line 61, in handle_noargs
        emit_post_sync_signal(models.get_models(), verbosity, interactive)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/core/management/sql.py", line 205, in emit_post_sync_signal
        interactive=interactive)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/dispatch/dispatcher.py", line 166, in send
        response = receiver(signal=self, sender=sender, **named)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/contrib/auth/management/__init__.py", line 28, in create_permissions
        defaults={'name': name, 'content_type': ctype})
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/manager.py", line 123, in get_or_create
        return self.get_query_set().get_or_create(**kwargs)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/query.py", line 335, in get_or_create
        obj.save(force_insert=True)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/base.py", line 410, in save
        self.save_base(force_insert=force_insert, force_update=force_update)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/base.py", line 495, in save_base
        result = manager._insert(values, return_id=update_pk)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/manager.py", line 177, in _insert
        return insert_query(self.model, values, **kwargs)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/query.py", line 1087, in insert_query
        return query.execute_sql(return_id)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/sql/subqueries.py", line 320, in execute_sql
        cursor = super(InsertQuery, self).execute_sql(None)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/models/sql/query.py", line 2369, in execute_sql
        cursor.execute(sql, params)
      File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/db/backends/mysql/base.py", line 84, in execute
        return self.cursor.execute(query, args)
      File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 175, in execute
      File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 89, in _warning_check
      File "/usr/lib64/python2.4/warnings.py", line 61, in warn
        warn_explicit(message, category, filename, lineno, module, registry)
      File "/usr/lib64/python2.4/warnings.py", line 96, in warn_explicit
        raise message
    Warning: Data truncated for column 'name' at row 1

答案似乎是您无法在setUp中创建管理员用户,但您可以在任何其他功能中使用,因此如果您想要管理员用户进行测试,请使用灯具!

2 个答案:

答案 0 :(得分:51)

我会使用内置的create_superuser并在发出任何请求之前将用户登录。以下应该有效:

from django.contrib.auth.models import User
from django.test.client import Client

# store the password to login later
password = 'mypassword' 

my_admin = User.objects.create_superuser('myuser', 'myemail@test.com', password)

c = Client()

# You'll need to log him in before you can send requests through the client
c.login(username=my_admin.username, password=password)

# tests go here

答案 1 :(得分:4)

更新2

执行代码片段以在测试用例(django.test.TestCase的子类)中创建超级用户。一切都很顺利。还使用UserProfile创建并保存了user = self.adminuser的实例。这太有用了。

<强>更新

这条线很有意思:

File "/usr/lib/python2.4/site-packages/Django-1.1.1-py2.4.egg/django/contrib/auth/management/__init__.py", line 28, in create_permissions
        defaults={'name': name, 'content_type': ctype})

创建权限时看起来执行失败。

原始答案

  

警告:第1行的列'name'的数据被截断

奇怪。我从Django shell尝试了这个,它对我有用。我在Ubuntu Jaunty上使用Postgresql 8.3和Django 1.2.1。您能否详细介绍一下您使用的Django /数据库版本?

User也没有name属性。你能仔细检查一下是否使用auth.User

  

我是否必须将管理员用户作为固定装置加载?

你不必须。但是,如果您仅为测试目的创建此管理员用户,那么添加Fixture将是一个好主意。这就是我在我的项目中所做的。