我正在为我的应用程序编写测试。我在互联网上搜索了相同的错误,但找不到任何东西。我昨天有这个错误,但是最后期限很紧。今天,我面临着同样的错误。似乎仅当我尝试将用户onetoone字段两次分配给两个不同的模型时,才会出现此错误。发生的事情是我正在尝试创建两个不同的配置文件。一个配置文件是卖方配置文件,另一个是买方配置文件。测试代码->
from django.test import TestCase, Client
from django.urls import reverse
from shop.models import Category, Product
from users.models import Profile, User, SellerProfile
class TestViews(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create_user(
email='johndoe@gmail.com',
password='secretpass203',
username='johndoe',
)
# Now log in the test client
password = "secretpass203"
self.is_authenticated = self.client.login(
username=self.user.username, password=password)
# Test if a user profile can be created successfully
def test_user_profile_creation(self):
url = reverse("users:create-profile")
# Send a post request
with open("test_data/profile.jpg", 'rb') as dp:
response = self.client.post(url, {
'firstname': 'John',
'lastname': 'Doe',
'profile_picture': dp
})
# assert the response returned if's it is a redirect
self.assertEquals(response.status_code, 302)
# Check if the profile was created
self.assertEquals(Profile.objects.last().firstname, 'John')
def test_user_is_logged_in(self):
self.assertEquals(self.is_authenticated, True)
def test_edit_user_profile(self):
# Initialize data
user = User.objects.get(pk=1)
with open('test_data/profile.jpg', 'rb') as dp:
self.profile = Profile.objects.create(
firstname='Peter',
lastname='Griffin',
user=user
)
# Create a profile
url = reverse("users:edit-profile",
kwargs={'pk': self.user.profile.pk})
# Now make a post request
with open("test_data/profile2.jpg", 'rb') as dp2:
response = self.client.post(url, {
'profile_picture': dp2
})
# If a redirect happens that means the profile has been successfully
# update
self.assertEquals(response.status_code, 302)
def test_seller_profile_creation(self):
url = reverse("users:create-seller-profile")
# Send a post request
with open("test_data/profile.jpg", 'rb') as dp:
response = self.client.post(url, {
'tradename': 'Abantusoft',
'firstname': 'John',
'lastname': 'Doe',
'phone_number': '+263782841339',
'email': 'johndoe@gmail.com',
'city': 'Gweru',
'state': 'Midlands',
'address': '320 Fake Location 12',
'bank_account': '102002040',
'brand_logo': dp
})
# assert the response returned if's it is a redirect
self.assertEquals(response.status_code, 302)
# Check if the profile was created
self.assertEquals(SellerProfile.objects.last().tradename, 'Abantusoft')
# Test editing seller profile
def test_edit_seller_profile(self):
# Initialize data
user = User.objects.get(pk=1)
with open('test_data/profile2.jpg', 'rb') as dp:
self.profile = SellerProfile.objects.create(
tradename="Abantuware",
firstname='Peter',
lastname='Griffin',
phone_number='+263782841339',
email='petergriffin@gmail.com',
city="Quahog",
state='Midlands',
address='456 Fake Street 12',
bank_account='203040240506',
brand_logo=dp,
user=user
)
# Create a profile
url = reverse("users:edit-profile",
kwargs={'pk': self.user.profile.pk})
# Now make a post request
with open("test_data/profile2.jpg", 'rb') as dp2:
response = self.client.post(url, {
'profile_picture': dp2
})
# If a redirect happens that means the profile has been successfully
# update
self.assertEquals(response.status_code, 302)
在添加以下测试之前,一切正常。
# Test editing seller profile
def test_edit_seller_profile(self):
# Initialize data
user = User.objects.get(pk=1)
with open('test_data/profile2.jpg', 'rb') as dp:
self.profile = SellerProfile.objects.create(
tradename="Abantuware",
firstname='Peter',
lastname='Griffin',
phone_number='+263782841339',
email='petergriffin@gmail.com',
city="Quahog",
state='Midlands',
address='456 Fake Street 12',
bank_account='203040240506',
brand_logo=dp,
user=user
)
# Create a profile
url = reverse("users:edit-profile",
kwargs={'pk': self.user.profile.pk})
# Now make a post request
with open("test_data/profile2.jpg", 'rb') as dp2:
response = self.client.post(url, {
'profile_picture': dp2
})
# If a redirect happens that means the profile has been successfully
# update
self.assertEquals(response.status_code, 302)
这是我执行测试时收到的错误消息->
======================================================================
ERROR: test_edit_seller_profile (users.tests.test_views.TestViews)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/patrice/django-apps/ecommerce/ecommerce_site/users/tests/test_views.py", line 98, in test_edit_seller_profile
user=self.user
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/query.py", line 433, in create
obj.save(force_insert=True, using=self.db)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/base.py", line 746, in save
force_update=force_update, update_fields=update_fields)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/base.py", line 784, in save_base
force_update, using, update_fields,
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/base.py", line 887, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/base.py", line 926, in _do_insert
using=using, raw=raw,
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/query.py", line 1204, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1391, in execute_sql
for sql, params in self.as_sql():
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1336, in as_sql
for obj in self.query.objs
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1336, in <listcomp>
for obj in self.query.objs
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1335, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1286, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/patrice/django-apps/ecommerce/lib/python3.6/site-packages/django/db/models/fields/files.py", line 286, in pre_save
if file and not file._committed:
AttributeError: '_io.BufferedReader' object has no attribute '_committed'
----------------------------------------------------------------------
Ran 5 tests in 1.810s
FAILED (errors=1)
Destroying test database for alias 'default'...
我做错了,启发我。