Skip to content

Commit

Permalink
feat: introduce tests and CI for testing
Browse files Browse the repository at this point in the history
Co-authored-by: Jakob Schnell <Jakob.Schnell@heigit.org>
  • Loading branch information
merydian and koebi authored Aug 23, 2024
1 parent d5ede0c commit 1f0e123
Show file tree
Hide file tree
Showing 15 changed files with 1,169 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

name: Testing

on:
pull_request:

jobs:
test_3_16:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run test 3.16
run: |
docker run -v ${GITHUB_WORKSPACE}:/src -w /src qgis/qgis:release-3_16 sh -c 'apt-get -y update && apt-get -y install xvfb && export ORS_API_KEY=${{ secrets.ORS_API_KEY }} && export DISPLAY=:0.0 && pip install -U pytest && xvfb-run pytest'
env:
DOCKER_IMAGE: ${{ steps.docker-build.outputs.FULL_IMAGE_NAME }}
test_3_22:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run test 3.22
run: |
docker run -v ${GITHUB_WORKSPACE}:/src -w /src qgis/qgis:release-3_22 sh -c 'apt-get -y update && apt-get -y install xvfb && export DISPLAY=:0.0 && export ORS_API_KEY=${{ secrets.ORS_API_KEY }} && export DISPLAY=:0.0 && pip install -U pytest && xvfb-run pytest'
env:
DOCKER_IMAGE: ${{ steps.docker-build.outputs.FULL_IMAGE_NAME }}
test_latest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run test latest
run: |
docker run -v ${GITHUB_WORKSPACE}:/src -w /src qgis/qgis:latest sh -c 'apt-get -y update && apt-get -y install xvfb && export DISPLAY=:0.0 && export ORS_API_KEY=${{ secrets.ORS_API_KEY }} && apt install python3-pytest && xvfb-run pytest'
env:
DOCKER_IMAGE: ${{ steps.docker-build.outputs.FULL_IMAGE_NAME }}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ RELEASING:
- Improved type hints


# Unreleased
### Added
- Unit- and e2e-testing

## [1.7.1] - 2024-01-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion ORStools/common/directions_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def build_default_parameters(


def get_extra_info_features_directions(
response: dict, extra_info_order: list[str], to_from_values: Optional[list] = None
response: dict, extra_info_order: List[str], to_from_values: Optional[list] = None
):
extra_info_order = [
key if key != "waytype" else "waytypes" for key in extra_info_order
Expand Down
6 changes: 5 additions & 1 deletion ORStools/gui/ORStoolsDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
import os
from typing import Optional

import processing
try:
import processing
except ModuleNotFoundError:
pass

import webbrowser

from qgis._core import Qgis, QgsAnnotation
Expand Down
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ORS Tools QGIS plugin

![Testing](https://github.com/Merydian/orstools-qgis-plugin/actions/workflows/test.yml/badge.svg)
![Ruff](https://github.com/Merydian/orstools-qgis-plugin/actions/workflows/ruff.yml/badge.svg)

![ORS Tools](https://user-images.githubusercontent.com/23240110/122937401-3ee72400-d372-11eb-8e3b-6c435d1dd964.png)

Set of tools for QGIS to use the [openrouteservice](https://openrouteservice.org) (ORS) API.
Expand Down Expand Up @@ -120,6 +123,65 @@ where `<qgis_plugins_path>` is one of:
- Windows: `C:\Users\USER\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins\ORStools`
- Mac OS: `Library/Application Support/QGIS/QGIS3/profiles/default/python/plugins/ORStools`

### CI
#### Testing
The repository tests on the QGis Versions *3.16*, *3.22* and the *latest* version.
Until now, it's only possible to test one version at a time.

#### Linux
On linux machines you can run the tests with your local QGIS installation.

1. Install QGIS and make sure it's available in your currently activated environment.

You will need an ORS-API key. Either set it as an environment variable or do `export ORS_API_KEY=[Your API key here]` before you run the tests.

To run the tests do:
```shell
cd orstools-qgis-plugin
pytest
```

#### Windows
Do all the following steps in a [*WSL*](https://learn.microsoft.com/en-us/windows/wsl/install). To run tests locally you can use a [conda installation](https://github.com/opengisch/qgis-conda-builder) of the QGis version you want to test.
You will also have to install *xvfb* to run the tests on involving an interface.
Lastly, we need [*Pytest*](https://docs.pytest.org/en/8.0.x/) to run tests in general.

To do the above run use these commands:
1. Install a version of anaconda, preferrably [*miniforge*](https://github.com/conda-forge/miniforge).

2. Create and prepare the environment.

```shell
# create environment
conda create --name qgis_test
# activate environment
conda activate qgis_test
# install pip
conda install pip
```

3. Install QGis using mamba.
```shell
conda install -c conda-forge qgis=[3.16, 3.22, latest] # choose one
```

4. Install *xvfb*
```shell
sudo apt-get update
sudo apt install xvfb
```

5. Install *Pytest* using pip in testing environment.
```shell
pip install -U pytest
```

To run the tests you will need an ORS-API key:
```shell
cd orstools-qgis-plugin
export ORS_API_KEY=[Your API key here] && xvfb-run pytest
```

### Debugging
In the **PyCharm community edition** you will have to use logging and printing to inspect elements.
The First Aid QGIS plugin can probably also be used additionally.
Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# developement
ruff
pytest

# testing
pyyaml
pytest
Empty file added tests/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import yaml

from ORStools.utils.configmanager import read_config

with open("ORStools/config.yml", "r+") as file:
data = yaml.safe_load(file)


def pytest_sessionstart(session):
"""
Called after the Session object has been created and
before performing collection and entering the run test loop.
"""
if data["providers"][0]["key"] == "":
data["providers"][0]["key"] = os.environ.get("ORS_API_KEY")
with open("ORStools/config.yml", "w") as file:
yaml.dump(data, file)
else:
raise ValueError("API key is not empty.")


def pytest_sessionfinish(session, exitstatus):
"""
Called after whole test run finished, right before
returning the exit status to the system.
"""
with open("ORStools/config.yml", "w") as file:
if not data["providers"][0]["key"] == "":
data['providers'][0]['key'] = '' # fmt: skip
yaml.dump(data, file)

config = read_config()
assert config["providers"][0]["key"] == '' # fmt: skip
Loading

0 comments on commit 1f0e123

Please sign in to comment.