Skip to content

Commit

Permalink
Use pytest-describe
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhollmann committed Dec 27, 2021
1 parent 6cbb357 commit b875583
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ interactions:
CF-Cache-Status:
- DYNAMIC
CF-RAY:
- 6c4562fe8f814150-HAM
- 6c4610995b65d46f-HAM
Connection:
- keep-alive
Content-Type:
- application/json
Date:
- Mon, 27 Dec 2021 20:44:25 GMT
- Mon, 27 Dec 2021 22:42:55 GMT
Expect-CT:
- max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
NEL:
- '{"success_fraction":0,"report_to":"cf-nel","max_age":604800}'
Report-To:
- '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=q%2FRnu40KPvdxP0O1VsZqTV2MXCQ93nguvl2IK42KSIL6ekDdRGRyD6EliMwUx%2BI7NoANELfLR0%2BuM%2BETOspZ94zYSKrRyQZDi9Koyo1WDekzKxhgeKmHPmUqU3ecHIu%2FDB1HTA%3D%3D"}],"group":"cf-nel","max_age":604800}'
- '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=RQUq0%2Bpsvm%2FiC6u0UBEz7LFHhMX6l%2FC0CX3Y%2BF4tOsF7V2Mar1FU7KPVJKaKBW%2F82fGgNpT0wACkAwtytwguNDhIoVqhjFkkgsJOm8EtP1JBvHAb%2Bqve0jzFSbqu9Zgm9poBiA%3D%3D"}],"group":"cf-nel","max_age":604800}'
Server:
- cloudflare
Transfer-Encoding:
Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pytest-cov = "^3.0.0"
pytest-vcr = "^1.0.2"
responses = "^0.16.0"
autoflake = "^1.4"
pytest-describe = "^2.0.1"

[tool.black]
target-version = ['py37']
Expand Down
131 changes: 59 additions & 72 deletions tests/blueskyapi/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@
import blueskyapi


class TestInit:
def test_defaults(self, mocker):
mocker.patch.multiple(
"blueskyapi.default_config",
base_url="the base url",
api_key="the api key",
)

client = blueskyapi.Client()
assert client.base_url == "the base url"
assert client.api_key == "the api key"

def test_with_args(self):
client = blueskyapi.Client("api-key", base_url="https://example.com/api")
assert client.base_url == "https://example.com/api"
assert client.api_key == "api-key"


@pytest.fixture()
def client():
return blueskyapi.Client()
Expand All @@ -41,67 +23,72 @@ def add_api_response(path, *, result=default_result, status=200):
)


class TestLatestForecast:
@responses.activate
def test_defaults(self, client):
add_api_response("/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5")
client.latest_forecast(53.5, 13.5)

@responses.activate
def test_with_distances_as_array(self, client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&prediction_distances=0,24"
)
client.latest_forecast(53.5, 13.5, prediction_distances=[0, 24])

@responses.activate
def test_with_distances_as_string(self, client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&prediction_distances=0,24"
def describe_init():
def test_defaults(mocker):
mocker.patch.multiple(
"blueskyapi.default_config",
base_url="the base url",
api_key="the api key",
)
client.latest_forecast(53.5, 13.5, prediction_distances="0,24")

@responses.activate
def test_with_invalid_distances(self, client):
with pytest.raises(TypeError, match="prediction_distances should be"):
client.latest_forecast(53.5, 13.5, prediction_distances=1.5)
client = blueskyapi.Client()
assert client.base_url == "the base url"
assert client.api_key == "the api key"

@responses.activate
def test_with_columns_as_array(self, client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&columns=col_a,col_b"
)
client.latest_forecast(53.5, 13.5, columns=["col_a", "col_b"])
def test_with_args():
client = blueskyapi.Client("api-key", base_url="https://example.com/api")
assert client.base_url == "https://example.com/api"
assert client.api_key == "api-key"

@responses.activate
def test_with_columns_as_string(self, client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&columns=col_a,col_b"
)
client.latest_forecast(53.5, 13.5, columns="col_a,col_b")

def describe_latest_forecast():
@responses.activate
def test_with_invalid_columns(self, client):
with pytest.raises(TypeError, match="columns should be"):
client.latest_forecast(53.5, 13.5, columns=1)
def test_defaults(client):
add_api_response("/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5")
client.latest_forecast(53.5, 13.5)

@responses.activate
def test_with_selection(self, client):
add_api_response(
"/forecasts/gfs_0p25/latest"
"?lat=53.5&lon=13.5"
"&prediction_distances=0,24"
"&columns=apparent_temperature_at_2m"
)
client.latest_forecast(
53.5,
13.5,
prediction_distances=[0, 24],
columns=["apparent_temperature_at_2m"],
)
def describe_prediction_distances():
@responses.activate
def with_array(client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&prediction_distances=0,24"
)
client.latest_forecast(53.5, 13.5, prediction_distances=[0, 24])

@responses.activate
def with_string(client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&prediction_distances=0,24"
)
client.latest_forecast(53.5, 13.5, prediction_distances="0,24")

@responses.activate
def with_invalid_value(client):
with pytest.raises(TypeError, match="prediction_distances should be"):
client.latest_forecast(53.5, 13.5, prediction_distances=1.5)

def describe_columns():
@responses.activate
def with_array(client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&columns=col_a,col_b"
)
client.latest_forecast(53.5, 13.5, columns=["col_a", "col_b"])

@responses.activate
def with_string(client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5&columns=col_a,col_b"
)
client.latest_forecast(53.5, 13.5, columns="col_a,col_b")

@responses.activate
def with_invalid_value(client):
with pytest.raises(TypeError, match="columns should be"):
client.latest_forecast(53.5, 13.5, columns=1)

@responses.activate
def test_over_rate_limit(self, client):
def test_over_rate_limit(client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5",
result={"the": "result"},
Expand All @@ -111,7 +98,7 @@ def test_over_rate_limit(self, client):
client.latest_forecast(53.5, 13.5)

@responses.activate
def test_result(self, client):
def test_result(client):
add_api_response(
"/forecasts/gfs_0p25/latest?lat=53.5&lon=13.5",
result=[{"prediction_moment": "2021-12-27T18:00:00Z", "some_column": 5}],
Expand All @@ -123,7 +110,7 @@ def test_result(self, client):
assert np.all(result.some_column == [5])

@pytest.mark.vcr()
def test_integration(self, client):
def test_integration(client):
result = client.latest_forecast(53.5, 13.5)

assert len(result.columns) == 35
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import os

import pytest


pytest_plugins = []


@pytest.fixture(scope="module")
def vcr_cassette_dir(request):
return os.path.join("cassettes", request.module.__name__)

0 comments on commit b875583

Please sign in to comment.