我需要帮助。当我在本地服务器上运行时,我的注册系统工作得非常好,但当我运行我的测试时。它失败了。我不明白为什么? 我无法找出问题所在,我已经删除了整个数据库和迁移,甚至我还创建了新的应用程序,并在那里做了一切。但是,我仍然得到这个错误。
My test_setup.py
from rest_framework.test import APITestCase
from django.urls import reverse
class TestSetup(APITestCase):
def setUp(self):
self.register_url = '/register/'
self.user_data = {"Username":"frontend123", "First name":"Frontend", "Last
name":"Developer", "Password":"bitspro##1", "Confirm
password":"bitspro##1","Email":"rida@bitspro.com","Date of
birth":"1997-12-27"}
return super().setUp()
def tearDown(self):
return super().tearDown()
My testViews.py
from authentication.models import User
from authentication.tests.test_setup import TestSetup
class TestViews(TestSetup):
def test_user_cannot_register_without_any_data(self):
res = self.client.post(self.register_url)
self.assertEqual(res.status_code, 400)
def test_user_can_register_correctly(self):
res = self.client.post(self.register_url, self.user_data, format="json")
self.assertEqual(res.status_code, 201)
我的view.py(我只在测试注册系统
from rest_framework.response import Response
from rest_framework.views import APIView
from authentication.models import User as Client
class Register(APIView):
def post(self, request):
username = request.data.get('Username')
first_name = request.data.get('First name')
last_name = request.data.get('Last name')
password = request.data.get('Password')
confirm_password = request.data.get('Confirm password')
date_of_birth = request.data.get('Date of birth')
email = request.data.get('Email')
if password == confirm_password:
if Client.objects.filter(username = username).exists():
return Response({"message": "this username already exist"}, 400)
elif Client.objects.filter(email = email).exists():
return Response({"message": "This email-address is already taken"}, 400)
else:
user= Client.objects.create(username = username, password =password, email=email,
date_of_birth=date_of_birth)
user.save()
return Response({"message":"User has been created"}, 201)
else:
return Response({"message": "Try it again"}, 404)
my urls.py
from django.urls import path
from authentication.views import Login, Logout, Register, Password_Reset
urlpatterns = [
path('register/', Register.as_view(), name = "register"),
My models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
email = models.EmailField(verbose_name="Email", max_length=60, unique=True)
date_of_birth= models.DateField(verbose_name="Date of Birth", max_length=10)
def __str__(self):
return self.username
我真的很感谢你的帮助
my error
(venv) PS C:\Users\Lenovo\Documents\GitHub\E-commerce\garments> python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
======================================================================
ERROR: test_user_cannot_register_without_any_data (authentication.tests.test_views.TestViews)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: authentication_user.password
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\garments\authentication\tests\test_views.py", line 8, in test_user_cannot_register_without_any_data
res = self.client.post(self.register_url)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\test.py", line 295, in post
response = super().post(
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\test.py", line 209, in post
return self.generic('POST', path, data, content_type, **extra)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\test.py", line 233, in generic
return super().generic(
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\test\client.py", line 473, in generic
return self.request(**r)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\test.py", line 285, in request
return super().request(**kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\test.py", line 237, in request
request = super().request(**kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\test\client.py", line 719, in request
self.check_exception(response)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\test\client.py", line 580, in check_exception
raise exc_value
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\garments\authentication\views.py", line 38, in post
user= Client.objects.create(username = username, password =password, email=email, date_of_birth=date_of_birth)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
super().save(*args, **kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
return manager._insert(
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\Lenovo\Documents\GitHub\E-commerce\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: authentication_user.password
----------------------------------------------------------------------
Ran 2 tests in 0.083s