Python Django测试数据库中的自定义SQL函数
我正在使用PostgreSQL数据库为Django创建Python Django测试数据库中的自定义SQL函数,python,django,Python,Django,我正在使用PostgreSQL数据库为Django创建unittest测试。我试图调用项目数据库中的函数,但测试失败,因为它找不到该函数 我认为这是因为测试数据库是在测试运行时创建的,它不会加载原始数据库中的函数 我的问题是,如何将该函数包含在Django测试数据库中?手动创建迁移,并从那里运行sql文件。大致如下: from django.db import migrations, connection # you can add more sql files here later on,
unittest
测试。我试图调用项目数据库中的函数,但测试失败,因为它找不到该函数
我认为这是因为测试数据库是在测试运行时创建的,它不会加载原始数据库中的函数
我的问题是,如何将该函数包含在Django测试数据库中?手动创建迁移,并从那里运行sql文件。大致如下:
from django.db import migrations, connection
# you can add more sql files here later on, bc these are just for testing
def run_raw_sql(apps, schema_editor):
f = open('/home/edgle/web/plan/sql/get_plan_data.sql', 'r')
sql = f.read()
with connection.cursor() as cursor:
cursor.execute(sql)
f = open('/home/edgle/web/plan/sql/role_recursion.sql', 'r')
sql = f.read()
with connection.cursor() as cursor:
cursor.execute(sql)
def reverse_run_raw_sql(apps, schema_editor):
pass # fake
class Migration(migrations.Migration):
dependencies = [
('app_name', 'previous_migration_name'),
]
operations = [
migrations.RunPython(run_raw_sql, reverse_run_raw_sql),
]
存在一个migrations.RunSQL: