2016-02-27 165 views
1

我已经看到很多类似于此处发布的问题,但我没有发现任何帮助。我正在尝试使用Flask-Testing和Flask-Fixtures编写单元测试。我遇到了问题,因为Flask-Fixtures在传递给它的数据库上调用create_all,在这种情况下,它实际上并不创建表。我检查db.metadata.tables.keys这表明所有型号的表创建使用db.ModelFlask-SQLAlchemy create_all不起作用

config.py

from flask_api import FlaskAPI 
from flask.ext.sqlalchemy import SQLAlchemy 
from .models import db 


app = FlaskAPI(__name__) 
app.config.from_object('app.test_config') 

ctx = app.app_context() 
ctx.push() 
db.init_app(app) 
db.create_all() 

答:当我在做这个

import os 
from config import BASE_DIR 

# SQLALCHEMY_DATABASE_URI = 'sqlite:///test-db' 
SQLALCHEMY_DATABASE_URI = 'sqlite:////Users/XXX/projects/XXX-api/app/inventory/test-db' 
testing = True 
debug = True 

FIXTURES_DIRS = (
    os.path.join(BASE_DIR, "inventory/"), 
) 

表的创建失败vars(db.metadata)给出这个:

{'_bind': None, 
'_fk_memos': defaultdict(list, {}), 
'_schemas': set(), 
'_sequences': {}, 
'naming_convention': immutabledict({'ix': 'ix_%(column_0_label)s'}), 
'schema': None, 
'tables': immutabledict({'gr_merchant_ftp_info': Table('gr_merchant_ftp_info', MetaData(bind=None), Column('id', BigInteger(), table=<gr_merchant_ftp_info>, primary_key=True, nullable=False), Column('merchant_id', BigInteger(), table=<gr_merchant_ftp_info>), Column('chain_id', BigInteger(), table=<gr_merchant_ftp_info>), Column('hostname', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('username', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('password', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('path', String(length=512), table=<gr_merchant_ftp_info>, nullable=False), Column('install_ts', DateTime(timezone=True), table=<gr_merchant_ftp_info>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007dd10>, for_update=False)), Column('parsed_file_base_path', String(length=256), table=<gr_merchant_ftp_info>), schema=None), 'gr_article_product_mapping': Table('gr_article_product_mapping', MetaData(bind=None), Column('id', BigInteger(), table=<gr_article_product_mapping>, primary_key=True, nullable=False, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11006fc10>, for_update=False)), Column('product_id', BigInteger(), table=<gr_article_product_mapping>), Column('article_id', String(length=128), table=<gr_article_product_mapping>), Column('install_ts', DateTime(timezone=True), table=<gr_article_product_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d090>, for_update=False)), Column('store_code', String(length=128), table=<gr_article_product_mapping>), Column('conversion_factor', Float(precision=53), table=<gr_article_product_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d210>, for_update=False)), schema=None), 'gr_store_merchant_mapping': Table('gr_store_merchant_mapping', MetaData(bind=None), Column('id', BigInteger(), table=<gr_store_merchant_mapping>, primary_key=True, nullable=False, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x10f67f790>, for_update=False)), Column('merchant_id', BigInteger(), table=<gr_store_merchant_mapping>), Column('store_id', BigInteger(), table=<gr_store_merchant_mapping>), Column('install_ts', DateTime(timezone=True), table=<gr_store_merchant_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d650>, for_update=False)), Column('store_code', String(length=128), table=<gr_store_merchant_mapping>), schema=None)})} 

任何指针将不胜感激,谢谢!

+0

单元测试是如此有用,但这是一种屁股疼痛的屁股。我无法从您的问题中判断是否存在实际的堆栈跟踪,警告或此处发生的无声故障。我也不知道你的ORM模型是否可以自己工作,但当你尝试使用它们进行单元测试时会失败。 – AlexLordThorsen

+0

这是'失败'的默默,我想通了。 – madhukar93

回答

1

我想通了。问题是我使用了多个数据库,并且我没有在我的test_config中指定SQLALCHEMY_BINDS。尽管后来我遇到了一个新问题,尽管create_all现在可以创建我的表格,因为它经历了所有绑定,Flask-Fixtures显然不支持它,它只是在插入数据时使用默认引擎。我通过保持SQLALCHEMY_DATABASE_URI与我的绑定相同来解决这个问题,所以都连接到同一个数据库。一个非常粗略的解决方案,但这是我目前所拥有的。