-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
97 lines (71 loc) · 2.33 KB
/
Copy pathtest_main.py
File metadata and controls
97 lines (71 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pytest
import requests
from weatcher import AccuWeather
from unittest.mock import patch
class ResponseGetMock(object):
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def raise_for_status():
pass
def json():
return [
{
"Version": 1,
"Key": "2696858",
"Type": "City",
"Rank": 85,
"LocalizedName": "Warszawa",
}
]
@pytest.fixture(autouse=True)
def no_requests(monkeypatch):
monkeypatch.delattr("requests.sessions.Session.request")
@pytest.fixture(params=["Warszawa", "Gdansk"], name="weatcher")
def fixture_weatcher(request, monkeypatch):
my_weatcher = AccuWeather()
def monkey_return(url, params):
return ResponseGetMock
monkeypatch.setattr(requests, "get", monkey_return)
my_weatcher.city_token = request.param
yield my_weatcher
def test_weatcher_initialization():
assert AccuWeather
def test_weatcher_set_city(monkeypatch):
weatcher = AccuWeather()
def monkey_get_city(city: str):
if city == "Gdansk":
return "275174"
return None
monkeypatch.setattr(weatcher, "get_city", monkey_get_city)
weatcher.city_token = "Gdansk"
assert (weatcher.city_token, "275174")
WEATCHER_API = {
"DailyForecasts": [
{
"Date": "2023-01-27T07:00:00+01:00",
"EpochDate": 1674799200,
"Temperature": {
"Minimum": {"Value": -2.6, "Unit": "C", "UnitType": 17},
"Maximum": {"Value": 3.5, "Unit": "C", "UnitType": 17},
},
}
]
}
# Unit test patching
@patch.object(AccuWeather, "get_forecasts", return_value=WEATCHER_API)
def test_weatcher_get_forecasts(weatcher_mock, weatcher):
with patch(
"weatcher.AccuWeather.get_forecasts", return_value=WEATCHER_API
):
assert (weatcher.get_forecasts(), dict)
# Using monkeypatch with pytests patching
def test_weatcher_get_day_temperature(weatcher, monkeypatch):
def get_forecasts():
return WEATCHER_API
monkeypatch.setattr(weatcher, "get_forecasts", get_forecasts)
weatcher.store_forecasts()
assert (
weatcher.get_day_temperature(),
"Date: 27.01.2023, temperature: 0.4°C",
)