我测试的景色,而这个测试我得到这个错误
self = <django.db.models.fields.AutoField: id>, value = ''
def get_prep_value(self, value):
from django.db.models.expressions import OuterRef
value = super().get_prep_value(value)
if value is None or isinstance(value, OuterRef):
return value
> return int(value)
E ValueError: invalid literal for int() with base 10: ''
/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:965: ValueError
而且我觉得我得到,因为request.user.profile.org原因mixer.blend(用户)这个错误会PIC一些用户这是不是在我的数据库意味着一些用户还没有一个配置文件,组织和许多东西。 所以,我想知道我怎么测试这个观点,我怎么给用户一些个人资料和组织(组织)。 还有,我也这样做,像我采取了一些用户这是我的数据库中的信息,并把它传递给#test_views.py - > test_dashboard_view() - > request.user您可以在test_dashboard_view)检查(我行评论.......
views.py
@login_required
def dashboard_view(request):
org = request.user.profile.org
week_responses = day_wise_responses(7, org)
user_org = request.user.profile.org.name
sms_sent = org.sms_counter
email_sent = org.email_counter
today = datetime.today().date()
responses_one_week = number_of_responses(7, org)
responses_two_week = number_of_responses(14, org)
average_rating = org_average_rating(org)
responses_last_week = responses_two_week - responses_one_week
if responses_last_week:
responses_percent_change = (abs(responses_one_week - responses_last_week)/responses_last_week)*100
else:
responses_percent_change = responses_one_week*100
# last n responses
last_5_responses = last_n_responses(5, org)
# print(last_5_responses)
context = {'week_responses': week_responses, 'user_org': user_org, 'today': today,
'responses_one_week': responses_one_week, 'responses_percent_change': responses_percent_change,
'last_5_responses': last_5_responses, 'sms_sent': sms_sent, 'email_sent': email_sent,
'average_rating': average_rating}
return render(request, 'events/dashboard.html', context)
urls.py
path('dashboard/', include('fancy_tsunami.events.urls')),
test_views.py
from events.views import dashboard_view
from django.test import RequestFactory
from django.urls import resolve, reverse
from django import test
import pytest
from django.contrib.auth.models import User, AnonymousUser
@pytest.mark.django_db
class TestViews(test.TestCase):
def test_dashboard_view(self):
path = reverse('event-dashboard')
request = RequestFactory().get(path)
# Org = [{'id': 1, 'name': 'Company', 'logo': None, 'share_google_url': None, 'sharing_destinations_id': None, 'sms_counter': 0, 'email_counter': 0}]
request.user = mixer.blend(User)
# CheckUser = {'id': 3, 'password': 'argon2$argon2i$v=19$m=512,t=2,p=2$bHZkZ3Q0bmE2bEJU$N6x/LFqwI4guA', 'last_login': None, 'is_superuser': True, 'username': 'nitin', 'first_name': '', 'last_name': '', 'email': 'nitin@gmail.com', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2019, 2, 21, 1, 10, 32, 146)}
# request.user = (CheckUser)
response = dashboard_view(request)
self.assertEqual(response.status_code, 200)