Skip to content
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

[107] Add Instructions and Script To Run postgis Tests #118

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/master)

### Added
- [dev] Add instructions and script for running `postgres` and `postgis` tests.

### Changed

Expand Down
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ make lint
```

If you don't follow the step 4, your PR may fail due to `black`, `isort`, `flake8` or `pydocstyle` warnings.

To run `postgresql` and `postgis` specific tests:

1. [Install `docker`](https://docs.docker.com/get-docker/).

2. Install the `postgis` dependencies. Follow the
[instructions from the Django docs](https://docs.djangoproject.com/en/3.1/ref/contrib/gis/install/geolibs/):

If you are on Ubuntu/Debian you run the following:

```shell
sudo apt update -y && sudo apt install -y binutils libproj-dev gdal-bin
```

3. Run the following script:

```shell
./postgis-tests.sh
```

That will spin up a `docker` container with `postgresql` and `postgis` enabled and run the full test
suite.
23 changes: 23 additions & 0 deletions postgis-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Set a different port than postgres' default (in case the user is already running postgres locally)
export PGPORT=4111
export PGUSER=postgres
export PGPASSWORD=postgres
export TEST_DB=postgis

# Run the postgis container
docker run --rm --name modelbakery -e POSTGRES_HOST_AUTH_METHOD=trust -p ${PGPORT}:5432 -d postgis/postgis:11-3.0

# Wait a few seconds so the DB container can start up
echo "Waiting for DB container..."
sleep 4s

# Enable all of the extensions needed on the template1 database
docker exec modelbakery /bin/bash -c "psql template1 -c \"CREATE EXTENSION IF NOT EXISTS citext;\" -U postgres"
docker exec modelbakery /bin/bash -c "psql template1 -c \"CREATE EXTENSION IF NOT EXISTS hstore;\" -U postgres"
docker exec modelbakery /bin/bash -c "psql template1 -c \"CREATE EXTENSION IF NOT EXISTS postgis;\" -U postgres"

# Run the tests
python -m pytest

# Spin down the postgis container
docker stop modelbakery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ def pytest_configure():

settings.configure(
DATABASES={
"default": {"ENGINE": db_engine, "NAME": db_name, "HOST": "localhost"}
"default": {
"ENGINE": db_engine,
"NAME": db_name,
"HOST": "localhost",
# The following DB settings are only used for `postgresql` and `postgis`
"PORT": os.environ.get("PGPORT", ""),
"USER": os.environ.get("PGUSER", ""),
"PASSWORD": os.environ.get("PGPASSWORD", ""),
}
},
INSTALLED_APPS=installed_apps,
LANGUAGE_CODE="en",
Expand Down