Skip to content

Commit

Permalink
Add mock tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhidg committed Aug 25, 2024
1 parent 28f4454 commit 14af80b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_mock.py
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

0 comments on commit 14af80b

Please sign in to comment.