New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for running tests with Postgres #299
Conversation
Note that this is actually a precursor to the fix for #296. As discussed on that ticket, the test for the fix involves threads, which don't work with sqlite3; so this sets up the Travis build to run the tests with Postgres. I am doing this as its own PR because these changes are independent of the fix for #296, and it didn't feel right to me to lump them all together in a single pull request. |
a20c895
to
08b304c
Compare
include: | ||
- python: 3.6 | ||
env: TOXENV=i18n | ||
include: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation in this file was inconsistent. The Python versions were indented with two spaces, but everything else was indented with one space.
I opted to indent everything two spaces. Happy to revert these particular changes if they're objectionable for some reason.
test_settings.py
Outdated
except ImportError: | ||
print('Using the DATABASE_URL variable requires dj-database-url and ' | ||
'psycopg2. Try:\n\npip install -r travis.txt') | ||
sys.exit(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My feeling is that it's better to error and exit in this scenario rather than falling back to a database backend that isn't what was specified for the environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also either (a) let the exception bubble up or (b) re-raise after printing the error message. Either way, an exception at this point will halt execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, better not to call sys.exit()
and let it exit on its own by re-raising.
I don't love the implicit dependency on psycopg2
just because someone has defined DATABASE_URL
at all—it might be useful to run the tests against MariaDB or to use PyGreSQL. But I don't think it's worth blocking this on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree re: the dependency on psycopg2. I was taking a "Don't build it until you need it" approach, figuring that the next person who comes along and wants to run tests locally with a different database could submit their own pull request to make the logic to handle the ImportError
more surgical. Perhaps that was selfish of me.
I'm happy to make the logic here a little more conservative, only requiring psycopg2 if the DB backend is actually Postgres.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, I think your approach is right, we can add if we need later
@@ -8,6 +8,7 @@ deps = | |||
django111: Django>=1.11,<2.0 | |||
django20: Django>=2.0,<2.1 | |||
-rtravis.txt | |||
passenv = DATABASE_URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, tox only passes the PATH
environment variable. This is necessary to get the value for DATABASE_URL
defined in .travis.yml passed to tox.
@@ -2,6 +2,8 @@ | |||
# versions. | |||
mock==1.3.0 | |||
Jinja2>=2.7.1 | |||
dj-database-url==0.5.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just provides a convenient way of parsing the DATABASE_URL
environment variable. I felt it was fairly harmless to add a dependency here, since these are after all only the CI requirements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like I've implemented this myself with urlparse
so many times. TIL!
django-jinja>=2.1,<3 | ||
psycopg2>=2.7.5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is obviously to allow Postgres to be used as the DB engine.
The CI build is failing, but I'm not sure why. As far as I know I followed the instructions for setting up PostgreSQL in Travis correctly. The docs say:
Yet the test failure seems to indicate:
So I don't know. I'm going to try explicitly setting the port number, but if that doesn't work I'm out of ideas. Maybe somebody else knows what magic incantation is required to get it working. |
972ba5a
to
1be78f4
Compare
Ah, I see my mistake now. I misread: the docs only say that the default user is postgres; it seems the hostname to use to connect to the database server is just localhost. Now the tests are passing :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment to .travis.yml
and/or the README
to inform other developers that Travis tests use PostgreSQL, and explain why.
@jsocol any feedback? If not, I will merge in ~72 hours.
test_settings.py
Outdated
except ImportError: | ||
print('Using the DATABASE_URL variable requires dj-database-url and ' | ||
'psycopg2. Try:\n\npip install -r travis.txt') | ||
sys.exit(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also either (a) let the exception bubble up or (b) re-raise after printing the error message. Either way, an exception at this point will halt execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_settings.py
Outdated
except ImportError: | ||
print('Using the DATABASE_URL variable requires dj-database-url and ' | ||
'psycopg2. Try:\n\npip install -r travis.txt') | ||
sys.exit(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, better not to call sys.exit()
and let it exit on its own by re-raising.
I don't love the implicit dependency on psycopg2
just because someone has defined DATABASE_URL
at all—it might be useful to run the tests against MariaDB or to use PyGreSQL. But I don't think it's worth blocking this on.
@@ -2,6 +2,8 @@ | |||
# versions. | |||
mock==1.3.0 | |||
Jinja2>=2.7.1 | |||
dj-database-url==0.5.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like I've implemented this myself with urlparse
so many times. TIL!
This adds support for setting the DATABASE_URL environment variable to run tests with database engines other than sqlite3 locally. It also configures the CI build to run the tests with Postgres.
1be78f4
to
1617605
Compare
OK, I've added a comment to .travis.yml as @clintonb suggested and changed the logic in test_settings.py to raise an I squashed these commits because it's my understanding that is preferred for this library. |
This adds support for setting the DATABASE_URL environment variable to run tests with database engines other than sqlite3 locally. It also configures the CI build to run the tests with Postgres.