Skip to content

Commit

Permalink
Bug/windows tests failing (#322)
Browse files Browse the repository at this point in the history
* Fix test examples

- Change the testing of notebook to pytest-notebook for better windows compatibility
- Add comment in electromobility_example.ipynb
- Fix Error in electromobility_example.ipynb through false filepath
  - Get module path instead of non explicit definition of filepath.
  - Testing the notebooks is using tempfiles there the normal paths are pointing to nothing.

´´´
from edisgo import __file__
module_path = os.path.dirname(os.path.dirname(__file__))
´´´

* Increase timout

* Change test workflow

* Add colors in automated tests

* Fix testing windows with python 3.9

* Added function to download vg250 data in electromobility notebook

* Fix conda env files

- Python version 3.8 or 3.9

* Increase timeout

* Update github testing action

* Minor change to electromobility notebook

* Delete old import

Co-authored-by: birgits <birgit.schachler@rl-institut.de>
Co-authored-by: AnyaHe <anya.heider@rl-institut.de>
Co-authored-by: Kilian Helfenbein <Kilian.Helfenbein@rl-institut.de>
Co-authored-by: mltja <malte.jahn@rl-institut.com>
Co-authored-by: Maike Held <Maike.Held@rl-institut.de>
  • Loading branch information
6 people committed Sep 30, 2022
1 parent d77f17d commit 95271ab
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 72 deletions.
30 changes: 12 additions & 18 deletions .github/workflows/tests-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ concurrency:

jobs:
build:
name: "eDisGo-${{ matrix.name-suffix }} at Python ${{ matrix.python-version }} on ${{ matrix.os }}"
name: "${{ matrix.name-suffix }} at py${{ matrix.python-version }} on ${{ matrix.os }}"
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
include:
- name-suffix: "basic"
os: ubuntu-latest
python-version: 3.8
- name-suffix: "dev"
- name-suffix: "coverage"
os: ubuntu-latest
python-version: 3.8
- name-suffix: "basic"
Expand All @@ -30,6 +27,9 @@ jobs:
- name-suffix: "basic"
os: windows-latest
python-version: 3.8
- name-suffix: "basic"
os: windows-latest
python-version: 3.9

steps:
- name: Checkout repo
Expand All @@ -40,18 +40,12 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install packages (Linux - Basic)
if: runner.os == 'Linux' && matrix.name-suffix == 'basic'
- name: Install packages (Linux)
if: runner.os == 'Linux'
run: |
pip install --upgrade pip wheel setuptools
pip install -e "."
- name: Install packages (Linux - Dev)
if: runner.os == 'Linux' && matrix.name-suffix == 'dev'
run: |
pip install --upgrade pip wheel setuptools
pip install -e ".[dev]"
- name: Install packages (Windows)
if: runner.os == 'Windows'
uses: conda-incubator/setup-miniconda@v2
Expand All @@ -62,16 +56,16 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Run tests
if: ${{ !(runner.os == 'Linux' && matrix.python-version == 3.8 && matrix.name-suffix == 'dev') }}
if: ${{ !(runner.os == 'Linux' && matrix.python-version == 3.8 && matrix.name-suffix == 'coverage') }}
run: |
python -m pip install pytest pytest-notebook
python -m pytest --runslow -vv
python -m pytest --runslow --disable-warnings --color=yes -v
- name: Run tests, coverage and send to coveralls
if: runner.os == 'Linux' && matrix.python-version == 3.8 && matrix.name-suffix == 'dev'
if: runner.os == 'Linux' && matrix.python-version == 3.8 && matrix.name-suffix == 'coverage'
run: |
pip install coveralls
coverage run --source=edisgo -m pytest --runslow -vv
pip install pytest pytest-notebook coveralls
coverage run --source=edisgo -m pytest --runslow --disable-warnings --color=yes -v
coveralls
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions eDisGo_env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ channels:
- conda-forge
- defaults
dependencies:
- python = 3.8
- python >=3.8,<3.10
- pip
- pandas >= 1.2
- pandas >=1.2
- conda-forge::fiona
- conda-forge::geopy
- conda-forge::geopandas
Expand Down
4 changes: 2 additions & 2 deletions eDisGo_env_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ channels:
- conda-forge
- defaults
dependencies:
- python = 3.8
- python >=3.8,<3.10
- pip
- pandas >= 1.2
- pandas >=1.2
- conda-forge::fiona
- conda-forge::geopy
- conda-forge::geopandas
Expand Down
2 changes: 1 addition & 1 deletion examples/edisgo_simple_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.8.13"
},
"toc": {
"base_numbering": 1,
Expand Down
49 changes: 45 additions & 4 deletions examples/electromobility_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"\n",
"from edisgo.edisgo import EDisGo\n",
"from edisgo.tools.logger import setup_logger\n",
"from edisgo.tools.plots import plot_dash, plot_plotly"
"from edisgo.tools.plots import plot_dash, plot_plotly\n",
"\n"
]
},
{
Expand All @@ -65,6 +66,7 @@
"metadata": {},
"outputs": [],
"source": [
"# interactive matplotlib\n",
"%matplotlib notebook"
]
},
Expand Down Expand Up @@ -215,9 +217,18 @@
"source": [
"### Download 'Verwaltungsgebiete' data\n",
"\n",
"The following code shows you how to download the original municipality geodata. To make the example faster we will however skip the download and use a reduced version of the dataset containing only the municipalities that intersect with the grid district used in this example. The reduced dataset is loaded in the next cell.\n",
"\n",
"The following code shows you how to download the original municipality geodata. To make the example faster we will however skip the download and use a reduced version of the dataset containing only the municipalities that intersect with the grid district used in this example. The reduced dataset is loaded in the next cell."
]
},
{
"cell_type": "markdown",
"id": "ccb74f72",
"metadata": {},
"source": [
"```python\n",
"vg250_path = os.path.join(\n",
" os.path.expanduser(\"~\"), \".edisgo\", \"vg250\"\n",
" )\n",
"target = Path(vg250_path, \"vg250_01-01.geo84.shape.ebenen/vg250_ebenen_0101/VG250_GEM.shp\")\n",
"\n",
"if not target.is_file():\n",
Expand Down Expand Up @@ -245,7 +256,37 @@
"metadata": {},
"outputs": [],
"source": [
"vg250 = gpd.read_file(os.path.join(\"data\", \"vg250.shp\"))"
"def download_vg250_data():\n",
"\n",
" # create directory to save vg250 data into\n",
" vg250_path = os.path.join(\n",
" os.path.expanduser(\"~\"), \".edisgo\", \"vg250\"\n",
" )\n",
" os.makedirs(vg250_path, exist_ok=True)\n",
" \n",
" # download files\n",
" filenames = [\n",
" \"vg250.cpg\",\n",
" \"vg250.dbf\",\n",
" \"vg250.prj\",\n",
" \"vg250.shp\",\n",
" \"vg250.shx\",\n",
" ]\n",
"\n",
" for file in filenames:\n",
" req = requests.get(\n",
" \"https://raw.githubusercontent.com/openego/eDisGo/dev/examples/data/{}\".format(\n",
" file\n",
" )\n",
" )\n",
" filename = os.path.join(vg250_path, f\"{file}\")\n",
" with open(filename, \"wb\") as fout:\n",
" fout.write(req.content)\n",
"\n",
" return vg250_path\n",
"\n",
"vg250_path = download_vg250_data()\n",
"vg250 = gpd.read_file(os.path.join(vg250_path, \"vg250.shp\"))"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.8.13"
},
"toc": {
"base_numbering": 1,
Expand Down
68 changes: 24 additions & 44 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging
import os
import subprocess
import tempfile

import nbformat
import pytest
import pytest_notebook

Expand All @@ -15,64 +12,47 @@ def setup_class(self):
os.path.dirname(os.path.dirname(__file__)), "examples"
)

def _notebook_run(self, path):
"""
Execute a notebook via nbconvert and collect output.
Returns (parsed nb object, execution errors)
"""
dirname, __ = os.path.split(path)
os.chdir(dirname)
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = [
"jupyter",
"nbconvert",
path,
"--output",
fout.name,
"--to",
"notebook",
"--execute",
"--ExecutePreprocessor.timeout=150",
]
subprocess.check_call(args)

fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)

errors = [
output
for cell in nb.cells
if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"
]

return nb, errors

@pytest.mark.slow
def test_plot_example_ipynb(self):
path = os.path.join(self.examples_dir_path, "plot_example.ipynb")
notebook = pytest_notebook.notebook.load_notebook(path=path)
result = pytest_notebook.execution.execute_notebook(
notebook, with_coverage=False, timeout=20
notebook,
with_coverage=False,
timeout=600,
)
if result.exec_error is not None:
print(result.exec_error)
assert result.exec_error is None

@pytest.mark.slow
def test_electromobility_example_ipynb(self):
nb, errors = self._notebook_run(
os.path.join(self.examples_dir_path, "electromobility_example.ipynb")
path = os.path.join(self.examples_dir_path, "electromobility_example.ipynb")
notebook = pytest_notebook.notebook.load_notebook(path=path)
result = pytest_notebook.execution.execute_notebook(
notebook,
with_coverage=False,
timeout=600,
)
assert errors == []
if result.exec_error is not None:
print(result.exec_error)
assert result.exec_error is None

@pytest.mark.slow
def test_edisgo_simple_example_ipynb(self):
nb, errors = self._notebook_run(
os.path.join(self.examples_dir_path, "edisgo_simple_example.ipynb")
path = os.path.join(self.examples_dir_path, "edisgo_simple_example.ipynb")
notebook = pytest_notebook.notebook.load_notebook(path=path)
result = pytest_notebook.execution.execute_notebook(
notebook,
with_coverage=False,
timeout=600,
)
assert errors == []
if result.exec_error is not None:
print(result.exec_error)
assert result.exec_error is None

@classmethod
def teardown_class(cls):
logger = logging.getLogger("edisgo")
logger.handlers.clear()
logger.propagate = True

0 comments on commit 95271ab

Please sign in to comment.