我在测试tastypie中的resourcemodel时遇到了异常错误。
我有这个测试
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”中,很奇怪,因为如果我评论该方法,我的测试通过(该方法完全为空)
答案 0 :(得分:2)
setUp
方法运行 test_*
。当您注释掉空的测试用例时,setUp
只运行一次。如果未注释掉其他test_*
方法,则setUp
会运行两次,这就是违反唯一性约束的方式。
我会创建一个tearDown
方法,删除您在另一个测试用例中创建的用户和API密钥。