測試開發進階(三十五)

導入測試數據

在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

  1. def list(self, request, *args, **kwargs):

  2. queryset = self.filter_queryset(self.get_queryset())

  3. page = self.paginate_queryset(queryset)

  4. if page is not None:

  5. serializer = self.get_serializer(page, many=True)

  6. datas = serializer.data

  7. datas = get_count_by_project(datas)

  8. return self.get_paginated_response(datas)

  9. serializer = self.get_serializer(queryset, many=True)

  10. datas = serializer.data

  11. datas = get_count_by_project(datas)

  12. return Response(datas)

將對數據的處理寫在 get_count_by_project函數中

通過調試可以看到獲取到的datas對象爲一個包含字典的列表

很容易的想到使用 正則來進行提取,並修改字典內容

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

  2. item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)

獲取項目數據

獲取接口總數

  1. project_id = item['id']

  2. interfaces_testcases_objs = Interfaces.objects.values('id').annotate(testcases=Count('testcases')). \

  3. filter(project_id=project_id, is_delete=False)

  4. 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. testcases_count = 0

  2. for one_dict in interfaces_testcases_objs:

  3. testcases_count += one_dict['testcases']

獲取配置數量

  1. interfaces_configures_objs = Interfaces.objects.values('id').annotate(configures=Count('configures')). \

  2. filter(project_id=project_id, is_delete=False)

  3. configures_count = 0

  4. for one_dict in interfaces_configures_objs:

  5. 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. import re

  2. from django.db.models import Count

  3. from interfaces.models import Interfaces

  4. from testsuits.models import Testsuits

  5. def get_count_by_project(datas):

  6. datas_list = []

  7. for item in datas:

  8. mtch = re.search(r'(.*)T(.*)\..*?', item['create_time'])

  9. item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)

  10. project_id = item['id']

  11. interfaces_testcases_objs = Interfaces.objects.values('id').annotate(testcases=Count('testcases')). \

  12. filter(project_id=project_id, is_delete=False)

  13. interfaces_count = interfaces_testcases_objs.count()

  14. testcases_count = 0

  15. for one_dict in interfaces_testcases_objs:

  16. testcases_count += one_dict['testcases']

  17. interfaces_configures_objs = Interfaces.objects.values('id').annotate(configures=Count('configures')). \

  18. filter(project_id=project_id, is_delete=False)

  19. configures_count = 0

  20. for one_dict in interfaces_configures_objs:

  21. configures_count += one_dict['configures']

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

  23. item['interfaces'] = interfaces_count

  24. item['testsuits'] = testsuits_count

  25. item['testcases'] = testcases_count

  26. item['configures'] = configures_count

  27. datas_list.append(item)

  28. return datas_list

結果

返回分頁內容

配置 setting.py

  1. REST_FRAMEWORK = {

  2. 'PAGE_SIZE': 10,

  3. 'DEFAULT_PAGINATION_CLASS': 'utils.pagination.PageNumberPaginationManual',

  4. }

編寫自定義分頁操作

utils.pagination.PageNumberPaginationManual

  1. from django.conf import settings

  2. from rest_framework import pagination

  3. class PageNumberPaginationManual(pagination.PageNumberPagination):

  4. max_page_size = 50

  5. page_size_query_param = 'size'

  6. page_query_description = '第幾頁'

  7. page_size_query_description = '每頁幾條'

  8. def get_paginated_response(self, data):

  9. response = super(PageNumberPaginationManual, self).get_paginated_response(data)

  10. response.data['total_pages'] = self.page.paginator.num_pages

  11. response.data['current_page_num'] = self.page.number

  12. return response

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章