码迷,mamicode.com
首页 > 其他好文 > 详细

接口平台测试开发--17

时间:2020-02-19 19:13:10      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:choice   bbb   tar   efi   bst   请求   OLE   a20   email   

一、pycharm 从git拉取代码

1、

技术图片

2、技术图片

二、执行manage.py

1、pycharm路径

技术图片

2、创建项目

startapp +项目名称 
例如:startapp sksystem

3、为新创建的项目新增urls.py
earth/urls.py 增加路径

技术图片或者这样修改,将不用的example去除

技术图片

4、settings.py 增加新增的项目

技术图片

技术图片三、数据库设计:

技术图片

技术图片

技术图片

技术图片

技术图片

技术图片

技术图片

技术图片

models.py

from django.db import models
from utils import tools
from earth import settings


# Create your models here.


class BaseModel(models.Model):
‘‘‘公共字段‘‘‘
is_delete_choice = (
(0, ‘删除‘),
(1, ‘正常‘)
)
is_delete = models.SmallIntegerField(choices=is_delete_choice, default=1, verbose_name=‘是否被删除‘)
create_time = models.DateTimeField(verbose_name=‘创建时间‘, auto_now_add=True) # auto_now_add的意思,插入数据的时候,自动取当前时间
update_time = models.DateTimeField(verbose_name=‘修改时间‘, auto_now=True) # 修改数据的时候,时间会自动变

class Meta:
abstract = True # 只是用来继承的,不会创建这个表


class User(BaseModel):
‘‘‘用户表‘‘‘
phone = models.CharField(verbose_name=‘手机号‘, max_length=11, unique=True)
email = models.EmailField(verbose_name=‘邮箱‘, max_length=25, unique=True)
password = models.CharField(verbose_name=‘密码‘, max_length=32)
username = models.CharField(verbose_name=‘昵称‘, default=‘Python小学生‘, max_length=20)

@staticmethod
def make_password(raw_password):
‘‘‘生成密码‘‘‘
before_password = ‘%s%s‘ % (raw_password, settings.SECRET_KEY) # 生成密码的算法,可以自己改
after_password = tools.md5(before_password)
return after_password

def set_password(self, raw_password):
‘‘‘设置密码‘‘‘
self.password = self.make_password(raw_password)

def check_password(self, raw_password):
‘‘‘校验登录密码‘‘‘
return self.make_password(raw_password) == self.password

def __str__(self):
return self.username

class Meta:
verbose_name = ‘用户表‘
verbose_name_plural = verbose_name
db_table = ‘user‘

class Parameter(BaseModel):#继承BaseModel
‘‘‘全局参数表‘‘‘
name = models.CharField(verbose_name=‘参数名‘, max_length=100, unique=True)#unique=True 唯一
desc = models.CharField(verbose_name=‘描述‘, max_length=200, null=True)#null=True允许传空
value = models.CharField(verbose_name=‘参数值‘, max_length=100)

def __str__(self):
return self.name

class Meta:
verbose_name = ‘全局参数‘
verbose_name_plural = verbose_name
db_table = ‘parameter‘
ordering = [‘-id‘] # 按照id倒排。


class Project(BaseModel):
‘‘‘项目表‘‘‘
name = models.CharField(verbose_name=‘项目名‘, max_length=100, unique=True)
desc = models.CharField(verbose_name=‘描述‘, max_length=200, null=True)
host = models.CharField(verbose_name=‘域名‘, max_length=1024)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘创建者‘)
#on_delete=models.DO_NOTHING 当删除的时候不做任何事情 db_constraint=False 不是真正的去创建外键

def __str__(self):
return self.name

class Meta:
verbose_name = ‘项目表‘
verbose_name_plural = verbose_name
db_table = ‘project‘
ordering = [‘-id‘] # 按照id倒排。


class Interface(BaseModel):
‘‘‘接口‘‘‘
name = models.CharField(verbose_name=‘接口名称‘, max_length=100, unique=True)
uri = models.CharField(verbose_name=‘接口路径‘, max_length=1024)
params = models.CharField(verbose_name=‘默认参数‘, max_length=2048,null=True,blank=True)
headers = models.CharField(verbose_name=‘默认Headers‘, max_length=2048,null=True,blank=True)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘创建者‘)
project = models.ForeignKey(Project, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘归属项目‘)

def __str__(self):
return self.name

class Meta:
verbose_name = ‘接口表‘
verbose_name_plural = verbose_name
db_table = ‘interface‘
ordering = [‘-id‘] # 按照id倒排。


class Case(BaseModel):
‘‘‘用例表‘‘‘
title = models.CharField(verbose_name=‘用例标题‘, max_length=100)
project = models.ForeignKey(Project, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘归属项目‘)
interface = models.ForeignKey(Interface, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘接口‘)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘创建用户‘)
method_choice = (
(1, ‘POST‘),
(2, ‘GET‘),
(3, ‘DELETE‘),
(4, ‘PUT‘),
)
method = models.SmallIntegerField(choices=method_choice, verbose_name=‘请求方式‘)

cache_field = models.CharField(verbose_name=‘缓存字段‘, max_length=128, null=True, blank=True)#null=True,可能为空 blank=True 可能不传

check = models.CharField(verbose_name=‘校验点‘, max_length=512)
params = models.CharField(verbose_name=‘请求参数‘, max_length=2048, null=True, blank=True)
headers = models.CharField(verbose_name=‘请求头信息‘, max_length=2048, null=True, blank=True)
is_json = models.BooleanField(verbose_name=‘参数是否是json‘, default=False)
json = models.CharField(verbose_name=‘json类型参数‘, max_length=2048, null=True, blank=True)
status_choice = (
(1, ‘通过‘),
(2, ‘未运行‘),
(3, ‘运行中‘),
(999, ‘失败‘)
)
status = models.SmallIntegerField(choices=status_choice, verbose_name=‘用例状态‘,
default=2) # 记录上一次的状态 每次执行后需要更新下这个表的这个字段
report_batch = models.CharField(verbose_name=‘最后一次执行的批次号‘, null=True, max_length=512, blank=True)
def __str__(self):
return self.name

class Meta:
verbose_name = ‘用例表‘
verbose_name_plural = verbose_name
db_table = ‘case‘
ordering = [‘-id‘] # 按照id倒排


class CaseCollection(BaseModel):
‘‘‘用例集合‘‘‘
name = models.CharField(verbose_name=‘集合名‘, max_length=100, unique=True)
desc = models.CharField(verbose_name=‘描述‘, max_length=200, null=True)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘创建者‘)
project = models.ForeignKey(Project, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘归属项目‘)
report_batch = models.CharField(verbose_name=‘最后一次执行的批次号‘, null=True, max_length=512, blank=True)
status_choice = (
(2, ‘未运行‘),
(3, ‘运行中‘),
(4, ‘执行完毕‘)
)
status = models.SmallIntegerField(choices=status_choice, verbose_name=‘用例状态‘,
default=2)
case = models.ManyToManyField(Case,verbose_name=‘集合下的用例‘)


def __str__(self):
return self.name

class Meta:
verbose_name = ‘用例集合表‘
verbose_name_plural = verbose_name
db_table = ‘case_collection‘
ordering = [‘-id‘] # 按照id倒排
class Report(BaseModel):
‘‘‘用例报告表‘‘‘
url = models.CharField(verbose_name=‘请求URL‘, max_length=1024)
project = models.ForeignKey(Project, verbose_name=‘项目‘, on_delete=models.DO_NOTHING, db_constraint=False)
title = models.CharField(verbose_name=‘用例名称‘, max_length=100)
params = models.CharField(verbose_name=‘请求参数‘, max_length=2048)
response = models.CharField(verbose_name=‘接口返回值结果‘, max_length=2048)
case = models.ForeignKey(Case, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘结果所属用例‘)
case_collection = models.ForeignKey(CaseCollection, on_delete=models.DO_NOTHING, db_constraint=False,
verbose_name=‘结果所属集合‘, null=True)
batch = models.CharField(verbose_name=‘批次‘, null=True, max_length=128) # 用于区分运行的第几批集合的用例
reason = models.CharField(verbose_name=‘失败原因‘, null=True, max_length=128, blank=True)
status_choice = (
(1, ‘通过‘),
(999, ‘失败‘)
)
duration = models.IntegerField(verbose_name=‘用例耗时‘)
status = models.SmallIntegerField(choices=status_choice, verbose_name=‘用例结果状态‘)
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name=‘运行用户‘)

class Meta:
verbose_name = ‘用例报告表‘
verbose_name_plural = verbose_name
db_table = ‘report‘
ordering = [‘-id‘]
class CasePremise(BaseModel):
‘‘‘依赖用例的关系表‘‘‘
case = models.ForeignKey(Case, verbose_name=‘用例id‘, on_delete=models.DO_NOTHING, db_constraint=False, unique=False,related_name=‘case‘)
premise_case = models.ForeignKey(Case, verbose_name=‘被依赖用例的id‘, on_delete=models.DO_NOTHING, db_constraint=False,
unique=False,related_name=‘premise_case‘)

def __str__(self):
return self.case

class Meta:
unique_together = (‘case‘, ‘premise_case‘) # 联合主键,一个case对另外一个case只能依赖一次
verbose_name = ‘依赖关系表‘
verbose_name_plural = verbose_name
db_table = ‘case_premise‘
生成数据库:
1、makemigrations skssystem

技术图片

2、migrate skssystem

技术图片

3、生成结果如图:

技术图片

四、数据库test——练习
1、在项目下创建文件model_test.py
2、复制如下内容到文件中model_test.py
import django, os
os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘earth.settings‘) # 设置django的配置文件
django.setup()
from skssystem import models

练习一、外键操作练习
user表增加一条内容,
pycharm--database 增加数据方式

技术图片

2、增加完数据后

技术图片

然后model_test.py 内容如下

技术图片

运行后,project 表中有一条内容

技术图片


练习二、正向查询

技术图片

练习三、反向查询

技术图片


练习四、删除

技术图片

现象:project 表数据被删除

练习五、外键自关联
1、使用database 中使用sql语句插入数据

技术图片


2、输入内容然后执行操作:

技术图片

















接口平台测试开发--17

标签:choice   bbb   tar   efi   bst   请求   OLE   a20   email   

原文地址:https://www.cnblogs.com/luoxinmoli/p/12332473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!