测开之路五十九:接口测试之被测接口准备

 

准备两个接口方便后面进行测试

from flask import Flask
from flask import jsonify
from flask import request
from pymongo import MongoClient

app = Flask(__name__)

mongo = MongoClient(host="127.0.0.1", port=27017) # 连接MongoDB
database = mongo.get_database("exercise") # 定义数据库名
collection = database.get_collection("score") # 定义表名


@app.route("/api/search")
def api_test_get():
""" 查询数据接口 """
data = request.values.to_dict() # 接收数据转为字典格式
""" data = {"name": "XXX"} """
result = collection.find_one(data) # 插入数据
return jsonify({
'status': 0,
'message': 'success',
'data': result
})


@app.route("/api/create", methods=['POST'])
def api_test_post():
""" 新增数据接口 """
data = request.values.to_dict() # 接收数据转为字典格式
"""
data = {
"name": "XXX",
"id": "3512",
"math": 100,
"chinese": 59,
"computer": 89,
"english": 76
}
"""
data.setdefault('_id', data['id'])
result = collection.insert_one(data) # 插入数据
return jsonify({
'status': 0,
'message': 'success',
'data': result.inserted_id
})


if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=8012,
debug=True,
)

 

posted @ 2019-07-01 20:20  向前走。  阅读(319)  评论(0编辑  收藏  举报