python - 完整性错误: column user_id is not unique in django tastypie test unit

标签 python django unit-testing tastypie

我在 tastypie 中测试资源模型时遇到奇怪的错误。

我有这个测试

class LocationResourceTest(ResourceTestCase):
    # Use ``fixtures`` & ``urls`` as normal. See Django's ``TestCase``
    # documentation for the gory details.
    fixtures = ['admin_user.json']

    def runTest(self):
        super(LocationResourceTest, self).runTest()


    def setUp(self):
        super(LocationResourceTest, self).setUp()

        # Create a user.
        self.user = User.objects.create_user(username='johndoe',email='johndoe@example.com',password='password')
        # Create an API key for the user:
        ApiKey.objects.create(user=self.user)

    def get_credentials(self):
        return self.create_apikey(username=self.user.username, api_key=self.user.api_key.key)

    def test_create_apikey(self):
        # Try api key authentication using ResourceTestCase.create_apikey().
        credentials = self.get_credentials()
        resp = self.api_client.get('/api/v1/location/',authentication=credentials,format='json')
        self.assertHttpOK(resp)

    def test_get_list_unauthorzied(self):
        pass

当我执行测试时,出现以下错误

======================================================================
ERROR: test_get_list_unauthorzied (geolocation.tests.api.LocationResourceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/phantomis/Memoria/smartaxi_server/geolocation/tests/api.py", line 24, in setUp
    ApiKey.objects.create(user=self.user)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/manager.py", line 137, in create
    return self.get_query_set().create(**kwargs)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/query.py", line 377, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django_tastypie-0.9.12_alpha-py2.7.egg/tastypie/models.py", line 47, in save
    return super(ApiKey, self).save(*args, **kwargs)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/query.py", line 1593, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 912, in execute_sql
    cursor.execute(sql, params)
  File "/Users/phantomis/Virtualenvs/django-memoria/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 344, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: column user_id is not unique

错误出现在方法“test_get_list_unauthorzied”中,很奇怪,因为如果我评论该方法,我的测试就会通过(该方法完全是空的)

最佳答案

setUp 会针对您拥有的每个 test_* 方法运行。当您注释掉空测试用例时,setUp 仅运行一次。在未注释掉另一个 test_* 方法的情况下,setUp 会运行两次,这就是违反唯一性约束的方式。

我将创建一个 tearDown 方法来删​​除您在其他测试用例中创建的用户和 api key 。

关于python - 完整性错误: column user_id is not unique in django tastypie test unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14451245/

相关文章:

python - 以数据框为输入的模型上的多处理

python - 未经授权与谷歌日历 API v3 交互

python - 如何使用 Python 提取硬件 ID?

python - 张量运算python中的内存和时间

django - 如何在 Django 中将 CharField 渲染为静态文本?

python - Django:更新后 CSS 背景图片不工作

django.db.utils.ProgrammingError : relation "bot_trade" does not exist

Ruby,如何创建一个 Rack::Request 用于测试目的?

python - Django test.Client 已登录但在 View 中作为 AnonymousUser 传递

java - Spring Boot Maven 单元测试未执行