2013-01-22 41 views
1

我有一个estrange错误测试tastypie中的resourcemodel。IntegrityError:列user_id在django 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='[email protected]',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”的方法,是奇怪的,因为如果我评论说,方法,我的测试通过(该方法是完全空的)

回答

2

setUp针对您所使用的每个test_*方法运行。当您注释掉空的测试用例时,setUp仅运行一次。与其他test_*方法未被注释掉,setUp运行两次,这是如何违反唯一性约束。

我会创建一个tearDown方法,删除在另一个测试用例中创建的用户和API密钥。