From b77d1363c958cbbde0f0b92bc836a6a8945a55e5 Mon Sep 17 00:00:00 2001 From: barneydobson Date: Wed, 23 Oct 2024 16:38:27 +0100 Subject: [PATCH 1/7] add test cli --- tests/test_cli.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/test_cli.py diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 00000000..d1776327 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,40 @@ +"""Integration test for the swmmanywhere command-line interface.""" +import sys +import tempfile +import yaml +from pathlib import Path + +import pytest + +from swmmanywhere import __main__ + +def test_swmmanywhere_cli(capsys): + """Test that the CLI can successfully run with an actual configuration.""" + + with tempfile.TemporaryDirectory() as tempdir: + base_dir = Path(tempdir) + # Define minimum viable config + config = { + "base_dir": str(base_dir), + "project": "my_first_swmm", + "bbox": [1.52740, 42.50524, 1.54273, 42.51259], + } + + config_path = base_dir / "config.yml" + with config_path.open("w") as config_file: + yaml.dump(config, config_file) + + # Mock sys.argv to simulate command-line arguments + sys.argv = [ + "swmmanywhere", + "--config_path", str(config_path), + "--verbose", "True" + ] + + # Run the CLI entry point + __main__.run() + + # Capture the output + captured = capsys.readouterr() + expected = "No real network provided, returning SWMM .inp file." + assert expected in captured.out From 7338c93cd5fcb0fe071e22b9557aaab7795fa763 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:39:19 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_cli.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index d1776327..3e4f417c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,16 +1,18 @@ """Integration test for the swmmanywhere command-line interface.""" + +from __future__ import annotations + import sys import tempfile -import yaml from pathlib import Path -import pytest +import yaml from swmmanywhere import __main__ + def test_swmmanywhere_cli(capsys): """Test that the CLI can successfully run with an actual configuration.""" - with tempfile.TemporaryDirectory() as tempdir: base_dir = Path(tempdir) # Define minimum viable config @@ -27,8 +29,10 @@ def test_swmmanywhere_cli(capsys): # Mock sys.argv to simulate command-line arguments sys.argv = [ "swmmanywhere", - "--config_path", str(config_path), - "--verbose", "True" + "--config_path", + str(config_path), + "--verbose", + "True", ] # Run the CLI entry point From a19e317b64b2dfd1ea142a3c8e89e669f6193f1e Mon Sep 17 00:00:00 2001 From: barneydobson Date: Thu, 24 Oct 2024 12:36:12 +0100 Subject: [PATCH 3/7] fix test --- tests/test_cli.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3e4f417c..528a360b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -9,12 +9,15 @@ import yaml from swmmanywhere import __main__ +from swmmanywhere.logging import logger, set_verbose -def test_swmmanywhere_cli(capsys): +def test_swmmanywhere_cli(): """Test that the CLI can successfully run with an actual configuration.""" + with tempfile.TemporaryDirectory() as tempdir: base_dir = Path(tempdir) + # Define minimum viable config config = { "base_dir": str(base_dir), @@ -35,10 +38,16 @@ def test_swmmanywhere_cli(capsys): "True", ] - # Run the CLI entry point - __main__.run() + expected = b"No real network provided, returning SWMM .inp file." + + with tempfile.NamedTemporaryFile(suffix=".log", mode="w+b", delete=False) as temp_file: + fid = Path(temp_file.name) + logger.add(fid) + + # Run the CLI entry point + __main__.run() - # Capture the output - captured = capsys.readouterr() - expected = "No real network provided, returning SWMM .inp file." - assert expected in captured.out + # Capture the output + assert expected in temp_file.read() + logger.remove() + From 21b7b1e14a173e8a4157b6625f1a497a36359c8c Mon Sep 17 00:00:00 2001 From: barneydobson Date: Thu, 24 Oct 2024 12:38:30 +0100 Subject: [PATCH 4/7] update imports --- tests/test_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 528a360b..0faa0e88 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -9,7 +9,7 @@ import yaml from swmmanywhere import __main__ -from swmmanywhere.logging import logger, set_verbose +from swmmanywhere.logging import logger def test_swmmanywhere_cli(): From f0a446ab6cde2efa66cd6b12b375c62aeb40901f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:38:50 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_cli.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 0faa0e88..eb4ba10f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -14,7 +14,6 @@ def test_swmmanywhere_cli(): """Test that the CLI can successfully run with an actual configuration.""" - with tempfile.TemporaryDirectory() as tempdir: base_dir = Path(tempdir) @@ -40,14 +39,15 @@ def test_swmmanywhere_cli(): expected = b"No real network provided, returning SWMM .inp file." - with tempfile.NamedTemporaryFile(suffix=".log", mode="w+b", delete=False) as temp_file: + with tempfile.NamedTemporaryFile( + suffix=".log", mode="w+b", delete=False + ) as temp_file: fid = Path(temp_file.name) logger.add(fid) # Run the CLI entry point __main__.run() - # Capture the output + # Capture the output assert expected in temp_file.read() logger.remove() - From bbf43de99087fc5df8cb97f42c8cdea209094e23 Mon Sep 17 00:00:00 2001 From: barneydobson Date: Fri, 25 Oct 2024 13:53:20 +0100 Subject: [PATCH 6/7] Update tests/test_cli.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Diego Alonso Álvarez <6095790+dalonsoa@users.noreply.github.com> --- tests/test_cli.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index eb4ba10f..3ab6bb48 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -12,10 +12,8 @@ from swmmanywhere.logging import logger -def test_swmmanywhere_cli(): +def test_swmmanywhere_cli(tmp_path): """Test that the CLI can successfully run with an actual configuration.""" - with tempfile.TemporaryDirectory() as tempdir: - base_dir = Path(tempdir) # Define minimum viable config config = { From 2cf15a27823e37a328379644a58d3d05cbf705a4 Mon Sep 17 00:00:00 2001 From: barneydobson Date: Fri, 25 Oct 2024 14:00:13 +0100 Subject: [PATCH 7/7] indent --- tests/test_cli.py | 71 ++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3ab6bb48..b94d93cd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -14,38 +14,39 @@ def test_swmmanywhere_cli(tmp_path): """Test that the CLI can successfully run with an actual configuration.""" - - # Define minimum viable config - config = { - "base_dir": str(base_dir), - "project": "my_first_swmm", - "bbox": [1.52740, 42.50524, 1.54273, 42.51259], - } - - config_path = base_dir / "config.yml" - with config_path.open("w") as config_file: - yaml.dump(config, config_file) - - # Mock sys.argv to simulate command-line arguments - sys.argv = [ - "swmmanywhere", - "--config_path", - str(config_path), - "--verbose", - "True", - ] - - expected = b"No real network provided, returning SWMM .inp file." - - with tempfile.NamedTemporaryFile( - suffix=".log", mode="w+b", delete=False - ) as temp_file: - fid = Path(temp_file.name) - logger.add(fid) - - # Run the CLI entry point - __main__.run() - - # Capture the output - assert expected in temp_file.read() - logger.remove() + base_dir = Path(tmp_path) + + # Define minimum viable config + config = { + "base_dir": str(base_dir), + "project": "my_first_swmm", + "bbox": [1.52740, 42.50524, 1.54273, 42.51259], + } + + config_path = base_dir / "config.yml" + with config_path.open("w") as config_file: + yaml.dump(config, config_file) + + # Mock sys.argv to simulate command-line arguments + sys.argv = [ + "swmmanywhere", + "--config_path", + str(config_path), + "--verbose", + "True", + ] + + expected = b"No real network provided, returning SWMM .inp file." + + with tempfile.NamedTemporaryFile( + suffix=".log", mode="w+b", delete=False + ) as temp_file: + fid = Path(temp_file.name) + logger.add(fid) + + # Run the CLI entry point + __main__.run() + + # Capture the output + assert expected in temp_file.read() + logger.remove()