【发布时间】:2016-06-03 22:21:45
【问题描述】:
我正在尝试使用 pyramid framework 和 sqlalchemy ORM 将方法添加到用 python 编写的(相当大的)现有项目中。我想用 sqlalchemy 执行一个 sql 查询,但我以前从未用金字塔或 sqlalchemy 开发过。所以我想对其进行测试并查看查询是否返回我所期望的,但我不想添加无用的代码来测试我的查询(如新模板、视图等)。我的 SQL 查询是:
select a.account_type, u.user_id from accounts a inner join account_users au on a.account_id=au.account_id inner join users u on u.user_id=au.user_id where u.user_id = ?;
我的方法是:
def find_account_type_from_user_id(self,user_id):
'''
Method that finds the account type (one/several points of sale...)
from the id of the user who is linked to this account
:param user_id:
:return:(string) account_type
'''
q = self.query(Account)\
.join(AccountUser)\
.join(User)\
.filter(User.user_id == user_id)\
.one()
return q
ps:我已经在互联网上搜索过,但我只找到诸如:单元测试等之类的东西,而我从未这样做过。 (菜鸟很抱歉)。
【问题讨论】:
标签: sqlalchemy pyramid