-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import json | ||
from pathlib import Path | ||
from unittest.mock import Mock, patch | ||
|
||
import sunblock | ||
|
||
|
||
# Mocking allows us to replace interfaces that require network | ||
# connectivity or external infrastructure with a interface that mimics | ||
# the return values, thus enabling testing of such code without having | ||
# to setup aforesaid infrastructure. Here we mimic the requests.get() | ||
# call to OpenMeteo. | ||
@patch("sunblock.requests") | ||
def test_fetch(mock_requests): | ||
loc = sunblock.Location(51.76, -1.24) | ||
with Path(__file__).with_name("openmeteo.json").open() as fp: | ||
data = json.load(fp) | ||
mock_requests.get.return_value = Mock( | ||
**{ | ||
"status_code": 200, | ||
"json.return_value": data, | ||
} | ||
) | ||
res = sunblock.fetch(loc) | ||
mock_requests.get.assert_called_once() | ||
assert len(res["hourly"]["time"]) == 24 * 7 # hourly data for 7 days |