测试开发进阶(三十五)

测试开发进阶(三十五)

导入测试数据

在Pycharm中连接数据库

Pycharm连接数据库

执行sql

测试数据在:https://github.com/zx490336534/ApiTest/tree/master/test按照顺序执行

执行sql

选择执行的目标

选择执行的目标

处理时间格式

时间

从数据库中可以看出,使用models.DateTimeField生成的时间数据格式为2019-11-06 06:21:19.355242

我们需要返回一个``2019-11-06 06:21:19`的内容

所以我们需要重写list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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对象为一个包含字典的列表

datas

很容易的想到使用正则来进行提取,并修改字典内容

1
2
mtch = re.search(r'(.*)T(.*)\..*?', item['create_time'])
item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)

获取项目数据

获取接口总数

1
2
3
4
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语句

1
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

获取用例总数

1
2
3
testcases_count = 0
for one_dict in interfaces_testcases_objs:
testcases_count += one_dict['testcases']

获取配置数量

1
2
3
4
5
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

1
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

获取测试套件总数

1
testsuits_count = Testsuits.objects.filter(project_id=project_id, is_delete=False).count()

整个函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

1
2
3
4
REST_FRAMEWORK = {
'PAGE_SIZE': 10,
'DEFAULT_PAGINATION_CLASS': 'utils.pagination.PageNumberPaginationManual',
}

编写自定义分页操作

utils.pagination.PageNumberPaginationManual

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

结果

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!