-
Notifications
You must be signed in to change notification settings - Fork 5
PostgreSQL testing
The procedures and libraries we use for testing the PostgreSQL backend for pepys-import have tripped a few people up - so this page attempts to document the details of how - and why - we test PostgreSQL.
We use the testing.postgresql library for testing our PostgreSQL interfaces. This library actually starts and stops new Postgres servers as part of each test. The benefits of this are that:
- We don't have to have a Postgres server set up on a specific port and with specific users for the tests to work
- We can test the creation and destruction of the database
- Anything we do for the tests will be deleted straight after the test - and we can't affect any 'real' data
However, doing things this way means that the tests look a little strange in places, and that we have to have a Postgres server set up on the machine that we're running the tests on, and have the various executables (principally postgres
and initdb
) available on the PATH. This means we cannot run the tests against a remote Postgres server.
In the tests, we create a testing.postgresql.Postgresql
instance in the setUp
method of a test class, as follows:
self.postgres = Postgresql(
database="test",
host="localhost",
user="postgres",
password="postgres",
port=55527,
)
It should be remembered that this is not anything from within pepys, and is not setting up a connection to a server. This is a call to a testing.postgresql
function that will, when run, create a Postgres server which can be accessed using the credentials, host and port provided.
Once we've created the server to connect to, we then use pepys functions to connect to and initialise a DataStore:
self.store = DataStore(
db_name="test",
db_host="localhost",
db_username="postgres",
db_password="postgres",
db_port=55527,
)
self.store.initialise()
In the tearDown method of the test class, we stop the Postgres server:
self.postgres.stop()
testing.postgresql
has a few issues on Windows. To work at all on Windows, the latest master
version from the testing.postgresql Github repo must be installed, as this has a fix to stop the server correctly on Windows. (Note: requirements_dev.txt
installs the master version).