2016-08-05 3 views
-1

私はDjango REST Frameworkアプリのユニットテストを書いており、 factory_boyを使って私の偽のテストデータを作成しています。テストを実行すると、次のエラーが表示されます。Django - UNIQUE制約に失敗しました:auth_user.username

File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 254, in _find_tests 
    module = self._get_module_from_name(name) 
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name 
    __import__(name) 
File "/Users/thomasheatwole/osf-meetings/meetings/conferences/tests.py", line 27, in <module> 
    class SubmissionFactory(factory.DjangoModelFactory): 
File "/Users/thomasheatwole/osf-meetings/meetings/conferences/tests.py", line 34, in SubmissionFactory 
    username = 'contributor' 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 67, in __call__ 
    return cls.create(**kwargs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 594, in create 
    return cls._generate(True, attrs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 519, in _generate 
    obj = cls._prepare(create, **attrs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/base.py", line 494, in _prepare 
    return cls._create(model_class, *args, **kwargs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/factory/django.py", line 181, in _create 
    return manager.create(*args, **kwargs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 401, in create 
    obj.save(force_insert=True, using=self.db) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 74, in save 
    super(AbstractBaseUser, self).save(*args, **kwargs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 708, in save 
    force_update=force_update, update_fields=update_fields) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 736, in save_base 
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 820, in _save_table 
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/base.py", line 859, in _do_insert 
    using=using, raw=raw) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/query.py", line 1039, in _insert 
    return query.get_compiler(using=using).execute_sql(return_id) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql 
    cursor.execute(sql, params) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
File "/Users/thomasheatwole/.virtualenvs/django/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 323, in execute 
    return Database.Cursor.execute(self, query, params) 
IntegrityError: UNIQUE constraint failed: auth_user.username 

意味が分かりません。工場が定義されているのはここです:インスタンスが作成される場所

class UserFactory(factory.DjangoModelFactory): 
    class Meta: 
     model = User 

はここにあります:tests.py

簡単に修正があるかどう私に教えてください:

contributor = UserFactory(
    username = 'contributor' 
    ) 

ここで完全なファイルです。 ありがとう!

答えて

0

私は工場を2度電話していたので、両方のユーザーがusername = 'contributor'を取得していました。簡単な修正は、工場でユーザー名を定義するのではなく、工場に電話するときに定義することです。

0

あなたの回答に記載されているように、問題はunique=Trueフィールドに固定値を使用していることです。

1つの解決方法は、UserFactoryへの呼び出しごとにusernameを渡すことです。ただし、factory_boyの目的は、テストに特定の値が必要でない限り、そのようなフィールドを指定する必要がないようにすることです。あなたの例では

は、次からインスピレーションを定義使用する必要があります。その定義により

class UserFactory(factory.django.DjangoModelFactory): 
    class Meta: 
     model = models.User 

    username = factory.Faker('username') 

を、UserFactoryへの各呼び出しは、別のユーザー名とUserを提供します。

特定のテストのためにusernameを設定する必要がある場合は、UserFactory(username='john')と呼んでください。

関連する問題