Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- def create_app(config_filename = None):
- app = Flask(__name__)
- if config_filename is None:
- app.config.from_pyfile('config.py', silent=True)
- else:
- app.config.from_mapping(config_filename)
- from .model import db
- db.init_app(app)
- db.create_all(app=app)
- return app
- from flask_sqlalchemy import SQLAlchemy
- db = SQLAlchemy()
- class Document(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- document_uri = db.Column(db.String, nullable=False, unique=True)
- import pytest
- from model import Document, db
- from myapplication import create_app
- @pytest.fixture
- def app():
- config = {
- 'SQLALCHEMY_DATABASE_URI': f"sqlite:///:memory:",
- 'TESTING': True,
- 'SQLALCHEMY_TRACK_MODIFICATIONS': False
- }
- app = create_app(config)
- yield app
- with app.app_context():
- db.drop_all()
- @pytest.fixture
- def app_with_documents(app):
- with app.app_context():
- document_1 = Document(document_uri='Document 1')
- document_2 = Document(document_uri='Document 2')
- document_3 = Document(document_uri='Document 3')
- document_4 = Document(document_uri='Document 4')
- db.session.add_all([document_1, document_2, document_3, document_4])
- db.session.commit()
- return app
- def test_unit_test_1(app_with_documents):
- ...
- def test_unit_test_2(app_with_documents):
- ...
- def do_execute(self, cursor, statement, parameters, context=None):
- > cursor.execute(statement, parameters)
- E sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: document [SQL: 'INSERT INTO document (document_uri) VALUES (?)'] [parameters: ('Document 1',)] (Background on this error at: http://sqlalche.me/e/e3q8)
- @pytest.fixture
- def app():
- """
- A Flask application.
- """
- db_fd, db_filename = tempfile.mkstemp(suffix='.sqlite')
- config = {
- 'SQLALCHEMY_DATABASE_URI': f"sqlite:///{db_filename}",
- 'TESTING': True,
- 'SQLALCHEMY_TRACK_MODIFICATIONS': False
- }
- yield create_app(config)
- os.close(db_fd)
- os.unlink(db_filename)
Add Comment
Please, Sign In to add comment