我有以下django视图:
@api_view(['GET'])
@csrf_exempt
def last_status_by_department_view(request, department_id=0):
if request.META.get('HTTP_AUTHORIZATION') is None:
return JsonResponse({'exception_message': 'No authorization provided.'}, status=401)
try:
return JsonResponse(last_status_by_department(department_id), safe=False)
except Exception as e:
return JsonResponse({'exception_message': str(e), 'full_traceback': traceback.format_exc()}, status=400)
哪个使用此功能:
from tokitimer.models import Activity
def last_status_by_department(department_id):
all_activities = Activity.objects.order_by('user__user_name', '-activity_datetime').distinct(
'user__user_name').exclude(bucket__bucket_name='Offline').filter(
bucket__category__department_id=department_id)
rts = []
for activity in all_activities:
rts.append(
{
'user_id': activity.user.user_id,
'user_name': activity.user.user_name,
'activity_datetime': activity.activity_datetime,
'bucket_id': activity.bucket.bucket_id,
'bucket_name': activity.bucket.bucket_name,
'category_id': activity.bucket.category.category_id,
'category_name': activity.bucket.category.category_name
}
)
return rts
我为此进行了以下测试:
class RealTimeStatusTests(TestCase):
def setUp(self):
self.user, self.master_account, self.category, self.bucket, self.activity, self.department, \
self.department_history = general_set_up2()
self.tokens = json.loads(self.client.post(reverse('authenticate'),
data={'username': self.user.user_name, 'password': 'abcd'},
content_type='application/json').content)
def test_get_real_time_status(self):
response = self.client.get(reverse('real_time_status_department',
kwargs={'department_id': self.department.department_id}),
follow=True, **{'HTTP_AUTHORIZATION': self.tokens['access']})
expected_response = [{
'user_id': self.user.user_id,
'user_name': self.user.user_name,
'activity_datetime': self.activity.activity_datetime,
'bucket_id': self.bucket.bucket_id,
'bucket_name': self.bucket.bucket_name,
'category_id': self.category.category_id,
'category_name': self.category.category_name
}]
print(self.department.department_id)
print(str(response.content, encoding='utf8'))
print(expected_response)
self.assertJSONEqual(str(response.content, encoding='utf8'), expected_response)
该视图基本上为所有用户提供最新活动。当我在本地运行此视图时,该视图将返回正确的结果。运行测试时,对象“被创建”是因为我对所有对象都进行了测试,但是对于该特定视图,返回的是一个空列表,而不是在setUp中创建的活动。
我在这里做错什么了吗?为什么视图无法捕获由setUp创建的对象?
编辑:她是常规设置功能:
def general_set_up2():
django_user = User.objects.create_user(username='generic_user_name',
email='test@test.com',
password='abcd')
user = TokiUser(user_name='generic_user_name', email_address='test@test.com', created_datetime=now,
last_modified_datetime=now, is_admin=True, django_user=django_user)
master_account = Account(account_name='Toki Admin', created_datetime=now, last_modified_datetime=now,
is_master_account=True)
user.save()
master_account.owner = user
master_account.created_by = user
master_account.last_modified_by = user
master_account.save()
user.created_by = user
user.last_modified_by = user
user.account = master_account
user.save()
department = Department.objects.create(department_name='Test Department', created_by=user, created_datetime=now,
last_modified_by=user, last_modified_datetime=now, account=master_account)
department_history = DepartmentHistory.objects.create(user=user, department=department, start_datetime=now,
created_by=user, created_datetime=now,
last_modified_by=user, last_modified_datetime=now,
account=master_account)
category = Category.objects.create(category_name='Test Category', created_by=user, created_datetime=now,
last_modified_by=user, last_modified_datetime=now, account=master_account)
category.save()
bucket = Bucket.objects.create(bucket_name='TestBucket', category=category, created_by=user, created_datetime=now,
last_modified_by=user, last_modified_datetime=now, account=master_account)
bucket.save()
activity = Activity.objects.create(activity_datetime=now, user=user, bucket=bucket, created_datetime=now,
created_by=user, last_modified_datetime=now, last_modified_by=user,
account=master_account)
activity.save()
return user, master_account, category, bucket, activity, department, department_history