前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Django的电子商务网站开发(连载23)

基于Django的电子商务网站开发(连载23)

作者头像
顾翔
发布2019-12-11 16:12:08
2660
发布2019-12-11 16:12:08
举报

3.4.3 商品信息的模糊查询

1. urls.py

...url(r'^search_name/$', views.search_name),...

1. views.py

...# 商品搜索def search_name(request): util = Util() username = util.check_user(request) if username=="": uf = LoginForm() return render(request,"index.html",{'uf':uf,"error":"请登录后再进入"}) else: count = util.cookies_count(request) #获取查询数据 search_name = (request.POST.get("good", "")).strip() #通过objects.filter()方法进行模糊匹配查询,查询结果放入变量good_list good_list = Goods.objects.filter(name__icontains=search_name) #对查询结果进行分页显示 paginator = Paginator(good_list, 5) page = (request.GET.get('page')).strip() try: contacts = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. contacts = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. contacts = paginator.page(paginator.num_pages) return render(request, "goods_view.html", {"user": username, "goodss": contacts,"count":count})...

这里的实现方法与商品概要信息基本上是一致的,不同的地方在于在概要信息中使用代码good_list = Goods.objects.all()获取全部商品信息,而在模糊查询中使用代码good_list= Goods.objects.filter(name__icontains=search_name)来显示符合条件的商品信息。

2. 模板

同商品信息列表

3. 接口测试
1)测试用例

表3-5为商品模糊搜索的测试用例,在这里设计了三个测试用例。

(1)正常的测试用例,查询数据库中符合条件的商品信息,系统应该把这个商品信息正确地被显示出来。

(2)查询字符为空的字符串,系统应该把所有的数据都显示出来。

(3)主要检验模糊查询中是否存在SQL注入,在查询字符中输入SQL模糊查询通配符‘%’,系统应该显示商品标题中含有‘%’的商品,由于测试程序中不含有‘%’的商品,所以查询结果应该为空。

表3-5 商品信息搜索的测试用例

编号

输入数据

期望结果

1

目前已在存在商品名称的一部分

这个商品信息被被查询且显示出来

2

空字符

显示所有内容

3

%

不显示所有内容

2)XML文件

在goodsConfig.xml中加入如下内容。

... <!--- 输入数据:目前已经存在商品名称的子串,期望结果:这个商品被查询出来 --> <case> <TestId>goods-testcase003</TestId> <Title>商品信息</Title> <Method>post</Method> <Desc>查询商品</Desc> <Url>http://127.0.0.1:8000/search_name/</Url> <InptArg>{"good":"龙井"}</InptArg> <Result>200</Result> <CheckWord>龙井</CheckWord><!--- 包含查询商品名称的一部分 --> </case> <!--- 输入数据:空字符,期望结果:显示所有内容 --> <case> <TestId>goods-testcase004</TestId> <Title>商品信息</Title> <Method>post</Method> <Desc>查询商品</Desc> <Url>http://127.0.0.1:8000/search_name/</Url> <InptArg>{"good":""}</InptArg> <Result>200</Result> <CheckWord>龙井茶叶</CheckWord><!--- 与初始化商品名称保持一致 --> </case> <!--- 输入数据:%,期望结果:不显示所有内容 --> <case> <TestId>goods-testcase005</TestId> <Title>商品信息</Title> <Method>post</Method> <Desc>查询商品</Desc> <Url>http://127.0.0.1:8000/search_name/</Url> <InptArg>{"good":"%"}</InptArg> <Result>200</Result> <CheckWord>NOT,龙井茶叶</CheckWord><!---与初始化商品名称在商品列表中不显示,NOT加逗号表示不显示 --> </case>...

最后一个测试数据中<CheckWord>NOT,龙井茶叶</CheckWord>表示“龙井茶叶”不被查询出来,其中“NOT”表示不显示。

3)测试代码

根据<CheckWord>NOT,龙井茶叶</CheckWord>表示“龙井茶叶”不被查询出来,修改测试代码goodsInfoTest.py中的test_goods_info()方法。

...#开始测试 def test_goods_info(self): for mylist in self.mylists: data = self.util.run_test(mylist,self.userValues,self.sign) #验证返回码 self.assertEqual(mylist["Result"],str(data.status_code)) #验证返回文本 #如果mylist["CheckWord"]标签中存在"NOT"字符串,调用断言方法assertNotIn() if "NOT" in mylist["CheckWord"]: self.assertNotIn((mylist["CheckWord"]).split(",")[1],str(data.text)) #否则调用断言方法assertIn() else: self.assertIn(mylist["CheckWord"],str(data.text)) print (mylist["TestId"]+" is passsing!")...

请特别注意粗体字部分,这里不作进一步介绍了。

星云测试

http://www.teststars.cc

奇林软件

http://www.kylinpet.com

联合通测

http://www.quicktesting.net

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-01-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 软件测试培训 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3.4.3 商品信息的模糊查询
    • 1. urls.py
      • 1. views.py
        • 2. 模板
          • 3. 接口测试
            • 1)测试用例
            • 2)XML文件
            • 3)测试代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档