dimanche 29 octobre 2017

How to configure flask setUp function in unittest with pymongo database

I'm new here. After developing small CRUD application in Flask I need to write some API tests but I really don't know how to configure setUp function with unittest. This is my init.py file in app folder.

from flask import Flask
from flask_cors import CORS
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config.from_object('config')
app.config.from_envvar('APP_CONFIG_FILE')

CORS(app)
mongo = PyMongo(app)



from app.views.index import index_mod # noqa: 402
from app.views.lists import lists_routes # noqa: 402
from app.views.cards import cards_routes # noqa: 402

app.register_blueprint(index_mod)
app.register_blueprint(lists_routes, url_prefix='/board')
app.register_blueprint(cards_routes, url_prefix='/board/lists')

I made directory ./test/basic_test.py but also couldn't do: from app import app :( Below is example of route I want to test.

@lists_routes.route('/lists', methods=['GET', 'POST'])

def get_all_lists():
    lists = mongo.db.lists

if request.method == 'GET':
    output = []

    for q in lists.find():
        output.append({'name': q['name'], 'list_id': str(q['_id'])})

elif request.method == 'POST':
    name = request.json['name']

    list_post = lists.insert_one({'name': name}).inserted_id
    new_list = lists.find_one_or_404({'_id': list_post})

    output = {'name': new_list['name'], 'list_id': str(new_list['_id'])}

return jsonify(output)

Thank you for helping me.

Aucun commentaire:

Enregistrer un commentaire