导入测试数据
在Pycharm中连接数据库
执行sql
测试数据在:https://github.com/zx490336534/ApiTest/tree/master/test
按照顺序执行
选择执行的目标
处理时间格式
从数据库中可以看出,使用 models.DateTimeField
生成的时间数据格式为 2019-11-0606:21:19.355242
我们需要返回一个 `2019-11-06 06:21:19
的内容
所以我们需要重写 list
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
datas = serializer.data
datas = get_count_by_project(datas)
return self.get_paginated_response(datas)
serializer = self.get_serializer(queryset, many=True)
datas = serializer.data
datas = get_count_by_project(datas)
return Response(datas)
将对数据的处理写在 get_count_by_project
函数中
通过调试可以看到获取到的datas对象为一个包含字典的列表
很容易的想到使用 正则
来进行提取,并修改字典内容
mtch = re.search(r'(.*)T(.*)\..*?', item['create_time'])
item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)
获取项目数据
获取接口总数
project_id = item['id']
interfaces_testcases_objs = Interfaces.objects.values('id').annotate(testcases=Count('testcases')). \
filter(project_id=project_id, is_delete=False)
interfaces_count = interfaces_testcases_objs.count()
interfaces_testcases_objs
相当于调用了以下SQL语句
SELECT `tb_interfaces`.`id`, COUNT(`tb_testcases`.`id`) AS `testcases` FROM `tb_interfaces` LEFT OUTER JOIN `tb_testcases` ON (`tb_interfaces`.`id` = `tb_testcases`.`interface_id`) WHERE (`tb_interfaces`.`is_delete` = False AND `tb_interfaces`.`project_id` = 1) GROUP BY `tb_interfaces`.`id` ORDER BY NULL
获取用例总数
testcases_count = 0
for one_dict in interfaces_testcases_objs:
testcases_count += one_dict['testcases']
获取配置数量
interfaces_configures_objs = Interfaces.objects.values('id').annotate(configures=Count('configures')). \
filter(project_id=project_id, is_delete=False)
configures_count = 0
for one_dict in interfaces_configures_objs:
configures_count += one_dict['configures']
相当于执行了以下SQL
SELECT `tb_interfaces`.`id`, COUNT(`tb_configures`.`id`) AS `configures` FROM `tb_interfaces` LEFT OUTER JOIN `tb_configures` ON (`tb_interfaces`.`id` = `tb_configures`.`interface_id`) WHERE (`tb_interfaces`.`is_delete` = False AND `tb_interfaces`.`project_id` = 1) GROUP BY `tb_interfaces`.`id` ORDER BY NULL
获取测试套件总数
testsuits_count = Testsuits.objects.filter(project_id=project_id, is_delete=False).count()
整个函数
import re
from django.db.models import Count
from interfaces.models import Interfaces
from testsuits.models import Testsuits
def get_count_by_project(datas):
datas_list = []
for item in datas:
mtch = re.search(r'(.*)T(.*)\..*?', item['create_time'])
item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)
project_id = item['id']
interfaces_testcases_objs = Interfaces.objects.values('id').annotate(testcases=Count('testcases')). \
filter(project_id=project_id, is_delete=False)
interfaces_count = interfaces_testcases_objs.count()
testcases_count = 0
for one_dict in interfaces_testcases_objs:
testcases_count += one_dict['testcases']
interfaces_configures_objs = Interfaces.objects.values('id').annotate(configures=Count('configures')). \
filter(project_id=project_id, is_delete=False)
configures_count = 0
for one_dict in interfaces_configures_objs:
configures_count += one_dict['configures']
testsuits_count = Testsuits.objects.filter(project_id=project_id, is_delete=False).count()
item['interfaces'] = interfaces_count
item['testsuits'] = testsuits_count
item['testcases'] = testcases_count
item['configures'] = configures_count
datas_list.append(item)
return datas_list
结果
返回分页内容
配置 setting.py
REST_FRAMEWORK = {
'PAGE_SIZE': 10,
'DEFAULT_PAGINATION_CLASS': 'utils.pagination.PageNumberPaginationManual',
}
编写自定义分页操作
utils.pagination.PageNumberPaginationManual
from django.conf import settings
from rest_framework import pagination
class PageNumberPaginationManual(pagination.PageNumberPagination):
max_page_size = 50
page_size_query_param = 'size'
page_query_description = '第几页'
page_size_query_description = '每页几条'
def get_paginated_response(self, data):
response = super(PageNumberPaginationManual, self).get_paginated_response(data)
response.data['total_pages'] = self.page.paginator.num_pages
response.data['current_page_num'] = self.page.number
return response