2016-08-16 2 views
0

전자 메일 및 저장소에 대한 unique_together 제약 조건이 있습니다. 고유 한 제약 조건을 테스트하기 위해 다음 코드를 사용했습니다. 나는 assertRaise로 테스트를 통과하려고하지만 결과는 그 반대이다. 여기서 내가 놓친 게 뭐야? 1.8 :IntegrityError UNIQUE 제약 조건 테스트에 실패했습니다.

from django.db.backends.sqlite3.base import IntegrityError 

class TestUserModel(TestCase): 
    def setUp(self): 
     self.store1 = Store.objects.create(name='store1') 
     self.store2 = Store.objects.create(name='store2') 

    def multiple_user_with_same_email_and_store(self): 
     data1 = dict(email="[email protected]", password="a", store=self.store1) 
     data2 = dict(email="[email protected]", password="abc", store=self.store1) 
     self.user1 = User.objects.create(**data1) 
     user2 = User(**data2) 
     self.assertRaises(IntegrityError, user2.save) 

역 추적

Creating test database for alias 'default'... 
E 
====================================================================== 
ERROR: multiple_user_with_same_email_and_store (user.tests.test_models.TestUserModel) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/home/ram/nk/project/helper/helper/user/tests/test_models.py", line 25, in multiple_user_with_same_email_and_store 
    self.assertRaises(IntegrityError, user2.save) 
    File "/usr/lib/python2.7/unittest/case.py", line 473, in assertRaises 
    callableObj(*args, **kwargs) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/base.py", line 734, in save 
    force_update=force_update, update_fields=update_fields) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/base.py", line 762, in save_base 
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/base.py", line 846, in _save_table 
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/base.py", line 885, in _do_insert 
    using=using, raw=raw) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/query.py", line 920, in _insert 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 974, in execute_sql 
    cursor.execute(sql, params) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/home/ram/.virtual/helper/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 318, in execute 
    return Database.Cursor.execute(self, query, params) 
IntegrityError: UNIQUE constraint failed: user_user.email, user_user.store_id 


    ---------------------------------------------------------------------- 
    Ran 1 test in 0.003s 

    FAILED (errors=1) 

사용자 모델은

class UserManager(auth_models.BaseUserManager): 
    def create_user(self, store, email, password=None, **extra_fields): 
     """ 
     Creates and saves a User with the given email and 
     password. 
     """ 
     now = timezone.now() 
     if not email: 
      raise ValueError('The given email must be set') 
     email = UserManager.normalize_email(email) 
     try: 
      with transaction.atomic(): 
       store, created = Store.objects.get_or_create(name=store) 
       user = self.model(email=email, store=store, is_staff=False, is_active=True, is_superuser=False, 
         last_login=now, date_joined=now, **extra_fields) 
       user.set_password(password) 
       user.save(using=self._db) 
     except Error as e: 
      logging.ERROR(e) 
      raise Error("Internal Error: Unable to create user") 
     return user 

    def create_superuser(self, store, email, password, **extra_fields): 
     u = self.create_user(store, email, password, **extra_fields) 
     u.is_staff = True 
     u.is_active = True 
     u.is_superuser = True 
     u.save(using=self._db) 
     return u 

    def create_staffuser(self, store, email, password, **extra_fields): 
     u = self.create_user(store, email, password, **extra_fields) 
     u.is_staff = True 
     u.is_active = True 
     u.is_superuser = False 
     u.save(using=self._db) 
     return u 

버전을

class AbstractUser(auth_models.AbstractBaseUser, PermissionsMixin): 

    email = models.EmailField(_('email address')) 
    store = models.ForeignKey(Store, verbose_name=_('Store Name'), null=True) 
    first_name = models.CharField(_('First name'), max_length=255, blank=True) 
    last_name = models.CharField(_('Last name'), max_length=255, blank=True) 
    is_staff = models.BooleanField(
      _('Staff status'), default=False, 
      help_text=_('Designates whether the user can log into this admin ' 
         'site.')) 
    is_active = models.BooleanField(
      _('Active'), default=True, 
      help_text=_('Designates whether this user should be treated as ' 
         'active. Unselect this instead of deleting accounts.')) 
    date_joined = models.DateTimeField(_('date joined'), 
             default=timezone.now) 

    objects = UserManager() 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = ['store'] 

    class Meta: 
     abstract = True 
     verbose_name = _('User') 
     verbose_name_plural = _('Users') 
     unique_together = ('email', 'store') 

를 따라 UserManager 같다 AbstractUser (사용자에 추가 다름)를 확장하지

+0

당신이 당신의'User' 모델의 정의 (필드를 보여주십시오 수 : 그 꼭대기에, 장고 예외가 문제가 컨텍스트 관리자로 self.assertRaises를 사용하여 디버그 및 검증 할 수 django.db.utils.IntegrityError

래퍼가 있다면, __init__ 메소드와 전체 에러 스택 트레이스 – zsepi

+0

unique_together는 튜플의 튜플이어야합니다 (이 경우 전자 메일 자체는 고유하고 저장소 자체는 고유합니다). https : //를 참조하십시오. docs.djangoproject.com/en/1.10/ref/models/options/#django.db.models.Options.unique_together하지만 예상되는 줄에서 실패하지 않는 이유는 설명하지 않습니다 (여기서는 전체 오류 추적이help) – zsepi

+0

테스트에서, 어디에서'IntegrityError'를 가져 왔습니까? – zsepi

답변

2

오른쪽 라인 (assertRaises)에서 오류가 발생하고 있지만 여기에는 두 가지 종류의 IntegrityError 클래스가 있습니다. 테스트에서 가져온 클래스와 데이터베이스에서 발생한 클래스입니다. 백엔드.

예 : django.db.backends.postgresql.base.IntegrityErrordjango.db.backends.sqlite3.base.IntegrityError과 같지 않습니다.

with self.assertRaises(Exception) as raised: # top level exception as we want to figure out its exact type 
    user2.save() 
self.assertEqual(IntegrityError, type(raised.exception)) # if it fails, we'll get the correct type to import 
관련 문제