سؤال

I've created a file <APP>/<MODEL>.sql according to the Django docs in order to make use of a hook to pass arbitrary SQL after syncdb is run. Inside this file are two function declarations for PostgreSQL and a statement that creates a database view. This runs fine in production but -- just as the docs say -- the code is not run for the test database because the use of fixtures is suggested. Now my unittests are missing the crucial database views and functions and thus fail.

How do I test code that relies on raw sql functions / views?

UPDATE

I dug up this ticket which concerns this question directly and also presents a small workaround.

هل كانت مفيدة؟

المحلول

I found the best way to handle this is to put the custom SQL code into Django's migrations.

Django and South (which is the predecessor to Django's own migration framework) both provide commands to create custom (i.e. empty) migrations. The code for creating database views or functions can be put into an empty migration and will be run whenever a new installation of the project is migrated or the test suite is run.

A tutorial on how to use custom migrations for database views with South can be found here. The syntax is a bit different in Django's own migration framework but the documentation about RunSQL explains it all.

نصائح أخرى

Just run them natively like the sql that they are.

or

Use sqlcustom

or

Don't bother with them; you might find yourself swimming upstream to try and make good use of these functions and view via the ORM.

or

Consider another python framework (dare i say it) which is more attuned to using native sql.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top