查看其它操作MongoDB的讲的仅仅是查询,但是实际使用都要对查询的结果进行取值,用于其它地方。找了很多资料发现都没有人写。经研究实验发现
Retrieve Mongodb Records With Desired Fields
这个关键字中的参数returnDocuments=False
,默认返回的是字符串,而改为True返回的是一个列表,而这样就可以对返回结果进行提取操作了,具体演示如下
安装MongoDBLibrary
pip install -U Robotframework-MongoDB-Library
查询
说明:
此处用到了返回结果提取。先获取需要的id值,在下面的一条查询中会用到。最后返回拼接完整的header字符串
*** Settings ***
Library MongoDBLibrary
Resource ../Common/CommonParams.txt
*** Test Cases ***
test_mongodb
${ret} Get Header {"key":"value"}
Log ${ret}
*** Keywords ***
*** Keywords ***
Get Header
[Arguments] ${recordJSON}
Connect To MongoDB mongodb://${mongoUser}:${mongoPwd}@${mongoUrl}:${mongoPort}
${id} Retrieve Mongodb Records With Desired Fields ${mongoName} Account ${recordJSON} _id returnDocuments=True
${result} Retrieve Mongodb Records With Desired Fields ${mongoName} User {"AccountID":"${id}[0][_id]"} aa,bb returnDocuments=True
Disconnect From Mongodb
[Return] {"Content-Type":"application/json","aa":"${result}[0][aa]","bb":"${result}[0][bb]"}
out
Starting test: YAMI.Test.Test.test_mongodb
20210511 15:17:44.991 : INFO : ${id} = [{'_id': '601f7347b3efc161c7de5075'}]
20210511 15:17:45.029 : INFO : ${result} = [{'_id': '6066b0e5352a5aa6e9b397ce', 'Appid': '3/Vq9vzDysJAGji+', 'Secret': 'yLiaiTUGfBTNrSXZBbULWw=='}]
20210511 15:17:45.068 : INFO : ${ret} = {"Content-Type":"application/json","Appid":"3/Vq9vzDysJAGji+","Secret":"yLiaiTUGfBTNrSXZBbULWw=="}
20210511 15:17:45.070 : INFO : {"Content-Type":"application/json","Appid":"3/Vq9vzDysJAGji+","Secret":"yLiaiTUGfBTNrSXZBbULWw=="}
Ending test: YAMI.Test.Test.test_mongodb
代码说明
*** Settings ***
Library MongoDBLibrary
Resource ../Common/CommonParams.txt
导入MongoDBLibrary
由于我的MongoDB数据库的连接参数都是定义到变量文件中的,所以要先引用
*** Keywords ***
Get Header
[Arguments] ${recordJSON}
Connect To MongoDB mongodb://${mongoUser}:${mongoPwd}@${mongoUrl}:${mongoPort}
${id} Retrieve Mongodb Records With Desired Fields ${mongoName} Account ${recordJSON} _id returnDocuments=True
${result} Retrieve Mongodb Records With Desired Fields ${mongoName} User {"AccountID":"${id}[0][_id]"} aa,bb returnDocuments=True
Disconnect From Mongodb
[Return] {"Content-Type":"application/json","aa":"${result}[0][aa]","bb":"${result}[0][bb]"}
将数据库的整个操作定义了一个用户关键字Get Header
[Arguments]定义一个参数${recordJSON},使用该关键字需要传入,即MongoDB中查询需要传入的jsonConnect To MongoDB
连接MongoDB数据库
需要传的参数有4个,mongodb://user:pwd:@host:port。${id} Retrieve Mongodb Records With Desired Fields ${mongoName} A ${recordJSON} _id returnDocuments=True
此处是先从A中查询出_id值赋值给变量${id}作为下一个查询的输入.返回结果是[{’_id’: ‘xxx’}],则可以通过列表索引、字典key取值的方式取出’_id’的值为’xxx’${result} Retrieve Mongodb Records With Desired Fields ${mongoName} U {“AccountID”:"${id}[0][_id]"} aa,bb returnDocuments=True
从U中查询aa和bb两个字段的值,returnDocuments=True表示返回的是一个列表,方便后续提取数据。默认returnDocuments=False返回的是字符串不方便提取,只能用来校验查询结果Disconnect From Mongodb
查询结束后断开数据库连接[Return] {“Content-Type”:“application/json”,“key1”:"${result}[0][aa]",“key2”:"${result}[0][bb]"}
返回拼接好的字符串
${result}结果为[{’_id’: ‘xxx’, ‘aa’: ‘xxx’,‘bb’: ‘xxx’}],取出’aa’和’bb’的值分别为"${result}[0][aa]","${result}[0][bb]",之后再作为新字典’key1’、'key2’的value返回
*** Test Cases ***
test_mongodb
${ret} Get Header {"key":"value"}
Log ${ret}
最后在*** Test Cases ***中创建测试用例调用关键字测试
输出结果为
Starting test: YAMI.Test.Test.test_mongodb
20210511 15:17:44.991 : INFO : ${id} = [{'_id': 'xxx'}]
20210511 15:17:45.029 : INFO : ${result} = [{'_id': 'xxx', 'aa': 'xxx','bb': 'xxx'}]
20210511 15:17:45.068 : INFO : ${ret} = {"Content-Type":"application/json","aa":"xxx","bb":"xxx"}
20210511 15:17:45.070 : INFO : {"Content-Type":"application/json","key1":"xxx","key2":"xxx"}
Ending test: YAMI.Test.Test.test_mongodb
{“Content-Type”:“application/json”,“key1”:“xxx”,“key2”:“xxx”}就是返回的拼接后的字符串