相关疑难解决方法(0)

Python - 如何将 SQLAlchemy 连接到内存中的现有数据库

我正在从现有的 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

我怎样才能一起完成这项工作?

python sqlite sqlalchemy

7
推荐指数
1
解决办法
3282
查看次数

使用 pytest-mock 在 python 中模拟数据库调用

我定义了这个函数:

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 unit-testing mocking pytest python-3.x

7
推荐指数
1
解决办法
1万
查看次数