Skip to content

Commit

Permalink
Merge pull request #31 from agrenott/dev
Browse files Browse the repository at this point in the history
Do not require credentials for local files
  • Loading branch information
agrenott committed Nov 7, 2023
2 parents fec6523 + 2779f1d commit bd2137e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pyhgtmap/main.py
Expand Up @@ -492,17 +492,17 @@ def parseCommandLine(sys_args: List[str]) -> Tuple[Values, List[str]]:
opts.dataSource[opts.dataSource.index(s)] = "{0:s}v{1:.1f}".format(
s, opts.srtmVersion
)
else:
elif len(args) == 0:
# No explicit source nor input provided, try to download using default
opts.dataSource = []
if opts.viewfinder != 0:
opts.dataSource.append("view{0:d}".format(opts.viewfinder))
opts.dataSource.append(
"srtm{0:d}v{1:.1f}".format(opts.srtmResolution, opts.srtmVersion)
)
if not opts.area and not opts.polygon:
# this is a hint for makeOsmFilename() that files are specified on the
# command line
opts.dataSource = []
else:
# Input files provided, no download source
opts.dataSource = []
needsEarthexplorerLogin = False
for s in opts.dataSource:
if s.startswith("srtm") and "v3" in s:
Expand Down
48 changes: 44 additions & 4 deletions tests/test_main.py
@@ -1,14 +1,16 @@
import optparse
import os
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from pyhgtmap import main

from . import TEST_DATA_PATH


@patch("pyhgtmap.main.NASASRTMUtil")
@patch("pyhgtmap.main.HgtFilesProcessor")
def test_main_download_from_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None:
def test_main_download_from_poly(
HgtFilesProcessor_mock: MagicMock, NASASRTMUtil_mock: MagicMock
) -> None:
"""Only polygon option is used, without files; download tiles."""
# Prepare
sys_args = [
Expand Down Expand Up @@ -52,7 +54,9 @@ def test_main_download_from_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> N

@patch("pyhgtmap.main.NASASRTMUtil")
@patch("pyhgtmap.main.HgtFilesProcessor")
def test_main_manual_input_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None:
def test_main_manual_input_poly(
HgtFilesProcessor_mock: MagicMock, NASASRTMUtil_mock: MagicMock
) -> None:
"""Polygon option is used, with manual files; polygon must be applied to files."""
# Prepare
sys_args = [
Expand Down Expand Up @@ -80,9 +84,45 @@ def test_main_manual_input_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> No
)


@patch("pyhgtmap.main.configUtil")
@patch("pyhgtmap.main.NASASRTMUtil")
@patch("pyhgtmap.main.HgtFilesProcessor")
def test_main_manual_input_no_poly(HgtFilesProcessor_mock, NASASRTMUtil_mock) -> None:
def test_main_manual_input_poly_no_source(
HgtFilesProcessor_mock: MagicMock,
NASASRTMUtil_mock: MagicMock,
configUtil_mock: MagicMock,
) -> None:
"""Earthexplorer credentials shouldn't be required when providing tiles in input with a polygon."""
# Prepare
sys_args = [
f"--polygon={os.path.join(TEST_DATA_PATH, 'france.poly')}",
"N45E007.hgt",
"N46E007.hgt",
"N47E007.hgt",
]

# Test
main.main_internal(sys_args)

# Check
configUtil_mock.Config.assert_not_called()
NASASRTMUtil_mock.getFiles.assert_not_called()

HgtFilesProcessor_mock.assert_called_once()
parsed_options: optparse.Values = HgtFilesProcessor_mock.call_args.args[3]
# area must be properly computed from files names
assert parsed_options.area == "7:45:8:48"
# Polygon check must be enabled for all files
HgtFilesProcessor_mock.return_value.process_files.assert_called_once_with(
[("N45E007.hgt", True), ("N46E007.hgt", True), ("N47E007.hgt", True)]
)


@patch("pyhgtmap.main.NASASRTMUtil")
@patch("pyhgtmap.main.HgtFilesProcessor")
def test_main_manual_input_no_poly(
HgtFilesProcessor_mock: MagicMock, NASASRTMUtil_mock: MagicMock
) -> None:
"""Polygon option is NOT used, with manual files."""
# Prepare
sys_args = [
Expand Down

0 comments on commit bd2137e

Please sign in to comment.