danni / django-postgres-composite-types

Postgres composite types support for Django
BSD 3-Clause "New" or "Revised" License
51 stars 13 forks source link

Typecaster configuration lost across pytest-django test runs #16

Open dalberto opened 7 years ago

dalberto commented 7 years ago

When running a test suite in a project that uses django-postgres-composite-types I observed some unexpected behavior. The test suite succeeded on the first run (as expected). However, when re-running the test suite, tests that exercised models that have composite types, failed with an error like:

AttributeError: 'str' object has no attribute 'my_field'

After further inspection, I found that the types were not correctly cast in subsequent runs (even though they were always correctly persisted). In subsequent runs, the type for the composite type field was indeed str, while during the first run it was the correct type.

My workaround is to simply call the type's register_composite method at the very beginning of the test run in conftest.py (I'm using py.test).

This issue does not manifest itself when running Django's development server or when running Django with gunicorn, so it can very well have something to do with how the test suite is configured.

For reference, I'm running:

django 1.11.3
psycopg2 2.7.3
pytest 3.1.3
pytest-django 3.1.2

I'm also running pytest-django with the the --reuse-db flag, so migrations are not re-run, including MyType.Operation().

As I mentioned this can very well be the result of my test configuration, but any help or guidance would be appreciated.

danni commented 7 years ago

I've not used pytest-django, so I don't know how it initialises the database. Can you reduce it down to a test case?