基于 httprunner Django数据模型关系-正反向查询概念解析
数据库模型关系图

from django.db.models import Value
from django.test import TestCase
#运行 python manage.py test
# Create your tests here.
from sqtp.models import Case,Config,Step,Request
class TestRelateQuery(TestCase):
def setUp(self) -> None:
#创建用例
config1=Config.objects.create(name='case003',base_url='http://localhost')
config2 = Config.objects.create(name='case004', base_url='http://localhost')
self.case1=Case.objects.create(config=config1)
self.case2 = Case.objects.create(config=config2)
def test_steps_query(self):
step1=Step.objects.create(belong_case=self.case1,name='step1')
step1.linked_case=self.case2
step1.save()
step2 = Step.objects.create(belong_case=self.case2,name='step2')
#正向查询:
print('-----1------')
print(step1.belong_case)#查看step1所属用例
print(step1.linked_case)#查看step1关联的用例
#反向查询:
print('---------2-------')
print(self.case1.teststeps.all())#查看用例一的步骤
print(self.case2.linked_steps.all()) # 查看用例2被哪些步骤应用
class TestJsonField(TestCase):#执行 python manage.py test sqtp.tests.TestJsonField
def setUp(self) -> None:#request
req1=Request.objects.create(method=1,url='/abc/def/',
data={"name":"zhangsan","age":18,"address":"hangzhou"})
def test_json01(self):
res=Request.objects.all() .first()
print(res)
#测试修改
res.data={"name":"小强","age":18,"address":"hangzhou"}
res.save()
print('---------3-------')
print(Request.objects.all().first().data)#查看修改后的内容
#修改局部
print(Request.objects.all().first().data['name'])#修改前
res.data['name']='李四'
res.save()
print(Request.objects.all().first().data['name'])#修改后
print("********")
print(Request.objects.filter(method=1))
#完全匹配
print(Request.objects.filter(url='/abc/def/'))
#删除整体
# res.data=Value('null')
# #res.data = None 有区别
# print('---------4-------')
# res.save()
# print(Request.objects.all().first().data)
# 删除局部
# res.data.pop('name')
# res.save()
# print(Request.objects.all().first().data)
#条件查询
print('---------5-------')
res=Request.objects.filter(data__name='李四')
print(res)
class TestFieldQuery(TestCase):#执行 python manage.py test sqtp.tests.TestFieldQuery
def setUp(self) -> None:#request
req1=Request.objects.create(method=1,url='/abc/def/',
data={"name":"zhangsan","age":18,"address":"hangzhou"})
def test_query01(self):
res=Request.objects.all() .first()
print("********")
print(Request.objects.filter(method=1))
#完全匹配
print(Request.objects.filter(url='/abc/def/'))
#字段条件的语法是字段__条件名
print(Request.objects.filter(url__iexact='/abc/def/'))
print(Request.objects.filter(url__contains='def/'))
print(Request.objects.filter(url__in=['/abc/def/','/ab1c/def/']))
#跨关系查询
class TsetOverRelations(TestCase):#执行 python manage.py test sqtp.tests.TsetOverRelations
def setUp(self) -> None:
# 创建用例
config1 = Config.objects.create(name='case003', base_url='http://localhost1')
self.case1 = Case.objects.create(config=config1)
config2 = Config.objects.create(name='case004', base_url='http://localhost2')
self.case2 = Case.objects.create(config=config2)
def test_step_request(self):
#准备测试数据
step1 = Step.objects.create(belong_case=self.case1, name='step1')
step2 = Step.objects.create(belong_case=self.case1, name='step2')
step3 = Step.objects.create(belong_case=self.case2, name='step3')
step4 = Step.objects.create(belong_case=self.case2, name='step4')
req1 = Request.objects.create(method=1, url='/abc/def/1',
data={"name": "zhangsan1", "age": 18, "address": "hangzhou"},step=step1)
req2= Request.objects.create(method=1, url='/abc/def/2',
data={"name": "zhangsan2", "age": 18, "address": "hangzhou"}, step=step2)
req3 = Request.objects.create(method=1, url='/abc/def/3',
data={"name": "zhangsan3", "age": 18, "address": "hangzhou"}, step=step3)
req4 = Request.objects.create(method=1, url='/abc/def/4',
data={"name": "zhangsan4", "age": 18, "address": "hangzhou"}, step=step4)
#跨关系查询
print(req1.step.belong_case)#链式查找法
print('*************')
print(Request.objects.filter(step=step1))
print("**************")
#跨关系语法字段__关联字段
print(Request.objects.filter(step__belong_case=self.case1
))
print('***********')
print(Request.objects.filter(step__belong_case=self.case2
))
print('***********')
print(Request.objects.filter(step__belong_case__config__name='case003'
).filter(url__contains="/abc/def/1"))

浙公网安备 33010602011771号