Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. from flask import Flask
  2.  
  3. def create_app(config_filename = None):
  4. app = Flask(__name__)
  5. if config_filename is None:
  6. app.config.from_pyfile('config.py', silent=True)
  7. else:
  8. app.config.from_mapping(config_filename)
  9.  
  10. from .model import db
  11. db.init_app(app)
  12. db.create_all(app=app)
  13.  
  14. return app
  15.  
  16. from flask_sqlalchemy import SQLAlchemy
  17.  
  18. db = SQLAlchemy()
  19.  
  20. class Document(db.Model):
  21. id = db.Column(db.Integer, primary_key=True)
  22. document_uri = db.Column(db.String, nullable=False, unique=True)
  23.  
  24. import pytest
  25. from model import Document, db
  26. from myapplication import create_app
  27.  
  28. @pytest.fixture
  29. def app():
  30. config = {
  31. 'SQLALCHEMY_DATABASE_URI': f"sqlite:///:memory:",
  32. 'TESTING': True,
  33. 'SQLALCHEMY_TRACK_MODIFICATIONS': False
  34. }
  35. app = create_app(config)
  36. yield app
  37. with app.app_context():
  38. db.drop_all()
  39.  
  40. @pytest.fixture
  41. def app_with_documents(app):
  42. with app.app_context():
  43. document_1 = Document(document_uri='Document 1')
  44. document_2 = Document(document_uri='Document 2')
  45. document_3 = Document(document_uri='Document 3')
  46. document_4 = Document(document_uri='Document 4')
  47. db.session.add_all([document_1, document_2, document_3, document_4])
  48. db.session.commit()
  49. return app
  50.  
  51. def test_unit_test_1(app_with_documents):
  52. ...
  53.  
  54. def test_unit_test_2(app_with_documents):
  55. ...
  56.  
  57. def do_execute(self, cursor, statement, parameters, context=None):
  58. > cursor.execute(statement, parameters)
  59. 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)
  60.  
  61. @pytest.fixture
  62. def app():
  63. """
  64. A Flask application.
  65. """
  66. db_fd, db_filename = tempfile.mkstemp(suffix='.sqlite')
  67. config = {
  68. 'SQLALCHEMY_DATABASE_URI': f"sqlite:///{db_filename}",
  69. 'TESTING': True,
  70. 'SQLALCHEMY_TRACK_MODIFICATIONS': False
  71. }
  72. yield create_app(config)
  73. os.close(db_fd)
  74. os.unlink(db_filename)
Add Comment
Please, Sign In to add comment