gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:50:38 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com