Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklvsa committed Oct 17, 2024
1 parent a78e0cb commit 46e9bbf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mapbox_tilesets/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,12 @@ def validate_stream(features):


@cli.command("estimate-cu")
@click.option("--tileset", "-i", required=True, type=str, help="Tileset ID to estimate")
@click.argument("tileset", required=True, type=str)
@click.option(
"--sources",
"-s",
required=False,
type=str,
type=click.Path(exists=False),
help="Local sources represented in your tileset's recipe",
)
@click.option(
Expand All @@ -870,7 +870,7 @@ def estimate_cu(tileset, num_bands=15, sources=None, raw=False, token=None):

rio = utils.load_module("rasterio")

if not sources or len(sources) <= 0:
if sources is None:
click.echo(f"[warning] estimating '{tileset}' with a default global bounds")
sources = ""

Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ def token_environ(monkeypatch):
monkeypatch.setenv("MAPBOX_ACCESS_TOKEN", "pk.eyJ1IjoidGVzdC11c2VyIn0K")
monkeypatch.setenv("MapboxAccessToken", "test-token")

@pytest.fixture(scope="function")
def api_environ(monkeypatch):
monkeypatch.setenv("MAPBOX_API", "https://api.mapbox.com")


class _MockResponse:
def __init__(self, mock_json, status_code=200):
self.text = json.dumps(mock_json)
self._json = mock_json
self.status_code = status_code
self.ok = status_code < 400

def MockResponse(self):
return self
Expand Down
49 changes: 49 additions & 0 deletions tests/test_cli_estimate_cu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json
import pytest
from unittest import mock

from click.testing import CliRunner

from mapbox_tilesets.scripts.cli import estimate_cu
from utils import clean_runner_output

@pytest.mark.usefixtures("token_environ")
@pytest.mark.usefixtures("api_environ")
@mock.patch("requests.Session.get")
def test_cli_estimate_cu_tileset_no_sources(mock_request_get, MockResponse):
runner = CliRunner()

tileset_id = "my.tileset"
msg = {"message": "mock message"}

mock_request_get.return_value = MockResponse(msg)
result = runner.invoke(estimate_cu, [tileset_id])
mock_request_get.assert_called_with(
f"https://api.mapbox.com/tilesets/v1/{tileset_id}/estimate",
params={"band_count": 15, "access_token": "pk.eyJ1IjoidGVzdC11c2VyIn0K"}
)

assert result.exit_code == 1
assert result.output == f"[warning] estimating '{tileset_id}' with a default global bounds\nError: {json.dumps(msg)}\n"


@pytest.mark.usefixtures("token_environ")
@pytest.mark.usefixtures("api_environ")
@mock.patch("requests.Session.get")
@mock.patch("glob.glob")
def test_cli_estimate_cu_tilese_with_sources(mock_glob, mock_request_get, MockResponse):
runner = CliRunner()

tileset_id = "my.tileset"
msg = {"cu": "5"}

mock_request_get.return_value = MockResponse(msg)
mock_glob.return_value = ["myfile.grib2"]
result = runner.invoke(estimate_cu, [tileset_id, "-s", "/my/sources/*.grib2", "--raw"])
mock_request_get.assert_called_with(
f"https://api.mapbox.com/tilesets/v1/{tileset_id}/estimate",
params={"band_count": 15, "access_token": "pk.eyJ1IjoidGVzdC11c2VyIn0K"}
)

assert result.exit_code == 0
assert json.loads(result.output) == msg

0 comments on commit 46e9bbf

Please sign in to comment.