Django should load custom SQL when testing
|Reported by:||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Cc:||Danilo Bargen||Triage Stage:||Unreviewed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When running "python manage.py test appname", Django fails to load the custom SQL from the appname/sql folder. I am currently forced to use the following ugly workaround:
def load_sql(sender, connection, *args, **kwargs): if connection.settings_dict["NAME"] != "test_projectname": return f = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "sql", "custom_types.sql"), "r") sql = f.read() cursor = connection.cursor() cursor.execute(sql) transaction.commit_unless_managed() backends.signals.connection_created.connect(load_sql)
It is important to load the custom SQL, because I am using it to define composite objects in the Postgres backend and I have written Django wrappers for them, which work fine. These composite objects allow me to store information like "50 USD per ton" or "100 EUR per kg" in a single field. The SQL that's being loaded also defines a set of new operators, which allow me to sort based on this field on the server side, using the latest currency exchange rates and weight unit conversions. Previously I had to store this information in several standard fields and either write a complex query or load them all into Python and sort there.
Please fix this.
Change History (10)
comment:2 Changed 5 years ago by
|Status:||closed → reopened|
|Summary:||Django fails to load custom SQL when running tests → Django should load custom SQL when testing|
|Type:||Bug → New feature|