我正在从现有的 shema 创建数据库,并将其存储在:memory:.
db = Database(filename=':memory:', schema='schema.sql')
db.recreate()
Run Code Online (Sandbox Code Playgroud)
我现在想将其“链接”到 SQL Alchemy。遵循了不同的方法,但无法得到正确的结果。
我目前的尝试如下:
engine = create_engine('sqlite:///:memory:')
Base = automap_base()
Base.prepare(engine, reflect=True)
User = Base.classes.user
session = Session(engine)
Run Code Online (Sandbox Code Playgroud)
就像我尝试过的其他东西一样,这会抛出AttributeError: user。
我怎样才能一起完成这项工作?
我定义了这个函数:
def count_occurrences(cursor, cust_id):
cursor.execute("SELECT count(id) FROM users WHERE customer_id = %s", (cust_id))
total_count = cursor.fetchone()[0]
if total_count == 0:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
我想对它进行单元测试,因为我需要在这里模拟数据库调用。
如何使用 pytest、mock 来完成此操作?
database ×1
mocking ×1
pytest ×1
python ×1
python-3.x ×1
sqlalchemy ×1
sqlite ×1
unit-testing ×1