Skip to content

Commit

Permalink
Merge branch 'main' into 105-readthedocsapi
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Mar 26, 2024
2 parents a93bcf2 + 7044fb7 commit 5b8828c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
6 changes: 2 additions & 4 deletions swmmanywhere/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
>>> os.environ["SWMMANYWHERE_VERBOSE"] = "true"
>>> # logging is now enabled in any swmmanywhere module
>>> from swmmanywhere.logging import logger # You can now log yourself
>>> logger.info("This is an info message.") # Write to stdout
This is an info message.
>>> logger.add("file.log") # Add a log file
>>> logger.info("This is an info message.") # Write to stdout and file.log
>>> logger.info("This is an info message.") # Write to stdout # doctest: +SKIP
This is an info message.
>>> logger.add("file.log") # Add a log file # doctest: +SKIP
>>> os.environ["SWMMANYWHERE_VERBOSE"] = "false" # Disable logging
```
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_graph_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from swmmanywhere.graph_utilities import graphfcns as gu
from swmmanywhere.graph_utilities import iterate_graphfcns, load_graph, save_graph

os.environ['SWMMANYWHERE_VERBOSE'] = "false"

def load_street_network():
"""Load a street network."""
Expand Down Expand Up @@ -252,6 +251,7 @@ def test_iterate_graphfcns():
project_name = None,
bbox_number = None,
model_number = None)
os.environ['SWMMANYWHERE_VERBOSE'] = "false"
G = iterate_graphfcns(G,
['assign_id',
'format_osmnx_lanes'],
Expand Down
4 changes: 3 additions & 1 deletion tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_logger():
assert temp_file.read() != b""
logger.remove()
fid.unlink()
os.environ["SWMMANYWHERE_VERBOSE"] = "false"

def test_logger_disable():
"""Test the disable function."""
Expand Down Expand Up @@ -67,4 +68,5 @@ def test_logger_again():
logger.test_logger()
assert temp_file.read() != b""
logger.remove()
fid.unlink()
fid.unlink()
os.environ["SWMMANYWHERE_VERBOSE"] = "false"
6 changes: 4 additions & 2 deletions tests/test_metric_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def test_design_params():
real_G = G,
real_subs = subs,
real_results = results,
metric_list = design_results.keys())
metric_list = design_results.keys(),
metric_evaluation = MetricEvaluation())
for metric, val in metrics.items():
assert metric in design_results
assert np.isclose(val, 0)
Expand All @@ -326,7 +327,8 @@ def test_design_params():
real_G = G,
real_subs = subs,
real_results = results,
metric_list = design_results.keys())
metric_list = design_results.keys(),
metric_evaluation = MetricEvaluation())

for metric, val in metrics.items():
assert metric in design_results
Expand Down
44 changes: 27 additions & 17 deletions tests/test_prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,42 @@ def test_elevation_downloader_download():
# Test some property of data (not sure if they may change this
# data)
assert data.max().max() > 25, "Elevation data should be higher."

@pytest.fixture
def setup_mocks():
"""Set up get_country mock for the tests."""
# Mock for geolocator.reverse
mock_location = mock.Mock()
mock_location.raw = {'address': {'country_code': 'gb'}}

def test_get_uk():
# Mock Nominatim
nominatim_patch = mock.patch.object(Nominatim,
'reverse',
return_value=mock_location)
# Mock yaml.safe_load
yaml_patch = mock.patch.object(yaml, 'safe_load', return_value={'GB': 'GBR'})

with nominatim_patch, yaml_patch:
yield

def test_get_uk(setup_mocks):
"""Check a UK point."""
# Coordinates for London, UK
x = -0.1276
y = 51.5074

# Create a mock response for geolocator.reverse
mock_location = mock.Mock()
mock_location.raw = {'address': {'country_code': 'gb'}}

# Mock Nominatim
with mock.patch.object(Nominatim, 'reverse', return_value=mock_location):
# Mock yaml.safe_load
with mock.patch.object(yaml, 'safe_load', return_value={'GB': 'GBR'}):
# Call get_country
result = downloaders.get_country(x, y)

# Call get_country
result = downloaders.get_country(x, y)

assert result[2] == 'GB'
assert result[3] == 'GBR'

def test_building_downloader():
def test_building_downloader(setup_mocks):
"""Check buildings are downloaded."""
# Coordinates for small country (VAT)
x = 7.41839
y = 43.73205
# Coordinates
x = -0.1276
y = 51.5074

with tempfile.TemporaryDirectory() as temp_dir:
temp_fid = Path(temp_dir) / 'temp.parquet'
mock_response = mock.Mock()
Expand All @@ -154,7 +164,7 @@ def test_building_downloader():
response = downloaders.download_buildings(temp_fid, x, y)

# Assert that requests.get was called with the right arguments
mock_get.assert_called_once_with('https://data.source.coop/vida/google-microsoft-open-buildings/geoparquet/by_country/country_iso=MCO/MCO.parquet')
mock_get.assert_called_once_with('https://data.source.coop/vida/google-microsoft-open-buildings/geoparquet/by_country/country_iso=GBR/GBR.parquet')

# Check response
assert response == 200
Expand Down

0 comments on commit 5b8828c

Please sign in to comment.