2016-02-27 162 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與我的綁定相同來解決這個問題,所以都連接到同一個數據庫。一個非常粗略的解決方案,但這是我目前所擁有的。