已知:项目表、接口表、用例表,3表之间的关系
项目表与接口表的映射关系为一对多
接口表和用例表的映射关系为一对多
模型类如下:
项目模块
from django.db import models
from utils.base_models import BaseModel
class Projects(BaseModel):
name = models.CharField('项目名称', max_length=200, unique=True, help_text='项目名称')
leader = models.CharField('负责人', max_length=50, help_text='项目负责人')
tester = models.CharField('测试人员', max_length=50, help_text='项目测试人员')
programmer = models.CharField('开发人员', max_length=50, help_text='开发人员')
publish_app = models.CharField('发布应用', max_length=100, help_text='发布应用')
desc = models.CharField('简要描述', max_length=200, null=True, blank=True, default='', help_text='简要描述')
class Meta:
db_table = 'tb_projects'
verbose_name = '项目信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
接口模块
from django.db import models
from utils.base_models import BaseModel
class Interfaces(BaseModel):
name = models.CharField('接口名称', max_length=200, unique=True, help_text='接口名称')
project = models.ForeignKey('projects.Projects', on_delete=models.CASCADE,
related_name='interfaces', help_text='所属项目')
tester = models.CharField('测试人员', max_length=50, help_text='测试人员')
desc = models.CharField('简要描述', max_length=200, null=True, blank=True, help_text='简要描述')
class Meta:
db_table = 'tb_interfaces'
verbose_name = '接口信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
用例模块
from django.db import models
from django.db import models
from utils.base_models import BaseModel
class Testcases(BaseModel):
name = models.CharField('用例名称', max_length=50, unique=True, help_text='用例名称')
interface = models.ForeignKey('interfaces.Interfaces', on_delete=models.CASCADE,
help_text='所属接口')
include = models.TextField('前置', null=True, help_text='用例执行前置顺序')
author = models.CharField('编写人员', max_length=50, help_text='编写人员')
request = models.TextField('请求信息', help_text='请求信息')
class Meta:
db_table = 'tb_testcases'
verbose_name = '用例信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
需求:序列化输出项目名称、接口名称;反序列化输入项目id、接口id,序列化器怎么设计?
序列化器设计
from rest_framework import serializers
from interfaces.models import Interfaces
from projects.models import Projects
from testcases.models import Testcases
from utils.validators import ManualValidateExist
def test_pid(value):
count = Projects.objects.filter(id=value).count()
if not count:
raise serializers.ValidationError('所传的项目id不存在')
return value
def test_iid(value):
count = Interfaces.objects.filter(id=value).count()
if not count:
raise serializers.ValidationError('所传的接口id不存在')
return value
class InterfacesProjectsModelSerializer(serializers.ModelSerializer):
project = serializers.StringRelatedField(label='接口所属项目', help_text='接口所属项目', read_only=True)
pid = serializers.IntegerField(label='项目id',
help_text='项目id',
write_only=True,
validators=[test_pid])
iid = serializers.IntegerField(label='接口id',
help_text='接口id',
write_only=True,
validators=[test_iid])
class Meta:
model = Interfaces
fields = ['name', 'project', 'pid', 'iid']
extra_kwargs = {
'name': {
'read_only': True
}
}
def validate(self, attrs):
resuit = Interfaces.objects.filter(id=attrs.get('iid'), project_id=attrs.get('pid')).exists()
if not resuit:
raise serializers.ValidationError('所属项目id和接口id不匹配')
return attrs
class TestcasesModelSerializer(serializers.ModelSerializer):
interface = InterfacesProjectsModelSerializer(help_text='用例所属接口', label='用例所属接口')
class Meta:
model = Testcases
exclude = ['update_time', 'create_time']
extra_kwargs = {
'include': {
"write_only": True
},
'request': {
'write_only': True
}
}
def to_internal_value(self, data):
response=super().to_internal_value(data)
iid = data.get('interface').get('iid')
response['interface'] = Interfaces.objects.get(id=iid)
return response
class TestcasesRunSerializer(serializers.ModelSerializer):
env_id=serializers.IntegerField(label='所属环境配置id',help_text='所属环境配置uid',
validators=[ManualValidateExist('env')])
class Meta:
model=Testcases
fields=['id','env_id']
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/73954.html